Skip to content

Commit e28926c

Browse files
committed
add Semaphore
1 parent 383548f commit e28926c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

control/semaphore.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package control
2+
3+
type (
4+
Semaphore interface {
5+
Acquire()
6+
Release()
7+
}
8+
_semaphore struct {
9+
c chan struct{}
10+
}
11+
)
12+
13+
func NewSemaphore(count uint64) Semaphore {
14+
return &_semaphore{
15+
c: make(chan struct{}, count),
16+
}
17+
}
18+
19+
func (s *_semaphore) Acquire() {
20+
s.c <- struct{}{}
21+
}
22+
23+
func (s *_semaphore) Release() {
24+
<-s.c
25+
}

0 commit comments

Comments
 (0)