mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
gh-134745: Change PyThread_allocate_lock() implementation to PyMutex (#134747)
Co-authored-by: Sam Gross <colesbury@gmail.com>
This commit is contained in:
parent
45c6c48afc
commit
ebf6d13567
7 changed files with 86 additions and 491 deletions
|
|
@ -300,98 +300,6 @@ PyThread_hang_thread(void)
|
|||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Lock support. It has to be implemented as semaphores.
|
||||
* I [Dag] tried to implement it with mutex but I could find a way to
|
||||
* tell whether a thread already own the lock or not.
|
||||
*/
|
||||
PyThread_type_lock
|
||||
PyThread_allocate_lock(void)
|
||||
{
|
||||
PNRMUTEX mutex;
|
||||
|
||||
if (!initialized)
|
||||
PyThread_init_thread();
|
||||
|
||||
mutex = AllocNonRecursiveMutex() ;
|
||||
|
||||
PyThread_type_lock aLock = (PyThread_type_lock) mutex;
|
||||
assert(aLock);
|
||||
|
||||
return aLock;
|
||||
}
|
||||
|
||||
void
|
||||
PyThread_free_lock(PyThread_type_lock aLock)
|
||||
{
|
||||
FreeNonRecursiveMutex(aLock) ;
|
||||
}
|
||||
|
||||
// WaitForSingleObject() accepts timeout in milliseconds in the range
|
||||
// [0; 0xFFFFFFFE] (DWORD type). INFINITE value (0xFFFFFFFF) means no
|
||||
// timeout. 0xFFFFFFFE milliseconds is around 49.7 days.
|
||||
const DWORD TIMEOUT_MS_MAX = 0xFFFFFFFE;
|
||||
|
||||
/*
|
||||
* Return 1 on success if the lock was acquired
|
||||
*
|
||||
* and 0 if the lock was not acquired. This means a 0 is returned
|
||||
* if the lock has already been acquired by this thread!
|
||||
*/
|
||||
PyLockStatus
|
||||
PyThread_acquire_lock_timed(PyThread_type_lock aLock,
|
||||
PY_TIMEOUT_T microseconds, int intr_flag)
|
||||
{
|
||||
assert(aLock);
|
||||
|
||||
/* Fow now, intr_flag does nothing on Windows, and lock acquires are
|
||||
* uninterruptible. */
|
||||
PyLockStatus success;
|
||||
PY_TIMEOUT_T milliseconds;
|
||||
|
||||
if (microseconds >= 0) {
|
||||
milliseconds = microseconds / 1000;
|
||||
// Round milliseconds away from zero
|
||||
if (microseconds % 1000 > 0) {
|
||||
milliseconds++;
|
||||
}
|
||||
if (milliseconds > (PY_TIMEOUT_T)TIMEOUT_MS_MAX) {
|
||||
// bpo-41710: PyThread_acquire_lock_timed() cannot report timeout
|
||||
// overflow to the caller, so clamp the timeout to
|
||||
// [0, TIMEOUT_MS_MAX] milliseconds.
|
||||
//
|
||||
// _thread.Lock.acquire() and _thread.RLock.acquire() raise an
|
||||
// OverflowError if microseconds is greater than PY_TIMEOUT_MAX.
|
||||
milliseconds = TIMEOUT_MS_MAX;
|
||||
}
|
||||
assert(milliseconds != INFINITE);
|
||||
}
|
||||
else {
|
||||
milliseconds = INFINITE;
|
||||
}
|
||||
|
||||
if (EnterNonRecursiveMutex((PNRMUTEX)aLock,
|
||||
(DWORD)milliseconds) == WAIT_OBJECT_0) {
|
||||
success = PY_LOCK_ACQUIRED;
|
||||
}
|
||||
else {
|
||||
success = PY_LOCK_FAILURE;
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
int
|
||||
PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
|
||||
{
|
||||
return PyThread_acquire_lock_timed(aLock, waitflag ? -1 : 0, 0);
|
||||
}
|
||||
|
||||
void
|
||||
PyThread_release_lock(PyThread_type_lock aLock)
|
||||
{
|
||||
assert(aLock);
|
||||
(void)LeaveNonRecursiveMutex((PNRMUTEX) aLock);
|
||||
}
|
||||
|
||||
/* minimum/maximum thread stack sizes supported */
|
||||
#define THREAD_MIN_STACKSIZE 0x8000 /* 32 KiB */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue