3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-10 22:19:31 +01:00

make semaphores O(1) in storage

This commit is contained in:
Shivaram Lingamneni 2019-10-02 19:22:10 -04:00
parent 7ef55c654a
commit 45476079a1

View File

@ -4,33 +4,33 @@
package utils package utils
import ( import (
"context"
"log" "log"
"runtime/debug" "runtime/debug"
"time" "time"
) )
// Semaphore is a counting semaphore. Note that a capacity of n requires O(n) storage. type e struct{}
// Semaphore is a counting semaphore.
// A semaphore of capacity 1 can be used as a trylock. // A semaphore of capacity 1 can be used as a trylock.
type Semaphore (chan bool) type Semaphore (chan e)
// Initialize initializes a semaphore to a given capacity. // Initialize initializes a semaphore to a given capacity.
func (semaphore *Semaphore) Initialize(capacity int) { func (semaphore *Semaphore) Initialize(capacity int) {
*semaphore = make(chan bool, capacity) *semaphore = make(chan e, capacity)
for i := 0; i < capacity; i++ {
(*semaphore) <- true
}
} }
// Acquire acquires a semaphore, blocking if necessary. // Acquire acquires a semaphore, blocking if necessary.
func (semaphore *Semaphore) Acquire() { func (semaphore *Semaphore) Acquire() {
<-(*semaphore) (*semaphore) <- e{}
} }
// TryAcquire tries to acquire a semaphore, returning whether the acquire was // TryAcquire tries to acquire a semaphore, returning whether the acquire was
// successful. It never blocks. // successful. It never blocks.
func (semaphore *Semaphore) TryAcquire() (acquired bool) { func (semaphore *Semaphore) TryAcquire() (acquired bool) {
select { select {
case <-(*semaphore): case (*semaphore) <- e{}:
return true return true
default: default:
return false return false
@ -47,7 +47,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
timer := time.NewTimer(timeout) timer := time.NewTimer(timeout)
select { select {
case <-(*semaphore): case (*semaphore) <- e{}:
acquired = true acquired = true
case <-timer.C: case <-timer.C:
acquired = false acquired = false
@ -56,11 +56,24 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
return return
} }
// AcquireWithContext tries to acquire a semaphore, blocking at most until
// the context expires. It returns whether the acquire was successful.
// Note that if the context is already expired, the acquire may succeed anyway.
func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
select {
case (*semaphore) <- e{}:
acquired = true
case <-ctx.Done():
acquired = false
}
return
}
// Release releases a semaphore. It never blocks. (This is not a license // Release releases a semaphore. It never blocks. (This is not a license
// to program spurious releases.) // to program spurious releases.)
func (semaphore *Semaphore) Release() { func (semaphore *Semaphore) Release() {
select { select {
case (*semaphore) <- true: case <-(*semaphore):
// good // good
default: default:
// spurious release // spurious release