v2ray-core/common/signal/semaphore.go

24 lines
342 B
Go
Raw Normal View History

2017-02-13 22:29:34 +00:00
package signal
type Semaphore struct {
2018-02-08 14:39:46 +00:00
token chan struct{}
2017-02-13 22:29:34 +00:00
}
func NewSemaphore(n int) *Semaphore {
s := &Semaphore{
2018-02-08 14:39:46 +00:00
token: make(chan struct{}, n),
2017-02-13 22:29:34 +00:00
}
for i := 0; i < n; i++ {
2018-02-08 14:39:46 +00:00
s.token <- struct{}{}
2017-02-13 22:29:34 +00:00
}
return s
}
2018-02-08 14:39:46 +00:00
func (s *Semaphore) Wait() <-chan struct{} {
2017-02-13 22:29:34 +00:00
return s.token
}
func (s *Semaphore) Signal() {
2018-02-08 14:39:46 +00:00
s.token <- struct{}{}
2017-02-13 22:29:34 +00:00
}