File: semaphore.go

package info (click to toggle)
golang-github-siddontang-go 0.0~git20170517.0.cb568a3-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 492 kB
  • sloc: makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,188 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
	}
}