init: git clone https://github.com/iuroc/bilidown.git
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package util
|
||||
|
||||
import "sync"
|
||||
|
||||
type Semaphore struct {
|
||||
ch chan struct{}
|
||||
wg sync.WaitGroup
|
||||
}
|
||||
|
||||
func NewSemaphore(concurrency int) *Semaphore {
|
||||
return &Semaphore{
|
||||
ch: make(chan struct{}, concurrency),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Semaphore) Acquire() {
|
||||
s.ch <- struct{}{}
|
||||
s.wg.Add(1)
|
||||
}
|
||||
|
||||
func (s *Semaphore) Release() {
|
||||
<-s.ch
|
||||
s.wg.Done()
|
||||
}
|
||||
|
||||
func (s *Semaphore) Wait() {
|
||||
s.wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user