Repository navigation
Expand file tree
/
Copy pathguards.py
More file actions
196 lines (153 loc) · 4.86 KB
/
Copy pathguards.py
File metadata and controls
196 lines (153 loc) · 4.86 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/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/>.
"""
__date__ = 'May 2010'
import os
if os.environ.has_key('CSP'):
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 *
class Skip(Guard):
"""Guard which will always return C{True}. Useful in L{Alt}s where
the programmer wants to ensure that L{Alt.select} will always
synchronise with at least one guard.
"""
def __init__(self):
Guard.__init__(self)
self.name = None
def is_selectable(self):
"""Skip is always selectable."""
return True
def enable(self):
"""Has no effect."""
return
def disable(self):
"""Has no effect."""
return
def select(self):
"""Has no effect."""
return 'Skip'
def __str__(self):
return 'Skip guard is always selectable.'
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 = processing.Condition()
return
if os.environ.has_key('CSP'):
if os.environ['CSP'] == 'PROCESSES':
Barrier = BarrierProcessing
elif os.environ['CSP'] == 'THREADS':
Barrier = BarrierThreading
else:
Barrier = BarrierProcessing
else:
Barrier = BarrierProcessing