Skip to content

Commit 4371c15

Browse files
committed
Update to use new "PyInterpreterGuard" names.
1 parent 51413fc commit 4371c15

File tree

5 files changed

+119
-117
lines changed

5 files changed

+119
-117
lines changed

Include/cpython/pystate.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -284,20 +284,22 @@ PyAPI_FUNC(void) _PyInterpreterState_SetEvalFrameFunc(
284284

285285
/* Interpreter locks */
286286

287-
typedef uintptr_t PyInterpreterLock;
287+
typedef uintptr_t PyInterpreterGuard;
288288
typedef uintptr_t PyInterpreterView;
289289

290290

291-
PyAPI_FUNC(PyInterpreterLock) PyInterpreterLock_FromCurrent(void);
292-
PyAPI_FUNC(PyInterpreterLock) PyInterpreterLock_Copy(PyInterpreterLock lock);
293-
PyAPI_FUNC(void) PyInterpreterLock_Release(PyInterpreterLock lock);
294-
PyAPI_FUNC(PyInterpreterState *) PyInterpreterLock_GetInterpreter(PyInterpreterLock lock);
295-
PyAPI_FUNC(PyInterpreterLock) PyInterpreterLock_FromView(PyInterpreterView view);
291+
PyAPI_FUNC(PyInterpreterGuard) PyInterpreterGuard_FromCurrent(void);
292+
PyAPI_FUNC(PyInterpreterGuard) PyInterpreterGuard_Copy(PyInterpreterGuard guard);
293+
PyAPI_FUNC(void) PyInterpreterGuard_Release(PyInterpreterGuard guard);
294+
PyAPI_FUNC(PyInterpreterState *) PyInterpreterGuard_GetInterpreter(PyInterpreterGuard guard);
295+
PyAPI_FUNC(PyInterpreterGuard) PyInterpreterGuard_FromView(PyInterpreterView view);
296296

297-
#define PyInterpreterLock_Release(lock) do { \
298-
PyInterpreterLock_Release(lock); \
299-
lock = 0; \
297+
#ifdef Py_DEBUG
298+
#define PyInterpreterGuard_Release(guard) do { \
299+
PyInterpreterGuard_Release(guard); \
300+
guard = 0; \
300301
} while (0)
302+
#endif
301303

302304
/* Interpreter views */
303305

@@ -312,16 +314,17 @@ PyAPI_FUNC(void) PyInterpreterView_Close(PyInterpreterView view);
312314
PyAPI_FUNC(PyInterpreterView) PyUnstable_InterpreterView_FromDefault(void);
313315

314316

317+
#ifdef Py_DEBUG
315318
#define PyInterpreterView_Close(view) do { \
316319
PyInterpreterView_Close(view); \
317320
view = 0; \
318321
} while (0)
319-
322+
#endif
320323

321324
/* Thread views */
322325

323326
typedef uintptr_t PyThreadView;
324327

325-
PyAPI_FUNC(PyThreadView) PyThreadState_Ensure(PyInterpreterLock lock);
328+
PyAPI_FUNC(PyThreadView) PyThreadState_Ensure(PyInterpreterGuard guard);
326329

327330
PyAPI_FUNC(void) PyThreadState_Release(PyThreadView thread_ref);

Modules/_testcapimodule.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2563,32 +2563,32 @@ toggle_reftrace_printer(PyObject *ob, PyObject *arg)
25632563
}
25642564

25652565
static void
2566-
test_interp_locks_common(void)
2566+
test_interp_guards_common(void)
25672567
{
25682568
PyInterpreterState *interp = PyInterpreterState_Get();
2569-
PyInterpreterLock lock = PyInterpreterLock_FromCurrent();
2570-
assert(lock != 0);
2571-
assert(PyInterpreterLock_GetInterpreter(lock) == interp);
2569+
PyInterpreterGuard guard = PyInterpreterGuard_FromCurrent();
2570+
assert(guard != 0);
2571+
assert(PyInterpreterGuard_GetInterpreter(guard) == interp);
25722572

2573-
PyInterpreterLock lock_2 = PyInterpreterLock_Copy(lock);
2574-
assert(lock_2 != 0);
2575-
assert(PyInterpreterLock_GetInterpreter(lock_2) == interp);
2573+
PyInterpreterGuard guard_2 = PyInterpreterGuard_Copy(guard);
2574+
assert(guard_2 != 0);
2575+
assert(PyInterpreterGuard_GetInterpreter(guard_2) == interp);
25762576

25772577
// We can close the references in any order
2578-
PyInterpreterLock_Release(lock_2);
2579-
PyInterpreterLock_Release(lock);
2578+
PyInterpreterGuard_Release(guard_2);
2579+
PyInterpreterGuard_Release(guard);
25802580
}
25812581

