| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | package signal | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"sync" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 15:52:16 +08:00
										 |  |  | // Done is a utility for notifications of something being done. | 
					
						
							| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | type Done struct { | 
					
						
							|  |  |  | 	access sync.Mutex | 
					
						
							|  |  |  | 	c      chan struct{} | 
					
						
							|  |  |  | 	closed bool | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 23:28:42 +01:00
										 |  |  | // NewDone returns a new Done. | 
					
						
							| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | func NewDone() *Done { | 
					
						
							|  |  |  | 	return &Done{ | 
					
						
							|  |  |  | 		c: make(chan struct{}), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-11 23:28:42 +01:00
										 |  |  | // Done returns true if Close() is called. | 
					
						
							| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | func (d *Done) Done() bool { | 
					
						
							|  |  |  | 	select { | 
					
						
							| 
									
										
										
										
											2018-04-15 20:40:47 +02:00
										 |  |  | 	case <-d.Wait(): | 
					
						
							| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | 		return true | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-15 20:40:47 +02:00
										 |  |  | // Wait returns a channel for waiting for done. | 
					
						
							|  |  |  | func (d *Done) Wait() <-chan struct{} { | 
					
						
							| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | 	return d.c | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-15 10:32:10 +08:00
										 |  |  | // Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status. | 
					
						
							| 
									
										
										
										
											2018-02-08 15:39:46 +01:00
										 |  |  | func (d *Done) Close() error { | 
					
						
							|  |  |  | 	d.access.Lock() | 
					
						
							|  |  |  | 	defer d.access.Unlock() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if d.closed { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	d.closed = true | 
					
						
							|  |  |  | 	close(d.c) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |