forked from owasp-amass/amass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semaphores.go
152 lines (129 loc) · 3.09 KB
/
semaphores.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
143
144
145
146
147
148
149
150
151
152
// Copyright 2017 Jeff Foley. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
package utils
import (
"time"
)
// Semaphore is the interface for the several types of semaphore implemented by Amass.
type Semaphore interface {
// Acquire blocks until num resource counts have been obtained
Acquire(num int)
// TryAcquire attempts to obtain num resource counts without blocking
TryAcquire(num int) bool
// Release causes num resource counts to be released
Release(num int)
}
// SimpleSemaphore implements a synchronization object
// type capable of being a counting semaphore.
type SimpleSemaphore struct {
c chan struct{}
}
// NewSimpleSemaphore returns a SimpleSemaphore initialized to max resource counts.
func NewSimpleSemaphore(max int) Semaphore {
sem := &SimpleSemaphore{
c: make(chan struct{}, max),
}
for i := 0; i < max; i++ {
sem.c <- struct{}{}
}
return sem
}
// Acquire blocks until num resource counts have been obtained.
func (s *SimpleSemaphore) Acquire(num int) {
for i := 0; i < num; i++ {
<-s.c
}
}
// TryAcquire attempts to obtain num resource counts without blocking.
// The method returns true when successful in acquiring the resource counts.
func (s *SimpleSemaphore) TryAcquire(num int) bool {
var count int
loop:
for i := 0; i < num; i++ {
select {
case <-s.c:
count++
default:
break loop
}
}
if count == num {
return true
}
s.Release(count)
return false
}
// Release causes num resource counts to be released.
func (s *SimpleSemaphore) Release(num int) {
for i := 0; i < num; i++ {
s.c <- struct{}{}
}
}
// TimedSemaphore implements a synchronization object
// type capable of being a counting semaphore.
type TimedSemaphore struct {
c chan struct{}
rel chan int
del time.Duration
}
// NewTimedSemaphore returns a TimedSemaphore initialized to max resource counts
// and delay release frequency.
func NewTimedSemaphore(max int, delay time.Duration) Semaphore {
sem := &TimedSemaphore{
c: make(chan struct{}, max),
rel: make(chan int, max),
del: delay,
}
for i := 0; i < max; i++ {
sem.c <- struct{}{}
}
go sem.processReleases()
return sem
}
// Acquire blocks until num resource counts have been obtained.
func (t *TimedSemaphore) Acquire(num int) {
for i := 0; i < num; i++ {
<-t.c
}
}
// TryAcquire attempts to obtain num resource counts without blocking.
// The method returns true when successful in acquiring the resource counts.
func (t *TimedSemaphore) TryAcquire(num int) bool {
var count int
loop:
for i := 0; i < num; i++ {
select {
case <-t.c:
count++
default:
break loop
}
}
if count == num {
return true
}
for i := 0; i < count; i++ {
t.c <- struct{}{}
}
return false
}
// Release causes num resource counts to be released.
func (t *TimedSemaphore) Release(num int) {
t.rel <- num
}
func (t *TimedSemaphore) processReleases() {
tick := time.NewTicker(t.del)
defer tick.Stop()
var rcount int
for {
select {
case <-tick.C:
if rcount > 0 {
t.c <- struct{}{}
rcount--
}
case num := <-t.rel:
rcount += num
}
}
}