1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-03 01:38:24 -04:00
v2fly/common/signal/semaphore.go

24 lines
342 B
Go
Raw Normal View History

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