-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathguards.py
More file actions
172 lines (136 loc) · 4.32 KB
/
Copy pathguards.py
File metadata and controls
172 lines (136 loc) · 4.32 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
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
"""
Builtin guard types. For builtin processes see csp.builtins.
Copyright (C) Sarah Mount, 2010.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have rceeived a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = 'Sarah Mount <s.mount@wlv.ac.uk>'
__date__ = 'May 2010'
import os
import multiprocessing
import threading
import time
if 'CSP' in os.environ:
if os.environ['CSP'] == 'PROCESSES':
from csp.cspprocess import *
elif os.environ['CSP'] == 'THREADS':
from csp.cspthread import *
else:
from csp.cspprocess import *
else:
from csp.cspprocess import *
### Names exported by this module
__all__ = ['Timer', 'Barrier']
class Timer(Guard):
"""Guard which only commits to synchronisation when a timer has expired.
"""
def __init__(self):
super(Timer, self).__init__()
self.now = time.time()
self.name = 'Timer guard created at:' + str(self.now)
self.alarm = None
return
def set_alarm(self, timeout):
self.now = time.time()
self.alarm = self.now + timeout
return
def is_selectable(self):
self.now = time.time()
if self.alarm is None:
return True
elif self.now < self.alarm:
return False
return True
def read(self):
"""Return current time.
"""
self.now = time.time()
return self.now
def sleep(self, timeout):
"""Put this process to sleep for a number of seconds.
"""
time.sleep(timeout)
return
def enable(self):
return
def disable(self):
return
def select(self):
return
class AbstractBarrier(object):
def __init__(self, participants=0):
self.participants = participants
self.not_ready = participants
self.lock = None # MUST be overridden in subclass
self.reset(participants)
return
def reset(self, participants):
assert participants >= 0
with self.lock:
self.participants = participants
self.not_ready = participants
return
def enrol(self):
with self.lock:
self.participants += 1
self.not_ready += 1
return
def retire(self):
with self.lock:
self.participants -= 1
self.not_ready -= 1
if self.not_ready == 0:
self.not_ready = self.participants
self.lock.notifyAll()
assert self.not_ready >= 0
return
def synchronise(self):
with self.lock:
self.not_ready -= 1
if self.not_ready > 0:
self.lock.wait()
else:
self.not_ready = self.participants
self.lock.notifyAll()
return
def synchronise_withN(self, n):
"""Only syncrhonise when N participants are enrolled.
"""
with self.lock:
if self.participants != n:
self.lock.wait()
self.not_ready -= 1
if self.not_ready > 0:
self.lock.wait()
else:
self.not_ready = self.participants
self.lock.notifyAll()
return
class BarrierThreading(AbstractBarrier):
def __init__(self):
super(BarrierThreading, self).__init__()
self.lock = threading.Condition()
return
class BarrierProcessing(AbstractBarrier):
def __init__(self):
super(BarrierProcessing, self).__init__()
self.lock = multiprocessing.Condition()
return
if 'CSP' in os.environ:
if os.environ['CSP'] == 'PROCESSES':
Barrier = BarrierProcessing
elif os.environ['CSP'] == 'THREADS':
Barrier = BarrierThreading
else:
Barrier = BarrierProcessing
else:
Barrier = BarrierProcessing