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.
v2ray-core/common/signal/semaphore.go

28 lines
543 B

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{}{}
}