-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgo_abql.go
More file actions
40 lines (35 loc) · 729 Bytes
/
Copy pathgo_abql.go
File metadata and controls
40 lines (35 loc) · 729 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
package main
import (
"sync/atomic"
"time"
)
type Go_ABQL struct {
arr []int
queue_sz int
ticket uint32
dequeueCount uint32
}
func NewGo_ABQL() *Go_ABQL {
abql := Go_ABQL{
make([]int, QUEUE_SZ),
QUEUE_SZ,
0,
0}
abql.arr[0] = 1
return &abql
}
func (l *Go_ABQL) Lock() int {
ticket := atomic.AddUint32(&l.ticket, 1) - 1
for ticket-atomic.LoadUint32(&l.dequeueCount) >= uint32(l.queue_sz) {
time.Sleep(SLEEP_NS * time.Nanosecond)
}
for l.arr[ticket%uint32(l.queue_sz)] != 1 {
time.Sleep(SLEEP_NS * time.Nanosecond)
}
return int(ticket)
}
func (l *Go_ABQL) Unlock(ticket int) {
l.arr[ticket%l.queue_sz] = 0
l.arr[(ticket+1)%l.queue_sz] = 1
atomic.AddUint32(&l.dequeueCount, 1)
}