root / lib / locking.py @ c6997f21
History | View | Annotate | Download (37 kB)
1 | 162c1c1f | Guido Trotter | #
|
---|---|---|---|
2 | 162c1c1f | Guido Trotter | #
|
3 | 162c1c1f | Guido Trotter | |
4 | 162c1c1f | Guido Trotter | # Copyright (C) 2006, 2007 Google Inc.
|
5 | 162c1c1f | Guido Trotter | #
|
6 | 162c1c1f | Guido Trotter | # This program is free software; you can redistribute it and/or modify
|
7 | 162c1c1f | Guido Trotter | # it under the terms of the GNU General Public License as published by
|
8 | 162c1c1f | Guido Trotter | # the Free Software Foundation; either version 2 of the License, or
|
9 | 162c1c1f | Guido Trotter | # (at your option) any later version.
|
10 | 162c1c1f | Guido Trotter | #
|
11 | 162c1c1f | Guido Trotter | # This program is distributed in the hope that it will be useful, but
|
12 | 162c1c1f | Guido Trotter | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 162c1c1f | Guido Trotter | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 162c1c1f | Guido Trotter | # General Public License for more details.
|
15 | 162c1c1f | Guido Trotter | #
|
16 | 162c1c1f | Guido Trotter | # You should have received a copy of the GNU General Public License
|
17 | 162c1c1f | Guido Trotter | # along with this program; if not, write to the Free Software
|
18 | 162c1c1f | Guido Trotter | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 162c1c1f | Guido Trotter | # 02110-1301, USA.
|
20 | 162c1c1f | Guido Trotter | |
21 | 162c1c1f | Guido Trotter | """Module implementing the Ganeti locking code."""
|
22 | 162c1c1f | Guido Trotter | |
23 | d76167a5 | Michael Hanselmann | import os |
24 | d76167a5 | Michael Hanselmann | import select |
25 | 162c1c1f | Guido Trotter | import threading |
26 | d76167a5 | Michael Hanselmann | import time |
27 | d76167a5 | Michael Hanselmann | import errno |
28 | 84e344d4 | Michael Hanselmann | |
29 | a95fd5d7 | Guido Trotter | from ganeti import errors |
30 | 7ee7c0c7 | Guido Trotter | from ganeti import utils |
31 | 162c1c1f | Guido Trotter | |
32 | 162c1c1f | Guido Trotter | |
33 | 42a999d1 | Guido Trotter | def ssynchronized(lock, shared=0): |
34 | 42a999d1 | Guido Trotter | """Shared Synchronization decorator.
|
35 | 42a999d1 | Guido Trotter |
|
36 | 42a999d1 | Guido Trotter | Calls the function holding the given lock, either in exclusive or shared
|
37 | 42a999d1 | Guido Trotter | mode. It requires the passed lock to be a SharedLock (or support its
|
38 | 42a999d1 | Guido Trotter | semantics).
|
39 | 42a999d1 | Guido Trotter |
|
40 | 42a999d1 | Guido Trotter | """
|
41 | 42a999d1 | Guido Trotter | def wrap(fn): |
42 | 42a999d1 | Guido Trotter | def sync_function(*args, **kwargs): |
43 | 42a999d1 | Guido Trotter | lock.acquire(shared=shared) |
44 | 42a999d1 | Guido Trotter | try:
|
45 | 42a999d1 | Guido Trotter | return fn(*args, **kwargs)
|
46 | 42a999d1 | Guido Trotter | finally:
|
47 | 42a999d1 | Guido Trotter | lock.release() |
48 | 42a999d1 | Guido Trotter | return sync_function
|
49 | 42a999d1 | Guido Trotter | return wrap
|
50 | 42a999d1 | Guido Trotter | |
51 | 42a999d1 | Guido Trotter | |
52 | d76167a5 | Michael Hanselmann | class _SingleActionPipeConditionWaiter(object): |
53 | d76167a5 | Michael Hanselmann | """Callable helper class for _SingleActionPipeCondition.
|
54 | d76167a5 | Michael Hanselmann |
|
55 | d76167a5 | Michael Hanselmann | """
|
56 | d76167a5 | Michael Hanselmann | __slots__ = [ |
57 | d76167a5 | Michael Hanselmann | "_cond",
|
58 | d76167a5 | Michael Hanselmann | "_fd",
|
59 | d76167a5 | Michael Hanselmann | "_poller",
|
60 | d76167a5 | Michael Hanselmann | ] |
61 | d76167a5 | Michael Hanselmann | |
62 | d76167a5 | Michael Hanselmann | def __init__(self, cond, poller, fd): |
63 | d76167a5 | Michael Hanselmann | """Initializes this class.
|
64 | d76167a5 | Michael Hanselmann |
|
65 | d76167a5 | Michael Hanselmann | @type cond: L{_SingleActionPipeCondition}
|
66 | d76167a5 | Michael Hanselmann | @param cond: Parent condition
|
67 | d76167a5 | Michael Hanselmann | @type poller: select.poll
|
68 | d76167a5 | Michael Hanselmann | @param poller: Poller object
|
69 | d76167a5 | Michael Hanselmann | @type fd: int
|
70 | d76167a5 | Michael Hanselmann | @param fd: File descriptor to wait for
|
71 | d76167a5 | Michael Hanselmann |
|
72 | d76167a5 | Michael Hanselmann | """
|
73 | d76167a5 | Michael Hanselmann | object.__init__(self) |
74 | d76167a5 | Michael Hanselmann | |
75 | d76167a5 | Michael Hanselmann | self._cond = cond
|
76 | d76167a5 | Michael Hanselmann | self._poller = poller
|
77 | d76167a5 | Michael Hanselmann | self._fd = fd
|
78 | d76167a5 | Michael Hanselmann | |
79 | d76167a5 | Michael Hanselmann | def __call__(self, timeout): |
80 | d76167a5 | Michael Hanselmann | """Wait for something to happen on the pipe.
|
81 | d76167a5 | Michael Hanselmann |
|
82 | d76167a5 | Michael Hanselmann | @type timeout: float or None
|
83 | d76167a5 | Michael Hanselmann | @param timeout: Timeout for waiting (can be None)
|
84 | d76167a5 | Michael Hanselmann |
|
85 | d76167a5 | Michael Hanselmann | """
|
86 | d76167a5 | Michael Hanselmann | start_time = time.time() |
87 | d76167a5 | Michael Hanselmann | remaining_time = timeout |
88 | d76167a5 | Michael Hanselmann | |
89 | d76167a5 | Michael Hanselmann | while timeout is None or remaining_time > 0: |
90 | d76167a5 | Michael Hanselmann | try:
|
91 | d76167a5 | Michael Hanselmann | result = self._poller.poll(remaining_time)
|
92 | d76167a5 | Michael Hanselmann | except EnvironmentError, err: |
93 | d76167a5 | Michael Hanselmann | if err.errno != errno.EINTR:
|
94 | d76167a5 | Michael Hanselmann | raise
|
95 | d76167a5 | Michael Hanselmann | result = None
|
96 | d76167a5 | Michael Hanselmann | |
97 | d76167a5 | Michael Hanselmann | # Check whether we were notified
|
98 | d76167a5 | Michael Hanselmann | if result and result[0][0] == self._fd: |
99 | d76167a5 | Michael Hanselmann | break
|
100 | d76167a5 | Michael Hanselmann | |
101 | d76167a5 | Michael Hanselmann | # Re-calculate timeout if necessary
|
102 | d76167a5 | Michael Hanselmann | if timeout is not None: |
103 | d76167a5 | Michael Hanselmann | remaining_time = start_time + timeout - time.time() |
104 | d76167a5 | Michael Hanselmann | |
105 | d76167a5 | Michael Hanselmann | |
106 | d76167a5 | Michael Hanselmann | class _SingleActionPipeCondition(object): |
107 | d76167a5 | Michael Hanselmann | """Wrapper around a pipe for usage inside conditions.
|
108 | d76167a5 | Michael Hanselmann |
|
109 | d76167a5 | Michael Hanselmann | This class contains a POSIX pipe(2) and a poller to poll it. The pipe is
|
110 | d76167a5 | Michael Hanselmann | always allocated when constructing the class. Extra care is taken to always
|
111 | d76167a5 | Michael Hanselmann | close the file descriptors.
|
112 | d76167a5 | Michael Hanselmann |
|
113 | d76167a5 | Michael Hanselmann | An additional class, L{_SingleActionPipeConditionWaiter}, is used to wait for
|
114 | d76167a5 | Michael Hanselmann | notifications.
|
115 | d76167a5 | Michael Hanselmann |
|
116 | d76167a5 | Michael Hanselmann | Warning: This class is designed to be used as the underlying component of a
|
117 | d76167a5 | Michael Hanselmann | locking condition, but is not by itself thread safe, and needs to be
|
118 | d76167a5 | Michael Hanselmann | protected by an external lock.
|
119 | d76167a5 | Michael Hanselmann |
|
120 | d76167a5 | Michael Hanselmann | """
|
121 | d76167a5 | Michael Hanselmann | __slots__ = [ |
122 | d76167a5 | Michael Hanselmann | "_poller",
|
123 | d76167a5 | Michael Hanselmann | "_read_fd",
|
124 | d76167a5 | Michael Hanselmann | "_write_fd",
|
125 | d76167a5 | Michael Hanselmann | "_nwaiters",
|
126 | d76167a5 | Michael Hanselmann | ] |
127 | d76167a5 | Michael Hanselmann | |
128 | d76167a5 | Michael Hanselmann | _waiter_class = _SingleActionPipeConditionWaiter |
129 | d76167a5 | Michael Hanselmann | |
130 | d76167a5 | Michael Hanselmann | def __init__(self): |
131 | d76167a5 | Michael Hanselmann | """Initializes this class.
|
132 | d76167a5 | Michael Hanselmann |
|
133 | d76167a5 | Michael Hanselmann | """
|
134 | d76167a5 | Michael Hanselmann | object.__init__(self) |
135 | d76167a5 | Michael Hanselmann | |
136 | d76167a5 | Michael Hanselmann | self._nwaiters = 0 |
137 | d76167a5 | Michael Hanselmann | |
138 | d76167a5 | Michael Hanselmann | # Just assume the unpacking is successful, otherwise error handling gets
|
139 | d76167a5 | Michael Hanselmann | # very complicated.
|
140 | d76167a5 | Michael Hanselmann | (self._read_fd, self._write_fd) = os.pipe() |
141 | d76167a5 | Michael Hanselmann | try:
|
142 | d76167a5 | Michael Hanselmann | # The poller looks for closure of the write side
|
143 | d76167a5 | Michael Hanselmann | poller = select.poll() |
144 | d76167a5 | Michael Hanselmann | poller.register(self._read_fd, select.POLLHUP)
|
145 | d76167a5 | Michael Hanselmann | |
146 | d76167a5 | Michael Hanselmann | self._poller = poller
|
147 | d76167a5 | Michael Hanselmann | except:
|
148 | d76167a5 | Michael Hanselmann | if self._read_fd is not None: |
149 | d76167a5 | Michael Hanselmann | os.close(self._read_fd)
|
150 | d76167a5 | Michael Hanselmann | if self._write_fd is not None: |
151 | d76167a5 | Michael Hanselmann | os.close(self._write_fd)
|
152 | d76167a5 | Michael Hanselmann | raise
|
153 | d76167a5 | Michael Hanselmann | |
154 | d76167a5 | Michael Hanselmann | # There should be no code here anymore, otherwise the pipe file descriptors
|
155 | d76167a5 | Michael Hanselmann | # may be not be cleaned up properly in case of errors.
|
156 | d76167a5 | Michael Hanselmann | |
157 | d76167a5 | Michael Hanselmann | def StartWaiting(self): |
158 | d76167a5 | Michael Hanselmann | """Return function to wait for notification.
|
159 | d76167a5 | Michael Hanselmann |
|
160 | d76167a5 | Michael Hanselmann | @rtype: L{_SingleActionPipeConditionWaiter}
|
161 | d76167a5 | Michael Hanselmann | @return: Function to wait for notification
|
162 | d76167a5 | Michael Hanselmann |
|
163 | d76167a5 | Michael Hanselmann | """
|
164 | d76167a5 | Michael Hanselmann | assert self._nwaiters >= 0 |
165 | d76167a5 | Michael Hanselmann | |
166 | d76167a5 | Michael Hanselmann | if self._poller is None: |
167 | d76167a5 | Michael Hanselmann | raise RuntimeError("Already cleaned up") |
168 | d76167a5 | Michael Hanselmann | |
169 | d76167a5 | Michael Hanselmann | # Create waiter function and increase number of waiters
|
170 | d76167a5 | Michael Hanselmann | wait_fn = self._waiter_class(self, self._poller, self._read_fd) |
171 | d76167a5 | Michael Hanselmann | self._nwaiters += 1 |
172 | d76167a5 | Michael Hanselmann | return wait_fn
|
173 | d76167a5 | Michael Hanselmann | |
174 | d76167a5 | Michael Hanselmann | def DoneWaiting(self): |
175 | d76167a5 | Michael Hanselmann | """Decrement number of waiters and automatic cleanup.
|
176 | d76167a5 | Michael Hanselmann |
|
177 | d76167a5 | Michael Hanselmann | Must be called after waiting for a notification.
|
178 | d76167a5 | Michael Hanselmann |
|
179 | d76167a5 | Michael Hanselmann | @rtype: bool
|
180 | d76167a5 | Michael Hanselmann | @return: Whether this was the last waiter
|
181 | d76167a5 | Michael Hanselmann |
|
182 | d76167a5 | Michael Hanselmann | """
|
183 | d76167a5 | Michael Hanselmann | assert self._nwaiters > 0 |
184 | d76167a5 | Michael Hanselmann | |
185 | d76167a5 | Michael Hanselmann | self._nwaiters -= 1 |
186 | d76167a5 | Michael Hanselmann | |
187 | d76167a5 | Michael Hanselmann | if self._nwaiters == 0: |
188 | d76167a5 | Michael Hanselmann | self._Cleanup()
|
189 | d76167a5 | Michael Hanselmann | return True |
190 | d76167a5 | Michael Hanselmann | |
191 | d76167a5 | Michael Hanselmann | return False |
192 | d76167a5 | Michael Hanselmann | |
193 | d76167a5 | Michael Hanselmann | def notifyAll(self): |
194 | d76167a5 | Michael Hanselmann | """Close the writing side of the pipe to notify all waiters.
|
195 | d76167a5 | Michael Hanselmann |
|
196 | d76167a5 | Michael Hanselmann | """
|
197 | d76167a5 | Michael Hanselmann | if self._write_fd is None: |
198 | d76167a5 | Michael Hanselmann | raise RuntimeError("Can only notify once") |
199 | d76167a5 | Michael Hanselmann | |
200 | d76167a5 | Michael Hanselmann | os.close(self._write_fd)
|
201 | d76167a5 | Michael Hanselmann | self._write_fd = None |
202 | d76167a5 | Michael Hanselmann | |
203 | d76167a5 | Michael Hanselmann | def _Cleanup(self): |
204 | d76167a5 | Michael Hanselmann | """Close all file descriptors.
|
205 | d76167a5 | Michael Hanselmann |
|
206 | d76167a5 | Michael Hanselmann | """
|
207 | d76167a5 | Michael Hanselmann | if self._read_fd is not None: |
208 | d76167a5 | Michael Hanselmann | os.close(self._read_fd)
|
209 | d76167a5 | Michael Hanselmann | self._read_fd = None |
210 | d76167a5 | Michael Hanselmann | |
211 | d76167a5 | Michael Hanselmann | if self._write_fd is not None: |
212 | d76167a5 | Michael Hanselmann | os.close(self._write_fd)
|
213 | d76167a5 | Michael Hanselmann | self._write_fd = None |
214 | d76167a5 | Michael Hanselmann | |
215 | d76167a5 | Michael Hanselmann | self._poller = None |
216 | d76167a5 | Michael Hanselmann | |
217 | d76167a5 | Michael Hanselmann | def __del__(self): |
218 | d76167a5 | Michael Hanselmann | """Called on object deletion.
|
219 | d76167a5 | Michael Hanselmann |
|
220 | d76167a5 | Michael Hanselmann | Ensure no file descriptors are left open.
|
221 | d76167a5 | Michael Hanselmann |
|
222 | d76167a5 | Michael Hanselmann | """
|
223 | d76167a5 | Michael Hanselmann | self._Cleanup()
|
224 | d76167a5 | Michael Hanselmann | |
225 | d76167a5 | Michael Hanselmann | |
226 | 48dabc6a | Michael Hanselmann | class _PipeCondition(object): |
227 | 48dabc6a | Michael Hanselmann | """Group-only non-polling condition with counters.
|
228 | 48dabc6a | Michael Hanselmann |
|
229 | 48dabc6a | Michael Hanselmann | This condition class uses pipes and poll, internally, to be able to wait for
|
230 | 48dabc6a | Michael Hanselmann | notification with a timeout, without resorting to polling. It is almost
|
231 | 48dabc6a | Michael Hanselmann | compatible with Python's threading.Condition, but only supports notifyAll and
|
232 | 48dabc6a | Michael Hanselmann | non-recursive locks. As an additional features it's able to report whether
|
233 | 48dabc6a | Michael Hanselmann | there are any waiting threads.
|
234 | 48dabc6a | Michael Hanselmann |
|
235 | 48dabc6a | Michael Hanselmann | """
|
236 | 48dabc6a | Michael Hanselmann | __slots__ = [ |
237 | 48dabc6a | Michael Hanselmann | "_lock",
|
238 | 48dabc6a | Michael Hanselmann | "_nwaiters",
|
239 | 48dabc6a | Michael Hanselmann | "_pipe",
|
240 | 48dabc6a | Michael Hanselmann | "acquire",
|
241 | 48dabc6a | Michael Hanselmann | "release",
|
242 | 48dabc6a | Michael Hanselmann | ] |
243 | 48dabc6a | Michael Hanselmann | |
244 | 48dabc6a | Michael Hanselmann | _pipe_class = _SingleActionPipeCondition |
245 | 48dabc6a | Michael Hanselmann | |
246 | 48dabc6a | Michael Hanselmann | def __init__(self, lock): |
247 | 48dabc6a | Michael Hanselmann | """Initializes this class.
|
248 | 48dabc6a | Michael Hanselmann |
|
249 | 48dabc6a | Michael Hanselmann | """
|
250 | 48dabc6a | Michael Hanselmann | object.__init__(self) |
251 | 48dabc6a | Michael Hanselmann | |
252 | 48dabc6a | Michael Hanselmann | # Recursive locks are not supported
|
253 | 48dabc6a | Michael Hanselmann | assert not hasattr(lock, "_acquire_restore") |
254 | 48dabc6a | Michael Hanselmann | assert not hasattr(lock, "_release_save") |
255 | 48dabc6a | Michael Hanselmann | |
256 | 48dabc6a | Michael Hanselmann | self._lock = lock
|
257 | 48dabc6a | Michael Hanselmann | |
258 | 48dabc6a | Michael Hanselmann | # Export the lock's acquire() and release() methods
|
259 | 48dabc6a | Michael Hanselmann | self.acquire = lock.acquire
|
260 | 48dabc6a | Michael Hanselmann | self.release = lock.release
|
261 | 48dabc6a | Michael Hanselmann | |
262 | 48dabc6a | Michael Hanselmann | self._nwaiters = 0 |
263 | 48dabc6a | Michael Hanselmann | self._pipe = None |
264 | 48dabc6a | Michael Hanselmann | |
265 | 48dabc6a | Michael Hanselmann | def _is_owned(self): |
266 | 48dabc6a | Michael Hanselmann | """Check whether lock is owned by current thread.
|
267 | 48dabc6a | Michael Hanselmann |
|
268 | 48dabc6a | Michael Hanselmann | """
|
269 | 48dabc6a | Michael Hanselmann | if self._lock.acquire(0): |
270 | 48dabc6a | Michael Hanselmann | self._lock.release()
|
271 | 48dabc6a | Michael Hanselmann | return False |
272 | 48dabc6a | Michael Hanselmann | |
273 | 48dabc6a | Michael Hanselmann | return True |
274 | 48dabc6a | Michael Hanselmann | |
275 | 48dabc6a | Michael Hanselmann | def _check_owned(self): |
276 | 48dabc6a | Michael Hanselmann | """Raise an exception if the current thread doesn't own the lock.
|
277 | 48dabc6a | Michael Hanselmann |
|
278 | 48dabc6a | Michael Hanselmann | """
|
279 | 48dabc6a | Michael Hanselmann | if not self._is_owned(): |
280 | 48dabc6a | Michael Hanselmann | raise RuntimeError("cannot work with un-aquired lock") |
281 | 48dabc6a | Michael Hanselmann | |
282 | 48dabc6a | Michael Hanselmann | def wait(self, timeout=None): |
283 | 48dabc6a | Michael Hanselmann | """Wait for a notification.
|
284 | 48dabc6a | Michael Hanselmann |
|
285 | 48dabc6a | Michael Hanselmann | @type timeout: float or None
|
286 | 48dabc6a | Michael Hanselmann | @param timeout: Waiting timeout (can be None)
|
287 | 48dabc6a | Michael Hanselmann |
|
288 | 48dabc6a | Michael Hanselmann | """
|
289 | 48dabc6a | Michael Hanselmann | self._check_owned()
|
290 | 48dabc6a | Michael Hanselmann | |
291 | 48dabc6a | Michael Hanselmann | if not self._pipe: |
292 | 48dabc6a | Michael Hanselmann | self._pipe = self._pipe_class() |
293 | 48dabc6a | Michael Hanselmann | |
294 | 48dabc6a | Michael Hanselmann | # Keep local reference to the pipe. It could be replaced by another thread
|
295 | 48dabc6a | Michael Hanselmann | # notifying while we're waiting.
|
296 | 48dabc6a | Michael Hanselmann | pipe = self._pipe
|
297 | 48dabc6a | Michael Hanselmann | |
298 | 48dabc6a | Michael Hanselmann | assert self._nwaiters >= 0 |
299 | 48dabc6a | Michael Hanselmann | self._nwaiters += 1 |
300 | 48dabc6a | Michael Hanselmann | try:
|
301 | 48dabc6a | Michael Hanselmann | # Get function to wait on the pipe
|
302 | 48dabc6a | Michael Hanselmann | wait_fn = pipe.StartWaiting() |
303 | 48dabc6a | Michael Hanselmann | try:
|
304 | 48dabc6a | Michael Hanselmann | # Release lock while waiting
|
305 | 48dabc6a | Michael Hanselmann | self.release()
|
306 | 48dabc6a | Michael Hanselmann | try:
|
307 | 48dabc6a | Michael Hanselmann | # Wait for notification
|
308 | 48dabc6a | Michael Hanselmann | wait_fn(timeout) |
309 | 48dabc6a | Michael Hanselmann | finally:
|
310 | 48dabc6a | Michael Hanselmann | # Re-acquire lock
|
311 | 48dabc6a | Michael Hanselmann | self.acquire()
|
312 | 48dabc6a | Michael Hanselmann | finally:
|
313 | 48dabc6a | Michael Hanselmann | # Destroy pipe if this was the last waiter and the current pipe is
|
314 | 48dabc6a | Michael Hanselmann | # still the same. The same pipe cannot be reused after cleanup.
|
315 | 48dabc6a | Michael Hanselmann | if pipe.DoneWaiting() and pipe == self._pipe: |
316 | 48dabc6a | Michael Hanselmann | self._pipe = None |
317 | 48dabc6a | Michael Hanselmann | finally:
|
318 | 48dabc6a | Michael Hanselmann | assert self._nwaiters > 0 |
319 | 48dabc6a | Michael Hanselmann | self._nwaiters -= 1 |
320 | 48dabc6a | Michael Hanselmann | |
321 | 48dabc6a | Michael Hanselmann | def notifyAll(self): |
322 | 48dabc6a | Michael Hanselmann | """Notify all currently waiting threads.
|
323 | 48dabc6a | Michael Hanselmann |
|
324 | 48dabc6a | Michael Hanselmann | """
|
325 | 48dabc6a | Michael Hanselmann | self._check_owned()
|
326 | 48dabc6a | Michael Hanselmann | |
327 | 48dabc6a | Michael Hanselmann | # Notify and forget pipe. A new one will be created on the next call to
|
328 | 48dabc6a | Michael Hanselmann | # wait.
|
329 | 48dabc6a | Michael Hanselmann | if self._pipe is not None: |
330 | 48dabc6a | Michael Hanselmann | self._pipe.notifyAll()
|
331 | 48dabc6a | Michael Hanselmann | self._pipe = None |
332 | 48dabc6a | Michael Hanselmann | |
333 | 48dabc6a | Michael Hanselmann | def has_waiting(self): |
334 | 48dabc6a | Michael Hanselmann | """Returns whether there are active waiters.
|
335 | 48dabc6a | Michael Hanselmann |
|
336 | 48dabc6a | Michael Hanselmann | """
|
337 | 48dabc6a | Michael Hanselmann | self._check_owned()
|
338 | 48dabc6a | Michael Hanselmann | |
339 | 48dabc6a | Michael Hanselmann | return bool(self._nwaiters) |
340 | 48dabc6a | Michael Hanselmann | |
341 | 48dabc6a | Michael Hanselmann | |
342 | 84e344d4 | Michael Hanselmann | class _CountingCondition(object): |
343 | 84e344d4 | Michael Hanselmann | """Wrapper for Python's built-in threading.Condition class.
|
344 | 84e344d4 | Michael Hanselmann |
|
345 | 84e344d4 | Michael Hanselmann | This wrapper keeps a count of active waiters. We can't access the internal
|
346 | 84e344d4 | Michael Hanselmann | "__waiters" attribute of threading.Condition because it's not thread-safe.
|
347 | 84e344d4 | Michael Hanselmann |
|
348 | 84e344d4 | Michael Hanselmann | """
|
349 | 84e344d4 | Michael Hanselmann | __slots__ = [ |
350 | 84e344d4 | Michael Hanselmann | "_cond",
|
351 | 84e344d4 | Michael Hanselmann | "_nwaiters",
|
352 | 84e344d4 | Michael Hanselmann | ] |
353 | 84e344d4 | Michael Hanselmann | |
354 | 84e344d4 | Michael Hanselmann | def __init__(self, lock): |
355 | 84e344d4 | Michael Hanselmann | """Initializes this class.
|
356 | 84e344d4 | Michael Hanselmann |
|
357 | 84e344d4 | Michael Hanselmann | """
|
358 | 84e344d4 | Michael Hanselmann | object.__init__(self) |
359 | 84e344d4 | Michael Hanselmann | self._cond = threading.Condition(lock=lock)
|
360 | 84e344d4 | Michael Hanselmann | self._nwaiters = 0 |
361 | 84e344d4 | Michael Hanselmann | |
362 | 84e344d4 | Michael Hanselmann | def notifyAll(self): |
363 | 84e344d4 | Michael Hanselmann | """Notifies the condition.
|
364 | 84e344d4 | Michael Hanselmann |
|
365 | 84e344d4 | Michael Hanselmann | """
|
366 | 84e344d4 | Michael Hanselmann | return self._cond.notifyAll() |
367 | 84e344d4 | Michael Hanselmann | |
368 | 84e344d4 | Michael Hanselmann | def wait(self, timeout=None): |
369 | 84e344d4 | Michael Hanselmann | """Waits for the condition to be notified.
|
370 | 84e344d4 | Michael Hanselmann |
|
371 | 84e344d4 | Michael Hanselmann | @type timeout: float or None
|
372 | 84e344d4 | Michael Hanselmann | @param timeout: Timeout in seconds
|
373 | 84e344d4 | Michael Hanselmann |
|
374 | 84e344d4 | Michael Hanselmann | """
|
375 | 84e344d4 | Michael Hanselmann | assert self._nwaiters >= 0 |
376 | 84e344d4 | Michael Hanselmann | |
377 | 84e344d4 | Michael Hanselmann | self._nwaiters += 1 |
378 | 84e344d4 | Michael Hanselmann | try:
|
379 | 84e344d4 | Michael Hanselmann | return self._cond.wait(timeout=timeout) |
380 | 84e344d4 | Michael Hanselmann | finally:
|
381 | 84e344d4 | Michael Hanselmann | self._nwaiters -= 1 |
382 | 84e344d4 | Michael Hanselmann | |
383 | 84e344d4 | Michael Hanselmann | def has_waiting(self): |
384 | 84e344d4 | Michael Hanselmann | """Returns whether there are active waiters.
|
385 | 84e344d4 | Michael Hanselmann |
|
386 | 84e344d4 | Michael Hanselmann | """
|
387 | 84e344d4 | Michael Hanselmann | return bool(self._nwaiters) |
388 | 84e344d4 | Michael Hanselmann | |
389 | 84e344d4 | Michael Hanselmann | |
390 | 84e344d4 | Michael Hanselmann | class SharedLock(object): |
391 | 162c1c1f | Guido Trotter | """Implements a shared lock.
|
392 | 162c1c1f | Guido Trotter |
|
393 | 162c1c1f | Guido Trotter | Multiple threads can acquire the lock in a shared way, calling
|
394 | 162c1c1f | Guido Trotter | acquire_shared(). In order to acquire the lock in an exclusive way threads
|
395 | 162c1c1f | Guido Trotter | can call acquire_exclusive().
|
396 | 162c1c1f | Guido Trotter |
|
397 | 162c1c1f | Guido Trotter | The lock prevents starvation but does not guarantee that threads will acquire
|
398 | 162c1c1f | Guido Trotter | the shared lock in the order they queued for it, just that they will
|
399 | 162c1c1f | Guido Trotter | eventually do so.
|
400 | 162c1c1f | Guido Trotter |
|
401 | 162c1c1f | Guido Trotter | """
|
402 | 84e344d4 | Michael Hanselmann | __slots__ = [ |
403 | 84e344d4 | Michael Hanselmann | "__active_shr_c",
|
404 | 84e344d4 | Michael Hanselmann | "__inactive_shr_c",
|
405 | 84e344d4 | Michael Hanselmann | "__deleted",
|
406 | 84e344d4 | Michael Hanselmann | "__exc",
|
407 | 84e344d4 | Michael Hanselmann | "__lock",
|
408 | 84e344d4 | Michael Hanselmann | "__pending",
|
409 | 84e344d4 | Michael Hanselmann | "__shr",
|
410 | 84e344d4 | Michael Hanselmann | ] |
411 | 84e344d4 | Michael Hanselmann | |
412 | c6997f21 | Michael Hanselmann | __condition_class = _PipeCondition |
413 | 84e344d4 | Michael Hanselmann | |
414 | 162c1c1f | Guido Trotter | def __init__(self): |
415 | 84e344d4 | Michael Hanselmann | """Construct a new SharedLock.
|
416 | 84e344d4 | Michael Hanselmann |
|
417 | 84e344d4 | Michael Hanselmann | """
|
418 | 84e344d4 | Michael Hanselmann | object.__init__(self) |
419 | 84e344d4 | Michael Hanselmann | |
420 | 84e344d4 | Michael Hanselmann | # Internal lock
|
421 | 162c1c1f | Guido Trotter | self.__lock = threading.Lock()
|
422 | 162c1c1f | Guido Trotter | |
423 | 84e344d4 | Michael Hanselmann | # Queue containing waiting acquires
|
424 | 84e344d4 | Michael Hanselmann | self.__pending = []
|
425 | 84e344d4 | Michael Hanselmann | |
426 | 84e344d4 | Michael Hanselmann | # Active and inactive conditions for shared locks
|
427 | 84e344d4 | Michael Hanselmann | self.__active_shr_c = self.__condition_class(self.__lock) |
428 | 84e344d4 | Michael Hanselmann | self.__inactive_shr_c = self.__condition_class(self.__lock) |
429 | 84e344d4 | Michael Hanselmann | |
430 | 84e344d4 | Michael Hanselmann | # Current lock holders
|
431 | 162c1c1f | Guido Trotter | self.__shr = set() |
432 | 162c1c1f | Guido Trotter | self.__exc = None |
433 | 162c1c1f | Guido Trotter | |
434 | a95fd5d7 | Guido Trotter | # is this lock in the deleted state?
|
435 | a95fd5d7 | Guido Trotter | self.__deleted = False |
436 | a95fd5d7 | Guido Trotter | |
437 | 84e344d4 | Michael Hanselmann | def __check_deleted(self): |
438 | 84e344d4 | Michael Hanselmann | """Raises an exception if the lock has been deleted.
|
439 | 84e344d4 | Michael Hanselmann |
|
440 | 84e344d4 | Michael Hanselmann | """
|
441 | 84e344d4 | Michael Hanselmann | if self.__deleted: |
442 | 84e344d4 | Michael Hanselmann | raise errors.LockError("Deleted lock") |
443 | 84e344d4 | Michael Hanselmann | |
444 | 162c1c1f | Guido Trotter | def __is_sharer(self): |
445 | 84e344d4 | Michael Hanselmann | """Is the current thread sharing the lock at this time?
|
446 | 84e344d4 | Michael Hanselmann |
|
447 | 84e344d4 | Michael Hanselmann | """
|
448 | 162c1c1f | Guido Trotter | return threading.currentThread() in self.__shr |
449 | 162c1c1f | Guido Trotter | |
450 | 162c1c1f | Guido Trotter | def __is_exclusive(self): |
451 | 84e344d4 | Michael Hanselmann | """Is the current thread holding the lock exclusively at this time?
|
452 | 84e344d4 | Michael Hanselmann |
|
453 | 84e344d4 | Michael Hanselmann | """
|
454 | 162c1c1f | Guido Trotter | return threading.currentThread() == self.__exc |
455 | 162c1c1f | Guido Trotter | |
456 | 162c1c1f | Guido Trotter | def __is_owned(self, shared=-1): |
457 | 162c1c1f | Guido Trotter | """Is the current thread somehow owning the lock at this time?
|
458 | 162c1c1f | Guido Trotter |
|
459 | 162c1c1f | Guido Trotter | This is a private version of the function, which presumes you're holding
|
460 | 162c1c1f | Guido Trotter | the internal lock.
|
461 | 162c1c1f | Guido Trotter |
|
462 | 162c1c1f | Guido Trotter | """
|
463 | 162c1c1f | Guido Trotter | if shared < 0: |
464 | 162c1c1f | Guido Trotter | return self.__is_sharer() or self.__is_exclusive() |
465 | 162c1c1f | Guido Trotter | elif shared:
|
466 | 162c1c1f | Guido Trotter | return self.__is_sharer() |
467 | 162c1c1f | Guido Trotter | else:
|
468 | 162c1c1f | Guido Trotter | return self.__is_exclusive() |
469 | 162c1c1f | Guido Trotter | |
470 | 162c1c1f | Guido Trotter | def _is_owned(self, shared=-1): |
471 | 162c1c1f | Guido Trotter | """Is the current thread somehow owning the lock at this time?
|
472 | 162c1c1f | Guido Trotter |
|
473 | c41eea6e | Iustin Pop | @param shared:
|
474 | c41eea6e | Iustin Pop | - < 0: check for any type of ownership (default)
|
475 | c41eea6e | Iustin Pop | - 0: check for exclusive ownership
|
476 | c41eea6e | Iustin Pop | - > 0: check for shared ownership
|
477 | 162c1c1f | Guido Trotter |
|
478 | 162c1c1f | Guido Trotter | """
|
479 | 162c1c1f | Guido Trotter | self.__lock.acquire()
|
480 | 162c1c1f | Guido Trotter | try:
|
481 | 84e344d4 | Michael Hanselmann | return self.__is_owned(shared=shared) |
482 | 162c1c1f | Guido Trotter | finally:
|
483 | 162c1c1f | Guido Trotter | self.__lock.release()
|
484 | 162c1c1f | Guido Trotter | |
485 | 84e344d4 | Michael Hanselmann | def _count_pending(self): |
486 | 84e344d4 | Michael Hanselmann | """Returns the number of pending acquires.
|
487 | a95fd5d7 | Guido Trotter |
|
488 | 84e344d4 | Michael Hanselmann | @rtype: int
|
489 | a95fd5d7 | Guido Trotter |
|
490 | a95fd5d7 | Guido Trotter | """
|
491 | 84e344d4 | Michael Hanselmann | self.__lock.acquire()
|
492 | 84e344d4 | Michael Hanselmann | try:
|
493 | 84e344d4 | Michael Hanselmann | return len(self.__pending) |
494 | 84e344d4 | Michael Hanselmann | finally:
|
495 | 84e344d4 | Michael Hanselmann | self.__lock.release()
|
496 | a95fd5d7 | Guido Trotter | |
497 | 84e344d4 | Michael Hanselmann | def __do_acquire(self, shared): |
498 | 84e344d4 | Michael Hanselmann | """Actually acquire the lock.
|
499 | 84e344d4 | Michael Hanselmann |
|
500 | 84e344d4 | Michael Hanselmann | """
|
501 | 84e344d4 | Michael Hanselmann | if shared:
|
502 | 84e344d4 | Michael Hanselmann | self.__shr.add(threading.currentThread())
|
503 | 84e344d4 | Michael Hanselmann | else:
|
504 | 84e344d4 | Michael Hanselmann | self.__exc = threading.currentThread()
|
505 | a95fd5d7 | Guido Trotter | |
506 | 84e344d4 | Michael Hanselmann | def __can_acquire(self, shared): |
507 | 84e344d4 | Michael Hanselmann | """Determine whether lock can be acquired.
|
508 | a95fd5d7 | Guido Trotter |
|
509 | a95fd5d7 | Guido Trotter | """
|
510 | 84e344d4 | Michael Hanselmann | if shared:
|
511 | 84e344d4 | Michael Hanselmann | return self.__exc is None |
512 | 84e344d4 | Michael Hanselmann | else:
|
513 | 84e344d4 | Michael Hanselmann | return len(self.__shr) == 0 and self.__exc is None |
514 | a95fd5d7 | Guido Trotter | |
515 | 84e344d4 | Michael Hanselmann | def __is_on_top(self, cond): |
516 | 84e344d4 | Michael Hanselmann | """Checks whether the passed condition is on top of the queue.
|
517 | a95fd5d7 | Guido Trotter |
|
518 | 84e344d4 | Michael Hanselmann | The caller must make sure the queue isn't empty.
|
519 | a95fd5d7 | Guido Trotter |
|
520 | 84e344d4 | Michael Hanselmann | """
|
521 | 84e344d4 | Michael Hanselmann | return self.__pending[0] == cond |
522 | 4d686df8 | Guido Trotter | |
523 | 84e344d4 | Michael Hanselmann | def __acquire_unlocked(self, shared=0, timeout=None): |
524 | 84e344d4 | Michael Hanselmann | """Acquire a shared lock.
|
525 | 9216a9f7 | Michael Hanselmann |
|
526 | 84e344d4 | Michael Hanselmann | @param shared: whether to acquire in shared mode; by default an
|
527 | 84e344d4 | Michael Hanselmann | exclusive lock will be acquired
|
528 | 84e344d4 | Michael Hanselmann | @param timeout: maximum waiting time before giving up
|
529 | 9216a9f7 | Michael Hanselmann |
|
530 | 9216a9f7 | Michael Hanselmann | """
|
531 | 84e344d4 | Michael Hanselmann | self.__check_deleted()
|
532 | 9216a9f7 | Michael Hanselmann | |
533 | 84e344d4 | Michael Hanselmann | # We cannot acquire the lock if we already have it
|
534 | 84e344d4 | Michael Hanselmann | assert not self.__is_owned(), "double acquire() on a non-recursive lock" |
535 | 84e344d4 | Michael Hanselmann | |
536 | 84e344d4 | Michael Hanselmann | # Check whether someone else holds the lock or there are pending acquires.
|
537 | 84e344d4 | Michael Hanselmann | if not self.__pending and self.__can_acquire(shared): |
538 | 84e344d4 | Michael Hanselmann | # Apparently not, can acquire lock directly.
|
539 | 84e344d4 | Michael Hanselmann | self.__do_acquire(shared)
|
540 | 84e344d4 | Michael Hanselmann | return True |
541 | 9216a9f7 | Michael Hanselmann | |
542 | 84e344d4 | Michael Hanselmann | if shared:
|
543 | 84e344d4 | Michael Hanselmann | wait_condition = self.__active_shr_c
|
544 | 9216a9f7 | Michael Hanselmann | |
545 | 84e344d4 | Michael Hanselmann | # Check if we're not yet in the queue
|
546 | 84e344d4 | Michael Hanselmann | if wait_condition not in self.__pending: |
547 | 84e344d4 | Michael Hanselmann | self.__pending.append(wait_condition)
|
548 | 84e344d4 | Michael Hanselmann | else:
|
549 | 84e344d4 | Michael Hanselmann | wait_condition = self.__condition_class(self.__lock) |
550 | 84e344d4 | Michael Hanselmann | # Always add to queue
|
551 | 84e344d4 | Michael Hanselmann | self.__pending.append(wait_condition)
|
552 | 84e344d4 | Michael Hanselmann | |
553 | 84e344d4 | Michael Hanselmann | try:
|
554 | 84e344d4 | Michael Hanselmann | # Wait until we become the topmost acquire in the queue or the timeout
|
555 | 84e344d4 | Michael Hanselmann | # expires.
|
556 | 84e344d4 | Michael Hanselmann | while not (self.__is_on_top(wait_condition) and |
557 | 84e344d4 | Michael Hanselmann | self.__can_acquire(shared)):
|
558 | 84e344d4 | Michael Hanselmann | # Wait for notification
|
559 | 84e344d4 | Michael Hanselmann | wait_condition.wait(timeout) |
560 | 84e344d4 | Michael Hanselmann | self.__check_deleted()
|
561 | 84e344d4 | Michael Hanselmann | |
562 | 84e344d4 | Michael Hanselmann | # A lot of code assumes blocking acquires always succeed. Loop
|
563 | 84e344d4 | Michael Hanselmann | # internally for that case.
|
564 | 84e344d4 | Michael Hanselmann | if timeout is not None: |
565 | 84e344d4 | Michael Hanselmann | break
|
566 | 84e344d4 | Michael Hanselmann | |
567 | 84e344d4 | Michael Hanselmann | if self.__is_on_top(wait_condition) and self.__can_acquire(shared): |
568 | 84e344d4 | Michael Hanselmann | self.__do_acquire(shared)
|
569 | 84e344d4 | Michael Hanselmann | return True |
570 | 9216a9f7 | Michael Hanselmann | finally:
|
571 | 84e344d4 | Michael Hanselmann | # Remove condition from queue if there are no more waiters
|
572 | 84e344d4 | Michael Hanselmann | if not wait_condition.has_waiting() and not self.__deleted: |
573 | 84e344d4 | Michael Hanselmann | self.__pending.remove(wait_condition)
|
574 | 9216a9f7 | Michael Hanselmann | |
575 | 84e344d4 | Michael Hanselmann | return False |
576 | 9216a9f7 | Michael Hanselmann | |
577 | 84e344d4 | Michael Hanselmann | def acquire(self, shared=0, timeout=None): |
578 | 162c1c1f | Guido Trotter | """Acquire a shared lock.
|
579 | 162c1c1f | Guido Trotter |
|
580 | 84e344d4 | Michael Hanselmann | @type shared: int
|
581 | c41eea6e | Iustin Pop | @param shared: whether to acquire in shared mode; by default an
|
582 | c41eea6e | Iustin Pop | exclusive lock will be acquired
|
583 | 84e344d4 | Michael Hanselmann | @type timeout: float
|
584 | 84e344d4 | Michael Hanselmann | @param timeout: maximum waiting time before giving up
|
585 | 162c1c1f | Guido Trotter |
|
586 | 162c1c1f | Guido Trotter | """
|
587 | 162c1c1f | Guido Trotter | self.__lock.acquire()
|
588 | 162c1c1f | Guido Trotter | try:
|
589 | 84e344d4 | Michael Hanselmann | return self.__acquire_unlocked(shared, timeout) |
590 | 162c1c1f | Guido Trotter | finally:
|
591 | 162c1c1f | Guido Trotter | self.__lock.release()
|
592 | 162c1c1f | Guido Trotter | |
593 | 162c1c1f | Guido Trotter | def release(self): |
594 | 162c1c1f | Guido Trotter | """Release a Shared Lock.
|
595 | 162c1c1f | Guido Trotter |
|
596 | 162c1c1f | Guido Trotter | You must have acquired the lock, either in shared or in exclusive mode,
|
597 | 162c1c1f | Guido Trotter | before calling this function.
|
598 | 162c1c1f | Guido Trotter |
|
599 | 162c1c1f | Guido Trotter | """
|
600 | 162c1c1f | Guido Trotter | self.__lock.acquire()
|
601 | 162c1c1f | Guido Trotter | try:
|
602 | 84e344d4 | Michael Hanselmann | assert self.__is_exclusive() or self.__is_sharer(), \ |
603 | 84e344d4 | Michael Hanselmann | "Cannot release non-owned lock"
|
604 | 84e344d4 | Michael Hanselmann | |
605 | 162c1c1f | Guido Trotter | # Autodetect release type
|
606 | 162c1c1f | Guido Trotter | if self.__is_exclusive(): |
607 | 162c1c1f | Guido Trotter | self.__exc = None |
608 | 84e344d4 | Michael Hanselmann | else:
|
609 | 162c1c1f | Guido Trotter | self.__shr.remove(threading.currentThread())
|
610 | 162c1c1f | Guido Trotter | |
611 | 84e344d4 | Michael Hanselmann | # Notify topmost condition in queue
|
612 | 84e344d4 | Michael Hanselmann | if self.__pending: |
613 | 84e344d4 | Michael Hanselmann | first_condition = self.__pending[0] |
614 | 84e344d4 | Michael Hanselmann | first_condition.notifyAll() |
615 | 4d686df8 | Guido Trotter | |
616 | 84e344d4 | Michael Hanselmann | if first_condition == self.__active_shr_c: |
617 | 84e344d4 | Michael Hanselmann | self.__active_shr_c = self.__inactive_shr_c |
618 | 84e344d4 | Michael Hanselmann | self.__inactive_shr_c = first_condition
|
619 | 162c1c1f | Guido Trotter | |
620 | 162c1c1f | Guido Trotter | finally:
|
621 | 162c1c1f | Guido Trotter | self.__lock.release()
|
622 | 162c1c1f | Guido Trotter | |
623 | 84e344d4 | Michael Hanselmann | def delete(self, timeout=None): |
624 | a95fd5d7 | Guido Trotter | """Delete a Shared Lock.
|
625 | a95fd5d7 | Guido Trotter |
|
626 | a95fd5d7 | Guido Trotter | This operation will declare the lock for removal. First the lock will be
|
627 | a95fd5d7 | Guido Trotter | acquired in exclusive mode if you don't already own it, then the lock
|
628 | a95fd5d7 | Guido Trotter | will be put in a state where any future and pending acquire() fail.
|
629 | a95fd5d7 | Guido Trotter |
|
630 | 84e344d4 | Michael Hanselmann | @type timeout: float
|
631 | 84e344d4 | Michael Hanselmann | @param timeout: maximum waiting time before giving up
|
632 | a95fd5d7 | Guido Trotter |
|
633 | a95fd5d7 | Guido Trotter | """
|
634 | a95fd5d7 | Guido Trotter | self.__lock.acquire()
|
635 | a95fd5d7 | Guido Trotter | try:
|
636 | 84e344d4 | Michael Hanselmann | assert not self.__is_sharer(), "Cannot delete() a lock while sharing it" |
637 | 84e344d4 | Michael Hanselmann | |
638 | 84e344d4 | Michael Hanselmann | self.__check_deleted()
|
639 | a95fd5d7 | Guido Trotter | |
640 | 84e344d4 | Michael Hanselmann | # The caller is allowed to hold the lock exclusively already.
|
641 | 84e344d4 | Michael Hanselmann | acquired = self.__is_exclusive()
|
642 | a95fd5d7 | Guido Trotter | |
643 | 84e344d4 | Michael Hanselmann | if not acquired: |
644 | 84e344d4 | Michael Hanselmann | acquired = self.__acquire_unlocked(timeout)
|
645 | 84e344d4 | Michael Hanselmann | |
646 | 84e344d4 | Michael Hanselmann | if acquired:
|
647 | 84e344d4 | Michael Hanselmann | self.__deleted = True |
648 | 84e344d4 | Michael Hanselmann | self.__exc = None |
649 | a95fd5d7 | Guido Trotter | |
650 | 84e344d4 | Michael Hanselmann | # Notify all acquires. They'll throw an error.
|
651 | 84e344d4 | Michael Hanselmann | while self.__pending: |
652 | 84e344d4 | Michael Hanselmann | self.__pending.pop().notifyAll()
|
653 | a95fd5d7 | Guido Trotter | |
654 | 84e344d4 | Michael Hanselmann | return acquired
|
655 | a95fd5d7 | Guido Trotter | finally:
|
656 | a95fd5d7 | Guido Trotter | self.__lock.release()
|
657 | a95fd5d7 | Guido Trotter | |
658 | aaae9bc0 | Guido Trotter | |
659 | f12eadb3 | Iustin Pop | # Whenever we want to acquire a full LockSet we pass None as the value
|
660 | 5bbd3f7f | Michael Hanselmann | # to acquire. Hide this behind this nicely named constant.
|
661 | e310b019 | Guido Trotter | ALL_SET = None
|
662 | e310b019 | Guido Trotter | |
663 | e310b019 | Guido Trotter | |
664 | aaae9bc0 | Guido Trotter | class LockSet: |
665 | aaae9bc0 | Guido Trotter | """Implements a set of locks.
|
666 | aaae9bc0 | Guido Trotter |
|
667 | aaae9bc0 | Guido Trotter | This abstraction implements a set of shared locks for the same resource type,
|
668 | aaae9bc0 | Guido Trotter | distinguished by name. The user can lock a subset of the resources and the
|
669 | aaae9bc0 | Guido Trotter | LockSet will take care of acquiring the locks always in the same order, thus
|
670 | aaae9bc0 | Guido Trotter | preventing deadlock.
|
671 | aaae9bc0 | Guido Trotter |
|
672 | aaae9bc0 | Guido Trotter | All the locks needed in the same set must be acquired together, though.
|
673 | aaae9bc0 | Guido Trotter |
|
674 | aaae9bc0 | Guido Trotter | """
|
675 | aaae9bc0 | Guido Trotter | def __init__(self, members=None): |
676 | aaae9bc0 | Guido Trotter | """Constructs a new LockSet.
|
677 | aaae9bc0 | Guido Trotter |
|
678 | c41eea6e | Iustin Pop | @param members: initial members of the set
|
679 | aaae9bc0 | Guido Trotter |
|
680 | aaae9bc0 | Guido Trotter | """
|
681 | aaae9bc0 | Guido Trotter | # Used internally to guarantee coherency.
|
682 | aaae9bc0 | Guido Trotter | self.__lock = SharedLock()
|
683 | aaae9bc0 | Guido Trotter | |
684 | aaae9bc0 | Guido Trotter | # The lockdict indexes the relationship name -> lock
|
685 | aaae9bc0 | Guido Trotter | # The order-of-locking is implied by the alphabetical order of names
|
686 | aaae9bc0 | Guido Trotter | self.__lockdict = {}
|
687 | aaae9bc0 | Guido Trotter | |
688 | aaae9bc0 | Guido Trotter | if members is not None: |
689 | aaae9bc0 | Guido Trotter | for name in members: |
690 | aaae9bc0 | Guido Trotter | self.__lockdict[name] = SharedLock()
|
691 | aaae9bc0 | Guido Trotter | |
692 | aaae9bc0 | Guido Trotter | # The owner dict contains the set of locks each thread owns. For
|
693 | aaae9bc0 | Guido Trotter | # performance each thread can access its own key without a global lock on
|
694 | aaae9bc0 | Guido Trotter | # this structure. It is paramount though that *no* other type of access is
|
695 | aaae9bc0 | Guido Trotter | # done to this structure (eg. no looping over its keys). *_owner helper
|
696 | aaae9bc0 | Guido Trotter | # function are defined to guarantee access is correct, but in general never
|
697 | aaae9bc0 | Guido Trotter | # do anything different than __owners[threading.currentThread()], or there
|
698 | aaae9bc0 | Guido Trotter | # will be trouble.
|
699 | aaae9bc0 | Guido Trotter | self.__owners = {}
|
700 | aaae9bc0 | Guido Trotter | |
701 | aaae9bc0 | Guido Trotter | def _is_owned(self): |
702 | aaae9bc0 | Guido Trotter | """Is the current thread a current level owner?"""
|
703 | aaae9bc0 | Guido Trotter | return threading.currentThread() in self.__owners |
704 | aaae9bc0 | Guido Trotter | |
705 | b2dabfd6 | Guido Trotter | def _add_owned(self, name=None): |
706 | aaae9bc0 | Guido Trotter | """Note the current thread owns the given lock"""
|
707 | b2dabfd6 | Guido Trotter | if name is None: |
708 | b2dabfd6 | Guido Trotter | if not self._is_owned(): |
709 | b2dabfd6 | Guido Trotter | self.__owners[threading.currentThread()] = set() |
710 | aaae9bc0 | Guido Trotter | else:
|
711 | b2dabfd6 | Guido Trotter | if self._is_owned(): |
712 | b2dabfd6 | Guido Trotter | self.__owners[threading.currentThread()].add(name)
|
713 | b2dabfd6 | Guido Trotter | else:
|
714 | b2dabfd6 | Guido Trotter | self.__owners[threading.currentThread()] = set([name]) |
715 | b2dabfd6 | Guido Trotter | |
716 | b2dabfd6 | Guido Trotter | def _del_owned(self, name=None): |
717 | aaae9bc0 | Guido Trotter | """Note the current thread owns the given lock"""
|
718 | aaae9bc0 | Guido Trotter | |
719 | b2dabfd6 | Guido Trotter | if name is not None: |
720 | b2dabfd6 | Guido Trotter | self.__owners[threading.currentThread()].remove(name)
|
721 | b2dabfd6 | Guido Trotter | |
722 | b2dabfd6 | Guido Trotter | # Only remove the key if we don't hold the set-lock as well
|
723 | b2dabfd6 | Guido Trotter | if (not self.__lock._is_owned() and |
724 | b2dabfd6 | Guido Trotter | not self.__owners[threading.currentThread()]): |
725 | aaae9bc0 | Guido Trotter | del self.__owners[threading.currentThread()] |
726 | aaae9bc0 | Guido Trotter | |
727 | aaae9bc0 | Guido Trotter | def _list_owned(self): |
728 | aaae9bc0 | Guido Trotter | """Get the set of resource names owned by the current thread"""
|
729 | aaae9bc0 | Guido Trotter | if self._is_owned(): |
730 | aaae9bc0 | Guido Trotter | return self.__owners[threading.currentThread()].copy() |
731 | aaae9bc0 | Guido Trotter | else:
|
732 | aaae9bc0 | Guido Trotter | return set() |
733 | aaae9bc0 | Guido Trotter | |
734 | aaae9bc0 | Guido Trotter | def __names(self): |
735 | aaae9bc0 | Guido Trotter | """Return the current set of names.
|
736 | aaae9bc0 | Guido Trotter |
|
737 | aaae9bc0 | Guido Trotter | Only call this function while holding __lock and don't iterate on the
|
738 | aaae9bc0 | Guido Trotter | result after releasing the lock.
|
739 | aaae9bc0 | Guido Trotter |
|
740 | aaae9bc0 | Guido Trotter | """
|
741 | 0cf257c5 | Guido Trotter | return self.__lockdict.keys() |
742 | aaae9bc0 | Guido Trotter | |
743 | aaae9bc0 | Guido Trotter | def _names(self): |
744 | aaae9bc0 | Guido Trotter | """Return a copy of the current set of elements.
|
745 | aaae9bc0 | Guido Trotter |
|
746 | aaae9bc0 | Guido Trotter | Used only for debugging purposes.
|
747 | cdb08f44 | Michael Hanselmann |
|
748 | aaae9bc0 | Guido Trotter | """
|
749 | d4803c24 | Guido Trotter | # If we don't already own the set-level lock acquired
|
750 | d4803c24 | Guido Trotter | # we'll get it and note we need to release it later.
|
751 | d4803c24 | Guido Trotter | release_lock = False
|
752 | d4803c24 | Guido Trotter | if not self.__lock._is_owned(): |
753 | d4803c24 | Guido Trotter | release_lock = True
|
754 | d4803c24 | Guido Trotter | self.__lock.acquire(shared=1) |
755 | aaae9bc0 | Guido Trotter | try:
|
756 | aaae9bc0 | Guido Trotter | result = self.__names()
|
757 | aaae9bc0 | Guido Trotter | finally:
|
758 | d4803c24 | Guido Trotter | if release_lock:
|
759 | d4803c24 | Guido Trotter | self.__lock.release()
|
760 | 0cf257c5 | Guido Trotter | return set(result) |
761 | aaae9bc0 | Guido Trotter | |
762 | aaae9bc0 | Guido Trotter | def acquire(self, names, blocking=1, shared=0): |
763 | aaae9bc0 | Guido Trotter | """Acquire a set of resource locks.
|
764 | aaae9bc0 | Guido Trotter |
|
765 | c41eea6e | Iustin Pop | @param names: the names of the locks which shall be acquired
|
766 | c41eea6e | Iustin Pop | (special lock names, or instance/node names)
|
767 | c41eea6e | Iustin Pop | @param shared: whether to acquire in shared mode; by default an
|
768 | c41eea6e | Iustin Pop | exclusive lock will be acquired
|
769 | c41eea6e | Iustin Pop | @param blocking: whether to block while trying to acquire or to
|
770 | c41eea6e | Iustin Pop | operate in try-lock mode (this locking mode is not supported yet)
|
771 | aaae9bc0 | Guido Trotter |
|
772 | c41eea6e | Iustin Pop | @return: True when all the locks are successfully acquired
|
773 | aaae9bc0 | Guido Trotter |
|
774 | c41eea6e | Iustin Pop | @raise errors.LockError: when any lock we try to acquire has
|
775 | c41eea6e | Iustin Pop | been deleted before we succeed. In this case none of the
|
776 | c41eea6e | Iustin Pop | locks requested will be acquired.
|
777 | aaae9bc0 | Guido Trotter |
|
778 | aaae9bc0 | Guido Trotter | """
|
779 | aaae9bc0 | Guido Trotter | if not blocking: |
780 | aaae9bc0 | Guido Trotter | # We don't have non-blocking mode for now
|
781 | aaae9bc0 | Guido Trotter | raise NotImplementedError |
782 | aaae9bc0 | Guido Trotter | |
783 | aaae9bc0 | Guido Trotter | # Check we don't already own locks at this level
|
784 | aaae9bc0 | Guido Trotter | assert not self._is_owned(), "Cannot acquire locks in the same set twice" |
785 | aaae9bc0 | Guido Trotter | |
786 | 3b7ed473 | Guido Trotter | if names is None: |
787 | 3b7ed473 | Guido Trotter | # If no names are given acquire the whole set by not letting new names
|
788 | 3b7ed473 | Guido Trotter | # being added before we release, and getting the current list of names.
|
789 | 3b7ed473 | Guido Trotter | # Some of them may then be deleted later, but we'll cope with this.
|
790 | 3b7ed473 | Guido Trotter | #
|
791 | 3b7ed473 | Guido Trotter | # We'd like to acquire this lock in a shared way, as it's nice if
|
792 | 3b7ed473 | Guido Trotter | # everybody else can use the instances at the same time. If are acquiring
|
793 | 3b7ed473 | Guido Trotter | # them exclusively though they won't be able to do this anyway, though,
|
794 | 3b7ed473 | Guido Trotter | # so we'll get the list lock exclusively as well in order to be able to
|
795 | 3b7ed473 | Guido Trotter | # do add() on the set while owning it.
|
796 | 3b7ed473 | Guido Trotter | self.__lock.acquire(shared=shared)
|
797 | b2dabfd6 | Guido Trotter | try:
|
798 | b2dabfd6 | Guido Trotter | # note we own the set-lock
|
799 | b2dabfd6 | Guido Trotter | self._add_owned()
|
800 | b2dabfd6 | Guido Trotter | names = self.__names()
|
801 | b2dabfd6 | Guido Trotter | except:
|
802 | b2dabfd6 | Guido Trotter | # We shouldn't have problems adding the lock to the owners list, but
|
803 | b2dabfd6 | Guido Trotter | # if we did we'll try to release this lock and re-raise exception.
|
804 | b2dabfd6 | Guido Trotter | # Of course something is going to be really wrong, after this.
|
805 | b2dabfd6 | Guido Trotter | self.__lock.release()
|
806 | b2dabfd6 | Guido Trotter | raise
|
807 | 3b7ed473 | Guido Trotter | |
808 | 806e20fd | Guido Trotter | try:
|
809 | 806e20fd | Guido Trotter | # Support passing in a single resource to acquire rather than many
|
810 | 806e20fd | Guido Trotter | if isinstance(names, basestring): |
811 | 806e20fd | Guido Trotter | names = [names] |
812 | 806e20fd | Guido Trotter | else:
|
813 | 2a21bc88 | Iustin Pop | names = sorted(names)
|
814 | 806e20fd | Guido Trotter | |
815 | 806e20fd | Guido Trotter | acquire_list = [] |
816 | 806e20fd | Guido Trotter | # First we look the locks up on __lockdict. We have no way of being sure
|
817 | 806e20fd | Guido Trotter | # they will still be there after, but this makes it a lot faster should
|
818 | 806e20fd | Guido Trotter | # just one of them be the already wrong
|
819 | 34ca3914 | Guido Trotter | for lname in utils.UniqueSequence(names): |
820 | 806e20fd | Guido Trotter | try:
|
821 | 4e07ec8c | Guido Trotter | lock = self.__lockdict[lname] # raises KeyError if lock is not there |
822 | 806e20fd | Guido Trotter | acquire_list.append((lname, lock)) |
823 | 806e20fd | Guido Trotter | except (KeyError): |
824 | 3b7ed473 | Guido Trotter | if self.__lock._is_owned(): |
825 | f12eadb3 | Iustin Pop | # We are acquiring all the set, it doesn't matter if this
|
826 | f12eadb3 | Iustin Pop | # particular element is not there anymore.
|
827 | 3b7ed473 | Guido Trotter | continue
|
828 | 3b7ed473 | Guido Trotter | else:
|
829 | 3b7ed473 | Guido Trotter | raise errors.LockError('non-existing lock in set (%s)' % lname) |
830 | 806e20fd | Guido Trotter | |
831 | 806e20fd | Guido Trotter | # This will hold the locknames we effectively acquired.
|
832 | 806e20fd | Guido Trotter | acquired = set()
|
833 | 806e20fd | Guido Trotter | # Now acquire_list contains a sorted list of resources and locks we want.
|
834 | 806e20fd | Guido Trotter | # In order to get them we loop on this (private) list and acquire() them.
|
835 | 806e20fd | Guido Trotter | # We gave no real guarantee they will still exist till this is done but
|
836 | 806e20fd | Guido Trotter | # .acquire() itself is safe and will alert us if the lock gets deleted.
|
837 | 806e20fd | Guido Trotter | for (lname, lock) in acquire_list: |
838 | aaae9bc0 | Guido Trotter | try:
|
839 | 806e20fd | Guido Trotter | lock.acquire(shared=shared) # raises LockError if the lock is deleted
|
840 | ea3f80bf | Guido Trotter | # now the lock cannot be deleted, we have it!
|
841 | b2dabfd6 | Guido Trotter | self._add_owned(name=lname)
|
842 | ea3f80bf | Guido Trotter | acquired.add(lname) |
843 | 806e20fd | Guido Trotter | except (errors.LockError):
|
844 | 3b7ed473 | Guido Trotter | if self.__lock._is_owned(): |
845 | f12eadb3 | Iustin Pop | # We are acquiring all the set, it doesn't matter if this
|
846 | f12eadb3 | Iustin Pop | # particular element is not there anymore.
|
847 | 3b7ed473 | Guido Trotter | continue
|
848 | 3b7ed473 | Guido Trotter | else:
|
849 | 3b7ed473 | Guido Trotter | name_fail = lname |
850 | 3b7ed473 | Guido Trotter | for lname in self._list_owned(): |
851 | 3b7ed473 | Guido Trotter | self.__lockdict[lname].release()
|
852 | b2dabfd6 | Guido Trotter | self._del_owned(name=lname)
|
853 | 3b7ed473 | Guido Trotter | raise errors.LockError('non-existing lock in set (%s)' % name_fail) |
854 | ea3f80bf | Guido Trotter | except:
|
855 | ea3f80bf | Guido Trotter | # We shouldn't have problems adding the lock to the owners list, but
|
856 | ea3f80bf | Guido Trotter | # if we did we'll try to release this lock and re-raise exception.
|
857 | ea3f80bf | Guido Trotter | # Of course something is going to be really wrong, after this.
|
858 | ea3f80bf | Guido Trotter | if lock._is_owned():
|
859 | ea3f80bf | Guido Trotter | lock.release() |
860 | 470ce2ee | Michael Hanselmann | raise
|
861 | 806e20fd | Guido Trotter | |
862 | 806e20fd | Guido Trotter | except:
|
863 | 3b7ed473 | Guido Trotter | # If something went wrong and we had the set-lock let's release it...
|
864 | 3b7ed473 | Guido Trotter | if self.__lock._is_owned(): |
865 | 3b7ed473 | Guido Trotter | self.__lock.release()
|
866 | 806e20fd | Guido Trotter | raise
|
867 | aaae9bc0 | Guido Trotter | |
868 | 0cc00929 | Guido Trotter | return acquired
|
869 | aaae9bc0 | Guido Trotter | |
870 | aaae9bc0 | Guido Trotter | def release(self, names=None): |
871 | aaae9bc0 | Guido Trotter | """Release a set of resource locks, at the same level.
|
872 | aaae9bc0 | Guido Trotter |
|
873 | aaae9bc0 | Guido Trotter | You must have acquired the locks, either in shared or in exclusive mode,
|
874 | aaae9bc0 | Guido Trotter | before releasing them.
|
875 | aaae9bc0 | Guido Trotter |
|
876 | c41eea6e | Iustin Pop | @param names: the names of the locks which shall be released
|
877 | c41eea6e | Iustin Pop | (defaults to all the locks acquired at that level).
|
878 | aaae9bc0 | Guido Trotter |
|
879 | aaae9bc0 | Guido Trotter | """
|
880 | aaae9bc0 | Guido Trotter | assert self._is_owned(), "release() on lock set while not owner" |
881 | aaae9bc0 | Guido Trotter | |
882 | aaae9bc0 | Guido Trotter | # Support passing in a single resource to release rather than many
|
883 | aaae9bc0 | Guido Trotter | if isinstance(names, basestring): |
884 | aaae9bc0 | Guido Trotter | names = [names] |
885 | aaae9bc0 | Guido Trotter | |
886 | aaae9bc0 | Guido Trotter | if names is None: |
887 | aaae9bc0 | Guido Trotter | names = self._list_owned()
|
888 | aaae9bc0 | Guido Trotter | else:
|
889 | aaae9bc0 | Guido Trotter | names = set(names)
|
890 | aaae9bc0 | Guido Trotter | assert self._list_owned().issuperset(names), ( |
891 | aaae9bc0 | Guido Trotter | "release() on unheld resources %s" %
|
892 | aaae9bc0 | Guido Trotter | names.difference(self._list_owned()))
|
893 | aaae9bc0 | Guido Trotter | |
894 | 3b7ed473 | Guido Trotter | # First of all let's release the "all elements" lock, if set.
|
895 | 3b7ed473 | Guido Trotter | # After this 'add' can work again
|
896 | 3b7ed473 | Guido Trotter | if self.__lock._is_owned(): |
897 | 3b7ed473 | Guido Trotter | self.__lock.release()
|
898 | b2dabfd6 | Guido Trotter | self._del_owned()
|
899 | 3b7ed473 | Guido Trotter | |
900 | aaae9bc0 | Guido Trotter | for lockname in names: |
901 | aaae9bc0 | Guido Trotter | # If we are sure the lock doesn't leave __lockdict without being
|
902 | aaae9bc0 | Guido Trotter | # exclusively held we can do this...
|
903 | aaae9bc0 | Guido Trotter | self.__lockdict[lockname].release()
|
904 | b2dabfd6 | Guido Trotter | self._del_owned(name=lockname)
|
905 | aaae9bc0 | Guido Trotter | |
906 | aaae9bc0 | Guido Trotter | def add(self, names, acquired=0, shared=0): |
907 | aaae9bc0 | Guido Trotter | """Add a new set of elements to the set
|
908 | aaae9bc0 | Guido Trotter |
|
909 | c41eea6e | Iustin Pop | @param names: names of the new elements to add
|
910 | c41eea6e | Iustin Pop | @param acquired: pre-acquire the new resource?
|
911 | c41eea6e | Iustin Pop | @param shared: is the pre-acquisition shared?
|
912 | aaae9bc0 | Guido Trotter |
|
913 | aaae9bc0 | Guido Trotter | """
|
914 | d2aff862 | Guido Trotter | # Check we don't already own locks at this level
|
915 | d2aff862 | Guido Trotter | assert not self._is_owned() or self.__lock._is_owned(shared=0), \ |
916 | d2aff862 | Guido Trotter | "Cannot add locks if the set is only partially owned, or shared"
|
917 | 3b7ed473 | Guido Trotter | |
918 | aaae9bc0 | Guido Trotter | # Support passing in a single resource to add rather than many
|
919 | aaae9bc0 | Guido Trotter | if isinstance(names, basestring): |
920 | aaae9bc0 | Guido Trotter | names = [names] |
921 | aaae9bc0 | Guido Trotter | |
922 | ab62526c | Guido Trotter | # If we don't already own the set-level lock acquired in an exclusive way
|
923 | 3b7ed473 | Guido Trotter | # we'll get it and note we need to release it later.
|
924 | 3b7ed473 | Guido Trotter | release_lock = False
|
925 | 3b7ed473 | Guido Trotter | if not self.__lock._is_owned(): |
926 | 3b7ed473 | Guido Trotter | release_lock = True
|
927 | 3b7ed473 | Guido Trotter | self.__lock.acquire()
|
928 | 3b7ed473 | Guido Trotter | |
929 | aaae9bc0 | Guido Trotter | try:
|
930 | 0cf257c5 | Guido Trotter | invalid_names = set(self.__names()).intersection(names) |
931 | aaae9bc0 | Guido Trotter | if invalid_names:
|
932 | aaae9bc0 | Guido Trotter | # This must be an explicit raise, not an assert, because assert is
|
933 | aaae9bc0 | Guido Trotter | # turned off when using optimization, and this can happen because of
|
934 | aaae9bc0 | Guido Trotter | # concurrency even if the user doesn't want it.
|
935 | aaae9bc0 | Guido Trotter | raise errors.LockError("duplicate add() (%s)" % invalid_names) |
936 | aaae9bc0 | Guido Trotter | |
937 | aaae9bc0 | Guido Trotter | for lockname in names: |
938 | aaae9bc0 | Guido Trotter | lock = SharedLock() |
939 | aaae9bc0 | Guido Trotter | |
940 | aaae9bc0 | Guido Trotter | if acquired:
|
941 | aaae9bc0 | Guido Trotter | lock.acquire(shared=shared) |
942 | aaae9bc0 | Guido Trotter | # now the lock cannot be deleted, we have it!
|
943 | aaae9bc0 | Guido Trotter | try:
|
944 | b2dabfd6 | Guido Trotter | self._add_owned(name=lockname)
|
945 | aaae9bc0 | Guido Trotter | except:
|
946 | aaae9bc0 | Guido Trotter | # We shouldn't have problems adding the lock to the owners list,
|
947 | aaae9bc0 | Guido Trotter | # but if we did we'll try to release this lock and re-raise
|
948 | aaae9bc0 | Guido Trotter | # exception. Of course something is going to be really wrong,
|
949 | aaae9bc0 | Guido Trotter | # after this. On the other hand the lock hasn't been added to the
|
950 | aaae9bc0 | Guido Trotter | # __lockdict yet so no other threads should be pending on it. This
|
951 | aaae9bc0 | Guido Trotter | # release is just a safety measure.
|
952 | aaae9bc0 | Guido Trotter | lock.release() |
953 | aaae9bc0 | Guido Trotter | raise
|
954 | aaae9bc0 | Guido Trotter | |
955 | aaae9bc0 | Guido Trotter | self.__lockdict[lockname] = lock
|
956 | aaae9bc0 | Guido Trotter | |
957 | aaae9bc0 | Guido Trotter | finally:
|
958 | 3b7ed473 | Guido Trotter | # Only release __lock if we were not holding it previously.
|
959 | 3b7ed473 | Guido Trotter | if release_lock:
|
960 | 3b7ed473 | Guido Trotter | self.__lock.release()
|
961 | aaae9bc0 | Guido Trotter | |
962 | aaae9bc0 | Guido Trotter | return True |
963 | aaae9bc0 | Guido Trotter | |
964 | aaae9bc0 | Guido Trotter | def remove(self, names, blocking=1): |
965 | aaae9bc0 | Guido Trotter | """Remove elements from the lock set.
|
966 | aaae9bc0 | Guido Trotter |
|
967 | aaae9bc0 | Guido Trotter | You can either not hold anything in the lockset or already hold a superset
|
968 | aaae9bc0 | Guido Trotter | of the elements you want to delete, exclusively.
|
969 | aaae9bc0 | Guido Trotter |
|
970 | c41eea6e | Iustin Pop | @param names: names of the resource to remove.
|
971 | c41eea6e | Iustin Pop | @param blocking: whether to block while trying to acquire or to
|
972 | c41eea6e | Iustin Pop | operate in try-lock mode (this locking mode is not supported
|
973 | c41eea6e | Iustin Pop | yet unless you are already holding exclusively the locks)
|
974 | aaae9bc0 | Guido Trotter |
|
975 | c41eea6e | Iustin Pop | @return:: a list of locks which we removed; the list is always
|
976 | c41eea6e | Iustin Pop | equal to the names list if we were holding all the locks
|
977 | c41eea6e | Iustin Pop | exclusively
|
978 | aaae9bc0 | Guido Trotter |
|
979 | aaae9bc0 | Guido Trotter | """
|
980 | aaae9bc0 | Guido Trotter | if not blocking and not self._is_owned(): |
981 | aaae9bc0 | Guido Trotter | # We don't have non-blocking mode for now
|
982 | aaae9bc0 | Guido Trotter | raise NotImplementedError |
983 | aaae9bc0 | Guido Trotter | |
984 | aaae9bc0 | Guido Trotter | # Support passing in a single resource to remove rather than many
|
985 | aaae9bc0 | Guido Trotter | if isinstance(names, basestring): |
986 | aaae9bc0 | Guido Trotter | names = [names] |
987 | aaae9bc0 | Guido Trotter | |
988 | aaae9bc0 | Guido Trotter | # If we own any subset of this lock it must be a superset of what we want
|
989 | aaae9bc0 | Guido Trotter | # to delete. The ownership must also be exclusive, but that will be checked
|
990 | aaae9bc0 | Guido Trotter | # by the lock itself.
|
991 | aaae9bc0 | Guido Trotter | assert not self._is_owned() or self._list_owned().issuperset(names), ( |
992 | aaae9bc0 | Guido Trotter | "remove() on acquired lockset while not owning all elements")
|
993 | aaae9bc0 | Guido Trotter | |
994 | 3f404fc5 | Guido Trotter | removed = [] |
995 | aaae9bc0 | Guido Trotter | |
996 | aaae9bc0 | Guido Trotter | for lname in names: |
997 | aaae9bc0 | Guido Trotter | # Calling delete() acquires the lock exclusively if we don't already own
|
998 | aaae9bc0 | Guido Trotter | # it, and causes all pending and subsequent lock acquires to fail. It's
|
999 | aaae9bc0 | Guido Trotter | # fine to call it out of order because delete() also implies release(),
|
1000 | aaae9bc0 | Guido Trotter | # and the assertion above guarantees that if we either already hold
|
1001 | aaae9bc0 | Guido Trotter | # everything we want to delete, or we hold none.
|
1002 | aaae9bc0 | Guido Trotter | try:
|
1003 | aaae9bc0 | Guido Trotter | self.__lockdict[lname].delete()
|
1004 | 3f404fc5 | Guido Trotter | removed.append(lname) |
1005 | aaae9bc0 | Guido Trotter | except (KeyError, errors.LockError): |
1006 | aaae9bc0 | Guido Trotter | # This cannot happen if we were already holding it, verify:
|
1007 | aaae9bc0 | Guido Trotter | assert not self._is_owned(), "remove failed while holding lockset" |
1008 | aaae9bc0 | Guido Trotter | else:
|
1009 | aaae9bc0 | Guido Trotter | # If no LockError was raised we are the ones who deleted the lock.
|
1010 | aaae9bc0 | Guido Trotter | # This means we can safely remove it from lockdict, as any further or
|
1011 | aaae9bc0 | Guido Trotter | # pending delete() or acquire() will fail (and nobody can have the lock
|
1012 | aaae9bc0 | Guido Trotter | # since before our call to delete()).
|
1013 | aaae9bc0 | Guido Trotter | #
|
1014 | aaae9bc0 | Guido Trotter | # This is done in an else clause because if the exception was thrown
|
1015 | aaae9bc0 | Guido Trotter | # it's the job of the one who actually deleted it.
|
1016 | aaae9bc0 | Guido Trotter | del self.__lockdict[lname] |
1017 | aaae9bc0 | Guido Trotter | # And let's remove it from our private list if we owned it.
|
1018 | aaae9bc0 | Guido Trotter | if self._is_owned(): |
1019 | b2dabfd6 | Guido Trotter | self._del_owned(name=lname)
|
1020 | aaae9bc0 | Guido Trotter | |
1021 | 3f404fc5 | Guido Trotter | return removed
|
1022 | aaae9bc0 | Guido Trotter | |
1023 | 7ee7c0c7 | Guido Trotter | |
1024 | 7ee7c0c7 | Guido Trotter | # Locking levels, must be acquired in increasing order.
|
1025 | 7ee7c0c7 | Guido Trotter | # Current rules are:
|
1026 | 7ee7c0c7 | Guido Trotter | # - at level LEVEL_CLUSTER resides the Big Ganeti Lock (BGL) which must be
|
1027 | 7ee7c0c7 | Guido Trotter | # acquired before performing any operation, either in shared or in exclusive
|
1028 | 7ee7c0c7 | Guido Trotter | # mode. acquiring the BGL in exclusive mode is discouraged and should be
|
1029 | 7ee7c0c7 | Guido Trotter | # avoided.
|
1030 | 7ee7c0c7 | Guido Trotter | # - at levels LEVEL_NODE and LEVEL_INSTANCE reside node and instance locks.
|
1031 | 7ee7c0c7 | Guido Trotter | # If you need more than one node, or more than one instance, acquire them at
|
1032 | 7ee7c0c7 | Guido Trotter | # the same time.
|
1033 | 7ee7c0c7 | Guido Trotter | LEVEL_CLUSTER = 0
|
1034 | 04e1bfaf | Guido Trotter | LEVEL_INSTANCE = 1
|
1035 | 04e1bfaf | Guido Trotter | LEVEL_NODE = 2
|
1036 | 7ee7c0c7 | Guido Trotter | |
1037 | 7ee7c0c7 | Guido Trotter | LEVELS = [LEVEL_CLUSTER, |
1038 | 04e1bfaf | Guido Trotter | LEVEL_INSTANCE, |
1039 | 04e1bfaf | Guido Trotter | LEVEL_NODE] |
1040 | 7ee7c0c7 | Guido Trotter | |
1041 | 7ee7c0c7 | Guido Trotter | # Lock levels which are modifiable
|
1042 | 7ee7c0c7 | Guido Trotter | LEVELS_MOD = [LEVEL_NODE, LEVEL_INSTANCE] |
1043 | 7ee7c0c7 | Guido Trotter | |
1044 | ea205dbc | Michael Hanselmann | LEVEL_NAMES = { |
1045 | ea205dbc | Michael Hanselmann | LEVEL_CLUSTER: "cluster",
|
1046 | ea205dbc | Michael Hanselmann | LEVEL_INSTANCE: "instance",
|
1047 | ea205dbc | Michael Hanselmann | LEVEL_NODE: "node",
|
1048 | ea205dbc | Michael Hanselmann | } |
1049 | ea205dbc | Michael Hanselmann | |
1050 | 08a6c581 | Guido Trotter | # Constant for the big ganeti lock
|
1051 | 7ee7c0c7 | Guido Trotter | BGL = 'BGL'
|
1052 | 7ee7c0c7 | Guido Trotter | |
1053 | 7ee7c0c7 | Guido Trotter | |
1054 | 7ee7c0c7 | Guido Trotter | class GanetiLockManager: |
1055 | 7ee7c0c7 | Guido Trotter | """The Ganeti Locking Library
|
1056 | 7ee7c0c7 | Guido Trotter |
|
1057 | 5bbd3f7f | Michael Hanselmann | The purpose of this small library is to manage locking for ganeti clusters
|
1058 | 7ee7c0c7 | Guido Trotter | in a central place, while at the same time doing dynamic checks against
|
1059 | 7ee7c0c7 | Guido Trotter | possible deadlocks. It will also make it easier to transition to a different
|
1060 | 7ee7c0c7 | Guido Trotter | lock type should we migrate away from python threads.
|
1061 | 7ee7c0c7 | Guido Trotter |
|
1062 | 7ee7c0c7 | Guido Trotter | """
|
1063 | 7ee7c0c7 | Guido Trotter | _instance = None
|
1064 | 7ee7c0c7 | Guido Trotter | |
1065 | 7ee7c0c7 | Guido Trotter | def __init__(self, nodes=None, instances=None): |
1066 | 7ee7c0c7 | Guido Trotter | """Constructs a new GanetiLockManager object.
|
1067 | 7ee7c0c7 | Guido Trotter |
|
1068 | 4e07ec8c | Guido Trotter | There should be only a GanetiLockManager object at any time, so this
|
1069 | 4e07ec8c | Guido Trotter | function raises an error if this is not the case.
|
1070 | 7ee7c0c7 | Guido Trotter |
|
1071 | c41eea6e | Iustin Pop | @param nodes: list of node names
|
1072 | c41eea6e | Iustin Pop | @param instances: list of instance names
|
1073 | 7ee7c0c7 | Guido Trotter |
|
1074 | 7ee7c0c7 | Guido Trotter | """
|
1075 | c41eea6e | Iustin Pop | assert self.__class__._instance is None, \ |
1076 | c41eea6e | Iustin Pop | "double GanetiLockManager instance"
|
1077 | c41eea6e | Iustin Pop | |
1078 | 7ee7c0c7 | Guido Trotter | self.__class__._instance = self |
1079 | 7ee7c0c7 | Guido Trotter | |
1080 | 7ee7c0c7 | Guido Trotter | # The keyring contains all the locks, at their level and in the correct
|
1081 | 7ee7c0c7 | Guido Trotter | # locking order.
|
1082 | 7ee7c0c7 | Guido Trotter | self.__keyring = {
|
1083 | 7ee7c0c7 | Guido Trotter | LEVEL_CLUSTER: LockSet([BGL]), |
1084 | 7ee7c0c7 | Guido Trotter | LEVEL_NODE: LockSet(nodes), |
1085 | 7ee7c0c7 | Guido Trotter | LEVEL_INSTANCE: LockSet(instances), |
1086 | 7ee7c0c7 | Guido Trotter | } |
1087 | 7ee7c0c7 | Guido Trotter | |
1088 | 7ee7c0c7 | Guido Trotter | def _names(self, level): |
1089 | 7ee7c0c7 | Guido Trotter | """List the lock names at the given level.
|
1090 | 7ee7c0c7 | Guido Trotter |
|
1091 | c41eea6e | Iustin Pop | This can be used for debugging/testing purposes.
|
1092 | c41eea6e | Iustin Pop |
|
1093 | c41eea6e | Iustin Pop | @param level: the level whose list of locks to get
|
1094 | 7ee7c0c7 | Guido Trotter |
|
1095 | 7ee7c0c7 | Guido Trotter | """
|
1096 | 7ee7c0c7 | Guido Trotter | assert level in LEVELS, "Invalid locking level %s" % level |
1097 | 7ee7c0c7 | Guido Trotter | return self.__keyring[level]._names() |
1098 | 7ee7c0c7 | Guido Trotter | |
1099 | 7ee7c0c7 | Guido Trotter | def _is_owned(self, level): |
1100 | 7ee7c0c7 | Guido Trotter | """Check whether we are owning locks at the given level
|
1101 | 7ee7c0c7 | Guido Trotter |
|
1102 | 7ee7c0c7 | Guido Trotter | """
|
1103 | 7ee7c0c7 | Guido Trotter | return self.__keyring[level]._is_owned() |
1104 | 7ee7c0c7 | Guido Trotter | |
1105 | d4f4b3e7 | Guido Trotter | is_owned = _is_owned |
1106 | d4f4b3e7 | Guido Trotter | |
1107 | 7ee7c0c7 | Guido Trotter | def _list_owned(self, level): |
1108 | 7ee7c0c7 | Guido Trotter | """Get the set of owned locks at the given level
|
1109 | 7ee7c0c7 | Guido Trotter |
|
1110 | 7ee7c0c7 | Guido Trotter | """
|
1111 | 7ee7c0c7 | Guido Trotter | return self.__keyring[level]._list_owned() |
1112 | 7ee7c0c7 | Guido Trotter | |
1113 | 7ee7c0c7 | Guido Trotter | def _upper_owned(self, level): |
1114 | 7ee7c0c7 | Guido Trotter | """Check that we don't own any lock at a level greater than the given one.
|
1115 | 7ee7c0c7 | Guido Trotter |
|
1116 | 7ee7c0c7 | Guido Trotter | """
|
1117 | 7ee7c0c7 | Guido Trotter | # This way of checking only works if LEVELS[i] = i, which we check for in
|
1118 | 7ee7c0c7 | Guido Trotter | # the test cases.
|
1119 | 7ee7c0c7 | Guido Trotter | return utils.any((self._is_owned(l) for l in LEVELS[level + 1:])) |
1120 | 7ee7c0c7 | Guido Trotter | |
1121 | 7ee7c0c7 | Guido Trotter | def _BGL_owned(self): |
1122 | 7ee7c0c7 | Guido Trotter | """Check if the current thread owns the BGL.
|
1123 | 7ee7c0c7 | Guido Trotter |
|
1124 | 7ee7c0c7 | Guido Trotter | Both an exclusive or a shared acquisition work.
|
1125 | 7ee7c0c7 | Guido Trotter |
|
1126 | 7ee7c0c7 | Guido Trotter | """
|
1127 | 7ee7c0c7 | Guido Trotter | return BGL in self.__keyring[LEVEL_CLUSTER]._list_owned() |
1128 | 7ee7c0c7 | Guido Trotter | |
1129 | 7ee7c0c7 | Guido Trotter | def _contains_BGL(self, level, names): |
1130 | c41eea6e | Iustin Pop | """Check if the level contains the BGL.
|
1131 | c41eea6e | Iustin Pop |
|
1132 | c41eea6e | Iustin Pop | Check if acting on the given level and set of names will change
|
1133 | c41eea6e | Iustin Pop | the status of the Big Ganeti Lock.
|
1134 | 7ee7c0c7 | Guido Trotter |
|
1135 | 7ee7c0c7 | Guido Trotter | """
|
1136 | 7ee7c0c7 | Guido Trotter | return level == LEVEL_CLUSTER and (names is None or BGL in names) |
1137 | 7ee7c0c7 | Guido Trotter | |
1138 | 7ee7c0c7 | Guido Trotter | def acquire(self, level, names, blocking=1, shared=0): |
1139 | 7ee7c0c7 | Guido Trotter | """Acquire a set of resource locks, at the same level.
|
1140 | 7ee7c0c7 | Guido Trotter |
|
1141 | c41eea6e | Iustin Pop | @param level: the level at which the locks shall be acquired;
|
1142 | 5bbd3f7f | Michael Hanselmann | it must be a member of LEVELS.
|
1143 | c41eea6e | Iustin Pop | @param names: the names of the locks which shall be acquired
|
1144 | c41eea6e | Iustin Pop | (special lock names, or instance/node names)
|
1145 | c41eea6e | Iustin Pop | @param shared: whether to acquire in shared mode; by default
|
1146 | c41eea6e | Iustin Pop | an exclusive lock will be acquired
|
1147 | c41eea6e | Iustin Pop | @param blocking: whether to block while trying to acquire or to
|
1148 | c41eea6e | Iustin Pop | operate in try-lock mode (this locking mode is not supported yet)
|
1149 | 7ee7c0c7 | Guido Trotter |
|
1150 | 7ee7c0c7 | Guido Trotter | """
|
1151 | 7ee7c0c7 | Guido Trotter | assert level in LEVELS, "Invalid locking level %s" % level |
1152 | 7ee7c0c7 | Guido Trotter | |
1153 | 7ee7c0c7 | Guido Trotter | # Check that we are either acquiring the Big Ganeti Lock or we already own
|
1154 | 7ee7c0c7 | Guido Trotter | # it. Some "legacy" opcodes need to be sure they are run non-concurrently
|
1155 | 7ee7c0c7 | Guido Trotter | # so even if we've migrated we need to at least share the BGL to be
|
1156 | 7ee7c0c7 | Guido Trotter | # compatible with them. Of course if we own the BGL exclusively there's no
|
1157 | 7ee7c0c7 | Guido Trotter | # point in acquiring any other lock, unless perhaps we are half way through
|
1158 | 7ee7c0c7 | Guido Trotter | # the migration of the current opcode.
|
1159 | 7ee7c0c7 | Guido Trotter | assert (self._contains_BGL(level, names) or self._BGL_owned()), ( |
1160 | 7ee7c0c7 | Guido Trotter | "You must own the Big Ganeti Lock before acquiring any other")
|
1161 | 7ee7c0c7 | Guido Trotter | |
1162 | 7ee7c0c7 | Guido Trotter | # Check we don't own locks at the same or upper levels.
|
1163 | 21a6c826 | Guido Trotter | assert not self._upper_owned(level), ("Cannot acquire locks at a level" |
1164 | 7ee7c0c7 | Guido Trotter | " while owning some at a greater one")
|
1165 | 7ee7c0c7 | Guido Trotter | |
1166 | 7ee7c0c7 | Guido Trotter | # Acquire the locks in the set.
|
1167 | 7ee7c0c7 | Guido Trotter | return self.__keyring[level].acquire(names, shared=shared, |
1168 | 7ee7c0c7 | Guido Trotter | blocking=blocking) |
1169 | 7ee7c0c7 | Guido Trotter | |
1170 | 7ee7c0c7 | Guido Trotter | def release(self, level, names=None): |
1171 | 7ee7c0c7 | Guido Trotter | """Release a set of resource locks, at the same level.
|
1172 | 7ee7c0c7 | Guido Trotter |
|
1173 | c41eea6e | Iustin Pop | You must have acquired the locks, either in shared or in exclusive
|
1174 | c41eea6e | Iustin Pop | mode, before releasing them.
|
1175 | 7ee7c0c7 | Guido Trotter |
|
1176 | c41eea6e | Iustin Pop | @param level: the level at which the locks shall be released;
|
1177 | 5bbd3f7f | Michael Hanselmann | it must be a member of LEVELS
|
1178 | c41eea6e | Iustin Pop | @param names: the names of the locks which shall be released
|
1179 | c41eea6e | Iustin Pop | (defaults to all the locks acquired at that level)
|
1180 | 7ee7c0c7 | Guido Trotter |
|
1181 | 7ee7c0c7 | Guido Trotter | """
|
1182 | 7ee7c0c7 | Guido Trotter | assert level in LEVELS, "Invalid locking level %s" % level |
1183 | 7ee7c0c7 | Guido Trotter | assert (not self._contains_BGL(level, names) or |
1184 | 7ee7c0c7 | Guido Trotter | not self._upper_owned(LEVEL_CLUSTER)), ( |
1185 | 7ee7c0c7 | Guido Trotter | "Cannot release the Big Ganeti Lock while holding something"
|
1186 | 7ee7c0c7 | Guido Trotter | " at upper levels")
|
1187 | 7ee7c0c7 | Guido Trotter | |
1188 | 7ee7c0c7 | Guido Trotter | # Release will complain if we don't own the locks already
|
1189 | 7ee7c0c7 | Guido Trotter | return self.__keyring[level].release(names) |
1190 | 7ee7c0c7 | Guido Trotter | |
1191 | 7ee7c0c7 | Guido Trotter | def add(self, level, names, acquired=0, shared=0): |
1192 | 7ee7c0c7 | Guido Trotter | """Add locks at the specified level.
|
1193 | 7ee7c0c7 | Guido Trotter |
|
1194 | c41eea6e | Iustin Pop | @param level: the level at which the locks shall be added;
|
1195 | 5bbd3f7f | Michael Hanselmann | it must be a member of LEVELS_MOD.
|
1196 | c41eea6e | Iustin Pop | @param names: names of the locks to acquire
|
1197 | c41eea6e | Iustin Pop | @param acquired: whether to acquire the newly added locks
|
1198 | c41eea6e | Iustin Pop | @param shared: whether the acquisition will be shared
|
1199 | c41eea6e | Iustin Pop |
|
1200 | 7ee7c0c7 | Guido Trotter | """
|
1201 | 7ee7c0c7 | Guido Trotter | assert level in LEVELS_MOD, "Invalid or immutable level %s" % level |
1202 | 7ee7c0c7 | Guido Trotter | assert self._BGL_owned(), ("You must own the BGL before performing other" |
1203 | 7ee7c0c7 | Guido Trotter | " operations")
|
1204 | 7ee7c0c7 | Guido Trotter | assert not self._upper_owned(level), ("Cannot add locks at a level" |
1205 | 7ee7c0c7 | Guido Trotter | " while owning some at a greater one")
|
1206 | 7ee7c0c7 | Guido Trotter | return self.__keyring[level].add(names, acquired=acquired, shared=shared) |
1207 | 7ee7c0c7 | Guido Trotter | |
1208 | 7ee7c0c7 | Guido Trotter | def remove(self, level, names, blocking=1): |
1209 | 7ee7c0c7 | Guido Trotter | """Remove locks from the specified level.
|
1210 | 7ee7c0c7 | Guido Trotter |
|
1211 | c41eea6e | Iustin Pop | You must either already own the locks you are trying to remove
|
1212 | c41eea6e | Iustin Pop | exclusively or not own any lock at an upper level.
|
1213 | 7ee7c0c7 | Guido Trotter |
|
1214 | c41eea6e | Iustin Pop | @param level: the level at which the locks shall be removed;
|
1215 | c41eea6e | Iustin Pop | it must be a member of LEVELS_MOD
|
1216 | c41eea6e | Iustin Pop | @param names: the names of the locks which shall be removed
|
1217 | c41eea6e | Iustin Pop | (special lock names, or instance/node names)
|
1218 | c41eea6e | Iustin Pop | @param blocking: whether to block while trying to operate in
|
1219 | c41eea6e | Iustin Pop | try-lock mode (this locking mode is not supported yet)
|
1220 | 7ee7c0c7 | Guido Trotter |
|
1221 | 7ee7c0c7 | Guido Trotter | """
|
1222 | 7ee7c0c7 | Guido Trotter | assert level in LEVELS_MOD, "Invalid or immutable level %s" % level |
1223 | 7ee7c0c7 | Guido Trotter | assert self._BGL_owned(), ("You must own the BGL before performing other" |
1224 | 7ee7c0c7 | Guido Trotter | " operations")
|
1225 | f12eadb3 | Iustin Pop | # Check we either own the level or don't own anything from here
|
1226 | f12eadb3 | Iustin Pop | # up. LockSet.remove() will check the case in which we don't own
|
1227 | f12eadb3 | Iustin Pop | # all the needed resources, or we have a shared ownership.
|
1228 | 7ee7c0c7 | Guido Trotter | assert self._is_owned(level) or not self._upper_owned(level), ( |
1229 | 7ee7c0c7 | Guido Trotter | "Cannot remove locks at a level while not owning it or"
|
1230 | 7ee7c0c7 | Guido Trotter | " owning some at a greater one")
|
1231 | cdb08f44 | Michael Hanselmann | return self.__keyring[level].remove(names, blocking=blocking) |