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 enum { RECURSIVE_MUTEX, SEMAPHORE };
13 
14 typedef struct {
15     PyObject_HEAD
16     SEM_HANDLE handle;
17     unsigned long last_tid;
18     int count;
19     int maxvalue;
20     int kind;
21     char *name;
22 } SemLockObject;
23 
24 #define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
25 
26 
27 #ifdef MS_WINDOWS
28 
29 /*
30  * Windows definitions
31  */
32 
33 #define SEM_FAILED NULL
34 
35 #define SEM_CLEAR_ERROR() SetLastError(0)
36 #define SEM_GET_LAST_ERROR() GetLastError()
37 #define SEM_CREATE(name, val, max) CreateSemaphore(NULL, val, max, NULL)
38 #define SEM_CLOSE(sem) (CloseHandle(sem) ? 0 : -1)
39 #define SEM_GETVALUE(sem, pval) _GetSemaphoreValue(sem, pval)
40 #define SEM_UNLINK(name) 0
41 
42 static int
_GetSemaphoreValue(HANDLE handle,long * value)43 _GetSemaphoreValue(HANDLE handle, long *value)
44 {
45     long previous;
46 
47     switch (WaitForSingleObjectEx(handle, 0, FALSE)) {
48     case WAIT_OBJECT_0:
49         if (!ReleaseSemaphore(handle, 1, &previous))
50             return MP_STANDARD_ERROR;
51         *value = previous + 1;
52         return 0;
53     case WAIT_TIMEOUT:
54         *value = 0;
55         return 0;
56     default:
57         return MP_STANDARD_ERROR;
58     }
59 }
60 
61 static PyObject *
semlock_acquire(SemLockObject * self,PyObject * args,PyObject * kwds)62 semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
63 {
64     int blocking = 1;
65     double timeout;
66     PyObject *timeout_obj = Py_None;
67     DWORD res, full_msecs, nhandles;
68     HANDLE handles[2], sigint_event;
69 
70     static char *kwlist[] = {"block", "timeout", NULL};
71 
72     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
73                                      &blocking, &timeout_obj))
74         return NULL;
75 
76     /* calculate timeout */
77     if (!blocking) {
78         full_msecs = 0;
79     } else if (timeout_obj == Py_None) {
80         full_msecs = INFINITE;
81     } else {
82         timeout = PyFloat_AsDouble(timeout_obj);
83         if (PyErr_Occurred())
84             return NULL;
85         timeout *= 1000.0;      /* convert to millisecs */
86         if (timeout < 0.0) {
87             timeout = 0.0;
88         } else if (timeout >= 0.5 * INFINITE) { /* 25 days */
89             PyErr_SetString(PyExc_OverflowError,
90                             "timeout is too large");
91             return NULL;
92         }
93         full_msecs = (DWORD)(timeout + 0.5);
94     }
95 
96     /* check whether we already own the lock */
97     if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
98         ++self->count;
99         Py_RETURN_TRUE;
100     }
101 
102     /* check whether we can acquire without releasing the GIL and blocking */
103     if (WaitForSingleObjectEx(self->handle, 0, FALSE) == WAIT_OBJECT_0) {
104         self->last_tid = GetCurrentThreadId();
105         ++self->count;
106         Py_RETURN_TRUE;
107     }
108 
109     /* prepare list of handles */
110     nhandles = 0;
111     handles[nhandles++] = self->handle;
112     if (_PyOS_IsMainThread()) {
113         sigint_event = _PyOS_SigintEvent();
114         assert(sigint_event != NULL);
115         handles[nhandles++] = sigint_event;
116     }
117     else {
118         sigint_event = NULL;
119     }
120 
121     /* do the wait */
122     Py_BEGIN_ALLOW_THREADS
123     if (sigint_event != NULL)
124         ResetEvent(sigint_event);
125     res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, full_msecs, FALSE);
126     Py_END_ALLOW_THREADS
127 
128     /* handle result */
129     switch (res) {
130     case WAIT_TIMEOUT:
131         Py_RETURN_FALSE;
132     case WAIT_OBJECT_0 + 0:
133         self->last_tid = GetCurrentThreadId();
134         ++self->count;
135         Py_RETURN_TRUE;
136     case WAIT_OBJECT_0 + 1:
137         errno = EINTR;
138         return PyErr_SetFromErrno(PyExc_OSError);
139     case WAIT_FAILED:
140         return PyErr_SetFromWindowsErr(0);
141     default:
142         PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
143                      "WaitForMultipleObjects() gave unrecognized "
144                      "value %u", res);
145         return NULL;
146     }
147 }
148 
149 static PyObject *
semlock_release(SemLockObject * self,PyObject * args)150 semlock_release(SemLockObject *self, PyObject *args)
151 {
152     if (self->kind == RECURSIVE_MUTEX) {
153         if (!ISMINE(self)) {
154             PyErr_SetString(PyExc_AssertionError, "attempt to "
155                             "release recursive lock not owned "
156                             "by thread");
157             return NULL;
158         }
159         if (self->count > 1) {
160             --self->count;
161             Py_RETURN_NONE;
162         }
163         assert(self->count == 1);
164     }
165 
166     if (!ReleaseSemaphore(self->handle, 1, NULL)) {
167         if (GetLastError() == ERROR_TOO_MANY_POSTS) {
168             PyErr_SetString(PyExc_ValueError, "semaphore or lock "
169                             "released too many times");
170             return NULL;
171         } else {
172             return PyErr_SetFromWindowsErr(0);
173         }
174     }
175 
176     --self->count;
177     Py_RETURN_NONE;
178 }
179 
180 #else /* !MS_WINDOWS */
181 
182 /*
183  * Unix definitions
184  */
185 
186 #define SEM_CLEAR_ERROR()
187 #define SEM_GET_LAST_ERROR() 0
188 #define SEM_CREATE(name, val, max) sem_open(name, O_CREAT | O_EXCL, 0600, val)
189 #define SEM_CLOSE(sem) sem_close(sem)
190 #define SEM_GETVALUE(sem, pval) sem_getvalue(sem, pval)
191 #define SEM_UNLINK(name) sem_unlink(name)
192 
193 /* OS X 10.4 defines SEM_FAILED as -1 instead of (sem_t *)-1;  this gives
194    compiler warnings, and (potentially) undefined behaviour. */
195 #ifdef __APPLE__
196 #  undef SEM_FAILED
197 #  define SEM_FAILED ((sem_t *)-1)
198 #endif
199 
200 #ifndef HAVE_SEM_UNLINK
201 #  define sem_unlink(name) 0
202 #endif
203 
204 #ifndef HAVE_SEM_TIMEDWAIT
205 #  define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save)
206 
207 static int
sem_timedwait_save(sem_t * sem,struct timespec * deadline,PyThreadState * _save)208 sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
209 {
210     int res;
211     unsigned long delay, difference;
212     struct timeval now, tvdeadline, tvdelay;
213 
214     errno = 0;
215     tvdeadline.tv_sec = deadline->tv_sec;
216     tvdeadline.tv_usec = deadline->tv_nsec / 1000;
217 
218     for (delay = 0 ; ; delay += 1000) {
219         /* poll */
220         if (sem_trywait(sem) == 0)
221             return 0;
222         else if (errno != EAGAIN)
223             return MP_STANDARD_ERROR;
224 
225         /* get current time */
226         if (gettimeofday(&now, NULL) < 0)
227             return MP_STANDARD_ERROR;
228 
229         /* check for timeout */
230         if (tvdeadline.tv_sec < now.tv_sec ||
231             (tvdeadline.tv_sec == now.tv_sec &&
232              tvdeadline.tv_usec <= now.tv_usec)) {
233             errno = ETIMEDOUT;
234             return MP_STANDARD_ERROR;
235         }
236 
237         /* calculate how much time is left */
238         difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
239             (tvdeadline.tv_usec - now.tv_usec);
240 
241         /* check delay not too long -- maximum is 20 msecs */
242         if (delay > 20000)
243             delay = 20000;
244         if (delay > difference)
245             delay = difference;
246 
247         /* sleep */
248         tvdelay.tv_sec = delay / 1000000;
249         tvdelay.tv_usec = delay % 1000000;
250         if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
251             return MP_STANDARD_ERROR;
252 
253         /* check for signals */
254         Py_BLOCK_THREADS
255         res = PyErr_CheckSignals();
256         Py_UNBLOCK_THREADS
257 
258         if (res) {
259             errno = EINTR;
260             return MP_EXCEPTION_HAS_BEEN_SET;
261         }
262     }
263 }
264 
265 #endif /* !HAVE_SEM_TIMEDWAIT */
266 
267 static PyObject *
semlock_acquire(SemLockObject * self,PyObject * args,PyObject * kwds)268 semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
269 {
270     int blocking = 1, res, err = 0;
271     double timeout;
272     PyObject *timeout_obj = Py_None;
273     struct timespec deadline = {0};
274     struct timeval now;
275     long sec, nsec;
276 
277     static char *kwlist[] = {"block", "timeout", NULL};
278 
279     if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
280                                      &blocking, &timeout_obj))
281         return NULL;
282 
283     if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
284         ++self->count;
285         Py_RETURN_TRUE;
286     }
287 
288     if (timeout_obj != Py_None) {
289         timeout = PyFloat_AsDouble(timeout_obj);
290         if (PyErr_Occurred())
291             return NULL;
292         if (timeout < 0.0)
293             timeout = 0.0;
294 
295         if (gettimeofday(&now, NULL) < 0) {
296             PyErr_SetFromErrno(PyExc_OSError);
297             return NULL;
298         }
299         sec = (long) timeout;
300         nsec = (long) (1e9 * (timeout - sec) + 0.5);
301         deadline.tv_sec = now.tv_sec + sec;
302         deadline.tv_nsec = now.tv_usec * 1000 + nsec;
303         deadline.tv_sec += (deadline.tv_nsec / 1000000000);
304         deadline.tv_nsec %= 1000000000;
305     }
306 
307     /* Check whether we can acquire without releasing the GIL and blocking */
308     do {
309         res = sem_trywait(self->handle);
310         err = errno;
311     } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
312     errno = err;
313 
314     if (res < 0 && errno == EAGAIN && blocking) {
315         /* Couldn't acquire immediately, need to block */
316         do {
317             Py_BEGIN_ALLOW_THREADS
318             if (timeout_obj == Py_None) {
319                 res = sem_wait(self->handle);
320             }
321             else {
322                 res = sem_timedwait(self->handle, &deadline);
323             }
324             Py_END_ALLOW_THREADS
325             err = errno;
326             if (res == MP_EXCEPTION_HAS_BEEN_SET)
327                 break;
328         } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
329     }
330 
331     if (res < 0) {
332         errno = err;
333         if (errno == EAGAIN || errno == ETIMEDOUT)
334             Py_RETURN_FALSE;
335         else if (errno == EINTR)
336             return NULL;
337         else
338             return PyErr_SetFromErrno(PyExc_OSError);
339     }
340 
341     ++self->count;
342     self->last_tid = PyThread_get_thread_ident();
343 
344     Py_RETURN_TRUE;
345 }
346 
347 static PyObject *
semlock_release(SemLockObject * self,PyObject * args)348 semlock_release(SemLockObject *self, PyObject *args)
349 {
350     if (self->kind == RECURSIVE_MUTEX) {
351         if (!ISMINE(self)) {
352             PyErr_SetString(PyExc_AssertionError, "attempt to "
353                             "release recursive lock not owned "
354                             "by thread");
355             return NULL;
356         }
357         if (self->count > 1) {
358             --self->count;
359             Py_RETURN_NONE;
360         }
361         assert(self->count == 1);
362     } else {
363 #ifdef HAVE_BROKEN_SEM_GETVALUE
364         /* We will only check properly the maxvalue == 1 case */
365         if (self->maxvalue == 1) {
366             /* make sure that already locked */
367             if (sem_trywait(self->handle) < 0) {
368                 if (errno != EAGAIN) {
369                     PyErr_SetFromErrno(PyExc_OSError);
370                     return NULL;
371                 }
372                 /* it is already locked as expected */
373             } else {
374                 /* it was not locked so undo wait and raise  */
375                 if (sem_post(self->handle) < 0) {
376                     PyErr_SetFromErrno(PyExc_OSError);
377                     return NULL;
378                 }
379                 PyErr_SetString(PyExc_ValueError, "semaphore "
380                                 "or lock released too many "
381                                 "times");
382                 return NULL;
383             }
384         }
385 #else
386         int sval;
387 
388         /* This check is not an absolute guarantee that the semaphore
389            does not rise above maxvalue. */
390         if (sem_getvalue(self->handle, &sval) < 0) {
391             return PyErr_SetFromErrno(PyExc_OSError);
392         } else if (sval >= self->maxvalue) {
393             PyErr_SetString(PyExc_ValueError, "semaphore or lock "
394                             "released too many times");
395             return NULL;
396         }
397 #endif
398     }
399 
400     if (sem_post(self->handle) < 0)
401         return PyErr_SetFromErrno(PyExc_OSError);
402 
403     --self->count;
404     Py_RETURN_NONE;
405 }
406 
407 #endif /* !MS_WINDOWS */
408 
409 /*
410  * All platforms
411  */
412 
413 static PyObject *
newsemlockobject(PyTypeObject * type,SEM_HANDLE handle,int kind,int maxvalue,char * name)414 newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
415                  char *name)
416 {
417     SemLockObject *self;
418 
419     self = PyObject_New(SemLockObject, type);
420     if (!self)
421         return NULL;
422     self->handle = handle;
423     self->kind = kind;
424     self->count = 0;
425     self->last_tid = 0;
426     self->maxvalue = maxvalue;
427     self->name = name;
428     return (PyObject*)self;
429 }
430 
431 static PyObject *
semlock_new(PyTypeObject * type,PyObject * args,PyObject * kwds)432 semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
433 {
434     SEM_HANDLE handle = SEM_FAILED;
435     int kind, maxvalue, value, unlink;
436     PyObject *result;
437     char *name, *name_copy = NULL;
438     static char *kwlist[] = {"kind", "value", "maxvalue", "name", "unlink",
439                              NULL};
440 
441     if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiisi", kwlist,
442                                      &kind, &value, &maxvalue, &name, &unlink))
443         return NULL;
444 
445     if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
446         PyErr_SetString(PyExc_ValueError, "unrecognized kind");
447         return NULL;
448     }
449 
450     if (!unlink) {
451         name_copy = PyMem_Malloc(strlen(name) + 1);
452         if (name_copy == NULL) {
453             return PyErr_NoMemory();
454         }
455         strcpy(name_copy, name);
456     }
457 
458     SEM_CLEAR_ERROR();
459     handle = SEM_CREATE(name, value, maxvalue);
460     /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
461     if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
462         goto failure;
463 
464     if (unlink && SEM_UNLINK(name) < 0)
465         goto failure;
466 
467     result = newsemlockobject(type, handle, kind, maxvalue, name_copy);
468     if (!result)
469         goto failure;
470 
471     return result;
472 
473   failure:
474     if (handle != SEM_FAILED)
475         SEM_CLOSE(handle);
476     PyMem_Free(name_copy);
477     if (!PyErr_Occurred()) {
478         _PyMp_SetError(NULL, MP_STANDARD_ERROR);
479     }
480     return NULL;
481 }
482 
483 static PyObject *
semlock_rebuild(PyTypeObject * type,PyObject * args)484 semlock_rebuild(PyTypeObject *type, PyObject *args)
485 {
486     SEM_HANDLE handle;
487     int kind, maxvalue;
488     char *name, *name_copy = NULL;
489 
490     if (!PyArg_ParseTuple(args, F_SEM_HANDLE "iiz",
491                           &handle, &kind, &maxvalue, &name))
492         return NULL;
493 
494     if (name != NULL) {
495         name_copy = PyMem_Malloc(strlen(name) + 1);
496         if (name_copy == NULL)
497             return PyErr_NoMemory();
498         strcpy(name_copy, name);
499     }
500 
501 #ifndef MS_WINDOWS
502     if (name != NULL) {
503         handle = sem_open(name, 0);
504         if (handle == SEM_FAILED) {
505             PyMem_Free(name_copy);
506             return PyErr_SetFromErrno(PyExc_OSError);
507         }
508     }
509 #endif
510 
511     return newsemlockobject(type, handle, kind, maxvalue, name_copy);
512 }
513 
514 static void
semlock_dealloc(SemLockObject * self)515 semlock_dealloc(SemLockObject* self)
516 {
517     if (self->handle != SEM_FAILED)
518         SEM_CLOSE(self->handle);
519     PyMem_Free(self->name);
520     PyObject_Del(self);
521 }
522 
523 static PyObject *
semlock_count(SemLockObject * self,PyObject * Py_UNUSED (ignored))524 semlock_count(SemLockObject *self, PyObject *Py_UNUSED(ignored))
525 {
526     return PyLong_FromLong((long)self->count);
527 }
528 
529 static PyObject *
semlock_ismine(SemLockObject * self,PyObject * Py_UNUSED (ignored))530 semlock_ismine(SemLockObject *self, PyObject *Py_UNUSED(ignored))
531 {
532     /* only makes sense for a lock */
533     return PyBool_FromLong(ISMINE(self));
534 }
535 
536 static PyObject *
semlock_getvalue(SemLockObject * self,PyObject * Py_UNUSED (ignored))537 semlock_getvalue(SemLockObject *self, PyObject *Py_UNUSED(ignored))
538 {
539 #ifdef HAVE_BROKEN_SEM_GETVALUE
540     PyErr_SetNone(PyExc_NotImplementedError);
541     return NULL;
542 #else
543     int sval;
544     if (SEM_GETVALUE(self->handle, &sval) < 0)
545         return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
546     /* some posix implementations use negative numbers to indicate
547        the number of waiting threads */
548     if (sval < 0)
549         sval = 0;
550     return PyLong_FromLong((long)sval);
551 #endif
552 }
553 
554 static PyObject *
semlock_iszero(SemLockObject * self,PyObject * Py_UNUSED (ignored))555 semlock_iszero(SemLockObject *self, PyObject *Py_UNUSED(ignored))
556 {
557 #ifdef HAVE_BROKEN_SEM_GETVALUE
558     if (sem_trywait(self->handle) < 0) {
559         if (errno == EAGAIN)
560             Py_RETURN_TRUE;
561         return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
562     } else {
563         if (sem_post(self->handle) < 0)
564             return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
565         Py_RETURN_FALSE;
566     }
567 #else
568     int sval;
569     if (SEM_GETVALUE(self->handle, &sval) < 0)
570         return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
571     return PyBool_FromLong((long)sval == 0);
572 #endif
573 }
574 
575 static PyObject *
semlock_afterfork(SemLockObject * self,PyObject * Py_UNUSED (ignored))576 semlock_afterfork(SemLockObject *self, PyObject *Py_UNUSED(ignored))
577 {
578     self->count = 0;
579     Py_RETURN_NONE;
580 }
581 
582 /*
583  * Semaphore methods
584  */
585 
586 static PyMethodDef semlock_methods[] = {
587     {"acquire", (PyCFunction)(void(*)(void))semlock_acquire, METH_VARARGS | METH_KEYWORDS,
588      "acquire the semaphore/lock"},
589     {"release", (PyCFunction)semlock_release, METH_NOARGS,
590      "release the semaphore/lock"},
591     {"__enter__", (PyCFunction)(void(*)(void))semlock_acquire, METH_VARARGS | METH_KEYWORDS,
592      "enter the semaphore/lock"},
593     {"__exit__", (PyCFunction)semlock_release, METH_VARARGS,
594      "exit the semaphore/lock"},
595     {"_count", (PyCFunction)semlock_count, METH_NOARGS,
596      "num of `acquire()`s minus num of `release()`s for this process"},
597     {"_is_mine", (PyCFunction)semlock_ismine, METH_NOARGS,
598      "whether the lock is owned by this thread"},
599     {"_get_value", (PyCFunction)semlock_getvalue, METH_NOARGS,
600      "get the value of the semaphore"},
601     {"_is_zero", (PyCFunction)semlock_iszero, METH_NOARGS,
602      "returns whether semaphore has value zero"},
603     {"_rebuild", (PyCFunction)semlock_rebuild, METH_VARARGS | METH_CLASS,
604      ""},
605     {"_after_fork", (PyCFunction)semlock_afterfork, METH_NOARGS,
606      "rezero the net acquisition count after fork()"},
607     {NULL}
608 };
609 
610 /*
611  * Member table
612  */
613 
614 static PyMemberDef semlock_members[] = {
615     {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY,
616      ""},
617     {"kind", T_INT, offsetof(SemLockObject, kind), READONLY,
618      ""},
619     {"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY,
620      ""},
621     {"name", T_STRING, offsetof(SemLockObject, name), READONLY,
622      ""},
623     {NULL}
624 };
625 
626 /*
627  * Semaphore type
628  */
629 
630 PyTypeObject _PyMp_SemLockType = {
631     PyVarObject_HEAD_INIT(NULL, 0)
632     /* tp_name           */ "_multiprocessing.SemLock",
633     /* tp_basicsize      */ sizeof(SemLockObject),
634     /* tp_itemsize       */ 0,
635     /* tp_dealloc        */ (destructor)semlock_dealloc,
636     /* tp_vectorcall_offset */ 0,
637     /* tp_getattr        */ 0,
638     /* tp_setattr        */ 0,
639     /* tp_as_async       */ 0,
640     /* tp_repr           */ 0,
641     /* tp_as_number      */ 0,
642     /* tp_as_sequence    */ 0,
643     /* tp_as_mapping     */ 0,
644     /* tp_hash           */ 0,
645     /* tp_call           */ 0,
646     /* tp_str            */ 0,
647     /* tp_getattro       */ 0,
648     /* tp_setattro       */ 0,
649     /* tp_as_buffer      */ 0,
650     /* tp_flags          */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
651     /* tp_doc            */ "Semaphore/Mutex type",
652     /* tp_traverse       */ 0,
653     /* tp_clear          */ 0,
654     /* tp_richcompare    */ 0,
655     /* tp_weaklistoffset */ 0,
656     /* tp_iter           */ 0,
657     /* tp_iternext       */ 0,
658     /* tp_methods        */ semlock_methods,
659     /* tp_members        */ semlock_members,
660     /* tp_getset         */ 0,
661     /* tp_base           */ 0,
662     /* tp_dict           */ 0,
663     /* tp_descr_get      */ 0,
664     /* tp_descr_set      */ 0,
665     /* tp_dictoffset     */ 0,
666     /* tp_init           */ 0,
667     /* tp_alloc          */ 0,
668     /* tp_new            */ semlock_new,
669 };
670 
671 /*
672  * Function to unlink semaphore names
673  */
674 
675 PyObject *
_PyMp_sem_unlink(PyObject * ignore,PyObject * args)676 _PyMp_sem_unlink(PyObject *ignore, PyObject *args)
677 {
678     char *name;
679 
680     if (!PyArg_ParseTuple(args, "s", &name))
681         return NULL;
682 
683     if (SEM_UNLINK(name) < 0) {
684         _PyMp_SetError(NULL, MP_STANDARD_ERROR);
685         return NULL;
686     }
687 
688     Py_RETURN_NONE;
689 }
690