-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.go
70 lines (60 loc) · 1.17 KB
/
sync.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
package limitedbuffer
import "sync"
// WithSync Sync Limited Buffer.
type WithSync struct {
lb LimitedBuffer
mu sync.Mutex
}
func (w *WithSync) Read(p []byte) (n int, err error) {
w.mu.Lock()
n, err = w.lb.Read(p)
w.mu.Unlock()
return
}
func (w *WithSync) Write(p []byte) (n int, err error) {
w.mu.Lock()
n, err = w.lb.Write(p)
w.mu.Unlock()
return
}
// IsFull if full cannot write
func (w *WithSync) IsFull() bool {
w.mu.Lock()
ret := w.lb.IsFull()
w.mu.Unlock()
return ret
}
// IsEmpty if empty cannot read
func (w *WithSync) IsEmpty() bool {
w.mu.Lock()
ret := w.lb.IsEmpty()
w.mu.Unlock()
return ret
}
// Reset reset read and write position to 0
func (w *WithSync) Reset() {
w.mu.Lock()
w.lb.Reset()
w.mu.Unlock()
}
// Capacity return fixed buffer size
func (w *WithSync) Capacity() int {
w.mu.Lock()
c := w.lb.Capacity()
w.mu.Unlock()
return c
}
// Status current buffer status
func (w *WithSync) Status() BufferStatus {
w.mu.Lock()
bs := w.lb.Status()
w.mu.Unlock()
return bs
}
// NewSyncCycleBuffer New Sync CycleBuffer
func NewSyncCycleBuffer(capacity int) LimitedBuffer {
return &WithSync{
mu: sync.Mutex{},
lb: NewCycleBuffer(capacity),
}
}