-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsemaphore.go
More file actions
33 lines (26 loc) · 769 Bytes
/
semaphore.go
File metadata and controls
33 lines (26 loc) · 769 Bytes
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
package main
// Semaphore is an interface to represent a Semaphore. This is used when
// executing a graph to control the number of concurrent processes running.
type Semaphore interface {
P()
V()
}
// NewSemaphore returns a new Semaphore implementation that limits the level of
// concurrency. If concurrency is less than 0, then it returns a semaphore that
// does not limit the amount of concurrency.
func NewSemaphore(concurrency uint) Semaphore {
if concurrency == 0 {
return new(unlimitedSemaphore)
}
return make(semaphore, concurrency)
}
type semaphore chan struct{}
func (s semaphore) P() {
s <- struct{}{}
}
func (s semaphore) V() {
<-s
}
type unlimitedSemaphore struct{}
func (s *unlimitedSemaphore) P() {}
func (s *unlimitedSemaphore) V() {}