25822582
static PyObject *
25832583
test_interpreter_locks(PyObject *self, PyObject *unused)
25842584
{
25852585
// Test the main interpreter
2586-
test_interp_locks_common();
2586+
test_interp_guards_common();
25872587

25882588
// Test a (legacy) subinterpreter
25892589
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
25902590
PyThreadState *interp_tstate = Py_NewInterpreter();
2591-
test_interp_locks_common();
2591+
test_interp_guards_common();
25922592
Py_EndInterpreter(interp_tstate);
25932593

25942594
// Test an isolated subinterpreter
@@ -2604,7 +2604,7 @@ test_interpreter_locks(PyObject *self, PyObject *unused)
26042604
return NULL;
26052605
}
26062606

2607-
test_interp_locks_common();
2607+
test_interp_guards_common();
26082608
Py_EndInterpreter(isolated_interp_tstate);
26092609
PyThreadState_Swap(save_tstate);
26102610
Py_RETURN_NONE;
@@ -2613,8 +2613,8 @@ test_interpreter_locks(PyObject *self, PyObject *unused)
26132613
static PyObject *
26142614
test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26152615
{
2616-
PyInterpreterLock lock = PyInterpreterLock_FromCurrent();
2617-
if (lock == 0) {
2616+
PyInterpreterGuard guard = PyInterpreterGuard_FromCurrent();
2617+
if (guard == 0) {
26182618
return NULL;
26192619
}
26202620
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
@@ -2623,9 +2623,9 @@ test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26232623

26242624
for (int i = 0; i < 10; ++i) {
26252625
// Test reactivation of the detached tstate.
2626-
thread_views[i] = PyThreadState_Ensure(lock);
2626+
thread_views[i] = PyThreadState_Ensure(guard);
26272627
if (thread_views[i] == 0) {
2628-
PyInterpreterLock_Release(lock);
2628+
PyInterpreterGuard_Release(guard);
26292629
return PyErr_NoMemory();
26302630
}
26312631

@@ -2640,11 +2640,11 @@ test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26402640
// If the (detached) gilstate matches the interpreter, then it shouldn't
26412641
// create a new thread state.
26422642
for (int i = 0; i < 10; ++i) {
2643-
thread_views[i] = PyThreadState_Ensure(lock);
2643+
thread_views[i] = PyThreadState_Ensure(guard);
26442644
if (thread_views[i] == 0) {
26452645
// This will technically leak other thread states, but it doesn't
26462646
// matter because this is a test.
2647-
PyInterpreterLock_Release(lock);
2647+
PyInterpreterGuard_Release(guard);
26482648
return PyErr_NoMemory();
26492649
}
26502650

@@ -2657,19 +2657,19 @@ test_thread_state_ensure_nested(PyObject *self, PyObject *unused)
26572657
}
26582658

26592659
assert(PyThreadState_GetUnchecked() == NULL);
2660-
PyInterpreterLock_Release(lock);
2660+
PyInterpreterGuard_Release(guard);
26612661
PyThreadState_Swap(save_tstate);
26622662
Py_RETURN_NONE;
26632663
}
26642664

26652665
static PyObject *
26662666
test_thread_state_ensure_crossinterp(PyObject *self, PyObject *unused)
26672667
{
2668-
PyInterpreterLock lock = PyInterpreterLock_FromCurrent();
2668+
PyInterpreterGuard guard = PyInterpreterGuard_FromCurrent();
26692669
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
26702670
PyThreadState *interp_tstate = Py_NewInterpreter();
26712671
if (interp_tstate == NULL) {
2672-
PyInterpreterLock_Release(lock);
2672+
PyInterpreterGuard_Release(guard);
26732673
return PyErr_NoMemory();
26742674
}
26752675

@@ -2686,22 +2686,22 @@ test_thread_state_ensure_crossinterp(PyObject *self, PyObject *unused)
26862686
interp = interpreters.create()
26872687
interp.exec(some_func)
26882688
*/
2689-
PyThreadView thread_view = PyThreadState_Ensure(lock);
2689+
PyThreadView thread_view = PyThreadState_Ensure(guard);
26902690
if (thread_view == 0) {
2691-
PyInterpreterLock_Release(lock);
2691+
PyInterpreterGuard_Release(guard);
26922692
return PyErr_NoMemory();
26932693
}
26942694

26952695
PyThreadState *ensured_tstate = PyThreadState_Get();
26962696
assert(ensured_tstate != save_tstate);
2697-
assert(PyInterpreterState_Get() == PyInterpreterLock_GetInterpreter(lock));
2697+
assert(PyInterpreterState_Get() == PyInterpreterGuard_GetInterpreter(guard));
26982698
assert(PyGILState_GetThisThreadState() == ensured_tstate);
26992699

27002700
// Now though, we should reactivate the thread state
2701-
PyThreadView other_thread_view = PyThreadState_Ensure(lock);
2701+
PyThreadView other_thread_view = PyThreadState_Ensure(guard);
27022702
if (other_thread_view == 0) {
27032703
PyThreadState_Release(thread_view);
2704-
PyInterpreterLock_Release(lock);
2704+
PyInterpreterGuard_Release(guard);
27052705
return PyErr_NoMemory();
27062706
}
27072707

@@ -2716,7 +2716,7 @@ test_thread_state_ensure_crossinterp(PyObject *self, PyObject *unused)
27162716
PyThreadState_Swap(interp_tstate);
27172717
Py_EndInterpreter(interp_tstate);
27182718

2719-
PyInterpreterLock_Release(lock);
2719+
PyInterpreterGuard_Release(guard);
27202720
PyThreadState_Swap(save_tstate);
27212721
Py_RETURN_NONE;
27222722
}
@@ -2736,14 +2736,14 @@ test_interp_view_after_shutdown(PyObject *self, PyObject *unused)
27362736
}
27372737

27382738
// As a sanity check, ensure that the view actually works
2739-
PyInterpreterLock lock = PyInterpreterLock_FromView(view);
2740-
PyInterpreterLock_Release(lock);
2739+
PyInterpreterGuard guard = PyInterpreterGuard_FromView(view);
2740+
PyInterpreterGuard_Release(guard);
27412741

27422742
// Now, destroy the interpreter and try to acquire a lock from a view.
27432743
// It should fail.
27442744
Py_EndInterpreter(interp_tstate);
2745-
lock = PyInterpreterLock_FromView(view);
2746-
assert(lock == 0);
2745+
guard = PyInterpreterGuard_FromView(view);
2746+
assert(guard == 0);
27472747

27482748
PyThreadState_Swap(save_tstate);
27492749
Py_RETURN_NONE;

Modules/_testinternalcapi.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,23 +2418,23 @@ set_vectorcall_nop(PyObject *self, PyObject *func)
24182418
Py_RETURN_NONE;
24192419
}
24202420

