@@ -20,7 +20,7 @@ cdef class FastRLock:
2020 def __cinit__ (self ):
2121 self ._real_lock = _LockStatus(
2222 lock = pythread.PyThread_allocate_lock(),
23- owner = - 1 , is_locked = False , pending_requests = 0 , entry_count = 0 )
23+ owner = 0 , is_locked = False , pending_requests = 0 , entry_count = 0 )
2424 if not self ._real_lock.lock:
2525 raise MemoryError ()
2626
@@ -33,31 +33,30 @@ cdef class FastRLock:
3333
3434 def acquire (self , bint blocking = True ):
3535 return _lock_rlock(
36- & self ._real_lock, < long > pythread.PyThread_get_thread_ident(), blocking)
36+ & self ._real_lock, pythread.PyThread_get_thread_ident(), blocking)
3737
3838 def release (self ):
39- if self ._real_lock.owner == - 1 :
39+ if self ._real_lock.entry_count == 0 :
4040 raise RuntimeError (" cannot release un-acquired lock" )
4141 _unlock_lock(& self ._real_lock)
4242
4343 def __enter__ (self ):
4444 # self.acquire()
4545 if not _lock_rlock(
46- & self ._real_lock, < long > pythread.PyThread_get_thread_ident(),
47- blocking = True ):
46+ & self ._real_lock, pythread.PyThread_get_thread_ident(), blocking = True ):
4847 raise LockNotAcquired()
4948
5049 def __exit__ (self , t , v , tb ):
5150 # self.release()
52- if self ._real_lock.owner != < long > pythread.PyThread_get_thread_ident():
51+ if self ._real_lock.entry_count == 0 or self ._real_lock. owner != pythread.PyThread_get_thread_ident():
5352 raise RuntimeError (" cannot release un-acquired lock" )
5453 _unlock_lock(& self ._real_lock)
5554
5655 def _is_owned (self ):
57- return self ._real_lock.owner == < long > pythread.PyThread_get_thread_ident()
56+ return self ._real_lock.entry_count > 0 and self ._real_lock. owner == pythread.PyThread_get_thread_ident()
5857
5958
60- cdef inline bint _lock_rlock(_LockStatus * lock, long current_thread,
59+ cdef inline bint _lock_rlock(_LockStatus * lock, pythread_t current_thread,
6160 bint blocking) nogil:
6261 # Note that this function *must* hold the GIL when being called.
6362 # We just use 'nogil' in the signature to make sure that no Python
@@ -90,10 +89,11 @@ cdef create_fastrlock():
9089cdef bint lock_fastrlock(rlock, long current_thread, bint blocking) except - 1 :
9190 """
9291 Public C level entry function for locking a FastRlock instance.
92+
93+ The 'current_thread' argument is deprecated and ignored. Pass -1 for backwards compatibility.
9394 """
94- if current_thread == - 1 :
95- current_thread = < long > pythread.PyThread_get_thread_ident()
96- return _lock_rlock(& (< FastRLock?> rlock)._real_lock, current_thread, blocking)
95+ # Note: 'current_thread' used to be set to -1 or the current thread ID, but -1 is signed while "pythread_t" isn't.
96+ return _lock_rlock(& (< FastRLock?> rlock)._real_lock, pythread.PyThread_get_thread_ident(), blocking)
9797
9898
9999cdef int unlock_fastrlock(rlock) except - 1 :
0 commit comments