123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790 |
- /*
- * A type which wraps a semaphore
- *
- * semaphore.c
- *
- * Copyright (c) 2006-2008, R Oudkerk
- * Licensed to PSF under a Contributor Agreement.
- */
- #include "multiprocessing.h"
- #ifdef HAVE_MP_SEMAPHORE
- enum { RECURSIVE_MUTEX, SEMAPHORE };
- typedef struct {
- PyObject_HEAD
- SEM_HANDLE handle;
- unsigned long last_tid;
- int count;
- int maxvalue;
- int kind;
- char *name;
- } SemLockObject;
- /*[python input]
- class SEM_HANDLE_converter(CConverter):
- type = "SEM_HANDLE"
- format_unit = '"F_SEM_HANDLE"'
- [python start generated code]*/
- /*[python end generated code: output=da39a3ee5e6b4b0d input=3e0ad43e482d8716]*/
- /*[clinic input]
- module _multiprocessing
- class _multiprocessing.SemLock "SemLockObject *" "&_PyMp_SemLockType"
- [clinic start generated code]*/
- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=935fb41b7d032599]*/
- #include "clinic/semaphore.c.h"
- #define ISMINE(o) (o->count > 0 && PyThread_get_thread_ident() == o->last_tid)
- #ifdef MS_WINDOWS
- /*
- * Windows definitions
- */
- #define SEM_FAILED NULL
- #define SEM_CLEAR_ERROR() SetLastError(0)
- #define SEM_GET_LAST_ERROR() GetLastError()
- #define SEM_CREATE(name, val, max) CreateSemaphore(NULL, val, max, NULL)
- #define SEM_CLOSE(sem) (CloseHandle(sem) ? 0 : -1)
- #define SEM_GETVALUE(sem, pval) _GetSemaphoreValue(sem, pval)
- #define SEM_UNLINK(name) 0
- static int
- _GetSemaphoreValue(HANDLE handle, long *value)
- {
- long previous;
- switch (WaitForSingleObjectEx(handle, 0, FALSE)) {
- case WAIT_OBJECT_0:
- if (!ReleaseSemaphore(handle, 1, &previous))
- return MP_STANDARD_ERROR;
- *value = previous + 1;
- return 0;
- case WAIT_TIMEOUT:
- *value = 0;
- return 0;
- default:
- return MP_STANDARD_ERROR;
- }
- }
- /*[clinic input]
- _multiprocessing.SemLock.acquire
- block as blocking: bool = True
- timeout as timeout_obj: object = None
- Acquire the semaphore/lock.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
- PyObject *timeout_obj)
- /*[clinic end generated code: output=f9998f0b6b0b0872 input=e5b45f5cbb775166]*/
- {
- double timeout;
- DWORD res, full_msecs, nhandles;
- HANDLE handles[2], sigint_event;
- /* calculate timeout */
- if (!blocking) {
- full_msecs = 0;
- } else if (timeout_obj == Py_None) {
- full_msecs = INFINITE;
- } else {
- timeout = PyFloat_AsDouble(timeout_obj);
- if (PyErr_Occurred())
- return NULL;
- timeout *= 1000.0; /* convert to millisecs */
- if (timeout < 0.0) {
- timeout = 0.0;
- } else if (timeout >= 0.5 * INFINITE) { /* 25 days */
- PyErr_SetString(PyExc_OverflowError,
- "timeout is too large");
- return NULL;
- }
- full_msecs = (DWORD)(timeout + 0.5);
- }
- /* check whether we already own the lock */
- if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
- ++self->count;
- Py_RETURN_TRUE;
- }
- /* check whether we can acquire without releasing the GIL and blocking */
- if (WaitForSingleObjectEx(self->handle, 0, FALSE) == WAIT_OBJECT_0) {
- self->last_tid = GetCurrentThreadId();
- ++self->count;
- Py_RETURN_TRUE;
- }
- /* prepare list of handles */
- nhandles = 0;
- handles[nhandles++] = self->handle;
- if (_PyOS_IsMainThread()) {
- sigint_event = _PyOS_SigintEvent();
- assert(sigint_event != NULL);
- handles[nhandles++] = sigint_event;
- }
- else {
- sigint_event = NULL;
- }
- /* do the wait */
- Py_BEGIN_ALLOW_THREADS
- if (sigint_event != NULL)
- ResetEvent(sigint_event);
- res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, full_msecs, FALSE);
- Py_END_ALLOW_THREADS
- /* handle result */
- switch (res) {
- case WAIT_TIMEOUT:
- Py_RETURN_FALSE;
- case WAIT_OBJECT_0 + 0:
- self->last_tid = GetCurrentThreadId();
- ++self->count;
- Py_RETURN_TRUE;
- case WAIT_OBJECT_0 + 1:
- errno = EINTR;
- return PyErr_SetFromErrno(PyExc_OSError);
- case WAIT_FAILED:
- return PyErr_SetFromWindowsErr(0);
- default:
- PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
- "WaitForMultipleObjects() gave unrecognized "
- "value %u", res);
- return NULL;
- }
- }
- /*[clinic input]
- _multiprocessing.SemLock.release
- Release the semaphore/lock.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock_release_impl(SemLockObject *self)
- /*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/
- {
- if (self->kind == RECURSIVE_MUTEX) {
- if (!ISMINE(self)) {
- PyErr_SetString(PyExc_AssertionError, "attempt to "
- "release recursive lock not owned "
- "by thread");
- return NULL;
- }
- if (self->count > 1) {
- --self->count;
- Py_RETURN_NONE;
- }
- assert(self->count == 1);
- }
- if (!ReleaseSemaphore(self->handle, 1, NULL)) {
- if (GetLastError() == ERROR_TOO_MANY_POSTS) {
- PyErr_SetString(PyExc_ValueError, "semaphore or lock "
- "released too many times");
- return NULL;
- } else {
- return PyErr_SetFromWindowsErr(0);
- }
- }
- --self->count;
- Py_RETURN_NONE;
- }
- #else /* !MS_WINDOWS */
- /*
- * Unix definitions
- */
- #define SEM_CLEAR_ERROR()
- #define SEM_GET_LAST_ERROR() 0
- #define SEM_CREATE(name, val, max) sem_open(name, O_CREAT | O_EXCL, 0600, val)
- #define SEM_CLOSE(sem) sem_close(sem)
- #define SEM_GETVALUE(sem, pval) sem_getvalue(sem, pval)
- #define SEM_UNLINK(name) sem_unlink(name)
- /* OS X 10.4 defines SEM_FAILED as -1 instead of (sem_t *)-1; this gives
- compiler warnings, and (potentially) undefined behaviour. */
- #ifdef __APPLE__
- # undef SEM_FAILED
- # define SEM_FAILED ((sem_t *)-1)
- #endif
- #ifndef HAVE_SEM_UNLINK
- # define sem_unlink(name) 0
- #endif
- #ifndef HAVE_SEM_TIMEDWAIT
- # define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save)
- static int
- sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
- {
- int res;
- unsigned long delay, difference;
- struct timeval now, tvdeadline, tvdelay;
- errno = 0;
- tvdeadline.tv_sec = deadline->tv_sec;
- tvdeadline.tv_usec = deadline->tv_nsec / 1000;
- for (delay = 0 ; ; delay += 1000) {
- /* poll */
- if (sem_trywait(sem) == 0)
- return 0;
- else if (errno != EAGAIN)
- return MP_STANDARD_ERROR;
- /* get current time */
- if (gettimeofday(&now, NULL) < 0)
- return MP_STANDARD_ERROR;
- /* check for timeout */
- if (tvdeadline.tv_sec < now.tv_sec ||
- (tvdeadline.tv_sec == now.tv_sec &&
- tvdeadline.tv_usec <= now.tv_usec)) {
- errno = ETIMEDOUT;
- return MP_STANDARD_ERROR;
- }
- /* calculate how much time is left */
- difference = (tvdeadline.tv_sec - now.tv_sec) * 1000000 +
- (tvdeadline.tv_usec - now.tv_usec);
- /* check delay not too long -- maximum is 20 msecs */
- if (delay > 20000)
- delay = 20000;
- if (delay > difference)
- delay = difference;
- /* sleep */
- tvdelay.tv_sec = delay / 1000000;
- tvdelay.tv_usec = delay % 1000000;
- if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
- return MP_STANDARD_ERROR;
- /* check for signals */
- Py_BLOCK_THREADS
- res = PyErr_CheckSignals();
- Py_UNBLOCK_THREADS
- if (res) {
- errno = EINTR;
- return MP_EXCEPTION_HAS_BEEN_SET;
- }
- }
- }
- #endif /* !HAVE_SEM_TIMEDWAIT */
- /*[clinic input]
- _multiprocessing.SemLock.acquire
- block as blocking: bool = True
- timeout as timeout_obj: object = None
- Acquire the semaphore/lock.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
- PyObject *timeout_obj)
- /*[clinic end generated code: output=f9998f0b6b0b0872 input=e5b45f5cbb775166]*/
- {
- int res, err = 0;
- struct timespec deadline = {0};
- if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
- ++self->count;
- Py_RETURN_TRUE;
- }
- int use_deadline = (timeout_obj != Py_None);
- if (use_deadline) {
- double timeout = PyFloat_AsDouble(timeout_obj);
- if (PyErr_Occurred()) {
- return NULL;
- }
- if (timeout < 0.0) {
- timeout = 0.0;
- }
- struct timeval now;
- if (gettimeofday(&now, NULL) < 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- long sec = (long) timeout;
- long nsec = (long) (1e9 * (timeout - sec) + 0.5);
- deadline.tv_sec = now.tv_sec + sec;
- deadline.tv_nsec = now.tv_usec * 1000 + nsec;
- deadline.tv_sec += (deadline.tv_nsec / 1000000000);
- deadline.tv_nsec %= 1000000000;
- }
- /* Check whether we can acquire without releasing the GIL and blocking */
- do {
- res = sem_trywait(self->handle);
- err = errno;
- } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
- errno = err;
- if (res < 0 && errno == EAGAIN && blocking) {
- /* Couldn't acquire immediately, need to block */
- do {
- Py_BEGIN_ALLOW_THREADS
- if (!use_deadline) {
- res = sem_wait(self->handle);
- }
- else {
- res = sem_timedwait(self->handle, &deadline);
- }
- Py_END_ALLOW_THREADS
- err = errno;
- if (res == MP_EXCEPTION_HAS_BEEN_SET)
- break;
- } while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
- }
- if (res < 0) {
- errno = err;
- if (errno == EAGAIN || errno == ETIMEDOUT)
- Py_RETURN_FALSE;
- else if (errno == EINTR)
- return NULL;
- else
- return PyErr_SetFromErrno(PyExc_OSError);
- }
- ++self->count;
- self->last_tid = PyThread_get_thread_ident();
- Py_RETURN_TRUE;
- }
- /*[clinic input]
- _multiprocessing.SemLock.release
- Release the semaphore/lock.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock_release_impl(SemLockObject *self)
- /*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/
- {
- if (self->kind == RECURSIVE_MUTEX) {
- if (!ISMINE(self)) {
- PyErr_SetString(PyExc_AssertionError, "attempt to "
- "release recursive lock not owned "
- "by thread");
- return NULL;
- }
- if (self->count > 1) {
- --self->count;
- Py_RETURN_NONE;
- }
- assert(self->count == 1);
- } else {
- #ifdef HAVE_BROKEN_SEM_GETVALUE
- /* We will only check properly the maxvalue == 1 case */
- if (self->maxvalue == 1) {
- /* make sure that already locked */
- if (sem_trywait(self->handle) < 0) {
- if (errno != EAGAIN) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- /* it is already locked as expected */
- } else {
- /* it was not locked so undo wait and raise */
- if (sem_post(self->handle) < 0) {
- PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
- }
- PyErr_SetString(PyExc_ValueError, "semaphore "
- "or lock released too many "
- "times");
- return NULL;
- }
- }
- #else
- int sval;
- /* This check is not an absolute guarantee that the semaphore
- does not rise above maxvalue. */
- if (sem_getvalue(self->handle, &sval) < 0) {
- return PyErr_SetFromErrno(PyExc_OSError);
- } else if (sval >= self->maxvalue) {
- PyErr_SetString(PyExc_ValueError, "semaphore or lock "
- "released too many times");
- return NULL;
- }
- #endif
- }
- if (sem_post(self->handle) < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
- --self->count;
- Py_RETURN_NONE;
- }
- #endif /* !MS_WINDOWS */
- /*
- * All platforms
- */
- static PyObject *
- newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
- char *name)
- {
- SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0);
- if (!self)
- return NULL;
- self->handle = handle;
- self->kind = kind;
- self->count = 0;
- self->last_tid = 0;
- self->maxvalue = maxvalue;
- self->name = name;
- return (PyObject*)self;
- }
- /*[clinic input]
- @classmethod
- _multiprocessing.SemLock.__new__
- kind: int
- value: int
- maxvalue: int
- name: str
- unlink: bool
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value,
- int maxvalue, const char *name, int unlink)
- /*[clinic end generated code: output=30727e38f5f7577a input=fdaeb69814471c5b]*/
- {
- SEM_HANDLE handle = SEM_FAILED;
- PyObject *result;
- char *name_copy = NULL;
- if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
- PyErr_SetString(PyExc_ValueError, "unrecognized kind");
- return NULL;
- }
- if (!unlink) {
- name_copy = PyMem_Malloc(strlen(name) + 1);
- if (name_copy == NULL) {
- return PyErr_NoMemory();
- }
- strcpy(name_copy, name);
- }
- SEM_CLEAR_ERROR();
- handle = SEM_CREATE(name, value, maxvalue);
- /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
- if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
- goto failure;
- if (unlink && SEM_UNLINK(name) < 0)
- goto failure;
- result = newsemlockobject(type, handle, kind, maxvalue, name_copy);
- if (!result)
- goto failure;
- return result;
- failure:
- if (!PyErr_Occurred()) {
- _PyMp_SetError(NULL, MP_STANDARD_ERROR);
- }
- if (handle != SEM_FAILED)
- SEM_CLOSE(handle);
- PyMem_Free(name_copy);
- return NULL;
- }
- /*[clinic input]
- @classmethod
- _multiprocessing.SemLock._rebuild
- handle: SEM_HANDLE
- kind: int
- maxvalue: int
- name: str(accept={str, NoneType})
- /
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
- int kind, int maxvalue,
- const char *name)
- /*[clinic end generated code: output=2aaee14f063f3bd9 input=f7040492ac6d9962]*/
- {
- char *name_copy = NULL;
- if (name != NULL) {
- name_copy = PyMem_Malloc(strlen(name) + 1);
- if (name_copy == NULL)
- return PyErr_NoMemory();
- strcpy(name_copy, name);
- }
- #ifndef MS_WINDOWS
- if (name != NULL) {
- handle = sem_open(name, 0);
- if (handle == SEM_FAILED) {
- PyErr_SetFromErrno(PyExc_OSError);
- PyMem_Free(name_copy);
- return NULL;
- }
- }
- #endif
- return newsemlockobject(type, handle, kind, maxvalue, name_copy);
- }
- static void
- semlock_dealloc(SemLockObject* self)
- {
- PyTypeObject *tp = Py_TYPE(self);
- PyObject_GC_UnTrack(self);
- if (self->handle != SEM_FAILED)
- SEM_CLOSE(self->handle);
- PyMem_Free(self->name);
- tp->tp_free(self);
- Py_DECREF(tp);
- }
- /*[clinic input]
- _multiprocessing.SemLock._count
- Num of `acquire()`s minus num of `release()`s for this process.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock__count_impl(SemLockObject *self)
- /*[clinic end generated code: output=5ba8213900e517bb input=36fc59b1cd1025ab]*/
- {
- return PyLong_FromLong((long)self->count);
- }
- /*[clinic input]
- _multiprocessing.SemLock._is_mine
- Whether the lock is owned by this thread.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock__is_mine_impl(SemLockObject *self)
- /*[clinic end generated code: output=92dc98863f4303be input=a96664cb2f0093ba]*/
- {
- /* only makes sense for a lock */
- return PyBool_FromLong(ISMINE(self));
- }
- /*[clinic input]
- _multiprocessing.SemLock._get_value
- Get the value of the semaphore.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock__get_value_impl(SemLockObject *self)
- /*[clinic end generated code: output=64bc1b89bda05e36 input=cb10f9a769836203]*/
- {
- #ifdef HAVE_BROKEN_SEM_GETVALUE
- PyErr_SetNone(PyExc_NotImplementedError);
- return NULL;
- #else
- int sval;
- if (SEM_GETVALUE(self->handle, &sval) < 0)
- return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
- /* some posix implementations use negative numbers to indicate
- the number of waiting threads */
- if (sval < 0)
- sval = 0;
- return PyLong_FromLong((long)sval);
- #endif
- }
- /*[clinic input]
- _multiprocessing.SemLock._is_zero
- Return whether semaphore has value zero.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock__is_zero_impl(SemLockObject *self)
- /*[clinic end generated code: output=815d4c878c806ed7 input=294a446418d31347]*/
- {
- #ifdef HAVE_BROKEN_SEM_GETVALUE
- if (sem_trywait(self->handle) < 0) {
- if (errno == EAGAIN)
- Py_RETURN_TRUE;
- return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
- } else {
- if (sem_post(self->handle) < 0)
- return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
- Py_RETURN_FALSE;
- }
- #else
- int sval;
- if (SEM_GETVALUE(self->handle, &sval) < 0)
- return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
- return PyBool_FromLong((long)sval == 0);
- #endif
- }
- /*[clinic input]
- _multiprocessing.SemLock._after_fork
- Rezero the net acquisition count after fork().
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock__after_fork_impl(SemLockObject *self)
- /*[clinic end generated code: output=718bb27914c6a6c1 input=190991008a76621e]*/
- {
- self->count = 0;
- Py_RETURN_NONE;
- }
- /*[clinic input]
- _multiprocessing.SemLock.__enter__
- Enter the semaphore/lock.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock___enter___impl(SemLockObject *self)
- /*[clinic end generated code: output=beeb2f07c858511f input=c5e27d594284690b]*/
- {
- return _multiprocessing_SemLock_acquire_impl(self, 1, Py_None);
- }
- /*[clinic input]
- _multiprocessing.SemLock.__exit__
- exc_type: object = None
- exc_value: object = None
- exc_tb: object = None
- /
- Exit the semaphore/lock.
- [clinic start generated code]*/
- static PyObject *
- _multiprocessing_SemLock___exit___impl(SemLockObject *self,
- PyObject *exc_type,
- PyObject *exc_value, PyObject *exc_tb)
- /*[clinic end generated code: output=3b37c1a9f8b91a03 input=7d644b64a89903f8]*/
- {
- return _multiprocessing_SemLock_release_impl(self);
- }
- static int
- semlock_traverse(SemLockObject *s, visitproc visit, void *arg)
- {
- Py_VISIT(Py_TYPE(s));
- return 0;
- }
- /*
- * Semaphore methods
- */
- static PyMethodDef semlock_methods[] = {
- _MULTIPROCESSING_SEMLOCK_ACQUIRE_METHODDEF
- _MULTIPROCESSING_SEMLOCK_RELEASE_METHODDEF
- _MULTIPROCESSING_SEMLOCK___ENTER___METHODDEF
- _MULTIPROCESSING_SEMLOCK___EXIT___METHODDEF
- _MULTIPROCESSING_SEMLOCK__COUNT_METHODDEF
- _MULTIPROCESSING_SEMLOCK__IS_MINE_METHODDEF
- _MULTIPROCESSING_SEMLOCK__GET_VALUE_METHODDEF
- _MULTIPROCESSING_SEMLOCK__IS_ZERO_METHODDEF
- _MULTIPROCESSING_SEMLOCK__REBUILD_METHODDEF
- _MULTIPROCESSING_SEMLOCK__AFTER_FORK_METHODDEF
- {NULL}
- };
- /*
- * Member table
- */
- static PyMemberDef semlock_members[] = {
- {"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY,
- ""},
- {"kind", T_INT, offsetof(SemLockObject, kind), READONLY,
- ""},
- {"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY,
- ""},
- {"name", T_STRING, offsetof(SemLockObject, name), READONLY,
- ""},
- {NULL}
- };
- /*
- * Semaphore type
- */
- static PyType_Slot _PyMp_SemLockType_slots[] = {
- {Py_tp_dealloc, semlock_dealloc},
- {Py_tp_getattro, PyObject_GenericGetAttr},
- {Py_tp_setattro, PyObject_GenericSetAttr},
- {Py_tp_methods, semlock_methods},
- {Py_tp_members, semlock_members},
- {Py_tp_alloc, PyType_GenericAlloc},
- {Py_tp_new, _multiprocessing_SemLock},
- {Py_tp_traverse, semlock_traverse},
- {Py_tp_free, PyObject_GC_Del},
- {Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")},
- {0, 0},
- };
- PyType_Spec _PyMp_SemLockType_spec = {
- .name = "_multiprocessing.SemLock",
- .basicsize = sizeof(SemLockObject),
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
- Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
- .slots = _PyMp_SemLockType_slots,
- };
- /*
- * Function to unlink semaphore names
- */
- PyObject *
- _PyMp_sem_unlink(const char *name)
- {
- if (SEM_UNLINK(name) < 0) {
- _PyMp_SetError(NULL, MP_STANDARD_ERROR);
- return NULL;
- }
- Py_RETURN_NONE;
- }
- #endif // HAVE_MP_SEMAPHORE
|