Skip to content

Add a Fair lock recipe? #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions diskcache/recipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,64 @@ def __exit__(self, *exc_info):
self.release()


class FairLock(object):
"""Recipe for cross-process and cross-thread fair lock.

Based on the "Ticket Lock" algorithm:
https://en.wikipedia.org/wiki/Ticket_lock

"""
def __init__(self, cache, prefix, expire=None, tag=None):
self._cache = cache
self._tickets_key = prefix + 'tickets'
self._serving_key = prefix + 'serving'
self._expire = expire
self._tag = tag

def setup(self):
self._cache.add(
self._tickets_key, 0,
expire=self._expire, tag=self._tag, retry=True,
)
self._cache.add(
self._serving_key, 1,
expire=self._expire, tag=self._tag, retry=True,
)

def acquire(self):
while True:
ticket = self._cache.incr(
self._tickets_key,
expire=self._expire, tag=self._tag, retry=True,
)
while True:
serving = self._cache.get(self._serving_key, retry=True)
if serving is None:
self._cache.add(
self._serving_key, ticket,
expire=self._expire, tag=self._tag, retry=True,
)
# TODO: How do we know the tickets key has not expired?
# If the tickets key has expired, is it possible that
# another acquire() method has the same ticket?
# Maybe a transaction is necessary to set both keys?
# If both keys are set then what if another thread has
# acquired the lock already?
elif serving < ticket:
time.sleep(0.001)
elif serving == ticket:
return
else:
assert serving > ticket
# We got skipped! Take a new ticket.
break

def release(self):
self._cache.incr(
self._serving_key, expire=self._expire, tag=self._tag, retry=True,
)


class BoundedSemaphore(object):
"""Recipe for cross-process and cross-thread bounded semaphore.

Expand Down