1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
/*
* C Extension module to test pycore_critical_section.h API.
*/
#include "parts.h"
#include "pycore_critical_section.h"
#ifdef Py_GIL_DISABLED
#define assert_nogil assert
#define assert_gil(x)
#else
#define assert_gil assert
#define assert_nogil(x)
#endif
static PyObject *
test_critical_sections(PyObject *self, PyObject *Py_UNUSED(args))
{
PyObject *d1 = PyDict_New();
assert(d1 != NULL);
PyObject *d2 = PyDict_New();
assert(d2 != NULL);
// Beginning a critical section should lock the associated object and
// push the critical section onto the thread's stack (in Py_GIL_DISABLED builds).
Py_BEGIN_CRITICAL_SECTION(d1);
assert_nogil(PyMutex_IsLocked(&d1->ob_mutex));
assert_nogil(_PyCriticalSection_IsActive(PyThreadState_GET()->critical_section));
assert_gil(PyThreadState_GET()->critical_section == 0);
Py_END_CRITICAL_SECTION();
assert_nogil(!PyMutex_IsLocked(&d1->ob_mutex));
assert_nogil(!PyMutex_IsLocked(&d1->ob_mutex));
assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
Py_BEGIN_CRITICAL_SECTION2(d1, d2);
assert_nogil(PyMutex_IsLocked(&d1->ob_mutex));
assert_nogil(PyMutex_IsLocked(&d2->ob_mutex));
Py_END_CRITICAL_SECTION2();
assert_nogil(!PyMutex_IsLocked(&d1->ob_mutex));
assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
// Passing the same object twice should work (and not deadlock).
assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
Py_BEGIN_CRITICAL_SECTION2(d2, d2);
assert_nogil(PyMutex_IsLocked(&d2->ob_mutex));
Py_END_CRITICAL_SECTION2();
assert_nogil(!PyMutex_IsLocked(&d2->ob_mutex));
Py_DECREF(d2);
Py_DECREF(d1);
Py_RETURN_NONE;
}
static void
lock_unlock_object(PyObject *obj, int recurse_depth)
{
Py_BEGIN_CRITICAL_SECTION(obj);
if (recurse_depth > 0) {
lock_unlock_object(obj, recurse_depth - 1);
}
Py_END_CRITICAL_SECTION();
}
static void
lock_unlock_two_objects(PyObject *a, PyObject *b, int recurse_depth)
{
Py_BEGIN_CRITICAL_SECTION2(a, b);
if (recurse_depth > 0) {
lock_unlock_two_objects(a, b, recurse_depth - 1);
}
Py_END_CRITICAL_SECTION2();
}
// Test that nested critical sections do not deadlock if they attempt to lock
// the same object.
static PyObject *
test_critical_sections_nest(PyObject *self, PyObject *Py_UNUSED(args))
{
PyObject *a = PyDict_New();
assert(a != NULL);
PyObject *b = PyDict_New();
assert(b != NULL);
// Locking an object recursively with this API should not deadlock.
assert_nogil(!PyMutex_IsLocked(&a->ob_mutex));
Py_BEGIN_CRITICAL_SECTION(a);
assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
lock_unlock_object(a, 10);
assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
Py_END_CRITICAL_SECTION();
assert_nogil(!PyMutex_IsLocked(&a->ob_mutex));
// Same test but with two objects.
Py_BEGIN_CRITICAL_SECTION2(b, a);
lock_unlock_two_objects(a, b, 10);
assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
assert_nogil(PyMutex_IsLocked(&b->ob_mutex));
Py_END_CRITICAL_SECTION2();
Py_DECREF(b);
Py_DECREF(a);
Py_RETURN_NONE;
}
// Test that a critical section is suspended by a Py_BEGIN_ALLOW_THREADS and
// resumed by a Py_END_ALLOW_THREADS.
static PyObject *
test_critical_sections_suspend(PyObject *self, PyObject *Py_UNUSED(args))
{
PyObject *a = PyDict_New();
assert(a != NULL);
Py_BEGIN_CRITICAL_SECTION(a);
assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
// Py_BEGIN_ALLOW_THREADS should suspend the active critical section
Py_BEGIN_ALLOW_THREADS
assert_nogil(!PyMutex_IsLocked(&a->ob_mutex));
Py_END_ALLOW_THREADS;
// After Py_END_ALLOW_THREADS the critical section should be resumed.
assert_nogil(PyMutex_IsLocked(&a->ob_mutex));
Py_END_CRITICAL_SECTION();
Py_DECREF(a);
Py_RETURN_NONE;
}
#ifdef Py_CAN_START_THREADS
struct test_data {
PyObject *obj1;
PyObject *obj2;
PyObject *obj3;
Py_ssize_t countdown;
PyEvent done_event;
};
static void
thread_critical_sections(void *arg)
{
const Py_ssize_t NUM_ITERS = 200;
struct test_data *test_data = arg;
PyGILState_STATE gil = PyGILState_Ensure();
for (Py_ssize_t i = 0; i < NUM_ITERS; i++) {
Py_BEGIN_CRITICAL_SECTION(test_data->obj1);
Py_END_CRITICAL_SECTION();
Py_BEGIN_CRITICAL_SECTION(test_data->obj2);
lock_unlock_object(test_data->obj1, 1);
Py_END_CRITICAL_SECTION();
Py_BEGIN_CRITICAL_SECTION2(test_data->obj3, test_data->obj1);
lock_unlock_object(test_data->obj2, 2);
Py_END_CRITICAL_SECTION2();
Py_BEGIN_CRITICAL_SECTION(test_data->obj3);
Py_BEGIN_ALLOW_THREADS
Py_END_ALLOW_THREADS
Py_END_CRITICAL_SECTION();
}
PyGILState_Release(gil);
if (_Py_atomic_add_ssize(&test_data->countdown, -1) == 1) {
// last thread to finish sets done_event
_PyEvent_Notify(&test_data->done_event);
}
}
static PyObject *
test_critical_sections_threads(PyObject *self, PyObject *Py_UNUSED(args))
{
const Py_ssize_t NUM_THREADS = 4;
struct test_data test_data = {
.obj1 = PyDict_New(),
.obj2 = PyDict_New(),
.obj3 = PyDict_New(),
.countdown = NUM_THREADS,
};
assert(test_data.obj1 != NULL);
assert(test_data.obj2 != NULL);
assert(test_data.obj3 != NULL);
for (int i = 0; i < NUM_THREADS; i++) {
PyThread_start_new_thread(&thread_critical_sections, &test_data);
}
PyEvent_Wait(&test_data.done_event);
Py_DECREF(test_data.obj3);
Py_DECREF(test_data.obj2);
Py_DECREF(test_data.obj1);
Py_RETURN_NONE;
}
static void
pysleep(int ms)
{
#ifdef MS_WINDOWS
Sleep(ms);
#else
usleep(ms * 1000);
#endif
}
struct test_data_gc {
PyObject *obj;
Py_ssize_t num_threads;
Py_ssize_t id;
Py_ssize_t countdown;
PyEvent done_event;
PyEvent ready;
};
static void
thread_gc(void *arg)
{
struct test_data_gc *test_data = arg;
PyGILState_STATE gil = PyGILState_Ensure();
Py_ssize_t id = _Py_atomic_add_ssize(&test_data->id, 1);
if (id == test_data->num_threads - 1) {
_PyEvent_Notify(&test_data->ready);
}
else {
// wait for all test threads to more reliably reproduce the issue.
PyEvent_Wait(&test_data->ready);
}
if (id == 0) {
Py_BEGIN_CRITICAL_SECTION(test_data->obj);
// pause long enough that the lock would be handed off directly to
// a waiting thread.
pysleep(5);
PyGC_Collect();
Py_END_CRITICAL_SECTION();
}
else if (id == 1) {
pysleep(1);
Py_BEGIN_CRITICAL_SECTION(test_data->obj);
pysleep(1);
Py_END_CRITICAL_SECTION();
}
else if (id == 2) {
// sleep long enough so that thread 0 is waiting to stop the world
pysleep(6);
Py_BEGIN_CRITICAL_SECTION(test_data->obj);
pysleep(1);
Py_END_CRITICAL_SECTION();
}
PyGILState_Release(gil);
if (_Py_atomic_add_ssize(&test_data->countdown, -1) == 1) {
// last thread to finish sets done_event
_PyEvent_Notify(&test_data->done_event);
}
}
static PyObject *
test_critical_sections_gc(PyObject *self, PyObject *Py_UNUSED(args))
{
// gh-118332: Contended critical sections should not deadlock with GC
const Py_ssize_t NUM_THREADS = 3;
struct test_data_gc test_data = {
.obj = PyDict_New(),
.countdown = NUM_THREADS,
.num_threads = NUM_THREADS,
};
assert(test_data.obj != NULL);
for (int i = 0; i < NUM_THREADS; i++) {
PyThread_start_new_thread(&thread_gc, &test_data);
}
PyEvent_Wait(&test_data.done_event);
Py_DECREF(test_data.obj);
Py_RETURN_NONE;
}
#endif
static PyMethodDef test_methods[] = {
{"test_critical_sections", test_critical_sections, METH_NOARGS},
{"test_critical_sections_nest", test_critical_sections_nest, METH_NOARGS},
{"test_critical_sections_suspend", test_critical_sections_suspend, METH_NOARGS},
#ifdef Py_CAN_START_THREADS
{"test_critical_sections_threads", test_critical_sections_threads, METH_NOARGS},
{"test_critical_sections_gc", test_critical_sections_gc, METH_NOARGS},
#endif
{NULL, NULL} /* sentinel */
};
int
_PyTestInternalCapi_Init_CriticalSection(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
return 0;
}
|