-
Notifications
You must be signed in to change notification settings - Fork 1
/
sema.go
122 lines (108 loc) · 2.7 KB
/
sema.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package concurrent
import "sync"
// Semaphore
type Semaphore interface {
// Acquire a permit from this semaphore, blocking until one is available.
Acquire()
// Return a permit to the semaphore.
// Over release will not make the available permits exceeds capacity.
Release()
// Acquire a permit from this semaphore, only if one is available at the time of invocation,
// never blocking.
TryAcquire() bool
}
// New a ChanSemaphore.
//
// permits: max permits this semaphore could be acquired.
func NewChanSemaphore(permits int64) *ChanSemaphore {
return &ChanSemaphore{
permits: make(chan int, permits),
}
}
// New a LockSemaphore.
//
// permits: max permits this semaphore could be acquired.
func NewLockSemaphore(permits int64) *LockSemaphore {
s := &LockSemaphore{
permits: permits,
}
s.notFull = sync.NewCond(&s.lock)
return s
}
// A Semaphore implementation using channel
type ChanSemaphore struct {
permits chan int
}
func (s *ChanSemaphore) Acquire() {
// put into the channel, if the channel is full, will block
s.permits <- 0
}
func (s *ChanSemaphore) Release() {
select {
case <-s.permits:
default:
// nothing in the channel
}
}
func (s *ChanSemaphore) TryAcquire() bool {
select {
case s.permits <- 0:
// channel not full
return true
default:
// channel full
return false
}
}
// A Semaphore implementation using sync.Mutex
type LockSemaphore struct {
notFull *sync.Cond
lock sync.Mutex
permits int64
count int64
}
func (s *LockSemaphore) Acquire() {
s.lock.Lock()
for s.isFull() {
s.notFull.Wait()
}
s.count++
s.lock.Unlock()
}
func (s *LockSemaphore) Release() {
s.lock.Lock()
defer s.lock.Unlock()
if s.count <= 0 {
return
}
s.count--
s.notFull.Broadcast()
}
func (s *LockSemaphore) TryAcquire() bool {
s.lock.Lock()
defer s.lock.Unlock()
if s.isFull() {
return false
}
s.count++
return true
}
func (s *LockSemaphore) isFull() bool {
return s.count >= s.permits
}