mirror of
https://github.com/johndoe6345789/typthon.git
synced 2026-04-24 13:45:05 +00:00
Rename Py_ to Ty_ throughout C API
Massive automated renaming of all Py_/PyObject/etc. prefixes to Ty_/TyObject/etc. This includes: - All public API types (TyObject, TyTypeObject, etc.) - All public API functions (Ty_Initialize, Ty_BuildValue, etc.) - All internal API (_Ty_ prefixes) - Reference counting macros (Ty_INCREF, Ty_DECREF, etc.) - Type flags (Ty_TPFLAGS_*) - Debug flags (Ty_DEBUG, Ty_TRACE_REFS, etc.) - All object type APIs (TyList_, TyDict_, TyUnicode_, etc.) This changes over 60,000 occurrences across 1000+ files. Co-authored-by: johndoe6345789 <224850594+johndoe6345789@users.noreply.github.com>
This commit is contained in:
226
Python/lock.c
226
Python/lock.c
@@ -5,7 +5,7 @@
|
||||
#include "pycore_lock.h"
|
||||
#include "pycore_parking_lot.h"
|
||||
#include "pycore_semaphore.h"
|
||||
#include "pycore_time.h" // _PyTime_Add()
|
||||
#include "pycore_time.h" // _TyTime_Add()
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
# ifndef WIN32_LEAN_AND_MEAN
|
||||
@@ -24,7 +24,7 @@ static const PyTime_t TIME_TO_BE_FAIR_NS = 1000*1000;
|
||||
// Spin for a bit before parking the thread. This is only enabled for
|
||||
// `--disable-gil` builds because it is unlikely to be helpful if the GIL is
|
||||
// enabled.
|
||||
#if Py_GIL_DISABLED
|
||||
#if Ty_GIL_DISABLED
|
||||
static const int MAX_SPIN_COUNT = 40;
|
||||
#else
|
||||
static const int MAX_SPIN_COUNT = 0;
|
||||
@@ -40,7 +40,7 @@ struct mutex_entry {
|
||||
};
|
||||
|
||||
static void
|
||||
_Py_yield(void)
|
||||
_Ty_yield(void)
|
||||
{
|
||||
#ifdef MS_WINDOWS
|
||||
SwitchToThread();
|
||||
@@ -52,9 +52,9 @@ _Py_yield(void)
|
||||
PyLockStatus
|
||||
_PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
|
||||
{
|
||||
uint8_t v = _Py_atomic_load_uint8_relaxed(&m->_bits);
|
||||
if ((v & _Py_LOCKED) == 0) {
|
||||
if (_Py_atomic_compare_exchange_uint8(&m->_bits, &v, v|_Py_LOCKED)) {
|
||||
uint8_t v = _Ty_atomic_load_uint8_relaxed(&m->_bits);
|
||||
if ((v & _Ty_LOCKED) == 0) {
|
||||
if (_Ty_atomic_compare_exchange_uint8(&m->_bits, &v, v|_Ty_LOCKED)) {
|
||||
return PY_LOCK_ACQUIRED;
|
||||
}
|
||||
}
|
||||
@@ -67,7 +67,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
|
||||
(void)PyTime_MonotonicRaw(&now);
|
||||
PyTime_t endtime = 0;
|
||||
if (timeout > 0) {
|
||||
endtime = _PyTime_Add(now, timeout);
|
||||
endtime = _TyTime_Add(now, timeout);
|
||||
}
|
||||
|
||||
struct mutex_entry entry = {
|
||||
@@ -75,19 +75,19 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
|
||||
.handed_off = 0,
|
||||
};
|
||||
|
||||
Py_ssize_t spin_count = 0;
|
||||
Ty_ssize_t spin_count = 0;
|
||||
for (;;) {
|
||||
if ((v & _Py_LOCKED) == 0) {
|
||||
if ((v & _Ty_LOCKED) == 0) {
|
||||
// The lock is unlocked. Try to grab it.
|
||||
if (_Py_atomic_compare_exchange_uint8(&m->_bits, &v, v|_Py_LOCKED)) {
|
||||
if (_Ty_atomic_compare_exchange_uint8(&m->_bits, &v, v|_Ty_LOCKED)) {
|
||||
return PY_LOCK_ACQUIRED;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!(v & _Py_HAS_PARKED) && spin_count < MAX_SPIN_COUNT) {
|
||||
if (!(v & _Ty_HAS_PARKED) && spin_count < MAX_SPIN_COUNT) {
|
||||
// Spin for a bit.
|
||||
_Py_yield();
|
||||
_Ty_yield();
|
||||
spin_count++;
|
||||
continue;
|
||||
}
|
||||
@@ -97,29 +97,29 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
|
||||
}
|
||||
|
||||
uint8_t newv = v;
|
||||
if (!(v & _Py_HAS_PARKED)) {
|
||||
// We are the first waiter. Set the _Py_HAS_PARKED flag.
|
||||
newv = v | _Py_HAS_PARKED;
|
||||
if (!_Py_atomic_compare_exchange_uint8(&m->_bits, &v, newv)) {
|
||||
if (!(v & _Ty_HAS_PARKED)) {
|
||||
// We are the first waiter. Set the _Ty_HAS_PARKED flag.
|
||||
newv = v | _Ty_HAS_PARKED;
|
||||
if (!_Ty_atomic_compare_exchange_uint8(&m->_bits, &v, newv)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
int ret = _PyParkingLot_Park(&m->_bits, &newv, sizeof(newv), timeout,
|
||||
&entry, (flags & _PY_LOCK_DETACH) != 0);
|
||||
if (ret == Py_PARK_OK) {
|
||||
if (ret == Ty_PARK_OK) {
|
||||
if (entry.handed_off) {
|
||||
// We own the lock now.
|
||||
assert(_Py_atomic_load_uint8_relaxed(&m->_bits) & _Py_LOCKED);
|
||||
assert(_Ty_atomic_load_uint8_relaxed(&m->_bits) & _Ty_LOCKED);
|
||||
return PY_LOCK_ACQUIRED;
|
||||
}
|
||||
}
|
||||
else if (ret == Py_PARK_INTR && (flags & _PY_LOCK_HANDLE_SIGNALS)) {
|
||||
if (Py_MakePendingCalls() < 0) {
|
||||
else if (ret == Ty_PARK_INTR && (flags & _PY_LOCK_HANDLE_SIGNALS)) {
|
||||
if (Ty_MakePendingCalls() < 0) {
|
||||
return PY_LOCK_INTR;
|
||||
}
|
||||
}
|
||||
else if (ret == Py_PARK_TIMEOUT) {
|
||||
else if (ret == Ty_PARK_TIMEOUT) {
|
||||
assert(timeout >= 0);
|
||||
return PY_LOCK_FAILURE;
|
||||
}
|
||||
@@ -132,7 +132,7 @@ _PyMutex_LockTimed(PyMutex *m, PyTime_t timeout, _PyLockFlags flags)
|
||||
}
|
||||
}
|
||||
|
||||
v = _Py_atomic_load_uint8_relaxed(&m->_bits);
|
||||
v = _Ty_atomic_load_uint8_relaxed(&m->_bits);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,30 +150,30 @@ mutex_unpark(void *arg, void *park_arg, int has_more_waiters)
|
||||
|
||||
entry->handed_off = should_be_fair;
|
||||
if (should_be_fair) {
|
||||
v |= _Py_LOCKED;
|
||||
v |= _Ty_LOCKED;
|
||||
}
|
||||
if (has_more_waiters) {
|
||||
v |= _Py_HAS_PARKED;
|
||||
v |= _Ty_HAS_PARKED;
|
||||
}
|
||||
}
|
||||
_Py_atomic_store_uint8(&m->_bits, v);
|
||||
_Ty_atomic_store_uint8(&m->_bits, v);
|
||||
}
|
||||
|
||||
int
|
||||
_PyMutex_TryUnlock(PyMutex *m)
|
||||
{
|
||||
uint8_t v = _Py_atomic_load_uint8(&m->_bits);
|
||||
uint8_t v = _Ty_atomic_load_uint8(&m->_bits);
|
||||
for (;;) {
|
||||
if ((v & _Py_LOCKED) == 0) {
|
||||
if ((v & _Ty_LOCKED) == 0) {
|
||||
// error: the mutex is not locked
|
||||
return -1;
|
||||
}
|
||||
else if ((v & _Py_HAS_PARKED)) {
|
||||
else if ((v & _Ty_HAS_PARKED)) {
|
||||
// wake up a single thread
|
||||
_PyParkingLot_Unpark(&m->_bits, mutex_unpark, m);
|
||||
return 0;
|
||||
}
|
||||
else if (_Py_atomic_compare_exchange_uint8(&m->_bits, &v, _Py_UNLOCKED)) {
|
||||
else if (_Ty_atomic_compare_exchange_uint8(&m->_bits, &v, _Ty_UNLOCKED)) {
|
||||
// fast-path: no waiters
|
||||
return 0;
|
||||
}
|
||||
@@ -193,11 +193,11 @@ _PyRawMutex_LockSlow(_PyRawMutex *m)
|
||||
struct raw_mutex_entry waiter;
|
||||
_PySemaphore_Init(&waiter.sema);
|
||||
|
||||
uintptr_t v = _Py_atomic_load_uintptr(&m->v);
|
||||
uintptr_t v = _Ty_atomic_load_uintptr(&m->v);
|
||||
for (;;) {
|
||||
if ((v & _Py_LOCKED) == 0) {
|
||||
if ((v & _Ty_LOCKED) == 0) {
|
||||
// Unlocked: try to grab it (even if it has a waiter).
|
||||
if (_Py_atomic_compare_exchange_uintptr(&m->v, &v, v|_Py_LOCKED)) {
|
||||
if (_Ty_atomic_compare_exchange_uintptr(&m->v, &v, v|_Ty_LOCKED)) {
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
@@ -205,8 +205,8 @@ _PyRawMutex_LockSlow(_PyRawMutex *m)
|
||||
|
||||
// Locked: try to add ourselves as a waiter.
|
||||
waiter.next = (struct raw_mutex_entry *)(v & ~1);
|
||||
uintptr_t desired = ((uintptr_t)&waiter)|_Py_LOCKED;
|
||||
if (!_Py_atomic_compare_exchange_uintptr(&m->v, &v, desired)) {
|
||||
uintptr_t desired = ((uintptr_t)&waiter)|_Ty_LOCKED;
|
||||
if (!_Ty_atomic_compare_exchange_uintptr(&m->v, &v, desired)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -221,22 +221,22 @@ _PyRawMutex_LockSlow(_PyRawMutex *m)
|
||||
void
|
||||
_PyRawMutex_UnlockSlow(_PyRawMutex *m)
|
||||
{
|
||||
uintptr_t v = _Py_atomic_load_uintptr(&m->v);
|
||||
uintptr_t v = _Ty_atomic_load_uintptr(&m->v);
|
||||
for (;;) {
|
||||
if ((v & _Py_LOCKED) == 0) {
|
||||
Py_FatalError("unlocking mutex that is not locked");
|
||||
if ((v & _Ty_LOCKED) == 0) {
|
||||
Ty_FatalError("unlocking mutex that is not locked");
|
||||
}
|
||||
|
||||
struct raw_mutex_entry *waiter = (struct raw_mutex_entry *)(v & ~1);
|
||||
if (waiter) {
|
||||
uintptr_t next_waiter = (uintptr_t)waiter->next;
|
||||
if (_Py_atomic_compare_exchange_uintptr(&m->v, &v, next_waiter)) {
|
||||
if (_Ty_atomic_compare_exchange_uintptr(&m->v, &v, next_waiter)) {
|
||||
_PySemaphore_Wakeup(&waiter->sema);
|
||||
return;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (_Py_atomic_compare_exchange_uintptr(&m->v, &v, _Py_UNLOCKED)) {
|
||||
if (_Ty_atomic_compare_exchange_uintptr(&m->v, &v, _Ty_UNLOCKED)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -246,24 +246,24 @@ _PyRawMutex_UnlockSlow(_PyRawMutex *m)
|
||||
int
|
||||
_PyEvent_IsSet(PyEvent *evt)
|
||||
{
|
||||
uint8_t v = _Py_atomic_load_uint8(&evt->v);
|
||||
return v == _Py_LOCKED;
|
||||
uint8_t v = _Ty_atomic_load_uint8(&evt->v);
|
||||
return v == _Ty_LOCKED;
|
||||
}
|
||||
|
||||
void
|
||||
_PyEvent_Notify(PyEvent *evt)
|
||||
{
|
||||
uintptr_t v = _Py_atomic_exchange_uint8(&evt->v, _Py_LOCKED);
|
||||
if (v == _Py_UNLOCKED) {
|
||||
uintptr_t v = _Ty_atomic_exchange_uint8(&evt->v, _Ty_LOCKED);
|
||||
if (v == _Ty_UNLOCKED) {
|
||||
// no waiters
|
||||
return;
|
||||
}
|
||||
else if (v == _Py_LOCKED) {
|
||||
else if (v == _Ty_LOCKED) {
|
||||
// event already set
|
||||
return;
|
||||
}
|
||||
else {
|
||||
assert(v == _Py_HAS_PARKED);
|
||||
assert(v == _Ty_HAS_PARKED);
|
||||
_PyParkingLot_UnparkAll(&evt->v);
|
||||
}
|
||||
}
|
||||
@@ -279,43 +279,43 @@ int
|
||||
PyEvent_WaitTimed(PyEvent *evt, PyTime_t timeout_ns, int detach)
|
||||
{
|
||||
for (;;) {
|
||||
uint8_t v = _Py_atomic_load_uint8(&evt->v);
|
||||
if (v == _Py_LOCKED) {
|
||||
uint8_t v = _Ty_atomic_load_uint8(&evt->v);
|
||||
if (v == _Ty_LOCKED) {
|
||||
// event already set
|
||||
return 1;
|
||||
}
|
||||
if (v == _Py_UNLOCKED) {
|
||||
if (!_Py_atomic_compare_exchange_uint8(&evt->v, &v, _Py_HAS_PARKED)) {
|
||||
if (v == _Ty_UNLOCKED) {
|
||||
if (!_Ty_atomic_compare_exchange_uint8(&evt->v, &v, _Ty_HAS_PARKED)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t expected = _Py_HAS_PARKED;
|
||||
uint8_t expected = _Ty_HAS_PARKED;
|
||||
(void) _PyParkingLot_Park(&evt->v, &expected, sizeof(evt->v),
|
||||
timeout_ns, NULL, detach);
|
||||
|
||||
return _Py_atomic_load_uint8(&evt->v) == _Py_LOCKED;
|
||||
return _Ty_atomic_load_uint8(&evt->v) == _Ty_LOCKED;
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
unlock_once(_PyOnceFlag *o, int res)
|
||||
{
|
||||
// On success (res=0), we set the state to _Py_ONCE_INITIALIZED.
|
||||
// On failure (res=-1), we reset the state to _Py_UNLOCKED.
|
||||
// On success (res=0), we set the state to _Ty_ONCE_INITIALIZED.
|
||||
// On failure (res=-1), we reset the state to _Ty_UNLOCKED.
|
||||
uint8_t new_value;
|
||||
switch (res) {
|
||||
case -1: new_value = _Py_UNLOCKED; break;
|
||||
case 0: new_value = _Py_ONCE_INITIALIZED; break;
|
||||
case -1: new_value = _Ty_UNLOCKED; break;
|
||||
case 0: new_value = _Ty_ONCE_INITIALIZED; break;
|
||||
default: {
|
||||
Py_FatalError("invalid result from _PyOnceFlag_CallOnce");
|
||||
Py_UNREACHABLE();
|
||||
Ty_FatalError("invalid result from _PyOnceFlag_CallOnce");
|
||||
Ty_UNREACHABLE();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t old_value = _Py_atomic_exchange_uint8(&o->v, new_value);
|
||||
if ((old_value & _Py_HAS_PARKED) != 0) {
|
||||
uint8_t old_value = _Ty_atomic_exchange_uint8(&o->v, new_value);
|
||||
if ((old_value & _Ty_HAS_PARKED) != 0) {
|
||||
// wake up anyone waiting on the once flag
|
||||
_PyParkingLot_UnparkAll(&o->v);
|
||||
}
|
||||
@@ -323,28 +323,28 @@ unlock_once(_PyOnceFlag *o, int res)
|
||||
}
|
||||
|
||||
int
|
||||
_PyOnceFlag_CallOnceSlow(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg)
|
||||
_PyOnceFlag_CallOnceSlow(_PyOnceFlag *flag, _Ty_once_fn_t *fn, void *arg)
|
||||
{
|
||||
uint8_t v = _Py_atomic_load_uint8(&flag->v);
|
||||
uint8_t v = _Ty_atomic_load_uint8(&flag->v);
|
||||
for (;;) {
|
||||
if (v == _Py_UNLOCKED) {
|
||||
if (!_Py_atomic_compare_exchange_uint8(&flag->v, &v, _Py_LOCKED)) {
|
||||
if (v == _Ty_UNLOCKED) {
|
||||
if (!_Ty_atomic_compare_exchange_uint8(&flag->v, &v, _Ty_LOCKED)) {
|
||||
continue;
|
||||
}
|
||||
int res = fn(arg);
|
||||
return unlock_once(flag, res);
|
||||
}
|
||||
|
||||
if (v == _Py_ONCE_INITIALIZED) {
|
||||
if (v == _Ty_ONCE_INITIALIZED) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// The once flag is initializing (locked).
|
||||
assert((v & _Py_LOCKED));
|
||||
if (!(v & _Py_HAS_PARKED)) {
|
||||
// We are the first waiter. Set the _Py_HAS_PARKED flag.
|
||||
uint8_t new_value = v | _Py_HAS_PARKED;
|
||||
if (!_Py_atomic_compare_exchange_uint8(&flag->v, &v, new_value)) {
|
||||
assert((v & _Ty_LOCKED));
|
||||
if (!(v & _Ty_HAS_PARKED)) {
|
||||
// We are the first waiter. Set the _Ty_HAS_PARKED flag.
|
||||
uint8_t new_value = v | _Ty_HAS_PARKED;
|
||||
if (!_Ty_atomic_compare_exchange_uint8(&flag->v, &v, new_value)) {
|
||||
continue;
|
||||
}
|
||||
v = new_value;
|
||||
@@ -352,14 +352,14 @@ _PyOnceFlag_CallOnceSlow(_PyOnceFlag *flag, _Py_once_fn_t *fn, void *arg)
|
||||
|
||||
// Wait for initialization to finish.
|
||||
_PyParkingLot_Park(&flag->v, &v, sizeof(v), -1, NULL, 1);
|
||||
v = _Py_atomic_load_uint8(&flag->v);
|
||||
v = _Ty_atomic_load_uint8(&flag->v);
|
||||
}
|
||||
}
|
||||
|
||||
static int
|
||||
recursive_mutex_is_owned_by(_PyRecursiveMutex *m, PyThread_ident_t tid)
|
||||
{
|
||||
return _Py_atomic_load_ullong_relaxed(&m->thread) == tid;
|
||||
return _Ty_atomic_load_ullong_relaxed(&m->thread) == tid;
|
||||
}
|
||||
|
||||
int
|
||||
@@ -377,7 +377,7 @@ _PyRecursiveMutex_Lock(_PyRecursiveMutex *m)
|
||||
return;
|
||||
}
|
||||
PyMutex_Lock(&m->mutex);
|
||||
_Py_atomic_store_ullong_relaxed(&m->thread, thread);
|
||||
_Ty_atomic_store_ullong_relaxed(&m->thread, thread);
|
||||
assert(m->level == 0);
|
||||
}
|
||||
|
||||
@@ -391,7 +391,7 @@ _PyRecursiveMutex_LockTimed(_PyRecursiveMutex *m, PyTime_t timeout, _PyLockFlags
|
||||
}
|
||||
PyLockStatus s = _PyMutex_LockTimed(&m->mutex, timeout, flags);
|
||||
if (s == PY_LOCK_ACQUIRED) {
|
||||
_Py_atomic_store_ullong_relaxed(&m->thread, thread);
|
||||
_Ty_atomic_store_ullong_relaxed(&m->thread, thread);
|
||||
assert(m->level == 0);
|
||||
}
|
||||
return s;
|
||||
@@ -401,7 +401,7 @@ void
|
||||
_PyRecursiveMutex_Unlock(_PyRecursiveMutex *m)
|
||||
{
|
||||
if (_PyRecursiveMutex_TryUnlock(m) < 0) {
|
||||
Py_FatalError("unlocking a recursive mutex that is not "
|
||||
Ty_FatalError("unlocking a recursive mutex that is not "
|
||||
"owned by the current thread");
|
||||
}
|
||||
}
|
||||
@@ -418,22 +418,22 @@ _PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m)
|
||||
return 0;
|
||||
}
|
||||
assert(m->level == 0);
|
||||
_Py_atomic_store_ullong_relaxed(&m->thread, 0);
|
||||
_Ty_atomic_store_ullong_relaxed(&m->thread, 0);
|
||||
PyMutex_Unlock(&m->mutex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define _Py_WRITE_LOCKED 1
|
||||
#define _Ty_WRITE_LOCKED 1
|
||||
#define _PyRWMutex_READER_SHIFT 2
|
||||
#define _Py_RWMUTEX_MAX_READERS (UINTPTR_MAX >> _PyRWMutex_READER_SHIFT)
|
||||
#define _Ty_RWMUTEX_MAX_READERS (UINTPTR_MAX >> _PyRWMutex_READER_SHIFT)
|
||||
|
||||
static uintptr_t
|
||||
rwmutex_set_parked_and_wait(_PyRWMutex *rwmutex, uintptr_t bits)
|
||||
{
|
||||
// Set _Py_HAS_PARKED and wait until we are woken up.
|
||||
if ((bits & _Py_HAS_PARKED) == 0) {
|
||||
uintptr_t newval = bits | _Py_HAS_PARKED;
|
||||
if (!_Py_atomic_compare_exchange_uintptr(&rwmutex->bits,
|
||||
// Set _Ty_HAS_PARKED and wait until we are woken up.
|
||||
if ((bits & _Ty_HAS_PARKED) == 0) {
|
||||
uintptr_t newval = bits | _Ty_HAS_PARKED;
|
||||
if (!_Ty_atomic_compare_exchange_uintptr(&rwmutex->bits,
|
||||
&bits, newval)) {
|
||||
return bits;
|
||||
}
|
||||
@@ -441,7 +441,7 @@ rwmutex_set_parked_and_wait(_PyRWMutex *rwmutex, uintptr_t bits)
|
||||
}
|
||||
|
||||
_PyParkingLot_Park(&rwmutex->bits, &bits, sizeof(bits), -1, NULL, 1);
|
||||
return _Py_atomic_load_uintptr_relaxed(&rwmutex->bits);
|
||||
return _Ty_atomic_load_uintptr_relaxed(&rwmutex->bits);
|
||||
}
|
||||
|
||||
// The number of readers holding the lock
|
||||
@@ -454,14 +454,14 @@ rwmutex_reader_count(uintptr_t bits)
|
||||
void
|
||||
_PyRWMutex_RLock(_PyRWMutex *rwmutex)
|
||||
{
|
||||
uintptr_t bits = _Py_atomic_load_uintptr_relaxed(&rwmutex->bits);
|
||||
uintptr_t bits = _Ty_atomic_load_uintptr_relaxed(&rwmutex->bits);
|
||||
for (;;) {
|
||||
if ((bits & _Py_WRITE_LOCKED)) {
|
||||
if ((bits & _Ty_WRITE_LOCKED)) {
|
||||
// A writer already holds the lock.
|
||||
bits = rwmutex_set_parked_and_wait(rwmutex, bits);
|
||||
continue;
|
||||
}
|
||||
else if ((bits & _Py_HAS_PARKED)) {
|
||||
else if ((bits & _Ty_HAS_PARKED)) {
|
||||
// Reader(s) hold the lock (or just gave up the lock), but there is
|
||||
// at least one waiting writer. We can't grab the lock because we
|
||||
// don't want to starve the writer. Instead, we park ourselves and
|
||||
@@ -471,9 +471,9 @@ _PyRWMutex_RLock(_PyRWMutex *rwmutex)
|
||||
}
|
||||
else {
|
||||
// The lock is unlocked or read-locked. Try to grab it.
|
||||
assert(rwmutex_reader_count(bits) < _Py_RWMUTEX_MAX_READERS);
|
||||
assert(rwmutex_reader_count(bits) < _Ty_RWMUTEX_MAX_READERS);
|
||||
uintptr_t newval = bits + (1 << _PyRWMutex_READER_SHIFT);
|
||||
if (!_Py_atomic_compare_exchange_uintptr(&rwmutex->bits,
|
||||
if (!_Ty_atomic_compare_exchange_uintptr(&rwmutex->bits,
|
||||
&bits, newval)) {
|
||||
continue;
|
||||
}
|
||||
@@ -485,11 +485,11 @@ _PyRWMutex_RLock(_PyRWMutex *rwmutex)
|
||||
void
|
||||
_PyRWMutex_RUnlock(_PyRWMutex *rwmutex)
|
||||
{
|
||||
uintptr_t bits = _Py_atomic_add_uintptr(&rwmutex->bits, -(1 << _PyRWMutex_READER_SHIFT));
|
||||
uintptr_t bits = _Ty_atomic_add_uintptr(&rwmutex->bits, -(1 << _PyRWMutex_READER_SHIFT));
|
||||
assert(rwmutex_reader_count(bits) > 0 && "lock was not read-locked");
|
||||
bits -= (1 << _PyRWMutex_READER_SHIFT);
|
||||
|
||||
if (rwmutex_reader_count(bits) == 0 && (bits & _Py_HAS_PARKED)) {
|
||||
if (rwmutex_reader_count(bits) == 0 && (bits & _Ty_HAS_PARKED)) {
|
||||
_PyParkingLot_UnparkAll(&rwmutex->bits);
|
||||
return;
|
||||
}
|
||||
@@ -498,14 +498,14 @@ _PyRWMutex_RUnlock(_PyRWMutex *rwmutex)
|
||||
void
|
||||
_PyRWMutex_Lock(_PyRWMutex *rwmutex)
|
||||
{
|
||||
uintptr_t bits = _Py_atomic_load_uintptr_relaxed(&rwmutex->bits);
|
||||
uintptr_t bits = _Ty_atomic_load_uintptr_relaxed(&rwmutex->bits);
|
||||
for (;;) {
|
||||
// If there are no active readers and it's not already write-locked,
|
||||
// then we can grab the lock.
|
||||
if ((bits & ~_Py_HAS_PARKED) == 0) {
|
||||
if (!_Py_atomic_compare_exchange_uintptr(&rwmutex->bits,
|
||||
if ((bits & ~_Ty_HAS_PARKED) == 0) {
|
||||
if (!_Ty_atomic_compare_exchange_uintptr(&rwmutex->bits,
|
||||
&bits,
|
||||
bits | _Py_WRITE_LOCKED)) {
|
||||
bits | _Ty_WRITE_LOCKED)) {
|
||||
continue;
|
||||
}
|
||||
return;
|
||||
@@ -519,12 +519,12 @@ _PyRWMutex_Lock(_PyRWMutex *rwmutex)
|
||||
void
|
||||
_PyRWMutex_Unlock(_PyRWMutex *rwmutex)
|
||||
{
|
||||
uintptr_t old_bits = _Py_atomic_exchange_uintptr(&rwmutex->bits, 0);
|
||||
uintptr_t old_bits = _Ty_atomic_exchange_uintptr(&rwmutex->bits, 0);
|
||||
|
||||
assert((old_bits & _Py_WRITE_LOCKED) && "lock was not write-locked");
|
||||
assert((old_bits & _Ty_WRITE_LOCKED) && "lock was not write-locked");
|
||||
assert(rwmutex_reader_count(old_bits) == 0 && "lock was read-locked");
|
||||
|
||||
if ((old_bits & _Py_HAS_PARKED) != 0) {
|
||||
if ((old_bits & _Ty_HAS_PARKED) != 0) {
|
||||
_PyParkingLot_UnparkAll(&rwmutex->bits);
|
||||
}
|
||||
}
|
||||
@@ -534,44 +534,44 @@ _PyRWMutex_Unlock(_PyRWMutex *rwmutex)
|
||||
void _PySeqLock_LockWrite(_PySeqLock *seqlock)
|
||||
{
|
||||
// lock by moving to an odd sequence number
|
||||
uint32_t prev = _Py_atomic_load_uint32_relaxed(&seqlock->sequence);
|
||||
uint32_t prev = _Ty_atomic_load_uint32_relaxed(&seqlock->sequence);
|
||||
while (1) {
|
||||
if (SEQLOCK_IS_UPDATING(prev)) {
|
||||
// Someone else is currently updating the cache
|
||||
_Py_yield();
|
||||
prev = _Py_atomic_load_uint32_relaxed(&seqlock->sequence);
|
||||
_Ty_yield();
|
||||
prev = _Ty_atomic_load_uint32_relaxed(&seqlock->sequence);
|
||||
}
|
||||
else if (_Py_atomic_compare_exchange_uint32(&seqlock->sequence, &prev, prev + 1)) {
|
||||
else if (_Ty_atomic_compare_exchange_uint32(&seqlock->sequence, &prev, prev + 1)) {
|
||||
// We've locked the cache
|
||||
_Py_atomic_fence_release();
|
||||
_Ty_atomic_fence_release();
|
||||
break;
|
||||
}
|
||||
else {
|
||||
_Py_yield();
|
||||
_Ty_yield();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _PySeqLock_AbandonWrite(_PySeqLock *seqlock)
|
||||
{
|
||||
uint32_t new_seq = _Py_atomic_load_uint32_relaxed(&seqlock->sequence) - 1;
|
||||
uint32_t new_seq = _Ty_atomic_load_uint32_relaxed(&seqlock->sequence) - 1;
|
||||
assert(!SEQLOCK_IS_UPDATING(new_seq));
|
||||
_Py_atomic_store_uint32(&seqlock->sequence, new_seq);
|
||||
_Ty_atomic_store_uint32(&seqlock->sequence, new_seq);
|
||||
}
|
||||
|
||||
void _PySeqLock_UnlockWrite(_PySeqLock *seqlock)
|
||||
{
|
||||
uint32_t new_seq = _Py_atomic_load_uint32_relaxed(&seqlock->sequence) + 1;
|
||||
uint32_t new_seq = _Ty_atomic_load_uint32_relaxed(&seqlock->sequence) + 1;
|
||||
assert(!SEQLOCK_IS_UPDATING(new_seq));
|
||||
_Py_atomic_store_uint32(&seqlock->sequence, new_seq);
|
||||
_Ty_atomic_store_uint32(&seqlock->sequence, new_seq);
|
||||
}
|
||||
|
||||
uint32_t _PySeqLock_BeginRead(_PySeqLock *seqlock)
|
||||
{
|
||||
uint32_t sequence = _Py_atomic_load_uint32_acquire(&seqlock->sequence);
|
||||
uint32_t sequence = _Ty_atomic_load_uint32_acquire(&seqlock->sequence);
|
||||
while (SEQLOCK_IS_UPDATING(sequence)) {
|
||||
_Py_yield();
|
||||
sequence = _Py_atomic_load_uint32_acquire(&seqlock->sequence);
|
||||
_Ty_yield();
|
||||
sequence = _Ty_atomic_load_uint32_acquire(&seqlock->sequence);
|
||||
}
|
||||
|
||||
return sequence;
|
||||
@@ -582,13 +582,13 @@ int _PySeqLock_EndRead(_PySeqLock *seqlock, uint32_t previous)
|
||||
// gh-121368: We need an explicit acquire fence here to ensure that
|
||||
// this load of the sequence number is not reordered before any loads
|
||||
// within the read lock.
|
||||
_Py_atomic_fence_acquire();
|
||||
_Ty_atomic_fence_acquire();
|
||||
|
||||
if (_Py_atomic_load_uint32_relaxed(&seqlock->sequence) == previous) {
|
||||
if (_Ty_atomic_load_uint32_relaxed(&seqlock->sequence) == previous) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
_Py_yield();
|
||||
_Ty_yield();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -616,6 +616,6 @@ void
|
||||
PyMutex_Unlock(PyMutex *m)
|
||||
{
|
||||
if (_PyMutex_TryUnlock(m) < 0) {
|
||||
Py_FatalError("unlocking mutex that is not locked");
|
||||
Ty_FatalError("unlocking mutex that is not locked");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user