8000 gh-117435: Make `SemLock` thread-safe in free-threaded build (#117436) · python/cpython@de5ca0b · GitHub
[go: up one dir, main page]

Skip to content

Commit de5ca0b

Browse files
authored
gh-117435: Make SemLock thread-safe in free-threaded build (#117436)
Use critical sections to make acquire, release, and _count thread-safe without the GIL.
1 parent 04697bc commit de5ca0b

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

Modules/_multiprocessing/clinic/semaphore.c.h

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_multiprocessing/semaphore.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ _GetSemaphoreValue(HANDLE handle, long *value)
8181
}
8282

8383
/*[clinic input]
84+
@critical_section
8485
_multiprocessing.SemLock.acquire
8586
8687
block as blocking: bool = True
@@ -92,7 +93,7 @@ Acquire the semaphore/lock.
9293
static PyObject *
9394
_multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
9495
PyObject *timeout_obj)
95-
/*[clinic end generated code: output=f9998f0b6b0b0872 input=e5b45f5cbb775166]*/
96+
/*[clinic end generated code: output=f9998f0b6b0b0872 input=079ca779975f3ad6]*/
9697
{
9798
double timeout;
9899
DWORD res, full_msecs, nhandles;
@@ -172,14 +173,15 @@ _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
172173
}
173174

174175
/*[clinic input]
176+
@critical_section
175177
_multiprocessing.SemLock.release
176178
177179
Release the semaphore/lock.
178180
[clinic start generated code]*/
179181

180182
static PyObject *
181183
_multiprocessing_SemLock_release_impl(SemLockObject *self)
182-
/*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/
184+
/*[clinic end generated code: output=b22f53ba96b0d1db input=9bd62d3645e7a531]*/
183185
{
184186
if (self->kind == RECURSIVE_MUTEX) {
185187
if (!ISMINE(self)) {
@@ -297,6 +299,7 @@ sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
297299
#endif /* !HAVE_SEM_TIMEDWAIT */
298300

299301
/*[clinic input]
302+
@critical_section
300303
_multiprocessing.SemLock.acquire
301304
302305
block as blocking: bool = True
@@ -308,7 +311,7 @@ Acquire the semaphore/lock.
308311
static PyObject *
309312
_multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
310313
PyObject *timeout_obj)
311-
/*[clinic end generated code: output=f9998f0b6b0b0872 input=e5b45f5cbb775166]*/
314+
/*[clinic end generated code: output=f9998f0b6b0b0872 input=079ca779975f3ad6]*/
312315
{
313316
int res, err = 0;
314317
struct timespec deadline = {0};
@@ -382,14 +385,15 @@ _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
382385
}
383386

384387
/*[clinic input]
388+
@critical_section
385389
_multiprocessing.SemLock.release
386390
387391
Release the semaphore/lock.
388392
[clinic start generated code]*/
389393

390394
static PyObject *
391395
_multiprocessing_SemLock_release_impl(SemLockObject *self)
392-
/*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/
396+
/*[clinic end generated code: output=b22f53ba96b0d1db input=9bd62d3645e7a531]*/
393397
{
394398
if (self->kind == RECURSIVE_MUTEX) {
395399
if (!ISMINE(self)) {
@@ -583,14 +587,15 @@ semlock_dealloc(SemLockObject* self)
583587
}
584588

585589
/*[clinic input]
590+
@critical_section
586591
_multiprocessing.SemLock._count
587592
588593
Num of `acquire()`s minus num of `release()`s for this process.
589594
[clinic start generated code]*/
590595

591596
static PyObject *
592597
_multiprocessing_SemLock__count_impl(SemLockObject *self)
593-
/*[clinic end generated code: output=5ba8213900e517bb input=36fc59b1cd1025ab]*/
598+
/*[clinic end generated code: output=5ba8213900e517bb input=9fa6e0b321b16935]*/
594599
{
595600
return PyLong_FromLong((long)self->count);
596601
}

0 commit comments

Comments
 (0)
0