Debian Codegraph @

Modules/_multiprocessing/semaphore.c

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