Skip to content

Commit

Permalink
de-obfuscated some delicate locking code
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Fulton committed Jun 19, 2016
1 parent 532fd78 commit 0ed7579
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/ZODB/MappingStorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,28 @@ def tpc_abort(self, transaction):
self._commit_lock.release()

# ZODB.interfaces.IStorage
@ZODB.utils.locked(opened)
def tpc_begin(self, transaction, tid=None):
# The tid argument exists to support testing.
if transaction is self._transaction:
raise ZODB.POSException.StorageTransactionError(
"Duplicate tpc_begin calls for same transaction")
self._lock.release()
with self._lock:

ZODB.utils.check_precondition(self.opened)

# The tid argument exists to support testing.
if transaction is self._transaction:
raise ZODB.POSException.StorageTransactionError(
"Duplicate tpc_begin calls for same transaction")

self._commit_lock.acquire()
self._lock.acquire()
self._transaction = transaction
self._tdata = {}
if tid is None:
if self._transactions:
old_tid = self._transactions.maxKey()
else:
old_tid = None
tid = ZODB.utils.newTid(old_tid)
self._tid = tid

with self._lock:
self._transaction = transaction
self._tdata = {}
if tid is None:
if self._transactions:
old_tid = self._transactions.maxKey()
else:
old_tid = None
tid = ZODB.utils.newTid(old_tid)
self._tid = tid

# ZODB.interfaces.IStorage
@ZODB.utils.locked(opened)
Expand Down
6 changes: 6 additions & 0 deletions src/ZODB/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ def mktemp(dir=None, prefix='tmp'):
os.close(handle)
return filename

def check_precondition(precondition):
if not precondition():
raise AssertionError(
"Failed precondition: ",
precondition.__doc__.strip())

class Locked(object):

def __init__(self, func, inst=None, class_=None, preconditions=()):
Expand Down

0 comments on commit 0ed7579

Please sign in to comment.