-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsemaphore.go
142 lines (127 loc) · 3.93 KB
/
semaphore.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
*
* sync - Synchronization facilities.
* Copyright (C) 2019 Antigloss Huang (https://github.com/antigloss) All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Package sync provides extra synchronization facilities such as semaphore in addition to the standard sync package.
package sync
import (
"container/list"
"sync"
"sync/atomic"
"time"
"unsafe"
)
// SemaphoreResource holds resources acquired from a Semaphore object.
type SemaphoreResource struct {
sema unsafe.Pointer
}
// Release releases resources acquired from a Semaphore object.
func (sr *SemaphoreResource) Release() {
var newVal unsafe.Pointer
old := atomic.SwapPointer(&sr.sema, newVal)
if old != nil {
(*Semaphore)(old).release()
}
}
// Semaphore is a mimic of the POSIX semaphore based on channel and sync.Mutex. It could be used to limit the number of concurrent running goroutines.
// Basic example:
//
// // Creates a ready-to-use semaphore
// sema := concurrent.NewSemaphore(InitValue)
// // Decrements the semaphore, blocks if value of the semaphore is less than 1
// semaResource := sema.Acquire()
// // Increments the semaphore. If the semaphore's value consequently becomes greater than zero,
// // then another goroutine blocked in sema.Acquire() will be woken up and acquire the resources.
// semaResource.Release()
type Semaphore struct {
lock sync.Mutex
value int
waiters list.List
}
// NewSemaphore creates a ready-to-use Semaphore.
//
// value: Initial value of the Semaphore.
func NewSemaphore(value int) *Semaphore {
return &Semaphore{value: value}
}
// Acquire decrements the semaphore, blocks if value of the semaphore is less than 1.
func (s *Semaphore) Acquire() *SemaphoreResource {
s.lock.Lock()
if s.value > 0 {
s.value--
s.lock.Unlock()
return &SemaphoreResource{sema: unsafe.Pointer(s)}
}
ready := make(chan bool)
s.waiters.PushBack(ready)
s.lock.Unlock()
<-ready
return &SemaphoreResource{sema: unsafe.Pointer(s)}
}
// TryAcquire tries to decrement the semaphore. It returns nil if the decrement cannot be done immediately.
func (s *Semaphore) TryAcquire() (sr *SemaphoreResource) {
s.lock.Lock()
if s.value > 0 {
s.value--
sr = &SemaphoreResource{sema: unsafe.Pointer(s)}
}
s.lock.Unlock()
return
}
// TimedAcquire waits at most a given time to decrement the semaphore. It returns nil if the decrement cannot be done after the given timeout.
func (s *Semaphore) TimedAcquire(duration time.Duration) (sr *SemaphoreResource) {
s.lock.Lock()
if s.value > 0 {
s.value--
s.lock.Unlock()
sr = &SemaphoreResource{sema: unsafe.Pointer(s)}
return
}
ready := make(chan bool)
elem := s.waiters.PushBack(ready)
s.lock.Unlock()
timer := time.NewTimer(duration)
select {
case <-timer.C:
s.lock.Lock()
select {
case <-ready:
sr = &SemaphoreResource{sema: unsafe.Pointer(s)}
default:
s.waiters.Remove(elem)
}
s.lock.Unlock()
case <-ready:
timer.Stop()
sr = &SemaphoreResource{sema: unsafe.Pointer(s)}
}
return
}
// release increments the semaphore. If the semaphore’s value consequently becomes greater than zero,
// then another goroutine blocked in sema.Acquire() will be woken up and acquire the resources.
func (s *Semaphore) release() {
s.lock.Lock()
waiter := s.waiters.Front()
if waiter == nil {
s.value++
} else {
s.waiters.Remove(waiter)
close(waiter.Value.(chan bool))
}
s.lock.Unlock()
}