remove unnecessary indirection in semaphore

This commit is contained in:
Shivaram Lingamneni
2021-04-07 08:44:17 -04:00
parent 549d06bc98
commit 5b33cd436f
7 changed files with 39 additions and 40 deletions
+13 -13
View File
@@ -12,21 +12,21 @@ import (
// A semaphore of capacity 1 can be used as a trylock.
type Semaphore (chan empty)
// Initialize initializes a semaphore to a given capacity.
func (semaphore *Semaphore) Initialize(capacity int) {
*semaphore = make(chan empty, capacity)
// NewSemaphore creates and initializes a semaphore to a given capacity.
func NewSemaphore(capacity int) Semaphore {
return make(chan empty, capacity)
}
// Acquire acquires a semaphore, blocking if necessary.
func (semaphore *Semaphore) Acquire() {
(*semaphore) <- empty{}
func (semaphore Semaphore) Acquire() {
semaphore <- empty{}
}
// TryAcquire tries to acquire a semaphore, returning whether the acquire was
// successful. It never blocks.
func (semaphore *Semaphore) TryAcquire() (acquired bool) {
func (semaphore Semaphore) TryAcquire() (acquired bool) {
select {
case (*semaphore) <- empty{}:
case semaphore <- empty{}:
return true
default:
return false
@@ -36,14 +36,14 @@ func (semaphore *Semaphore) TryAcquire() (acquired bool) {
// AcquireWithTimeout tries to acquire a semaphore, blocking for a maximum
// of approximately `d` while waiting for it. It returns whether the acquire
// was successful.
func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
func (semaphore Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired bool) {
if timeout < 0 {
return semaphore.TryAcquire()
}
timer := time.NewTimer(timeout)
select {
case (*semaphore) <- empty{}:
case semaphore <- empty{}:
acquired = true
case <-timer.C:
acquired = false
@@ -55,9 +55,9 @@ func (semaphore *Semaphore) AcquireWithTimeout(timeout time.Duration) (acquired
// 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) {
func (semaphore Semaphore) AcquireWithContext(ctx context.Context) (acquired bool) {
select {
case (*semaphore) <- empty{}:
case semaphore <- empty{}:
acquired = true
case <-ctx.Done():
acquired = false
@@ -66,6 +66,6 @@ func (semaphore *Semaphore) AcquireWithContext(ctx context.Context) (acquired bo
}
// Release releases a semaphore.
func (semaphore *Semaphore) Release() {
<-(*semaphore)
func (semaphore Semaphore) Release() {
<-semaphore
}
+2 -4
View File
@@ -10,8 +10,7 @@ import (
func TestTryAcquire(t *testing.T) {
count := 3
var sem Semaphore
sem.Initialize(count)
sem := NewSemaphore(count)
for i := 0; i < count; i++ {
assertEqual(sem.TryAcquire(), true, t)
@@ -24,8 +23,7 @@ func TestTryAcquire(t *testing.T) {
}
func TestAcquireWithTimeout(t *testing.T) {
var sem Semaphore
sem.Initialize(1)
sem := NewSemaphore(1)
assertEqual(sem.TryAcquire(), true, t)