2421-
#define NUM_LOCKS 100
2421+
#define NUM_GUARDS 100
24222422

24232423
static PyObject *
2424-
test_interp_lock_countdown(PyObject *self, PyObject *unused)
2424+
test_interp_guard_countdown(PyObject *self, PyObject *unused)
24252425
{
24262426
PyInterpreterState *interp = PyInterpreterState_Get();
24272427
assert(_PyInterpreterState_LockCountdown(interp) == 0);
2428-
PyInterpreterLock locks[NUM_LOCKS];
2429-
for (int i = 0; i < NUM_LOCKS; ++i) {
2430-
locks[i] = PyInterpreterLock_FromCurrent();
2431-
assert(locks[i] != 0);
2428+
PyInterpreterGuard guards[NUM_GUARDS];
2429+
for (int i = 0; i < NUM_GUARDS; ++i) {
2430+
guards[i] = PyInterpreterGuard_FromCurrent();
2431+
assert(guards[i] != 0);
24322432
assert(_PyInterpreterState_LockCountdown(interp) == i + 1);
24332433
}
24342434

2435-
for (int i = 0; i < NUM_LOCKS; ++i) {
2436-
PyInterpreterLock_Release(locks[i]);
2437-
assert(_PyInterpreterState_LockCountdown(interp) == (NUM_LOCKS - i - 1));
2435+
for (int i = 0; i < NUM_GUARDS; ++i) {
2436+
PyInterpreterGuard_Release(guards[i]);
2437+
assert(_PyInterpreterState_LockCountdown(interp) == (NUM_GUARDS - i - 1));
24382438
}
24392439

24402440
Py_RETURN_NONE;
@@ -2450,18 +2450,18 @@ test_interp_view_countdown(PyObject *self, PyObject *unused)
24502450
}
24512451
assert(_PyInterpreterState_LockCountdown(interp) == 0);
24522452

2453-
PyInterpreterLock locks[NUM_LOCKS];
2453+
PyInterpreterGuard guards[NUM_GUARDS];
24542454

2455-
for (int i = 0; i < NUM_LOCKS; ++i) {
2456-
locks[i] = PyInterpreterLock_FromView(view);
2457-
assert(locks[i] != 0);
2458-
assert(PyInterpreterLock_GetInterpreter(locks[i]) == interp);
2455+
for (int i = 0; i < NUM_GUARDS; ++i) {
2456+
guards[i] = PyInterpreterGuard_FromView(view);
2457+
assert(guards[i] != 0);
2458+
assert(PyInterpreterGuard_GetInterpreter(guards[i]) == interp);
24592459
assert(_PyInterpreterState_LockCountdown(interp) == i + 1);
24602460
}
24612461

2462-
for (int i = 0; i < NUM_LOCKS; ++i) {
2463-
PyInterpreterLock_Release(locks[i]);
2464-
assert(_PyInterpreterState_LockCountdown(interp) == (NUM_LOCKS - i - 1));
2462+
for (int i = 0; i < NUM_GUARDS; ++i) {
2463+
PyInterpreterGuard_Release(guards[i]);
2464+
assert(_PyInterpreterState_LockCountdown(interp) == (NUM_GUARDS - i - 1));
24652465
}
24662466

24672467
PyInterpreterView_Close(view);
@@ -2579,7 +2579,7 @@ static PyMethodDef module_functions[] = {
25792579
#endif
25802580
{"simple_pending_call", simple_pending_call, METH_O},
25812581
{"set_vectorcall_nop", set_vectorcall_nop, METH_O},
2582-
{"test_interp_lock_countdown", test_interp_lock_countdown, METH_NOARGS},
2582+
{"test_interp_guard_countdown", test_interp_guard_countdown, METH_NOARGS},
25832583
{"test_interp_view_countdown", test_interp_view_countdown, METH_NOARGS},
25842584
{NULL, NULL} /* sentinel */
25852585
};

Programs/_testembed.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,7 +2319,7 @@ const char *THREAD_CODE = \
23192319
"fib(10)";
23202320

23212321
typedef struct {
2322-
PyInterpreterLock lock;
2322+
PyInterpreterGuard guard;
23232323
int done;
23242324
} ThreadData;
23252325

@@ -2328,11 +2328,11 @@ do_tstate_ensure(void *arg)
23282328
{
23292329
ThreadData *data = (ThreadData *)arg;
23302330
PyThreadView refs[4];
2331-
refs[0] = PyThreadState_Ensure(data->lock);
2332-
refs[1] = PyThreadState_Ensure(data->lock);
2333-
refs[2] = PyThreadState_Ensure(data->lock);
2331+
refs[0] = PyThreadState_Ensure(data->guard);
2332+
refs[1] = PyThreadState_Ensure(data->guard);
2333+
refs[2] = PyThreadState_Ensure(data->guard);
23342334
PyGILState_STATE gstate = PyGILState_Ensure();
2335-
refs[3] = PyThreadState_Ensure(data->lock);
2335+
refs[3] = PyThreadState_Ensure(data->guard);
23362336
assert(refs[0] != 0);
23372337
assert(refs[1] != 0);
23382338
assert(refs[2] != 0);
@@ -2344,7 +2344,7 @@ do_tstate_ensure(void *arg)
23442344
PyThreadState_Release(refs[2]);
23452345
PyThreadState_Release(refs[1]);
23462346
PyThreadState_Release(refs[0]);
2347-
PyInterpreterLock_Release(data->lock);
2347+
PyInterpreterGuard_Release(data->guard);
23482348
data->done = 1;
23492349
}
23502350

@@ -2354,17 +2354,17 @@ test_thread_state_ensure(void)
23542354
_testembed_initialize();
23552355
PyThread_handle_t handle;
23562356
PyThread_ident_t ident;
2357-
PyInterpreterLock ref = PyInterpreterLock_FromCurrent();
2358-
if (ref == 0) {
2357+
PyInterpreterGuard guard = PyInterpreterGuard_FromCurrent();
2358+
if (guard == 0) {
23592359
return -1;
23602360
};
2361-
ThreadData data = { ref };
2361+
ThreadData data = { guard };
23622362
if (PyThread_start_joinable_thread(do_tstate_ensure, &data,
23632363
&ident, &handle) < 0) {
2364-
PyInterpreterLock_Release(ref);
2364+
PyInterpreterGuard_Release(guard);
23652365
return -1;
23662366
}
2367-
// We hold a strong interpreter reference, so we don't
2367+
// We hold an interpreter guard, so we don't
23682368
// have to worry about the interpreter shutting down before
23692369
// we finalize.
23702370
Py_Finalize();
@@ -2381,15 +2381,15 @@ test_main_interpreter_view(void)
23812381
PyInterpreterView view = PyUnstable_InterpreterView_FromDefault();
23822382
assert(view != 0);
23832383

2384-
PyInterpreterLock lock = PyInterpreterLock_FromView(view);
2385-
assert(lock != 0);
2386-
PyInterpreterLock_Release(lock);
2384+
PyInterpreterGuard guard = PyInterpreterGuard_FromView(view);
2385+
assert(guard != 0);
2386+
PyInterpreterGuard_Release(guard);
23872387

23882388
Py_Finalize();
23892389

23902390
// We shouldn't be able to get locks for the interpreter now
2391-
lock = PyInterpreterLock_FromView(view);
2392-
assert(lock == 0);
2391+
guard = PyInterpreterGuard_FromView(view);
2392+
assert(guard == 0);
23932393

23942394
PyInterpreterView_Close(view);
23952395

0 commit comments

Comments
 (0)