-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelp_queue.py
62 lines (48 loc) · 1.66 KB
/
help_queue.py
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
import threading
from typing import Optional
class Node:
def __init__(self, value: int) -> None:
self._padding: bytes = b"\x00" * (64 - (8 + 8))
self.value: int = value
self.next: Optional[Node] = None
self.mutex: threading.Lock = threading.Lock()
class Queue:
def __init__(self) -> None:
dummy_value: int = 0
self.dummy_node: Node = Node(dummy_value)
self.head: Node = self.dummy_node
self.tail: Node = self.dummy_node
self.mutex: threading.Lock = threading.Lock()
def try_remove_front(queue: Queue, front: int) -> bool:
with queue.mutex:
head: Node = queue.head
if head.next is not None:
with head.mutex:
next_node: Node = head.next
if next_node.value == front:
head.next = next_node.next
return True
return False
def help_finish_enq(queue: Queue) -> None:
next_node: Optional[Node] = queue.tail.next
if next_node is not None and next_node == queue.tail:
with queue.tail.mutex:
with queue.head.mutex:
queue.tail.next = next_node
queue.tail = next_node
def enqueue(queue: Queue, value: int) -> None:
node: Node = Node(value)
with queue.tail.mutex:
queue.tail.next = node
help_finish_enq(queue)
def main() -> None:
q: Queue = Queue()
front_value: int = 10
enqueue(q, front_value)
result: bool = try_remove_front(q, front_value)
if result:
print(f"Successfully removed front node: {front_value}")
else:
print("Failed to remove front node")
if __name__ == "__main__":
main()