27 lines
429 B
Go
27 lines
429 B
Go
package semaphore
|
|
|
|
import (
|
|
"sync/atomic"
|
|
)
|
|
|
|
type BurstAtomicSemaphore struct {
|
|
count atomic.Int64
|
|
limit int64
|
|
}
|
|
|
|
func NewBurstAtomicSemaphore(size int64) *BurstAtomicSemaphore {
|
|
return &BurstAtomicSemaphore{limit: size}
|
|
}
|
|
|
|
func (s *BurstAtomicSemaphore) Acquire() bool {
|
|
v := s.count.Add(1)
|
|
if v > s.limit {
|
|
s.count.Add(-1)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *BurstAtomicSemaphore) Release() {
|
|
s.count.Add(-1)
|
|
}
|