You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
624 B
47 lines
624 B
package util
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type Channel struct {
|
|
m sync.Mutex
|
|
closed bool
|
|
C chan interface{}
|
|
}
|
|
|
|
func NewChannel() *Channel {
|
|
return &Channel{
|
|
C: nil,
|
|
closed: true,
|
|
}
|
|
}
|
|
|
|
func (c *Channel) Open() {
|
|
c.m.Lock()
|
|
defer c.m.Unlock()
|
|
if c.isClosed() {
|
|
c.C = make(chan interface{})
|
|
c.closed = false
|
|
}
|
|
}
|
|
|
|
func (c *Channel) Close() {
|
|
c.m.Lock()
|
|
defer c.m.Unlock()
|
|
if !c.isClosed() {
|
|
close(c.C)
|
|
c.C = nil
|
|
c.closed = true
|
|
}
|
|
}
|
|
|
|
func (c *Channel) IsClosed() bool {
|
|
c.m.Lock()
|
|
defer c.m.Unlock()
|
|
return c.isClosed()
|
|
}
|
|
|
|
func (c *Channel) isClosed() bool {
|
|
return c.C == nil && c.closed
|
|
}
|
|
|