2008-06-11 13:44:04 -03:00
|
|
|
/*
|
|
|
|
* A type which wraps a semaphore
|
|
|
|
*
|
|
|
|
* semaphore.c
|
|
|
|
*
|
2012-04-30 08:13:55 -03:00
|
|
|
* Copyright (c) 2006-2008, R Oudkerk
|
|
|
|
* Licensed to PSF under a Contributor Agreement.
|
2008-06-11 13:44:04 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "multiprocessing.h"
|
|
|
|
|
2023-09-02 12:51:19 -03:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h> // gettimeofday()
|
|
|
|
#endif
|
|
|
|
|
2021-11-29 05:36:10 -04:00
|
|
|
#ifdef HAVE_MP_SEMAPHORE
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
enum { RECURSIVE_MUTEX, SEMAPHORE };
|
|
|
|
|
|
|
|
typedef struct {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
SEM_HANDLE handle;
|
2017-03-23 10:48:39 -03:00
|
|
|
unsigned long last_tid;
|
2010-05-09 12:52:27 -03:00
|
|
|
int count;
|
|
|
|
int maxvalue;
|
|
|
|
int kind;
|
2013-08-14 11:35:41 -03:00
|
|
|
char *name;
|
2008-06-11 13:44:04 -03:00
|
|
|
} SemLockObject;
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[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"
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
#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)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
long previous;
|
|
|
|
|
2013-01-25 09:25:48 -04:00
|
|
|
switch (WaitForSingleObjectEx(handle, 0, FALSE)) {
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock.acquire
|
|
|
|
|
2022-12-03 15:52:21 -04:00
|
|
|
block as blocking: bool = True
|
2020-07-12 13:11:11 -03:00
|
|
|
timeout as timeout_obj: object = None
|
|
|
|
|
|
|
|
Acquire the semaphore/lock.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
|
|
|
|
PyObject *timeout_obj)
|
2022-12-03 15:52:21 -04:00
|
|
|
/*[clinic end generated code: output=f9998f0b6b0b0872 input=e5b45f5cbb775166]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
double timeout;
|
2011-11-21 16:26:56 -04:00
|
|
|
DWORD res, full_msecs, nhandles;
|
|
|
|
HANDLE handles[2], sigint_event;
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-11-21 16:26:56 -04:00
|
|
|
/* check whether we can acquire without releasing the GIL and blocking */
|
2013-01-25 09:25:48 -04:00
|
|
|
if (WaitForSingleObjectEx(self->handle, 0, FALSE) == WAIT_OBJECT_0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
self->last_tid = GetCurrentThreadId();
|
|
|
|
++self->count;
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-11-21 16:26:56 -04:00
|
|
|
/* 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;
|
|
|
|
}
|
2016-07-23 01:13:14 -03:00
|
|
|
else {
|
|
|
|
sigint_event = NULL;
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2011-11-21 16:26:56 -04:00
|
|
|
/* do the wait */
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
if (sigint_event != NULL)
|
2010-05-09 12:52:27 -03:00
|
|
|
ResetEvent(sigint_event);
|
2013-01-25 09:29:13 -04:00
|
|
|
res = WaitForMultipleObjectsEx(nhandles, handles, FALSE, full_msecs, FALSE);
|
2011-11-21 16:26:56 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
/* handle result */
|
|
|
|
switch (res) {
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
Py_RETURN_FALSE;
|
2011-11-21 16:26:56 -04:00
|
|
|
case WAIT_OBJECT_0 + 0:
|
2010-05-09 12:52:27 -03:00
|
|
|
self->last_tid = GetCurrentThreadId();
|
|
|
|
++self->count;
|
|
|
|
Py_RETURN_TRUE;
|
2011-11-21 16:26:56 -04:00
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
errno = EINTR;
|
2017-04-16 04:46:38 -03:00
|
|
|
return PyErr_SetFromErrno(PyExc_OSError);
|
2010-05-09 12:52:27 -03:00
|
|
|
case WAIT_FAILED:
|
|
|
|
return PyErr_SetFromWindowsErr(0);
|
|
|
|
default:
|
|
|
|
PyErr_Format(PyExc_RuntimeError, "WaitForSingleObject() or "
|
|
|
|
"WaitForMultipleObjects() gave unrecognized "
|
2019-03-13 17:59:55 -03:00
|
|
|
"value %u", res);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock.release
|
|
|
|
|
|
|
|
Release the semaphore/lock.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock_release_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2013-01-01 13:29:44 -04:00
|
|
|
/* 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
|
|
|
|
|
Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
Issue 5619: Pass MS CRT debug flags into subprocesses
........
r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
Fix multiprocessing.event to match the new threading.Event API
........
r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
2009-04-05 18:24:58 -03:00
|
|
|
#ifndef HAVE_SEM_UNLINK
|
2008-06-11 13:44:04 -03:00
|
|
|
# define sem_unlink(name) 0
|
|
|
|
#endif
|
|
|
|
|
Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
Issue 5619: Pass MS CRT debug flags into subprocesses
........
r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
Fix multiprocessing.event to match the new threading.Event API
........
r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
2009-04-05 18:24:58 -03:00
|
|
|
#ifndef HAVE_SEM_TIMEDWAIT
|
2008-06-11 13:44:04 -03:00
|
|
|
# define sem_timedwait(sem,deadline) sem_timedwait_save(sem,deadline,_save)
|
|
|
|
|
2012-10-07 14:08:47 -03:00
|
|
|
static int
|
2008-06-11 13:44:04 -03:00
|
|
|
sem_timedwait_save(sem_t *sem, struct timespec *deadline, PyThreadState *_save)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !HAVE_SEM_TIMEDWAIT */
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock.acquire
|
|
|
|
|
2022-12-03 15:52:21 -04:00
|
|
|
block as blocking: bool = True
|
2020-07-12 13:11:11 -03:00
|
|
|
timeout as timeout_obj: object = None
|
|
|
|
|
|
|
|
Acquire the semaphore/lock.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock_acquire_impl(SemLockObject *self, int blocking,
|
|
|
|
PyObject *timeout_obj)
|
2022-12-03 15:52:21 -04:00
|
|
|
/*[clinic end generated code: output=f9998f0b6b0b0872 input=e5b45f5cbb775166]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2020-07-12 13:11:11 -03:00
|
|
|
int res, err = 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
struct timespec deadline = {0};
|
|
|
|
|
|
|
|
if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
|
|
|
|
++self->count;
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-31 20:25:59 -04:00
|
|
|
int use_deadline = (timeout_obj != Py_None);
|
|
|
|
if (use_deadline) {
|
|
|
|
double timeout = PyFloat_AsDouble(timeout_obj);
|
|
|
|
if (PyErr_Occurred()) {
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2020-01-31 20:25:59 -04:00
|
|
|
}
|
|
|
|
if (timeout < 0.0) {
|
2010-05-09 12:52:27 -03:00
|
|
|
timeout = 0.0;
|
2020-01-31 20:25:59 -04:00
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
2020-01-31 20:25:59 -04:00
|
|
|
struct timeval now;
|
2010-05-09 12:52:27 -03:00
|
|
|
if (gettimeofday(&now, NULL) < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-01-31 20:25:59 -04:00
|
|
|
long sec = (long) timeout;
|
|
|
|
long nsec = (long) (1e9 * (timeout - sec) + 0.5);
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-10-22 08:10:46 -03:00
|
|
|
/* Check whether we can acquire without releasing the GIL and blocking */
|
2010-05-09 12:52:27 -03:00
|
|
|
do {
|
2017-10-22 08:10:46 -03:00
|
|
|
res = sem_trywait(self->handle);
|
2011-12-16 07:28:32 -04:00
|
|
|
err = errno;
|
2010-05-09 12:52:27 -03:00
|
|
|
} while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
|
2017-10-22 08:10:46 -03:00
|
|
|
errno = err;
|
|
|
|
|
|
|
|
if (res < 0 && errno == EAGAIN && blocking) {
|
|
|
|
/* Couldn't acquire immediately, need to block */
|
|
|
|
do {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2020-01-31 20:25:59 -04:00
|
|
|
if (!use_deadline) {
|
2017-10-22 08:10:46 -03:00
|
|
|
res = sem_wait(self->handle);
|
2017-10-23 17:57:51 -03:00
|
|
|
}
|
|
|
|
else {
|
2017-10-22 08:10:46 -03:00
|
|
|
res = sem_timedwait(self->handle, &deadline);
|
2017-10-23 17:57:51 -03:00
|
|
|
}
|
2017-10-22 08:10:46 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
err = errno;
|
|
|
|
if (res == MP_EXCEPTION_HAS_BEEN_SET)
|
|
|
|
break;
|
|
|
|
} while (res < 0 && errno == EINTR && !PyErr_CheckSignals());
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
|
|
|
|
if (res < 0) {
|
2011-12-16 07:28:32 -04:00
|
|
|
errno = err;
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock.release
|
|
|
|
|
|
|
|
Release the semaphore/lock.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock_release_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=b22f53ba96b0d1db input=ba7e63a961885d3d]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
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 {
|
Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
Issue 5619: Pass MS CRT debug flags into subprocesses
........
r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
Fix multiprocessing.event to match the new threading.Event API
........
r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
2009-04-05 18:24:58 -03:00
|
|
|
#ifdef HAVE_BROKEN_SEM_GETVALUE
|
2010-05-09 12:52:27 -03:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
#else
|
2010-05-09 12:52:27 -03:00
|
|
|
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;
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
#endif
|
2010-05-09 12:52:27 -03:00
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (sem_post(self->handle) < 0)
|
|
|
|
return PyErr_SetFromErrno(PyExc_OSError);
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
--self->count;
|
|
|
|
Py_RETURN_NONE;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !MS_WINDOWS */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All platforms
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyObject *
|
2013-08-14 11:35:41 -03:00
|
|
|
newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
|
|
|
|
char *name)
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2022-07-11 09:12:36 -03:00
|
|
|
SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!self)
|
|
|
|
return NULL;
|
|
|
|
self->handle = handle;
|
|
|
|
self->kind = kind;
|
|
|
|
self->count = 0;
|
|
|
|
self->last_tid = 0;
|
|
|
|
self->maxvalue = maxvalue;
|
2013-08-14 11:35:41 -03:00
|
|
|
self->name = name;
|
2010-05-09 12:52:27 -03:00
|
|
|
return (PyObject*)self;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
_multiprocessing.SemLock.__new__
|
|
|
|
|
|
|
|
kind: int
|
|
|
|
value: int
|
|
|
|
maxvalue: int
|
|
|
|
name: str
|
2022-12-03 15:52:21 -04:00
|
|
|
unlink: bool
|
2020-07-12 13:11:11 -03:00
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock_impl(PyTypeObject *type, int kind, int value,
|
|
|
|
int maxvalue, const char *name, int unlink)
|
2022-12-03 15:52:21 -04:00
|
|
|
/*[clinic end generated code: output=30727e38f5f7577a input=fdaeb69814471c5b]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
SEM_HANDLE handle = SEM_FAILED;
|
|
|
|
PyObject *result;
|
2020-07-12 13:11:11 -03:00
|
|
|
char *name_copy = NULL;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
if (kind != RECURSIVE_MUTEX && kind != SEMAPHORE) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unrecognized kind");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
if (!unlink) {
|
|
|
|
name_copy = PyMem_Malloc(strlen(name) + 1);
|
2018-12-07 06:11:30 -04:00
|
|
|
if (name_copy == NULL) {
|
|
|
|
return PyErr_NoMemory();
|
|
|
|
}
|
2013-08-14 11:35:41 -03:00
|
|
|
strcpy(name_copy, name);
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
SEM_CLEAR_ERROR();
|
2013-08-14 11:35:41 -03:00
|
|
|
handle = SEM_CREATE(name, value, maxvalue);
|
2010-05-09 12:52:27 -03:00
|
|
|
/* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
|
|
|
|
if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
|
|
|
|
goto failure;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
if (unlink && SEM_UNLINK(name) < 0)
|
2010-05-09 12:52:27 -03:00
|
|
|
goto failure;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
result = newsemlockobject(type, handle, kind, maxvalue, name_copy);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (!result)
|
|
|
|
goto failure;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2010-05-09 12:52:27 -03:00
|
|
|
return result;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
|
|
|
failure:
|
2018-12-07 06:11:30 -04:00
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
|
|
|
}
|
2023-08-26 18:35:06 -03:00
|
|
|
if (handle != SEM_FAILED)
|
|
|
|
SEM_CLOSE(handle);
|
|
|
|
PyMem_Free(name_copy);
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
@classmethod
|
|
|
|
_multiprocessing.SemLock._rebuild
|
|
|
|
|
|
|
|
handle: SEM_HANDLE
|
|
|
|
kind: int
|
|
|
|
maxvalue: int
|
|
|
|
name: str(accept={str, NoneType})
|
|
|
|
/
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
|
|
|
|
int kind, int maxvalue,
|
|
|
|
const char *name)
|
|
|
|
/*[clinic end generated code: output=2aaee14f063f3bd9 input=f7040492ac6d9962]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2020-07-12 13:11:11 -03:00
|
|
|
char *name_copy = NULL;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
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) {
|
2023-08-26 18:35:06 -03:00
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
2013-08-14 11:35:41 -03:00
|
|
|
PyMem_Free(name_copy);
|
2023-08-26 18:35:06 -03:00
|
|
|
return NULL;
|
2013-08-14 11:35:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return newsemlockobject(type, handle, kind, maxvalue, name_copy);
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
semlock_dealloc(SemLockObject* self)
|
|
|
|
{
|
2022-07-20 16:26:01 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
|
|
|
PyObject_GC_UnTrack(self);
|
2010-05-09 12:52:27 -03:00
|
|
|
if (self->handle != SEM_FAILED)
|
|
|
|
SEM_CLOSE(self->handle);
|
2013-08-14 11:35:41 -03:00
|
|
|
PyMem_Free(self->name);
|
2022-07-20 16:26:01 -03:00
|
|
|
tp->tp_free(self);
|
|
|
|
Py_DECREF(tp);
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock._count
|
|
|
|
|
|
|
|
Num of `acquire()`s minus num of `release()`s for this process.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock__count_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=5ba8213900e517bb input=36fc59b1cd1025ab]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2011-05-09 15:55:03 -03:00
|
|
|
return PyLong_FromLong((long)self->count);
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock._is_mine
|
|
|
|
|
|
|
|
Whether the lock is owned by this thread.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock__is_mine_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=92dc98863f4303be input=a96664cb2f0093ba]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
/* only makes sense for a lock */
|
|
|
|
return PyBool_FromLong(ISMINE(self));
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock._get_value
|
|
|
|
|
|
|
|
Get the value of the semaphore.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock__get_value_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=64bc1b89bda05e36 input=cb10f9a769836203]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
Issue 5619: Pass MS CRT debug flags into subprocesses
........
r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
Fix multiprocessing.event to match the new threading.Event API
........
r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
2009-04-05 18:24:58 -03:00
|
|
|
#ifdef HAVE_BROKEN_SEM_GETVALUE
|
2010-05-09 12:52:27 -03:00
|
|
|
PyErr_SetNone(PyExc_NotImplementedError);
|
|
|
|
return NULL;
|
2008-06-11 13:44:04 -03:00
|
|
|
#else
|
2010-05-09 12:52:27 -03:00
|
|
|
int sval;
|
|
|
|
if (SEM_GETVALUE(self->handle, &sval) < 0)
|
2012-10-07 14:08:47 -03:00
|
|
|
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
2010-05-09 12:52:27 -03:00
|
|
|
/* some posix implementations use negative numbers to indicate
|
|
|
|
the number of waiting threads */
|
|
|
|
if (sval < 0)
|
|
|
|
sval = 0;
|
2011-05-09 15:55:03 -03:00
|
|
|
return PyLong_FromLong((long)sval);
|
2008-06-11 13:44:04 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock._is_zero
|
|
|
|
|
|
|
|
Return whether semaphore has value zero.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock__is_zero_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=815d4c878c806ed7 input=294a446418d31347]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
Merged revisions 70908,70939,71009,71022,71036 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r70908 | jesse.noller | 2009-03-31 17:20:35 -0500 (Tue, 31 Mar 2009) | 1 line
Issue 5619: Pass MS CRT debug flags into subprocesses
........
r70939 | jesse.noller | 2009-03-31 22:45:50 -0500 (Tue, 31 Mar 2009) | 1 line
Fix multiprocessing.event to match the new threading.Event API
........
r71009 | jesse.noller | 2009-04-01 19:03:28 -0500 (Wed, 01 Apr 2009) | 1 line
issue5545: Switch to Autoconf for multiprocessing; special thanks to Martin Lowis for help
........
r71022 | jesse.noller | 2009-04-01 21:32:55 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3110: Additional protection for SEM_VALUE_MAX on platforms, thanks to Martin Loewis
........
r71036 | jesse.noller | 2009-04-01 23:22:09 -0500 (Wed, 01 Apr 2009) | 1 line
Issue 3551: Raise ValueError if the size causes ERROR_NO_SYSTEM_RESOURCES
........
2009-04-05 18:24:58 -03:00
|
|
|
#ifdef HAVE_BROKEN_SEM_GETVALUE
|
2010-05-09 12:52:27 -03:00
|
|
|
if (sem_trywait(self->handle) < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
Py_RETURN_TRUE;
|
2012-10-07 14:08:47 -03:00
|
|
|
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
2010-05-09 12:52:27 -03:00
|
|
|
} else {
|
|
|
|
if (sem_post(self->handle) < 0)
|
2012-10-07 14:08:47 -03:00
|
|
|
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
2010-05-09 12:52:27 -03:00
|
|
|
Py_RETURN_FALSE;
|
|
|
|
}
|
2008-06-11 13:44:04 -03:00
|
|
|
#else
|
2010-05-09 12:52:27 -03:00
|
|
|
int sval;
|
|
|
|
if (SEM_GETVALUE(self->handle, &sval) < 0)
|
2012-10-07 14:08:47 -03:00
|
|
|
return _PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
2010-05-09 12:52:27 -03:00
|
|
|
return PyBool_FromLong((long)sval == 0);
|
2008-06-11 13:44:04 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_multiprocessing.SemLock._after_fork
|
|
|
|
|
|
|
|
Rezero the net acquisition count after fork().
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
static PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_multiprocessing_SemLock__after_fork_impl(SemLockObject *self)
|
|
|
|
/*[clinic end generated code: output=718bb27914c6a6c1 input=190991008a76621e]*/
|
2008-06-11 13:44:04 -03:00
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
self->count = 0;
|
|
|
|
Py_RETURN_NONE;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
2020-07-12 13:11:11 -03:00
|
|
|
/*[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);
|
|
|
|
}
|
|
|
|
|
2022-07-20 16:26:01 -03:00
|
|
|
static int
|
|
|
|
semlock_traverse(SemLockObject *s, visitproc visit, void *arg)
|
|
|
|
{
|
|
|
|
Py_VISIT(Py_TYPE(s));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-11 13:44:04 -03:00
|
|
|
/*
|
|
|
|
* Semaphore methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyMethodDef semlock_methods[] = {
|
2020-07-12 13:11:11 -03:00
|
|
|
_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
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL}
|
2008-06-11 13:44:04 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Member table
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyMemberDef semlock_members[] = {
|
2023-07-25 10:28:30 -03:00
|
|
|
{"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), Py_READONLY,
|
2010-05-09 12:52:27 -03:00
|
|
|
""},
|
2023-07-25 10:28:30 -03:00
|
|
|
{"kind", Py_T_INT, offsetof(SemLockObject, kind), Py_READONLY,
|
2010-05-09 12:52:27 -03:00
|
|
|
""},
|
2023-07-25 10:28:30 -03:00
|
|
|
{"maxvalue", Py_T_INT, offsetof(SemLockObject, maxvalue), Py_READONLY,
|
2010-05-09 12:52:27 -03:00
|
|
|
""},
|
2023-07-25 10:28:30 -03:00
|
|
|
{"name", Py_T_STRING, offsetof(SemLockObject, name), Py_READONLY,
|
2013-08-14 11:35:41 -03:00
|
|
|
""},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL}
|
2008-06-11 13:44:04 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Semaphore type
|
|
|
|
*/
|
|
|
|
|
2022-07-20 16:26:01 -03:00
|
|
|
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,
|
2008-06-11 13:44:04 -03:00
|
|
|
};
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Function to unlink semaphore names
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyObject *
|
2020-07-12 13:11:11 -03:00
|
|
|
_PyMp_sem_unlink(const char *name)
|
2013-08-14 11:35:41 -03:00
|
|
|
{
|
|
|
|
if (SEM_UNLINK(name) < 0) {
|
|
|
|
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
2021-11-29 05:36:10 -04:00
|
|
|
|
|
|
|
#endif // HAVE_MP_SEMAPHORE
|