mirror of
https://github.com/ergochat/ergo.git
synced 2025-12-20 02:00:11 -08:00
move StringSet to utils package
This commit is contained in:
parent
ddac7d94a8
commit
df8be72c6f
9 changed files with 50 additions and 40 deletions
|
|
@ -10,27 +10,25 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type e struct{}
|
||||
|
||||
// Semaphore is a counting semaphore.
|
||||
// A semaphore of capacity 1 can be used as a trylock.
|
||||
type Semaphore (chan e)
|
||||
type Semaphore (chan empty)
|
||||
|
||||
// Initialize initializes a semaphore to a given capacity.
|
||||
func (semaphore *Semaphore) Initialize(capacity int) {
|
||||
*semaphore = make(chan e, capacity)
|
||||
*semaphore = make(chan empty, capacity)
|
||||
}
|
||||
|
||||
// Acquire acquires a semaphore, blocking if necessary.
|
||||
func (semaphore *Semaphore) Acquire() {
|
||||
(*semaphore) <- e{}
|
||||
(*semaphore) <- empty{}
|
||||
}
|
||||
|
||||
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
|
||||
// successful. It never blocks.
|
||||
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
||||
select {
|
||||
case (*semaphore) <- e{}:
|
||||
case (*semaphore) <- empty{}:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
|
|
@ -47,7 +45,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
|
|||
|
||||
timer := time.NewTimer(timeout)
|
||||
select {
|
||||
case (*semaphore) <- e{}:
|
||||
case (*semaphore) <- empty{}:
|
||||
acquired = true
|
||||
case <-timer.C:
|
||||
acquired = false
|
||||
|
|
@ -61,7 +59,7 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
|
|||
// 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{}:
|
||||
case (*semaphore) <- empty{}:
|
||||
acquired = true
|
||||
case <-ctx.Done():
|
||||
acquired = false
|
||||
|
|
|
|||
17
irc/utils/types.go
Normal file
17
irc/utils/types.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
// Copyright (c) 2020 Shivaram Lingamneni
|
||||
// released under the MIT license
|
||||
|
||||
package utils
|
||||
|
||||
type empty struct{}
|
||||
|
||||
type StringSet map[string]empty
|
||||
|
||||
func (s StringSet) Has(str string) bool {
|
||||
_, ok := s[str]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s StringSet) Add(str string) {
|
||||
s[str] = empty{}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue