mirror of https://github.com/siddontang/go.git
add semaphore
This commit is contained in:
parent
dfa1fad4a1
commit
530a231625
|
@ -0,0 +1,65 @@
|
|||
package sync2
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewSemaphore(initialCount int) *Semaphore {
|
||||
res := &Semaphore{
|
||||
counter: int64(initialCount),
|
||||
}
|
||||
res.cond.L = &res.lock
|
||||
return res
|
||||
}
|
||||
|
||||
type Semaphore struct {
|
||||
lock sync.Mutex
|
||||
cond sync.Cond
|
||||
counter int64
|
||||
}
|
||||
|
||||
func (s *Semaphore) Release() {
|
||||
s.lock.Lock()
|
||||
s.counter += 1
|
||||
if s.counter >= 0 {
|
||||
s.cond.Signal()
|
||||
}
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Semaphore) Acquire() {
|
||||
s.lock.Lock()
|
||||
for s.counter < 1 {
|
||||
s.cond.Wait()
|
||||
}
|
||||
s.counter -= 1
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Semaphore) AcquireTimeout(timeout time.Duration) bool {
|
||||
done := make(chan bool, 1)
|
||||
// Gate used to communicate between the threads and decide what the result
|
||||
// is. If the main thread decides, we have timed out, otherwise we succeed.
|
||||
decided := new(int32)
|
||||
go func() {
|
||||
s.Acquire()
|
||||
if atomic.SwapInt32(decided, 1) == 0 {
|
||||
done <- true
|
||||
} else {
|
||||
// If we already decided the result, and this thread did not win
|
||||
s.Release()
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
return true
|
||||
case <-time.NewTimer(timeout).C:
|
||||
if atomic.SwapInt32(decided, 1) == 1 {
|
||||
// The other thread already decided the result
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
// Copyright 2012, Google Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package sync2
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestSemaNoTimeout(t *testing.T) {
|
||||
s := NewSemaphore(1)
|
||||
s.Acquire()
|
||||
released := false
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
released = true
|
||||
s.Release()
|
||||
}()
|
||||
s.Acquire()
|
||||
if !released {
|
||||
t.Errorf("want true, got false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSemaTimeout(t *testing.T) {
|
||||
s := NewSemaphore(1)
|
||||
s.Acquire()
|
||||
go func() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
s.Release()
|
||||
}()
|
||||
if ok := s.AcquireTimeout(5 * time.Millisecond); ok {
|
||||
t.Errorf("want false, got true")
|
||||
}
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
if ok := s.AcquireTimeout(5 * time.Millisecond); !ok {
|
||||
t.Errorf("want true, got false")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue