-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutilities_test.go
More file actions
58 lines (48 loc) · 836 Bytes
/
utilities_test.go
File metadata and controls
58 lines (48 loc) · 836 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
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
package gopool_test
import (
"sync"
)
type safeCounter struct {
mu sync.Mutex
counter int
}
func (c *safeCounter) Inc() {
c.mu.Lock()
defer c.mu.Unlock()
c.counter++
}
func (c *safeCounter) Set(val int) {
c.mu.Lock()
defer c.mu.Unlock()
c.counter = val
}
func (c *safeCounter) Val() int {
c.mu.Lock()
defer c.mu.Unlock()
return c.counter
}
type safeIntSlice struct {
mu sync.Mutex
slice []int
}
func (c *safeIntSlice) Init(val []int) *safeIntSlice {
c.mu.Lock()
defer c.mu.Unlock()
c.slice = val
return c
}
func (c *safeIntSlice) Set(key int, val int) {
c.mu.Lock()
defer c.mu.Unlock()
c.slice[key] = val
}
func (c *safeIntSlice) Get(key int) int {
c.mu.Lock()
defer c.mu.Unlock()
return c.slice[key]
}
func (c *safeIntSlice) Slice() []int {
c.mu.Lock()
defer c.mu.Unlock()
return c.slice
}