initial commit
This commit is contained in:
31
semaphore.go
Normal file
31
semaphore.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package commander
|
||||
|
||||
func NewSemaphore(len int) Semaphore {
|
||||
if len == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return make(Semaphore, len)
|
||||
}
|
||||
|
||||
type Semaphore chan struct{}
|
||||
|
||||
func (s Semaphore) Acquire() {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
|
||||
s <- struct{}{}
|
||||
}
|
||||
|
||||
func (s Semaphore) Release() {
|
||||
if s == nil {
|
||||
return
|
||||
}
|
||||
|
||||
<-s
|
||||
}
|
||||
|
||||
func (s Semaphore) Close() {
|
||||
close(s)
|
||||
}
|
||||
Reference in New Issue
Block a user