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