-
Notifications
You must be signed in to change notification settings - Fork 1
/
closable-chan.go
69 lines (57 loc) · 1.57 KB
/
closable-chan.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import "sync"
/*
ClosableChan holds a channel.
ClosableChan has a thread-safe re-entrant Close method
closing the channel exactly once without panics.
IsClosed indicates wether the channel has been closed
*/
type ClosableChan[T any] struct {
ch chan T
err error
isClosed AtomicBool
once sync.Once
}
// NewCloser ensures a chan does not throw
func NewCloser[T any](ch chan T) (cl *ClosableChan[T]) {
return &ClosableChan[T]{ch: ch}
}
// Ch retrieves the channel
func (cl *ClosableChan[T]) Ch() (ch chan T) {
return cl.ch
}
// Close ensures the channel is closed.
// Close does not panic.
// Close is thread-safe.
// Close does not return until the channel is closed.
// Upon return, all invocations have a possible close error in err.
// if errp is non-nil, it is updated with error status
func (cl *ClosableChan[T]) Close(errp ...*error) (err error, didClose bool) {
// first thread closes the channel
cl.once.Do(func() {
defer Recover(Annotation(), &cl.err, nil)
didClose = true
cl.isClosed.Set()
close(cl.ch)
})
// all threads provide the result
err = cl.err
if len(errp) > 0 {
*errp[0] = err
}
return
}
// IsClosed indicates whether the Close method has been invoked
func (cl *ClosableChan[T]) IsClosed() (isClosed bool) {
return cl.isClosed.IsTrue()
}
// Closer closes a channels and can be deferred
// and does not panic
func Closer[T any](ch chan T, errp *error) {
defer Recover(Annotation(), errp, nil)
close(ch)
}