-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprioq_impl.hny
More file actions
41 lines (35 loc) · 1.12 KB
/
prioq_impl.hny
File metadata and controls
41 lines (35 loc) · 1.12 KB
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
from synch import Lock, acquire, release
from alloc import malloc, free
def PrioQ(n) returns init:
init = { .n: n, .tails: [malloc(None) for _ in {0..n-1}], .lock: Lock() }
def put(pq, prio, v):
let node = malloc({ .value: v, .next: None }):
acquire(?pq->lock)
let slot = pq->tails[prio]:
let tail = !slot:
if tail == None:
node->next = node
!slot = node
else:
node->next = tail->next
tail->next = node
!slot = node
release(?pq->lock)
def get(pq) returns r:
acquire(?pq->lock)
var p = 0
while (p < pq->n) and (!pq->tails[p] == None):
p += 1
if p == pq->n:
r = (False, None)
else:
let slot = pq->tails[p]:
let tail = !slot:
let head = tail->next:
r = (True, head->value)
if head == tail:
!slot = None
else:
tail->next = head->next
free(head)
release(?pq->lock)