Debian Codegraph @

Modules/_testinternalcapi/test_lock.c

raw
1 // C Extension module to test pycore_lock.h API
2
3 #include "parts.h"
4 #include "pycore_lock.h"
5 #include "pycore_pythread.h" // PyThread_get_thread_ident_ex()
6
7 #include "clinic/test_lock.c.h"
8
9 #ifdef MS_WINDOWS
10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h>
12 #else
13 #include <unistd.h> // usleep()
14 #endif
15
16 /*[clinic input]
17 module _testinternalcapi
18 [clinic start generated code]*/
19 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7bb583d8c9eb9a78]*/
20
21
22 static void
23 pysleep(int ms)
24 {
25 #ifdef MS_WINDOWS
26 Sleep(ms);
27 #else
28 usleep(ms * 1000);
29 #endif
30 }
31
32 static PyObject *
33 test_lock_basic(PyObject *self, PyObject *obj)
34 {
35 PyMutex m = (PyMutex){0};
36
37 // uncontended lock and unlock
38 PyMutex_Lock(&m);
39 assert(m._bits == 1);
40 PyMutex_Unlock(&m);
41 assert(m._bits == 0);
42
43 Py_RETURN_NONE;
44 }
45
46 struct test_lock2_data {
47 PyMutex m;
48 PyEvent done;
49 int started;
50 };
51
52 static void
53 lock_thread(void *arg)
54 {
55 struct test_lock2_data *test_data = arg;
56 PyMutex *m = &test_data->m;
57 _Py_atomic_store_int(&test_data->started, 1);
58
59 PyMutex_Lock(m);
60 // gh-135641: in rare cases the lock may still have `_Py_HAS_PARKED` set
61 // (m->_bits == 3) due to bucket collisions in the parking lot hash table
62 // between this mutex and the `test_data.done` event.
63 assert(m->_bits == 1 || m->_bits == 3);
64
65 PyMutex_Unlock(m);
66 assert(m->_bits == 0);
67
68 _PyEvent_Notify(&test_data->done);
69 }
70
71 static PyObject *
72 test_lock_two_threads(PyObject *self, PyObject *obj)
73 {
74 // lock attempt by two threads
75 struct test_lock2_data test_data;
76 memset(&test_data, 0, sizeof(test_data));
77
78 PyMutex_Lock(&test_data.m);
79 assert(test_data.m._bits == 1);
80
81 PyThread_start_new_thread(lock_thread, &test_data);
82
83 // wait up to two seconds for the lock_thread to attempt to lock "m"
84 int iters = 0;
85 uint8_t v;
86 do {
87 pysleep(10); // allow some time for the other thread to try to lock
88 v = _Py_atomic_load_uint8_relaxed(&test_data.m._bits);
89 assert(v == 1 || v == 3);
90 iters++;
91 } while (v != 3 && iters < 200);
92
93 // both the "locked" and the "has parked" bits should be set
94 v = _Py_atomic_load_uint8_relaxed(&test_data.m._bits);
95 assert(v == 3);
96
97 PyMutex_Unlock(&test_data.m);
98 PyEvent_Wait(&test_data.done);
99 assert(test_data.m._bits == 0);
100
101 Py_RETURN_NONE;
102 }
103
104 #define COUNTER_THREADS 5
105 #define COUNTER_ITERS 10000
106
107 struct test_data_counter {
108 PyMutex m;
109 Py_ssize_t counter;
110 };
111
112 struct thread_data_counter {
113 struct test_data_counter *test_data;
114 PyEvent done_event;
115 };
116
117 static void
118 counter_thread(void *arg)
119 {
120 struct thread_data_counter *thread_data = arg;
121 struct test_data_counter *test_data = thread_data->test_data;
122
123 for (Py_ssize_t i = 0; i < COUNTER_ITERS; i++) {
124 PyMutex_Lock(&test_data->m);
125 test_data->counter++;
126 PyMutex_Unlock(&test_data->m);
127 }
128 _PyEvent_Notify(&thread_data->done_event);
129 }
130
131 static PyObject *
132 test_lock_counter(PyObject *self, PyObject *obj)
133 {
134 // Test with rapidly locking and unlocking mutex
135 struct test_data_counter test_data;
136 memset(&test_data, 0, sizeof(test_data));
137
138 struct thread_data_counter thread_data[COUNTER_THREADS];
139 memset(&thread_data, 0, sizeof(thread_data));
140
141 for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
142 thread_data[i].test_data = &test_data;
143 PyThread_start_new_thread(counter_thread, &thread_data[i]);
144 }
145
146 for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
147 PyEvent_Wait(&thread_data[i].done_event);
148 }
149
150 assert(test_data.counter == COUNTER_THREADS * COUNTER_ITERS);
151 Py_RETURN_NONE;
152 }
153
154 #define SLOW_COUNTER_ITERS 100
155
156 static void
157 slow_counter_thread(void *arg)
158 {
159 struct thread_data_counter *thread_data = arg;
160 struct test_data_counter *test_data = thread_data->test_data;
161
162 for (Py_ssize_t i = 0; i < SLOW_COUNTER_ITERS; i++) {
163 PyMutex_Lock(&test_data->m);
164 if (i % 7 == 0) {
165 pysleep(2);
166 }
167 test_data->counter++;
168 PyMutex_Unlock(&test_data->m);
169 }
170 _PyEvent_Notify(&thread_data->done_event);
171 }
172
173 static PyObject *
174 test_lock_counter_slow(PyObject *self, PyObject *obj)
175 {
176 // Test lock/unlock with occasional "long" critical section, which will
177 // trigger handoff of the lock.
178 struct test_data_counter test_data;
179 memset(&test_data, 0, sizeof(test_data));
180
181 struct thread_data_counter thread_data[COUNTER_THREADS];
182 memset(&thread_data, 0, sizeof(thread_data));
183
184 for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
185 thread_data[i].test_data = &test_data;
186 PyThread_start_new_thread(slow_counter_thread, &thread_data[i]);
187 }
188
189 for (Py_ssize_t i = 0; i < COUNTER_THREADS; i++) {
190 PyEvent_Wait(&thread_data[i].done_event);
191 }
192
193 assert(test_data.counter == COUNTER_THREADS * SLOW_COUNTER_ITERS);
194 Py_RETURN_NONE;
195 }
196
197 struct bench_data_locks {
198 int stop;
199 int use_pymutex;
200 int critical_section_length;
201 char padding[200];
202 PyThread_type_lock lock;
203 PyMutex m;
204 double value;
205 Py_ssize_t total_iters;
206 };
207
208 struct bench_thread_data {
209 struct bench_data_locks *bench_data;
210 Py_ssize_t iters;
211 PyEvent done;
212 };
213
214 static void
215 thread_benchmark_locks(void *arg)
216 {
217 struct bench_thread_data *thread_data = arg;
218 struct bench_data_locks *bench_data = thread_data->bench_data;
219 int use_pymutex = bench_data->use_pymutex;
220 int critical_section_length = bench_data->critical_section_length;
221
222 double my_value = 1.0;
223 Py_ssize_t iters = 0;
224 while (!_Py_atomic_load_int_relaxed(&bench_data->stop)) {
225 if (use_pymutex) {
226 PyMutex_Lock(&bench_data->m);
227 for (int i = 0; i < critical_section_length; i++) {
228 bench_data->value += my_value;
229 my_value = bench_data->value;
230 }
231 PyMutex_Unlock(&bench_data->m);
232 }
233 else {
234 PyThread_acquire_lock(bench_data->lock, 1);
235 for (int i = 0; i < critical_section_length; i++) {
236 bench_data->value += my_value;
237 my_value = bench_data->value;
238 }
239 PyThread_release_lock(bench_data->lock);
240 }
241 iters++;
242 }
243
244 thread_data->iters = iters;
245 _Py_atomic_add_ssize(&bench_data->total_iters, iters);
246 _PyEvent_Notify(&thread_data->done);
247 }
248
249 /*[clinic input]
250 _testinternalcapi.benchmark_locks
251
252 num_threads: Py_ssize_t
253 use_pymutex: bool = True
254 critical_section_length: int = 1
255 time_ms: int = 1000
256 /
257
258 [clinic start generated code]*/
259
260 static PyObject *
261 _testinternalcapi_benchmark_locks_impl(PyObject *module,
262 Py_ssize_t num_threads,
263 int use_pymutex,
264 int critical_section_length,
265 int time_ms)
266 /*[clinic end generated code: output=381df8d7e9a74f18 input=f3aeaf688738c121]*/
267 {
268 // Run from Tools/lockbench/lockbench.py
269 // Based on the WebKit lock benchmarks:
270 // https://github.com/WebKit/WebKit/blob/main/Source/WTF/benchmarks/LockSpeedTest.cpp
271 // See also https://webkit.org/blog/6161/locking-in-webkit/
272 PyObject *thread_iters = NULL;
273 PyObject *res = NULL;
274
275 struct bench_data_locks bench_data;
276 memset(&bench_data, 0, sizeof(bench_data));
277 bench_data.use_pymutex = use_pymutex;
278 bench_data.critical_section_length = critical_section_length;
279
280 bench_data.lock = PyThread_allocate_lock();
281 if (bench_data.lock == NULL) {
282 return PyErr_NoMemory();
283 }
284
285 struct bench_thread_data *thread_data = NULL;
286 thread_data = PyMem_Calloc(num_threads, sizeof(*thread_data));
287 if (thread_data == NULL) {
288 PyErr_NoMemory();
289 goto exit;
290 }
291
292 thread_iters = PyList_New(num_threads);
293 if (thread_iters == NULL) {
294 goto exit;
295 }
296
297 PyTime_t start, end;
298 if (PyTime_PerfCounter(&start) < 0) {
299 goto exit;
300 }
301
302 for (Py_ssize_t i = 0; i < num_threads; i++) {
303 thread_data[i].bench_data = &bench_data;
304 PyThread_start_new_thread(thread_benchmark_locks, &thread_data[i]);
305 }
306
307 // Let the threads run for `time_ms` milliseconds
308 pysleep(time_ms);
309 _Py_atomic_store_int(&bench_data.stop, 1);
310
311 // Wait for the threads to finish
312 for (Py_ssize_t i = 0; i < num_threads; i++) {
313 PyEvent_Wait(&thread_data[i].done);
314 }
315
316 Py_ssize_t total_iters = bench_data.total_iters;
317 if (PyTime_PerfCounter(&end) < 0) {
318 goto exit;
319 }
320
321 // Return the total number of acquisitions and the number of acquisitions
322 // for each thread.
323 for (Py_ssize_t i = 0; i < num_threads; i++) {
324 PyObject *iter = PyLong_FromSsize_t(thread_data[i].iters);
325 if (iter == NULL) {
326 goto exit;
327 }
328 PyList_SET_ITEM(thread_iters, i, iter);
329 }
330
331 assert(end != start);
332 double rate = total_iters * 1e9 / (end - start);
333 res = Py_BuildValue("(dO)", rate, thread_iters);
334
335 exit:
336 PyThread_free_lock(bench_data.lock);
337 PyMem_Free(thread_data);
338 Py_XDECREF(thread_iters);
339 return res;
340 }
341
342 static PyObject *
343 test_lock_benchmark(PyObject *module, PyObject *obj)
344 {
345 // Just make sure the benchmark runs without crashing
346 PyObject *res = _testinternalcapi_benchmark_locks_impl(
347 module, 1, 1, 1, 100);
348 if (res == NULL) {
349 return NULL;
350 }
351 Py_DECREF(res);
352 Py_RETURN_NONE;
353 }
354
355 static int
356 init_maybe_fail(void *arg)
357 {
358 int *counter = (int *)arg;
359 (*counter)++;
360 if (*counter < 5) {
361 // failure
362 return -1;
363 }
364 assert(*counter == 5);
365 return 0;
366 }
367
368 static PyObject *
369 test_lock_once(PyObject *self, PyObject *obj)
370 {
371 _PyOnceFlag once = {0};
372 int counter = 0;
373 for (int i = 0; i < 10; i++) {
374 int res = _PyOnceFlag_CallOnce(&once, init_maybe_fail, &counter);
375 if (i < 4) {
376 assert(res == -1);
377 }
378 else {
379 assert(res == 0);
380 assert(counter == 5);
381 }
382 }
383 Py_RETURN_NONE;
384 }
385
386 struct test_rwlock_data {
387 Py_ssize_t nthreads;
388 _PyRWMutex rw;
389 PyEvent step1;
390 PyEvent step2;
391 PyEvent step3;
392 PyEvent done;
393 };
394
395 static void
396 rdlock_thread(void *arg)
397 {
398 struct test_rwlock_data *test_data = arg;
399
400 // Acquire the lock in read mode
401 _PyRWMutex_RLock(&test_data->rw);
402 PyEvent_Wait(&test_data->step1);
403 _PyRWMutex_RUnlock(&test_data->rw);
404
405 _PyRWMutex_RLock(&test_data->rw);
406 PyEvent_Wait(&test_data->step3);
407 _PyRWMutex_RUnlock(&test_data->rw);
408
409 if (_Py_atomic_add_ssize(&test_data->nthreads, -1) == 1) {
410 _PyEvent_Notify(&test_data->done);
411 }
412 }
413 static void
414 wrlock_thread(void *arg)
415 {
416 struct test_rwlock_data *test_data = arg;
417
418 // First acquire the lock in write mode
419 _PyRWMutex_Lock(&test_data->rw);
420 PyEvent_Wait(&test_data->step2);
421 _PyRWMutex_Unlock(&test_data->rw);
422
423 if (_Py_atomic_add_ssize(&test_data->nthreads, -1) == 1) {
424 _PyEvent_Notify(&test_data->done);
425 }
426 }
427
428 static void
429 wait_until(uintptr_t *ptr, uintptr_t value)
430 {
431 // wait up to two seconds for *ptr == value
432 int iters = 0;
433 uintptr_t bits;
434 do {
435 pysleep(10);
436 bits = _Py_atomic_load_uintptr(ptr);
437 iters++;
438 } while (bits != value && iters < 200);
439 }
440
441 static PyObject *
442 test_lock_rwlock(PyObject *self, PyObject *obj)
443 {
444 struct test_rwlock_data test_data = {.nthreads = 3};
445
446 _PyRWMutex_Lock(&test_data.rw);
447 assert(test_data.rw.bits == 1);
448
449 _PyRWMutex_Unlock(&test_data.rw);
450 assert(test_data.rw.bits == 0);
451
452 // Start two readers
453 PyThread_start_new_thread(rdlock_thread, &test_data);
454 PyThread_start_new_thread(rdlock_thread, &test_data);
455
456 // wait up to two seconds for the threads to attempt to read-lock "rw"
457 wait_until(&test_data.rw.bits, 8);
458 assert(test_data.rw.bits == 8);
459
460 // start writer (while readers hold lock)
461 PyThread_start_new_thread(wrlock_thread, &test_data);
462 wait_until(&test_data.rw.bits, 10);
463 assert(test_data.rw.bits == 10);
464
465 // readers release lock, writer should acquire it
466 _PyEvent_Notify(&test_data.step1);
467 wait_until(&test_data.rw.bits, 3);
468 assert(test_data.rw.bits == 3);
469
470 // writer releases lock, readers acquire it
471 _PyEvent_Notify(&test_data.step2);
472 wait_until(&test_data.rw.bits, 8);
473 assert(test_data.rw.bits == 8);
474
475 // readers release lock again
476 _PyEvent_Notify(&test_data.step3);
477 wait_until(&test_data.rw.bits, 0);
478 assert(test_data.rw.bits == 0);
479
480 PyEvent_Wait(&test_data.done);
481 Py_RETURN_NONE;
482 }
483
484 static PyObject *
485 test_lock_recursive(PyObject *self, PyObject *obj)
486 {
487 _PyRecursiveMutex m = (_PyRecursiveMutex){0};
488 assert(!_PyRecursiveMutex_IsLockedByCurrentThread(&m));
489
490 _PyRecursiveMutex_Lock(&m);
491 assert(m.thread == PyThread_get_thread_ident_ex());
492 assert(PyMutex_IsLocked(&m.mutex));
493 assert(m.level == 0);
494
495 _PyRecursiveMutex_Lock(&m);
496 assert(m.level == 1);
497 _PyRecursiveMutex_Unlock(&m);
498
499 _PyRecursiveMutex_Unlock(&m);
500 assert(m.thread == 0);
501 assert(!PyMutex_IsLocked(&m.mutex));
502 assert(m.level == 0);
503
504 Py_RETURN_NONE;
505 }
506
507 static PyMethodDef test_methods[] = {
508 {"test_lock_basic", test_lock_basic, METH_NOARGS},
509 {"test_lock_two_threads", test_lock_two_threads, METH_NOARGS},
510 {"test_lock_counter", test_lock_counter, METH_NOARGS},
511 {"test_lock_counter_slow", test_lock_counter_slow, METH_NOARGS},
512 _TESTINTERNALCAPI_BENCHMARK_LOCKS_METHODDEF
513 {"test_lock_benchmark", test_lock_benchmark, METH_NOARGS},
514 {"test_lock_once", test_lock_once, METH_NOARGS},
515 {"test_lock_rwlock", test_lock_rwlock, METH_NOARGS},
516 {"test_lock_recursive", test_lock_recursive, METH_NOARGS},
517 {NULL, NULL} /* sentinel */
518 };
519
520 int
521 _PyTestInternalCapi_Init_Lock(PyObject *mod)
522 {
523 if (PyModule_AddFunctions(mod, test_methods) < 0) {
524 return -1;
525 }
526 return 0;
527 }