summaryrefslogtreecommitdiff
path: root/src/s3ql/multi_lock.py
diff options
context:
space:
mode:
authorNikolaus Rath <Nikolaus@rath.org>2016-03-09 10:08:24 -0800
committerNikolaus Rath <Nikolaus@rath.org>2016-03-09 10:08:24 -0800
commit538edbe3d8a600a9ee4ff4a7822abbc79cbf00e1 (patch)
tree21cd3a3ec21703fc89ffde47c88f23b2f4a3e785 /src/s3ql/multi_lock.py
parent5b62499deb280253671a3c468644335be9e55f04 (diff)
Import s3ql_1.2.orig.tar.bz2
Diffstat (limited to 'src/s3ql/multi_lock.py')
-rw-r--r--src/s3ql/multi_lock.py85
1 files changed, 0 insertions, 85 deletions
diff --git a/src/s3ql/multi_lock.py b/src/s3ql/multi_lock.py
deleted file mode 100644
index 865c16c..0000000
--- a/src/s3ql/multi_lock.py
+++ /dev/null
@@ -1,85 +0,0 @@
-'''
-multi_lock.py - this file is part of S3QL (http://s3ql.googlecode.com)
-
-Copyright (C) 2008-2009 Nikolaus Rath <Nikolaus@rath.org>
-
-This program can be distributed under the terms of the GNU LGPL.
-'''
-
-from __future__ import division, print_function
-
-import threading
-import logging
-from contextlib import contextmanager
-import time
-
-__all__ = [ "MultiLock" ]
-
-log = logging.getLogger("MultiLock")
-
-
-# For debugging, can be set to impose an artifical delay when
-# obtaining the lock. Introduced to debug a very
-# timing-critical bug.
-FAKEDELAY = False
-
-class MultiLock(object):
- """Provides locking for multiple objects.
-
- This class provides locking for a dynamically changing set of objects:
- The `acquire` and `release` methods have an additional argument, the
- locking key. Only locks with the same key can actually see each other,
- so that several threads can hold locks with different locking keys
- at the same time.
-
- MultiLock instances can be used with `with` statements as
-
- lock = MultiLock()
- with lock(key):
- pass
-
- Note that it is actually possible for one thread to release a lock
- that has been obtained by a different thread. This is not a bug,
- but a feature used in `BlockCache._expire_parallel`.
- """
-
- def __init__(self):
- self.locked_keys = set()
- self.cond = threading.Condition()
-
-
- @contextmanager
- def __call__(self, *key):
- self.acquire(*key)
- try:
- yield
- finally:
- self.release(*key)
-
- def acquire(self, *key):
- '''Acquire lock for given key'''
-
- if FAKEDELAY:
- time.sleep(FAKEDELAY)
-
- # Lock set of lockedkeys (global lock)
- with self.cond:
-
- # Wait for given key becoming unused
- while key in self.locked_keys:
- self.cond.wait()
-
- # Mark it as used (local lock)
- self.locked_keys.add(key)
-
- def release(self, *key):
- """Release lock on given key"""
-
- # Lock set of locked keys (global lock)
- with self.cond:
-
- # Mark key as free (release local lock)
- self.locked_keys.remove(key)
-
- # Notify other threads
- self.cond.notifyAll()