Revision 2419060d

b/lib/locking.py
103 103
        remaining_time = start_time + timeout - time.time()
104 104

  
105 105

  
106
class _BaseCondition(object):
107
  """Base class containing common code for conditions.
108

  
109
  Some of this code is taken from python's threading module.
110

  
111
  """
112
  __slots__ = [
113
    "_lock",
114
    "acquire",
115
    "release",
116
    ]
117

  
118
  def __init__(self, lock):
119
    """Constructor for _BaseCondition.
120

  
121
    @type lock: L{threading.Lock}
122
    @param lock: condition base lock
123

  
124
    """
125
    object.__init__(self)
126

  
127
    # Recursive locks are not supported
128
    assert not hasattr(lock, "_acquire_restore")
129
    assert not hasattr(lock, "_release_save")
130

  
131
    self._lock = lock
132

  
133
    # Export the lock's acquire() and release() methods
134
    self.acquire = lock.acquire
135
    self.release = lock.release
136

  
137
  def _is_owned(self):
138
    """Check whether lock is owned by current thread.
139

  
140
    """
141
    if self._lock.acquire(0):
142
      self._lock.release()
143
      return False
144

  
145
    return True
146

  
147
  def _check_owned(self):
148
    """Raise an exception if the current thread doesn't own the lock.
149

  
150
    """
151
    if not self._is_owned():
152
      raise RuntimeError("cannot work with un-aquired lock")
153

  
154

  
106 155
class _SingleActionPipeCondition(object):
107 156
  """Wrapper around a pipe for usage inside conditions.
108 157

  
......
223 272
    self._Cleanup()
224 273

  
225 274

  
226
class _PipeCondition(object):
275
class _PipeCondition(_BaseCondition):
227 276
  """Group-only non-polling condition with counters.
228 277

  
229 278
  This condition class uses pipes and poll, internally, to be able to wait for
......
233 282
  there are any waiting threads.
234 283

  
235 284
  """
236
  __slots__ = [
237
    "_lock",
285
  __slots__ = _BaseCondition.__slots__ + [
238 286
    "_nwaiters",
239 287
    "_pipe",
240
    "acquire",
241
    "release",
242 288
    ]
243 289

  
244 290
  _pipe_class = _SingleActionPipeCondition
......
247 293
    """Initializes this class.
248 294

  
249 295
    """
250
    object.__init__(self)
251

  
252
    # Recursive locks are not supported
253
    assert not hasattr(lock, "_acquire_restore")
254
    assert not hasattr(lock, "_release_save")
255

  
256
    self._lock = lock
257

  
258
    # Export the lock's acquire() and release() methods
259
    self.acquire = lock.acquire
260
    self.release = lock.release
261

  
296
    _BaseCondition.__init__(self, lock)
262 297
    self._nwaiters = 0
263 298
    self._pipe = None
264 299

  
265
  def _is_owned(self):
266
    """Check whether lock is owned by current thread.
267

  
268
    """
269
    if self._lock.acquire(0):
270
      self._lock.release()
271
      return False
272

  
273
    return True
274

  
275
  def _check_owned(self):
276
    """Raise an exception if the current thread doesn't own the lock.
277

  
278
    """
279
    if not self._is_owned():
280
      raise RuntimeError("cannot work with un-aquired lock")
281

  
282 300
  def wait(self, timeout=None):
283 301
    """Wait for a notification.
284 302

  

Also available in: Unified diff