summaryrefslogtreecommitdiff
path: root/eventlet/lock.py
blob: 4b21e0b4558b7fe76e85d7c344a6d8883fd7524e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from eventlet import hubs
from eventlet.semaphore import Semaphore


class Lock(Semaphore):

    """A lock.
    This is API-compatible with :class:`threading.Lock`.

    It is a context manager, and thus can be used in a with block::

      lock = Lock()
      with lock:
        do_some_stuff()
    """

    def release(self, blocking=True):
        """Modify behaviour vs :class:`Semaphore` to raise a RuntimeError
        exception if the value is greater than zero. This corrects behaviour
        to realign with :class:`threading.Lock`.
        """
        if self.counter > 0:
            raise RuntimeError("release unlocked lock")

        # Consciously *do not* call super().release(), but instead inline
        # Semaphore.release() here. We've seen issues with logging._lock
        # deadlocking because garbage collection happened to run mid-release
        # and eliminating the extra stack frame should help prevent that.
        # See https://github.com/eventlet/eventlet/issues/742
        self.counter += 1
        if self._waiters:
            hubs.get_hub().schedule_call_global(0, self._do_acquire)
        return True

    def _at_fork_reinit(self):
        self.counter = 1
        self._waiters.clear()