3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-26 13:59:44 +01:00

extract Semaphore to utils package

This commit is contained in:
Shivaram Lingamneni 2019-04-15 11:13:13 -04:00
parent a8f04ecc4d
commit 97e71dfef7
4 changed files with 59 additions and 48 deletions

View File

@ -40,7 +40,7 @@ type Channel struct {
accountToUMode map[string]modes.Mode accountToUMode map[string]modes.Mode
history history.Buffer history history.Buffer
stateMutex sync.RWMutex // tier 1 stateMutex sync.RWMutex // tier 1
writerSemaphore Semaphore // tier 1.5 writerSemaphore utils.Semaphore // tier 1.5
joinPartMutex sync.Mutex // tier 3 joinPartMutex sync.Mutex // tier 3
ensureLoaded utils.Once // manages loading stored registration info from the database ensureLoaded utils.Once // manages loading stored registration info from the database
dirtyBits uint dirtyBits uint

View File

@ -3,9 +3,9 @@
package irc package irc
import ( import (
"log"
"runtime" "runtime"
"runtime/debug"
"github.com/oragono/oragono/irc/utils"
) )
// See #237 for context. Operations that might allocate large amounts of temporary // See #237 for context. Operations that might allocate large amounts of temporary
@ -21,15 +21,12 @@ const (
MaxServerSemaphoreCapacity = 32 MaxServerSemaphoreCapacity = 32
) )
// Semaphore is a counting semaphore. Note that a capacity of n requires O(n) storage.
type Semaphore (chan bool)
// ServerSemaphores includes a named Semaphore corresponding to each concurrency-limited // ServerSemaphores includes a named Semaphore corresponding to each concurrency-limited
// sever operation. // sever operation.
type ServerSemaphores struct { type ServerSemaphores struct {
// each distinct operation MUST have its own semaphore; // each distinct operation MUST have its own semaphore;
// methods that acquire a semaphore MUST NOT call methods that acquire another // methods that acquire a semaphore MUST NOT call methods that acquire another
ClientDestroy Semaphore ClientDestroy utils.Semaphore
} }
// Initialize initializes a set of server semaphores. // Initialize initializes a set of server semaphores.
@ -41,40 +38,3 @@ func (serversem *ServerSemaphores) Initialize() {
serversem.ClientDestroy.Initialize(capacity) serversem.ClientDestroy.Initialize(capacity)
return return
} }
// Initialize initializes a semaphore to a given capacity.
func (semaphore *Semaphore) Initialize(capacity int) {
*semaphore = make(chan bool, capacity)
for i := 0; i < capacity; i++ {
(*semaphore) <- true
}
}
// Acquire acquires a semaphore, blocking if necessary.
func (semaphore *Semaphore) Acquire() {
<-(*semaphore)
}
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
// successful. It never blocks.
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
select {
case <-(*semaphore):
return true
default:
return false
}
}
// Release releases a semaphore. It never blocks. (This is not a license
// to program spurious releases.)
func (semaphore *Semaphore) Release() {
select {
case (*semaphore) <- true:
// good
default:
// spurious release
log.Printf("spurious semaphore release (full to capacity %d)", cap(*semaphore))
debug.PrintStack()
}
}

View File

@ -15,6 +15,8 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"github.com/oragono/oragono/irc/utils"
) )
var ( var (
@ -34,7 +36,7 @@ type Socket struct {
maxSendQBytes int maxSendQBytes int
// this is a trylock enforcing that only one goroutine can write to `conn` at a time // this is a trylock enforcing that only one goroutine can write to `conn` at a time
writerSemaphore Semaphore writerSemaphore utils.Semaphore
buffers [][]byte buffers [][]byte
totalLength int totalLength int

49
irc/utils/semaphores.go Normal file
View File

@ -0,0 +1,49 @@
// Copyright (c) 2018 Shivaram Lingamneni
package utils
import (
"log"
"runtime/debug"
)
// Semaphore is a counting semaphore. Note that a capacity of n requires O(n) storage.
// A semaphore of capacity 1 can be used as a trylock.
type Semaphore (chan bool)
// Initialize initializes a semaphore to a given capacity.
func (semaphore *Semaphore) Initialize(capacity int) {
*semaphore = make(chan bool, capacity)
for i := 0; i < capacity; i++ {
(*semaphore) <- true
}
}
// Acquire acquires a semaphore, blocking if necessary.
func (semaphore *Semaphore) Acquire() {
<-(*semaphore)
}
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
// successful. It never blocks.
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
select {
case <-(*semaphore):
return true
default:
return false
}
}
// Release releases a semaphore. It never blocks. (This is not a license
// to program spurious releases.)
func (semaphore *Semaphore) Release() {
select {
case (*semaphore) <- true:
// good
default:
// spurious release
log.Printf("spurious semaphore release (full to capacity %d)", cap(*semaphore))
debug.PrintStack()
}
}