-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1114_PrintInOrder.py
More file actions
161 lines (123 loc) · 4.65 KB
/
Copy path1114_PrintInOrder.py
File metadata and controls
161 lines (123 loc) · 4.65 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import time
from threading import Thread, Lock, Semaphore, Condition, Event, Barrier
class Foo:
"""
Ensure that the order of execution is First, Second, Third, in threading situations
"""
def __init__(self):
self.output_list = []
def update_list(self, i: int) -> None:
self.output_list.append(i)
def apply_function(self, id: int, wait_time: float) -> None:
time.sleep(wait_time)
if id == 0:
self.first_implementation()
elif id == 1:
self.second_implementation()
else:
self.third_implementation()
def first_implementation(self) -> None:
pass
def second_implementation(self) -> None:
pass
def third_implementation(self) -> None:
pass
class Foo_Lock(Foo):
def __init__(self):
super().__init__()
self.first_job_done = Lock()
self.second_job_done = Lock()
self.first_job_done.acquire()
self.second_job_done.acquire()
def first_implementation(self) -> None:
self.update_list(1)
self.first_job_done.release()
def second_implementation(self) -> None:
with self.first_job_done:
self.update_list(2)
self.second_job_done.release()
def third_implementation(self) -> None:
with self.second_job_done:
self.update_list(3)
class Foo_Semaphore(Foo):
def __init__(self):
super().__init__()
self.first_job_done = Semaphore(1)
self.second_job_done = Semaphore(1)
self.first_job_done.acquire()
self.second_job_done.acquire()
def first_implementation(self) -> None:
self.update_list(1)
self.first_job_done.release()
def second_implementation(self) -> None:
with self.first_job_done:
self.update_list(2)
self.second_job_done.release()
def third_implementation(self) -> None:
with self.second_job_done:
self.update_list(3)
class Foo_Condition(Foo):
def __init__(self):
super().__init__()
self.condition_channel = Condition()
self.first_job_done = self.second_job_done = False
def first_implementation(self) -> None:
with self.condition_channel:
self.update_list(1)
self.first_job_done = True
self.condition_channel.notify_all()
def second_implementation(self) -> None:
with self.condition_channel:
self.condition_channel.wait_for(lambda: self.first_job_done)
self.update_list(2)
self.second_job_done = True
self.condition_channel.notify_all()
def third_implementation(self) -> None:
with self.condition_channel:
self.condition_channel.wait_for(lambda: self.second_job_done)
self.update_list(3)
class Foo_Event(Foo):
def __init__(self):
super().__init__()
self.first_job_done = Event()
self.second_job_done = Event()
def first_implementation(self) -> None:
self.update_list(1)
self.first_job_done.set()
def second_implementation(self) -> None:
self.first_job_done.wait()
self.update_list(2)
self.second_job_done.set()
def third_implementation(self) -> None:
self.second_job_done.wait()
self.update_list(3)
class Foo_Barrier(Foo):
def __init__(self):
super().__init__()
self.first_job_done = Barrier(2)
self.second_job_done = Barrier(2)
def first_implementation(self) -> None:
self.update_list(1)
self.first_job_done.wait()
def second_implementation(self) -> None:
self.first_job_done.wait()
self.update_list(2)
self.second_job_done.wait()
def third_implementation(self) -> None:
self.second_job_done.wait()
self.update_list(3)
time_factor = .02
for object_class in [Foo_Lock, Foo_Semaphore, Foo_Condition, Foo_Event, Foo_Barrier, ]:
print("Testing", object_class.__name__)
for wait_permutation in [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1), ]:
object_instance = object_class()
thread_list = []
for thread_id, permutation_id in enumerate(wait_permutation):
thread_no = Thread(target=object_instance.apply_function, args=(thread_id,
wait_permutation[thread_id] * time_factor))
thread_no.start()
thread_list.append(thread_no)
for thread_no in thread_list:
thread_no.join()
assert object_instance.output_list == [1, 2, 3], object_instance.output_list
print("Test Passed for", object_class.__name__)