1 /*
2 * A type which wraps a semaphore
3 *
4 * semaphore.c
5 *
6 * Copyright (c) 2006-2008, R Oudkerk
7 * Licensed to PSF under a Contributor Agreement.
8 */
9
10 #include "multiprocessing.h"
11
12 #ifdef HAVE_SYS_TIME_H
13 # include <sys/time.h> // gettimeofday()
14 #endif
15
16 #ifdef HAVE_MP_SEMAPHORE
17
18 enum { RECURSIVE_MUTEX, SEMAPHORE };
19
20 typedef struct {
21 PyObject_HEAD
22 SEM_HANDLE handle;
23 unsigned long last_tid;
24 int count;
25 int maxvalue;
26 int kind;
27 char *name;
28 } SemLockObject;
29
30 /*[python input]
31 class SEM_HANDLE_converter(CConverter):
32 type = "SEM_HANDLE"
33 format_unit = '"F_SEM_HANDLE"'
34
35 [python start generated code]*/
36 /*[python end generated code: output=da39a3ee5e6b4b0d input=3e0ad43e482d8716]*/
37
38 /*[clinic input]
39 module _multiprocessing
40 class _multiprocessing.SemLock "SemLockObject *" "&_PyMp_SemLockType"
41 [clinic start generated code]*/
42 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=935fb41b7d032599]*/
43
44 #include "clinic/semaphore.c.h"
45
46 #define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
47
48
49 #ifdef MS_WINDOWS
50
51 /*
52 * Windows definitions
53 */
54
55 #define SEM_FAILED NULL
56
57 #define SEM_CLEAR_ERROR() SetLastError(0)
58 #define SEM_GET_LAST_ERROR() GetLastError()
59 #define SEM_CREATE(name, val, max) CreateSemaphore(NULL, val, max, NULL)
60 #define SEM_CLOSE(sem) (CloseHandle(sem) ? 0 : -1)
61 #define SEM_GETVALUE(sem, pval) _GetSemaphoreValue(sem, pval)
62 #define SEM_UNLINK(name) 0
63
64 static int
_GetSemaphoreValue(HANDLE handle,long * value)65 _GetSemaphoreValue(HANDLE handle, long *value)
66 {
67 long previous;
68
69 switch (WaitForSingleObjectEx(handle, 0, FALSE)) {
70 case WAIT_OBJECT_0:
71 if (!ReleaseSemaphore(handle, 1, &previous))
72 return MP_STANDARD_ERROR;
73 *value = previous + 1;
74 return 0;
75 case WAIT_TIMEOUT:
76 *value = 0;
77 return 0;
78 default:
79 return MP_STANDARD_ERROR;
80 }
81 }
82
83 /*[clinic input]
84 @critical_section
85 _multiprocessing.SemLock.acquire
86
87 block as blocking: bool = True
88 timeout as timeout_obj: object = None
89
90 Acquire the semaphore/lock.
91 [clinic start generated code]*/
92
93 static PyObject *
_multiprocessing_SemLock_acquire_impl(SemLockObject * self,int blocking,PyObject * timeout_obj)94 _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
95 PyObject *timeout_obj)
96 /*[clinic end generated code: output=f9998f0b6b0b0872 input=079ca779975f3ad6]*/
97 {
98 double timeout;
99 DWORD res, full_msecs, nhandles;
100 HANDLE handles[2], sigint_event;
101
102 /* calculate timeout */
103 if (!blocking) {
104 full_msecs = 0;
105 } else if (timeout_obj == Py_None) {
106 full_msecs = INFINITE;
107 } else {
108 timeout = PyFloat_AsDouble(timeout_obj);
109 if (PyErr_Occurred())
110 return NULL;
111 timeout *= 1000.0; /* convert to millisecs */
112 if (timeout < 0.0) {
113 timeout = 0.0;
114 } else if (timeout >= 0.5 * INFINITE) { /* 25 days */
115 PyErr_SetString(PyExc_OverflowError,
116 "timeout is too large");
117 return NULL;
118 }
119 full_msecs = (DWORD)(timeout + 0.5);
120 }
121
122 /* check whether we already own the lock */
123 if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
124 ++self->count;
125 Py_RETURN_TRUE;
126 }
127
128 /* check whether we can acquire without releasing the GIL and blocking */
129 if (WaitForSingleObjectEx(self->handle, 0, FALSE) == WAIT_OBJECT_0) {
130 self->last_tid = GetCurrentThreadId();
131 ++self->count;
132 Py_RETURN_TRUE;
133 }
134
135 /* prepare list of handles */
136 nhandles = 0;
137 handles[nhandles++] = self->handle;
138 if (_PyOS_IsMainThread()) {
139 sigint_event = _PyOS_SigintEvent();
140 assert(sigint_event != NULL);
141 handles[nhandles++] = sigint_event;
142 }
143 else {
144 sigint_event = NULL;
145 }
146
147 /* do the wait */
148 Py_BEGIN_ALLOW_THREADS
149 if (sigint_event != NULL)
150 ResetEvent(sigint_event);
151 res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, full_msecs, FALSE);
152 Py_END_ALLOW_THREADS
153
154 /* handle result */
155 switch (res) {
156 case WAIT_TIMEOUT:
157 Py_RETURN_FALSE;
158 case WAIT_OBJECT_0 + 0:
159 self->last_tid = GetCurrentThreadId();
160 ++self->count;
161 Py_RETURN_TRUE;
162 case WAIT_OBJECT_0 + 1:
163 errno = EINTR;
164 return PyErr_SetFromErrno(PyExc_OSError);
165 case WAIT_FAILED:
166 return PyErr_SetFromWindowsErr(0);
167 default:
168 PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
169 "WaitForMultipleObjects() gave unrecognized "
170 "value %u", res);
171 return NULL;
172 }
173 }
174
175 /*[clinic input]
176 @critical_section
177 _multiprocessing.SemLock.release
178
179 Release the semaphore/lock.
180 [clinic start generated code]*/
181
182 static PyObject *
_multiprocessing_SemLock_release_impl(SemLockObject * self)183 _multiprocessing_SemLock_release_impl(SemLockObject *self)
184 /*[clinic end generated code: output=b22f53ba96b0d1db input=9bd62d3645e7a531]*/
185 {
186 if (self->kind == RECURSIVE_MUTEX) {
187 if (!ISMINE(self)) {
188 PyErr_SetString(PyExc_AssertionError, "attempt to "
189 "release recursive lock not owned "
190 "by thread");
191 return NULL;
192 }
193 if (self->count > 1) {
194 --self->count;
195 Py_RETURN_NONE;
196 }
197 assert(self->count == 1);
198 }
199
200 if (!ReleaseSemaphore(self->handle, 1, NULL)) {
201 if (GetLastError() == ERROR_TOO_MANY_POSTS) {
202 PyErr_SetString(PyExc_ValueError, "semaphore or lock "
203 "released too many times");
204 return NULL;
205 } else {
206 return PyErr_SetFromWindowsErr(0);
207 }
208 }
209
210 --self->count;
211 Py_RETURN_NONE;
212 }
213
214 #else /* !MS_WINDOWS */
215
216 /*
217 * Unix definitions
218 */
219
220 #define SEM_CLEAR_ERROR()
221 #define SEM_GET_LAST_ERROR() 0
222 #define SEM_CREATE(name, val, max) sem_open(name, O_CREAT | O_EXCL, 0600, val)
223 #define SEM_CLOSE(sem) sem_close(sem)
224 #define SEM_GETVALUE(sem, pval) sem_getvalue(sem, pval)
225 #define SEM_UNLINK(name) sem_unlink(name)
226
227 /* OS X 10.4 defines SEM_FAILED as -1 instead of (sem_t *)-1; this gives
228 compiler warnings, and (potentially) undefined behaviour. */
229 #ifdef __APPLE__
230 # undef SEM_FAILED
231 # define SEM_FAILED ((sem_t *)-1)
232 #endif
233
234 #ifndef HAVE_SEM_UNLINK
235 # define sem_unlink(name) 0
236 #endif
237
238 #ifndef HAVE_SEM_TIMEDWAIT
239 # define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save)
240
241 static int
sem_timedwait_save(sem_t * sem,struct timespec * deadline,PyThreadState * _save)242 sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
243 {
244 int res;
245 unsigned long delay, difference;
246 struct timeval now, tvdeadline, tvdelay;
247
248 errno = 0;
249 tvdeadline.tv_sec = deadline->tv_sec;
250 tvdeadline.tv_usec = deadline->tv_nsec / 1000;
251
252 for (delay = 0 ; ; delay += 1000) {
253 /* poll */
254 if (sem_trywait(sem) == 0)
255 return 0;
256 else if (errno != EAGAIN)
257 return MP_STANDARD_ERROR;
258
259 /* get current time */
260 if (gettimeofday(&now, NULL) < 0)
261 return MP_STANDARD_ERROR;
262
263 /* check for timeout */
264 if (tvdeadline.tv_sec < now.tv_sec ||
265 (tvdeadline.tv_sec == now.tv_sec &&
266 tvdeadline.tv_usec <= now.tv_usec)) {
267 errno = ETIMEDOUT;
268 return MP_STANDARD_ERROR;
269 }
270
271 /* calculate how much time is left */
272 difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
273 (tvdeadline.tv_usec - now.tv_usec);
274
275 /* check delay not too long -- maximum is 20 msecs */
276 if (delay > 20000)
277 delay = 20000;
278 if (delay > difference)
279 delay = difference;
280
281 /* sleep */
282 tvdelay.tv_sec = delay / 1000000;
283 tvdelay.tv_usec = delay % 1000000;
284 if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
285 return MP_STANDARD_ERROR;
286
287 /* check for signals */
288 Py_BLOCK_THREADS
289 res = PyErr_CheckSignals();
290 Py_UNBLOCK_THREADS
291
292 if (res) {
293 errno = EINTR;
294 return MP_EXCEPTION_HAS_BEEN_SET;
295 }
296 }
297 }
298
299 #endif /* !HAVE_SEM_TIMEDWAIT */
300
301 /*[clinic input]
302 @critical_section
303 _multiprocessing.SemLock.acquire
304
305 block as blocking: bool = True
306 timeout as timeout_obj: object = None
307
308 Acquire the semaphore/lock.
309 [clinic start generated code]*/
310
311 static PyObject *
_multiprocessing_SemLock_acquire_impl(SemLockObject * self,int blocking,PyObject * timeout_obj)312 _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
313 PyObject *timeout_obj)
314 /*[clinic end generated code: output=f9998f0b6b0b0872 input=079ca779975f3ad6]*/
315 {
316 int res, err = 0;
317 struct timespec deadline = {0};
318
319 if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
320 ++self->count;
321 Py_RETURN_TRUE;
322 }
323
324 int use_deadline = (timeout_obj != Py_None);
325 if (use_deadline) {
326 double timeout = PyFloat_AsDouble(timeout_obj);
327 if (PyErr_Occurred()) {
328 return NULL;
329 }
330 if (timeout < 0.0) {
331 timeout = 0.0;
332 }
333
334 struct timeval now;
335 if (gettimeofday(&now, NULL) < 0) {
336 PyErr_SetFromErrno(PyExc_OSError);
337 return NULL;
338 }
339 long sec = (long) timeout;
340 long nsec = (long) (1e9 * (timeout - sec) + 0.5);
341 deadline.tv_sec = now.tv_sec + sec;
342 deadline.tv_nsec = now.tv_usec * 1000 + nsec;
343 deadline.tv_sec += (deadline.tv_nsec / 1000000000);
344 deadline.tv_nsec %= 1000000000;
345 }
346
347 /* Check whether we can acquire without releasing the GIL and blocking */
348 do {
349 res = sem_trywait(self->handle);
350 err = errno;
351 } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
352 errno = err;
353
354 if (res < 0 && errno == EAGAIN && blocking) {
355 /* Couldn't acquire immediately, need to block */
356 do {
357 Py_BEGIN_ALLOW_THREADS
358 if (!use_deadline) {
359 res = sem_wait(self->handle);
360 }
361 else {
362 res = sem_timedwait(self->handle, &deadline);
363 }
364 Py_END_ALLOW_THREADS
365 err = errno;
366 if (res == MP_EXCEPTION_HAS_BEEN_SET)
367 break;
368 } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
369 }
370
371 if (res < 0) {
372 errno = err;
373 if (errno == EAGAIN || errno == ETIMEDOUT)
374 Py_RETURN_FALSE;
375 else if (errno == EINTR)
376 return NULL;
377 else
378 return PyErr_SetFromErrno(PyExc_OSError);
379 }
380
381 ++self->count;
382 self->last_tid = PyThread_get_thread_ident();
383
384 Py_RETURN_TRUE;
385 }
386
387 /*[clinic input]
388 @critical_section
389 _multiprocessing.SemLock.release
390
391 Release the semaphore/lock.
392 [clinic start generated code]*/
393
394 static PyObject *
_multiprocessing_SemLock_release_impl(SemLockObject * self)395 _multiprocessing_SemLock_release_impl(SemLockObject *self)
396 /*[clinic end generated code: output=b22f53ba96b0d1db input=9bd62d3645e7a531]*/
397 {
398 if (self->kind == RECURSIVE_MUTEX) {
399 if (!ISMINE(self)) {
400 PyErr_SetString(PyExc_AssertionError, "attempt to "
401 "release recursive lock not owned "
402 "by thread");
403 return NULL;
404 }
405 if (self->count > 1) {
406 --self->count;
407 Py_RETURN_NONE;
408 }
409 assert(self->count == 1);
410 } else {
411 #ifdef HAVE_BROKEN_SEM_GETVALUE
412 /* We will only check properly the maxvalue == 1 case */
413 if (self->maxvalue == 1) {
414 /* make sure that already locked */
415 if (sem_trywait(self->handle) < 0) {
416 if (errno != EAGAIN) {
417 PyErr_SetFromErrno(PyExc_OSError);
418 return NULL;
419 }
420 /* it is already locked as expected */
421 } else {
422 /* it was not locked so undo wait and raise */
423 if (sem_post(self->handle) < 0) {
424 PyErr_SetFromErrno(PyExc_OSError);
425 return NULL;
426 }
427 PyErr_SetString(PyExc_ValueError, "semaphore "
428 "or lock released too many "
429 "times");
430 return NULL;
431 }
432 }
433 #else
434 int sval;
435
436 /* This check is not an absolute guarantee that the semaphore
437 does not rise above maxvalue. */
438 if (sem_getvalue(self->handle, &sval) < 0) {
439 return PyErr_SetFromErrno(PyExc_OSError);
440 } else if (sval >= self->maxvalue) {
441 PyErr_SetString(PyExc_ValueError, "semaphore or lock "
442 "released too many times");
443 return NULL;
444 }
445 #endif
446 }
447
448 if (sem_post(self->handle) < 0)
449 return PyErr_SetFromErrno(PyExc_OSError);
450
451 --self->count;
452 Py_RETURN_NONE;
453 }
454
455 #endif /* !MS_WINDOWS */
456
457 /*
458 * All platforms
459 */
460
461 static PyObject *
newsemlockobject(PyTypeObject * type,SEM_HANDLE handle,int kind,int maxvalue,char * name)462 newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
463 char *name)
464 {
465 SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0);
466 if (!self)
467 return NULL;
468 self->handle = handle;
469 self->kind = kind;
470 self->count = 0;
471 self->last_tid = 0;
472 self->maxvalue = maxvalue;
473 self->name = name;
474 return (PyObject*)self;
475 }
476
477 /*[clinic input]
478 @classmethod
479 _multiprocessing.SemLock.__new__
480
481 kind: int
482 value: int
483 maxvalue: int
484 name: str
485 unlink: bool
486
487 [clinic start generated code]*/
488
489 static PyObject *
_multiprocessing_SemLock_impl(PyTypeObject * type,int kind,int value,int maxvalue,const char * name,int unlink)490 _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value,
491 int maxvalue, const char *name, int unlink)
492 /*[clinic end generated code: output=30727e38f5f7577a input=fdaeb69814471c5b]*/
493 {
494 SEM_HANDLE handle = SEM_FAILED;
495 PyObject *result;
496 char *name_copy = NULL;
497
498 if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
499 PyErr_SetString(PyExc_ValueError, "unrecognized kind");
500 return NULL;
501 }
502
503 if (!unlink) {
504 name_copy = PyMem_Malloc(strlen(name) + 1);
505 if (name_copy == NULL) {
506 return PyErr_NoMemory();
507 }
508 strcpy(name_copy, name);
509 }
510
511 SEM_CLEAR_ERROR();
512 handle = SEM_CREATE(name, value, maxvalue);
513 /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
514 if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
515 goto failure;
516
517 if (unlink && SEM_UNLINK(name) < 0)
518 goto failure;
519
520 result = newsemlockobject(type, handle, kind, maxvalue, name_copy);
521 if (!result)
522 goto failure;
523
524 return result;
525
526 failure:
527 if (!PyErr_Occurred()) {
528 _PyMp_SetError(NULL, MP_STANDARD_ERROR);
529 }
530 if (handle != SEM_FAILED)
531 SEM_CLOSE(handle);
532 PyMem_Free(name_copy);
533 return NULL;
534 }
535
536 /*[clinic input]
537 @classmethod
538 _multiprocessing.SemLock._rebuild
539
540 handle: SEM_HANDLE
541 kind: int
542 maxvalue: int
543 name: str(accept={str, NoneType})
544 /
545
546 [clinic start generated code]*/
547
548 static PyObject *
_multiprocessing_SemLock__rebuild_impl(PyTypeObject * type,SEM_HANDLE handle,int kind,int maxvalue,const char * name)549 _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
550 int kind, int maxvalue,
551 const char *name)
552 /*[clinic end generated code: output=2aaee14f063f3bd9 input=f7040492ac6d9962]*/
553 {
554 char *name_copy = NULL;
555
556 if (name != NULL) {
557 name_copy = PyMem_Malloc(strlen(name) + 1);
558 if (name_copy == NULL)
559 return PyErr_NoMemory();
560 strcpy(name_copy, name);
561 }
562
563 #ifndef MS_WINDOWS
564 if (name != NULL) {
565 handle = sem_open(name, 0);
566 if (handle == SEM_FAILED) {
567 PyErr_SetFromErrno(PyExc_OSError);
568 PyMem_Free(name_copy);
569 return NULL;
570 }
571 }
572 #endif
573
574 return newsemlockobject(type, handle, kind, maxvalue, name_copy);
575 }
576
577 static void
semlock_dealloc(SemLockObject * self)578 semlock_dealloc(SemLockObject* self)
579 {
580 PyTypeObject *tp = Py_TYPE(self);
581 PyObject_GC_UnTrack(self);
582 if (self->handle != SEM_FAILED)
583 SEM_CLOSE(self->handle);
584 PyMem_Free(self->name);
585 tp->tp_free(self);
586 Py_DECREF(tp);
587 }
588
589 /*[clinic input]
590 @critical_section
591 _multiprocessing.SemLock._count
592
593 Num of `acquire()`s minus num of `release()`s for this process.
594 [clinic start generated code]*/
595
596 static PyObject *
_multiprocessing_SemLock__count_impl(SemLockObject * self)597 _multiprocessing_SemLock__count_impl(SemLockObject *self)
598 /*[clinic end generated code: output=5ba8213900e517bb input=9fa6e0b321b16935]*/
599 {
600 return PyLong_FromLong((long)self->count);
601 }
602
603 /*[clinic input]
604 _multiprocessing.SemLock._is_mine
605
606 Whether the lock is owned by this thread.
607 [clinic start generated code]*/
608
609 static PyObject *
_multiprocessing_SemLock__is_mine_impl(SemLockObject * self)610 _multiprocessing_SemLock__is_mine_impl(SemLockObject *self)
611 /*[clinic end generated code: output=92dc98863f4303be input=a96664cb2f0093ba]*/
612 {
613 /* only makes sense for a lock */
614 return PyBool_FromLong(ISMINE(self));
615 }
616
617 /*[clinic input]
618 _multiprocessing.SemLock._get_value
619
620 Get the value of the semaphore.
621 [clinic start generated code]*/
622
623 static PyObject *
_multiprocessing_SemLock__get_value_impl(SemLockObject * self)624 _multiprocessing_SemLock__get_value_impl(SemLockObject *self)
625 /*[clinic end generated code: output=64bc1b89bda05e36 input=cb10f9a769836203]*/
626 {
627 #ifdef HAVE_BROKEN_SEM_GETVALUE
628 PyErr_SetNone(PyExc_NotImplementedError);
629 return NULL;
630 #else
631 int sval;
632 if (SEM_GETVALUE(self->handle, &sval) < 0)
633 return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
634 /* some posix implementations use negative numbers to indicate
635 the number of waiting threads */
636 if (sval < 0)
637 sval = 0;
638 return PyLong_FromLong((long)sval);
639 #endif
640 }
641
642 /*[clinic input]
643 _multiprocessing.SemLock._is_zero
644
645 Return whether semaphore has value zero.
646 [clinic start generated code]*/
647
648 static PyObject *
_multiprocessing_SemLock__is_zero_impl(SemLockObject * self)649 _multiprocessing_SemLock__is_zero_impl(SemLockObject *self)
650 /*[clinic end generated code: output=815d4c878c806ed7 input=294a446418d31347]*/
651 {
652 #ifdef HAVE_BROKEN_SEM_GETVALUE
653 if (sem_trywait(self->handle) < 0) {
654 if (errno == EAGAIN)
655 Py_RETURN_TRUE;
656 return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
657 } else {
658 if (sem_post(self->handle) < 0)
659 return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
660 Py_RETURN_FALSE;
661 }
662 #else
663 int sval;
664 if (SEM_GETVALUE(self->handle, &sval) < 0)
665 return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
666 return PyBool_FromLong((long)sval == 0);
667 #endif
668 }
669
670 /*[clinic input]
671 _multiprocessing.SemLock._after_fork
672
673 Rezero the net acquisition count after fork().
674 [clinic start generated code]*/
675
676 static PyObject *
_multiprocessing_SemLock__after_fork_impl(SemLockObject * self)677 _multiprocessing_SemLock__after_fork_impl(SemLockObject *self)
678 /*[clinic end generated code: output=718bb27914c6a6c1 input=190991008a76621e]*/
679 {
680 self->count = 0;
681 Py_RETURN_NONE;
682 }
683
684 /*[clinic input]
685 @critical_section
686 _multiprocessing.SemLock.__enter__
687
688 Enter the semaphore/lock.
689 [clinic start generated code]*/
690
691 static PyObject *
_multiprocessing_SemLock___enter___impl(SemLockObject * self)692 _multiprocessing_SemLock___enter___impl(SemLockObject *self)
693 /*[clinic end generated code: output=beeb2f07c858511f input=d35c9860992ee790]*/
694 {
695 return _multiprocessing_SemLock_acquire_impl(self, 1, Py_None);
696 }
697
698 /*[clinic input]
699 @critical_section
700 _multiprocessing.SemLock.__exit__
701
702 exc_type: object = None
703 exc_value: object = None
704 exc_tb: object = None
705 /
706
707 Exit the semaphore/lock.
708 [clinic start generated code]*/
709
710 static PyObject *
_multiprocessing_SemLock___exit___impl(SemLockObject * self,PyObject * exc_type,PyObject * exc_value,PyObject * exc_tb)711 _multiprocessing_SemLock___exit___impl(SemLockObject *self,
712 PyObject *exc_type,
713 PyObject *exc_value, PyObject *exc_tb)
714 /*[clinic end generated code: output=3b37c1a9f8b91a03 input=1610c8cc3e0e337e]*/
715 {
716 return _multiprocessing_SemLock_release_impl(self);
717 }
718
719 static int
semlock_traverse(SemLockObject * s,visitproc visit,void * arg)720 semlock_traverse(SemLockObject *s, visitproc visit, void *arg)
721 {
722 Py_VISIT(Py_TYPE(s));
723 return 0;
724 }
725
726 /*
727 * Semaphore methods
728 */
729
730 static PyMethodDef semlock_methods[] = {
731 _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF
732 _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF
733 _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF
734 _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF
735 _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF
736 _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF
737 _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF
738 _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF
739 _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF
740 _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF
741 {NULL}
742 };
743
744 /*
745 * Member table
746 */
747
748 static PyMemberDef semlock_members[] = {
749 {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), Py_READONLY,
750 ""},
751 {"kind", Py_T_INT, offsetof(SemLockObject, kind), Py_READONLY,
752 ""},
753 {"maxvalue", Py_T_INT, offsetof(SemLockObject, maxvalue), Py_READONLY,
754 ""},
755 {"name", Py_T_STRING, offsetof(SemLockObject, name), Py_READONLY,
756 ""},
757 {NULL}
758 };
759
760 /*
761 * Semaphore type
762 */
763
764 static PyType_Slot _PyMp_SemLockType_slots[] = {
765 {Py_tp_dealloc, semlock_dealloc},
766 {Py_tp_getattro, PyObject_GenericGetAttr},
767 {Py_tp_setattro, PyObject_GenericSetAttr},
768 {Py_tp_methods, semlock_methods},
769 {Py_tp_members, semlock_members},
770 {Py_tp_alloc, PyType_GenericAlloc},
771 {Py_tp_new, _multiprocessing_SemLock},
772 {Py_tp_traverse, semlock_traverse},
773 {Py_tp_free, PyObject_GC_Del},
774 {Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")},
775 {0, 0},
776 };
777
778 PyType_Spec _PyMp_SemLockType_spec = {
779 .name = "_multiprocessing.SemLock",
780 .basicsize = sizeof(SemLockObject),
781 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
782 Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
783 .slots = _PyMp_SemLockType_slots,
784 };
785
786 /*
787 * Function to unlink semaphore names
788 */
789
790 PyObject *
_PyMp_sem_unlink(const char * name)791 _PyMp_sem_unlink(const char *name)
792 {
793 if (SEM_UNLINK(name) < 0) {
794 _PyMp_SetError(NULL, MP_STANDARD_ERROR);
795 return NULL;
796 }
797
798 Py_RETURN_NONE;
799 }
800
801 #endif // HAVE_MP_SEMAPHORE
802