|
8 | 8 | #include <Python.h> |
9 | 9 | #include "pycore_initconfig.h" // _PyConfig_InitCompatConfig() |
10 | 10 | #include "pycore_runtime.h" // _PyRuntime |
| 11 | +#include "pycore_semaphore.h" // _PySemaphore |
11 | 12 | #include "pycore_pythread.h" // PyThread_start_joinable_thread() |
12 | 13 | #include "pycore_import.h" // _PyImport_FrozenBootstrap |
13 | 14 | #include <inttypes.h> |
@@ -2342,26 +2343,32 @@ test_get_incomplete_frame(void) |
2342 | 2343 | } |
2343 | 2344 |
|
2344 | 2345 | static void |
2345 | | -do_gilstate_ensure(void *unused) |
| 2346 | +do_gilstate_ensure(void *semaphore_ptr) |
2346 | 2347 | { |
2347 | | - for (int i = 0; i < 50; ++i) { |
2348 | | - PyGILState_STATE gstate = PyGILState_Ensure(); |
2349 | | - assert(PyGILState_Check()); // Yuck |
2350 | | - PyGILState_Release(gstate); |
2351 | | - } |
| 2348 | + _PySemaphore *semaphore = (_PySemaphore *)semaphore_ptr; |
| 2349 | + // Signal to the calling thread that we've started |
| 2350 | + _PySemaphore_Wakeup(semaphore); |
| 2351 | + PyGILState_STATE gstate = PyGILState_Ensure(); // This should hang |
| 2352 | + assert(NULL); |
2352 | 2353 | } |
2353 | 2354 |
|
2354 | 2355 | static int |
2355 | 2356 | test_gilstate_after_finalization(void) |
2356 | 2357 | { |
2357 | 2358 | _testembed_Py_Initialize(); |
| 2359 | + Py_Finalize(); |
2358 | 2360 | PyThread_handle_t handle; |
2359 | 2361 | PyThread_ident_t ident; |
2360 | | - if (PyThread_start_joinable_thread(&do_gilstate_ensure, NULL, &ident, &handle) < 0) { |
| 2362 | + _PySemaphore semaphore; |
| 2363 | + _PySemaphore_Init(&semaphore); |
| 2364 | + if (PyThread_start_joinable_thread(&do_gilstate_ensure, &semaphore, &ident, &handle) < 0) { |
2361 | 2365 | return -1; |
2362 | 2366 | } |
2363 | | - Py_Finalize(); |
2364 | | - return PyThread_join_thread(handle); |
| 2367 | + _PySemaphore_Wait(&semaphore, /*timeout_ns=*/-1, /*detach=*/0); |
| 2368 | + // We're now pretty confident that the thread went for |
| 2369 | + // PyGILState_Ensure(), but that means it got hung. |
| 2370 | + _PySemaphore_Destroy(&semaphore); |
| 2371 | + return PyThread_detach_thread(handle); |
2365 | 2372 | } |
2366 | 2373 |
|
2367 | 2374 | /* ********************************************************* |
|
0 commit comments