-
Notifications
You must be signed in to change notification settings - Fork 8
/
GroupLock.py
71 lines (49 loc) · 2.77 KB
/
GroupLock.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
63
64
65
66
67
68
69
70
71
from threading import Lock, Condition
class GroupLock:
'''Queues asynchronus threads by group.
Args:
groups (key list list) : a list of lists of keys which represent the keys for each thread in each group.
e.g. -> [['thread1','thread2'], ['thread3']]'''
def __init__(self, groups):
self.groups = groups
self.activeGroup = 0
self.hasReleased = [{member:False for member in group} for group in groups]
self.numGroups = len(groups)
self._groupConditions = [{member:Condition(Lock()) for member in group} for group in groups]
def acquire(self, group, id):
'''Acquires the lock, blocks if not the thread's turn yet. A thread can acquire the lock once per group cycle.
e.g.
if GroupLock was initialized with [['thread1','thread2'], ['thread3']], a calls would be
GroupLock.acquire(1, 'thread3')
GroupLock.acquire(0, 'thread2')
Args:
group (int): number of calling thread's group
id (key): key given to identify the thread (given in init)'''
self._groupConditions[group][id].acquire()
if self.hasReleased[group][id] or self.activeGroup != group:
self._groupConditions[group][id].wait()
def release(self, group, id):
'''Releases the group lock. All threads in a lock must release before the next group's turn can begin.
e.g.
if GroupLock was initialized with [['thread1','thread2'], ['thread3']], a calls would be
GroupLock.acquire(1, 'thread3')
GroupLock.acquire(0, 'thread2')
Args:
group (int): number of calling thread's group
id (key): key given to identify the thread(given in init)'''
self._groupConditions[group][id].release()
self.hasReleased[group][id] = True
if all(self.hasReleased[group].values()):
self.hasReleased[group] = {member:False for member in self.hasReleased[group]}
self.activeGroup = (self.activeGroup + 1) % len(self.groups)
releasedGroup = self.activeGroup
for memberCondition in self._groupConditions[releasedGroup]:
self._groupConditions[releasedGroup][memberCondition].acquire()
self._groupConditions[releasedGroup][memberCondition].notify_all()
self._groupConditions[releasedGroup][memberCondition].release()
def releaseAll(self):
releasedGroup = self.activeGroup
for memberCondition in self._groupConditions[releasedGroup]:
self._groupConditions[releasedGroup][memberCondition].acquire()
self._groupConditions[releasedGroup][memberCondition].notify_all()
self._groupConditions[releasedGroup][memberCondition].release()