mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
28 lines
543 B
Go
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{}{}
|
|
}
|