|
@@ -3,9 +3,9 @@
|
3
|
3
|
package irc
|
4
|
4
|
|
5
|
5
|
import (
|
6
|
|
- "log"
|
7
|
6
|
"runtime"
|
8
|
|
- "runtime/debug"
|
|
7
|
+
|
|
8
|
+ "github.com/oragono/oragono/irc/utils"
|
9
|
9
|
)
|
10
|
10
|
|
11
|
11
|
// See #237 for context. Operations that might allocate large amounts of temporary
|
|
@@ -21,15 +21,12 @@ const (
|
21
|
21
|
MaxServerSemaphoreCapacity = 32
|
22
|
22
|
)
|
23
|
23
|
|
24
|
|
-// Semaphore is a counting semaphore. Note that a capacity of n requires O(n) storage.
|
25
|
|
-type Semaphore (chan bool)
|
26
|
|
-
|
27
|
24
|
// ServerSemaphores includes a named Semaphore corresponding to each concurrency-limited
|
28
|
25
|
// sever operation.
|
29
|
26
|
type ServerSemaphores struct {
|
30
|
27
|
// each distinct operation MUST have its own semaphore;
|
31
|
28
|
// methods that acquire a semaphore MUST NOT call methods that acquire another
|
32
|
|
- ClientDestroy Semaphore
|
|
29
|
+ ClientDestroy utils.Semaphore
|
33
|
30
|
}
|
34
|
31
|
|
35
|
32
|
// Initialize initializes a set of server semaphores.
|
|
@@ -41,40 +38,3 @@ func (serversem *ServerSemaphores) Initialize() {
|
41
|
38
|
serversem.ClientDestroy.Initialize(capacity)
|
42
|
39
|
return
|
43
|
40
|
}
|
44
|
|
-
|
45
|
|
-// Initialize initializes a semaphore to a given capacity.
|
46
|
|
-func (semaphore *Semaphore) Initialize(capacity int) {
|
47
|
|
- *semaphore = make(chan bool, capacity)
|
48
|
|
- for i := 0; i < capacity; i++ {
|
49
|
|
- (*semaphore) <- true
|
50
|
|
- }
|
51
|
|
-}
|
52
|
|
-
|
53
|
|
-// Acquire acquires a semaphore, blocking if necessary.
|
54
|
|
-func (semaphore *Semaphore) Acquire() {
|
55
|
|
- <-(*semaphore)
|
56
|
|
-}
|
57
|
|
-
|
58
|
|
-// TryAcquire tries to acquire a semaphore, returning whether the acquire was
|
59
|
|
-// successful. It never blocks.
|
60
|
|
-func (semaphore *Semaphore) TryAcquire() (acquired bool) {
|
61
|
|
- select {
|
62
|
|
- case <-(*semaphore):
|
63
|
|
- return true
|
64
|
|
- default:
|
65
|
|
- return false
|
66
|
|
- }
|
67
|
|
-}
|
68
|
|
-
|
69
|
|
-// Release releases a semaphore. It never blocks. (This is not a license
|
70
|
|
-// to program spurious releases.)
|
71
|
|
-func (semaphore *Semaphore) Release() {
|
72
|
|
- select {
|
73
|
|
- case (*semaphore) <- true:
|
74
|
|
- // good
|
75
|
|
- default:
|
76
|
|
- // spurious release
|
77
|
|
- log.Printf("spurious semaphore release (full to capacity %d)", cap(*semaphore))
|
78
|
|
- debug.PrintStack()
|
79
|
|
- }
|
80
|
|
-}
|