Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package done
 
import (
    "sync"
)
 
// Instance is a utility for notifications of something being done.
type Instance struct {
    access sync.Mutex
    c      chan struct{}
    closed bool
}
 
// New returns a new Done.
func New() *Instance {
    return &Instance{
        c: make(chan struct{}),
    }
}
 
// Done returns true if Close() is called.
func (d *Instance) Done() bool {
    select {
    case <-d.Wait():
        return true
    default:
        return false
    }
}
 
// Wait returns a channel for waiting for done.
func (d *Instance) Wait() <-chan struct{} {
    return d.c
}
 
// Close marks this Done 'done'. This method may be called multiple times. All calls after first call will have no effect on its status.
func (d *Instance) Close() error {
    d.access.Lock()
    defer d.access.Unlock()
 
    if d.closed {
        return nil
    }
 
    d.closed = true
    close(d.c)
 
    return nil
}