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