-
Notifications
You must be signed in to change notification settings - Fork 8
/
semaphore.go
81 lines (70 loc) · 2.02 KB
/
semaphore.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
package client
import "github.com/snower/slock/protocol"
type Semaphore struct {
db *Database
semaphoreKey [16]byte
timeout uint32
expried uint32
count uint16
}
func NewSemaphore(db *Database, semaphoreKey [16]byte, timeout uint32, expried uint32, count uint16) *Semaphore {
if count > 0 {
count = count - 1
}
return &Semaphore{db, semaphoreKey, timeout, expried, count}
}
func (self *Semaphore) GetSemaphoreKey() [16]byte {
return self.semaphoreKey
}
func (self *Semaphore) GetTimeout() uint32 {
return self.timeout
}
func (self *Semaphore) GetExpried() uint32 {
return self.expried
}
func (self *Semaphore) Acquire() (*protocol.LockResultCommand, error) {
lock := &Lock{self.db, self.db.GenLockId(), self.semaphoreKey, self.timeout, self.expried, self.count, 0}
return lock.Lock()
}
func (self *Semaphore) Release() (*protocol.LockResultCommand, error) {
lock := &Lock{self.db, [16]byte{}, self.semaphoreKey, self.timeout, self.expried, self.count, 0}
return lock.UnlockHead()
}
func (self *Semaphore) ReleaseN(n int) (int, error) {
lock := &Lock{self.db, [16]byte{}, self.semaphoreKey, self.timeout, self.expried, self.count, 0}
for i := 0; i < n; i++ {
_, err := lock.UnlockHead()
if err != nil {
return i + 1, err
}
}
return n, nil
}
func (self *Semaphore) ReleaseAll() error {
lock := &Lock{self.db, [16]byte{}, self.semaphoreKey, self.timeout, self.expried, self.count, 0}
for {
result, err := lock.UnlockHead()
if result != nil && result.Result == protocol.RESULT_UNLOCK_ERROR {
return nil
}
if err != nil {
return err
}
}
}
func (self *Semaphore) Count() (int, error) {
lock := &Lock{self.db, self.db.GenLockId(), self.semaphoreKey, 0, 0, self.count, 0}
result, err := lock.LockShow()
if result != nil {
if result.Result == protocol.RESULT_SUCCED {
return 0, nil
}
if result.Result == protocol.RESULT_UNOWN_ERROR {
return int(result.Lcount), nil
}
if result.Result == protocol.RESULT_TIMEOUT {
return int(self.count), nil
}
}
return 0, err
}