1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00
v2fly/common/signal/semaphore.go
2018-04-14 09:59:30 +02:00

28 lines
543 B
Go

package signal
// Semaphore is an implementation of semaphore.
type Semaphore struct {
token chan struct{}
}
// NewSemaphore create a new Semaphore with n permits.
func NewSemaphore(n int) *Semaphore {
s := &Semaphore{
token: make(chan struct{}, n),
}
for i := 0; i < n; i++ {
s.token <- struct{}{}
}
return s
}
// Wait returns a channel for acquiring a permit.
func (s *Semaphore) Wait() <-chan struct{} {
return s.token
}
// Signal releases a permit into the Semaphore.
func (s *Semaphore) Signal() {
s.token <- struct{}{}
}