Revision 41a26b68 lib/utils.py

b/lib/utils.py
3184 3184
      raise
3185 3185

  
3186 3186

  
3187
def LockedMethod(fn):
3188
  """Synchronized object access decorator.
3189

  
3190
  This decorator is intended to protect access to an object using the
3191
  object's own lock which is hardcoded to '_lock'.
3192

  
3193
  """
3194
  def _LockDebug(*args, **kwargs):
3195
    if debug_locks:
3196
      logging.debug(*args, **kwargs)
3197

  
3198
  def wrapper(self, *args, **kwargs):
3199
    # pylint: disable-msg=W0212
3200
    assert hasattr(self, '_lock')
3201
    lock = self._lock
3202
    _LockDebug("Waiting for %s", lock)
3203
    lock.acquire()
3204
    try:
3205
      _LockDebug("Acquired %s", lock)
3206
      result = fn(self, *args, **kwargs)
3207
    finally:
3208
      _LockDebug("Releasing %s", lock)
3209
      lock.release()
3210
      _LockDebug("Released %s", lock)
3211
    return result
3212
  return wrapper
3213

  
3214

  
3215 3187
def LockFile(fd):
3216 3188
  """Locks a file using POSIX locks.
3217 3189

  

Also available in: Unified diff