Debian Codegraph @

lupa/lock.pxi

raw
1
2 from cpython cimport pythread
3
4 cdef extern from *:
5 """
6 typedef unsigned long pythread_t;
7 """
8 ctypedef unsigned long pythread_t
9
10
11 cdef class FastRLock:
12 """Fast, re-entrant locking.
13
14 Under uncongested conditions, the lock is never acquired but only
15 counted. Only when a second thread comes in and notices that the
16 lock is needed, it acquires the lock and notifies the first thread
17 to release it when it's done. This is all made possible by the
18 wonderful GIL.
19 """
20 cdef pythread.PyThread_type_lock _real_lock
21 cdef pythread_t _owner # ID of thread owning the lock
22 cdef unsigned int _count # re-entry count
23 cdef unsigned int _pending_requests # number of pending requests for real lock
24 cdef bint _is_locked # whether the real lock is acquired
25
26 def __cinit__(self):
27 self._owner = 0
28 self._count = 0
29 self._is_locked = False
30 self._pending_requests = 0
31 self._real_lock = pythread.PyThread_allocate_lock()
32 if self._real_lock is NULL:
33 raise MemoryError
34
35 def __dealloc__(self):
36 if self._real_lock is not NULL:
37 pythread.PyThread_free_lock(self._real_lock)
38 self._real_lock = NULL
39
40 def acquire(self, bint blocking=True):
41 return lock_lock(self, pythread.PyThread_get_thread_ident(), blocking)
42
43 def release(self):
44 if self._count == 0 or self._owner != pythread.PyThread_get_thread_ident():
45 raise RuntimeError("cannot release un-acquired lock")
46 unlock_lock(self)
47
48 # compatibility with RLock
49
50 def __enter__(self):
51 # self.acquire()
52 return lock_lock(self, pythread.PyThread_get_thread_ident(), True)
53
54 def __exit__(self, t, v, tb):
55 # self.release()
56 if self._count == 0 or self._owner != pythread.PyThread_get_thread_ident():
57 raise RuntimeError("cannot release un-acquired lock")
58 unlock_lock(self)
59
60 def _is_owned(self):
61 return self._count > 0 and self._owner == pythread.PyThread_get_thread_ident()
62
63
64 cdef inline bint lock_lock(FastRLock lock, pythread_t current_thread, bint blocking) noexcept nogil:
65 # Note that this function *must* hold the GIL when being called.
66 # We just use 'nogil' in the signature to make sure that no Python
67 # code execution slips in that might free the GIL
68
69 if lock._count:
70 # locked! - by myself?
71 if current_thread == lock._owner:
72 lock._count += 1
73 return 1
74 elif lock._pending_requests == 0:
75 # not locked, not requested - go!
76 lock._owner = current_thread
77 lock._count = 1
78 return 1
79 # need to get the real lock
80 return _acquire_lock(
81 lock, current_thread,
82 pythread.WAIT_LOCK if blocking else pythread.NOWAIT_LOCK)
83
84 cdef bint _acquire_lock(FastRLock lock, pythread_t current_thread, int wait) noexcept nogil:
85 # Note that this function *must* hold the GIL when being called.
86 # We just use 'nogil' in the signature to make sure that no Python
87 # code execution slips in that might free the GIL
88
89 if not lock._is_locked and lock._pending_requests == 0:
90 # someone owns it but didn't acquire the real lock - do that
91 # now and tell the owner to release it when done. Note that we
92 # do not release the GIL here as we must absolutely be the one
93 # who acquires the lock now.
94 if not pythread.PyThread_acquire_lock(lock._real_lock, wait):
95 return 0
96 #assert not lock._is_locked
97 lock._is_locked = True
98 lock._pending_requests += 1
99 with nogil:
100 # wait for the lock owning thread to release it
101 locked = pythread.PyThread_acquire_lock(lock._real_lock, wait)
102 lock._pending_requests -= 1
103 #assert not lock._is_locked
104 #assert lock._count == 0
105 if not locked:
106 return 0
107 lock._is_locked = True
108 lock._owner = current_thread
109 lock._count = 1
110 return 1
111
112 cdef inline void unlock_lock(FastRLock lock) noexcept nogil:
113 # Note that this function *must* hold the GIL when being called.
114 # We just use 'nogil' in the signature to make sure that no Python
115 # code execution slips in that might free the GIL
116
117 #assert lock._owner == pythread.PyThread_get_thread_ident()
118 #assert lock._count > 0
119 lock._count -= 1
120 if lock._count == 0:
121 if lock._is_locked:
122 pythread.PyThread_release_lock(lock._real_lock)
123 lock._is_locked = False