summaryrefslogtreecommitdiff
path: root/lib/spack/llnl/util/lock.py
diff options
context:
space:
mode:
Diffstat (limited to 'lib/spack/llnl/util/lock.py')
-rw-r--r--lib/spack/llnl/util/lock.py30
1 files changed, 21 insertions, 9 deletions
diff --git a/lib/spack/llnl/util/lock.py b/lib/spack/llnl/util/lock.py
index bb3b15c9cf..3cd02befe5 100644
--- a/lib/spack/llnl/util/lock.py
+++ b/lib/spack/llnl/util/lock.py
@@ -29,13 +29,15 @@ import errno
import time
import socket
+# Default timeout for locks.
+DEFAULT_TIMEOUT = 60
+
+class _ReadLockContext(object):
+ """Context manager that takes and releases a read lock.
-class Read_Lock_Instance(object):
- """
- A context manager for getting shared access to the object lock
Arguments are lock and timeout (default 5 minutes)
"""
- def __init__(self, lock, timeout=300):
+ def __init__(self, lock, timeout=DEFAULT_TIMEOUT):
self._lock = lock
self._timeout = timeout
@@ -46,12 +48,12 @@ class Read_Lock_Instance(object):
self._lock.release_read()
-class Write_Lock_Instance(object):
- """
- A context manager for getting exclusive access to the object lock
+class _WriteLockContext(object):
+ """Context manager that takes and releases a write lock.
+
Arguments are lock and timeout (default 5 minutes)
"""
- def __init__(self, lock, timeout=300):
+ def __init__(self, lock, timeout=DEFAULT_TIMEOUT):
self._lock = lock
self._timeout = timeout
@@ -72,7 +74,17 @@ class Lock(object):
self._writes = 0
- def acquire_read(self,timeout):
+ def write_lock(self, timeout=DEFAULT_TIMEOUT):
+ """Convenience method that returns a write lock context."""
+ return _WriteLockContext(self, timeout)
+
+
+ def read_lock(self, timeout=DEFAULT_TIMEOUT):
+ """Convenience method that returns a read lock context."""
+ return _ReadLockContext(self, timeout)
+
+
+ def acquire_read(self, timeout):
"""
Implements recursive lock. If held in both read and write mode,
the write lock will be maintained until all locks are released