-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpool.go
103 lines (89 loc) · 2.04 KB
/
pool.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
package fgbase
import (
"sync"
)
// Pool of Node's
type Pool struct {
nodes []Node
size int
free int
mu sync.Mutex
}
// Nodes returns the Pool's slice of Node.
func (p *Pool) Nodes() []Node { return p.nodes }
// NumFree is the number of available Nodes in the Pool of Node's
func (p *Pool) NumFree() int { return p.free }
// Size is the total size of the Pool of Node's.
func (p *Pool) Size() int { return p.size }
// Mutex returns the mutex for this Pool.
func (p *Pool) Mutex() *sync.Mutex { return &p.mu }
// Free increments the number of free Node's in the Pool.
func (p *Pool) Free(n *Node, incr int) bool {
p.mu.Lock()
defer p.mu.Unlock()
if p.free+incr < p.size {
p.free += incr
p.Trace(n)
return true
}
n.LogError("Unexpected attempt to free node after pool is full.")
return false
}
// Alloc decrements the number of free Node's in the Pool.
func (p *Pool) Alloc(n *Node, decr int) bool {
p.mu.Lock()
defer p.mu.Unlock()
if p.free >= decr {
p.free -= decr
p.Trace(n)
return true
}
return false
}
// Trace logs the current number of free Pool Node's using "*"
// (or "X" every ten if Pool larger than 100).
func (p *Pool) Trace(n *Node) {
if TraceLevel >= V {
delta, c := 1, "*"
if p.size > 100 {
delta = 10
c = "X"
}
n.Tracef("\tpoolfree\t%d\t%s\n", p.free, func() string {
var s string
for i := 0; i < p.free; i += delta {
s += c
}
return s
}())
}
}
// MakePool returns a Pool of Nodes that share both
// data channels and the source ack channel.
func MakePool(
size int,
name string,
srcs, dsts []Edge,
ready NodeRdy,
fire NodeFire,
recurse, spread bool) *Pool {
var p Pool
p.size = size
p.nodes = MakeNodes(size)
p.free = size
for i := 0; i < size; i++ {
var srcs2, dsts2 []Edge
if !spread {
for j := 0; j < len(srcs); j++ {
srcs2 = append(srcs2, srcs[j])
}
} else {
srcs2 = append(srcs2, srcs[i])
}
for j := 0; j < len(dsts); j++ {
dsts2 = append(dsts2, dsts[j])
}
p.nodes[i] = makeNodeForPool(name, srcs2, dsts2, ready, fire, recurse)
}
return &p
}