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"
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
#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
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
int blocking = 1;
|
|
|
|
double timeout;
|
|
|
|
PyObject *timeout_obj = Py_None;
|
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
|
|
|
|
|
|
|
static char *kwlist[] = {"block", "timeout", NULL};
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
|
|
|
|
&blocking, &timeout_obj))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
semlock_release(SemLockObject *self, PyObject *args)
|
|
|
|
{
|
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 */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2011-12-16 07:28:32 -04:00
|
|
|
int blocking = 1, res, err = 0;
|
2010-05-09 12:52:27 -03:00
|
|
|
double timeout;
|
|
|
|
PyObject *timeout_obj = Py_None;
|
|
|
|
struct timespec deadline = {0};
|
|
|
|
struct timeval now;
|
|
|
|
long sec, nsec;
|
|
|
|
|
|
|
|
static char *kwlist[] = {"block", "timeout", NULL};
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|iO", kwlist,
|
|
|
|
&blocking, &timeout_obj))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (self->kind == RECURSIVE_MUTEX && ISMINE(self)) {
|
|
|
|
++self->count;
|
|
|
|
Py_RETURN_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout_obj != Py_None) {
|
|
|
|
timeout = PyFloat_AsDouble(timeout_obj);
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
if (timeout < 0.0)
|
|
|
|
timeout = 0.0;
|
|
|
|
|
|
|
|
if (gettimeofday(&now, NULL) < 0) {
|
|
|
|
PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sec = (long) timeout;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
2017-10-23 17:57:51 -03:00
|
|
|
if (timeout_obj == Py_None) {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
semlock_release(SemLockObject *self, PyObject *args)
|
|
|
|
{
|
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
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
SemLockObject *self;
|
|
|
|
|
|
|
|
self = PyObject_New(SemLockObject, type);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
SEM_HANDLE handle = SEM_FAILED;
|
2013-08-14 11:35:41 -03:00
|
|
|
int kind, maxvalue, value, unlink;
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject *result;
|
2013-08-14 11:35:41 -03:00
|
|
|
char *name, *name_copy = NULL;
|
|
|
|
static char *kwlist[] = {"kind", "value", "maxvalue", "name", "unlink",
|
|
|
|
NULL};
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiisi", kwlist,
|
|
|
|
&kind, &value, &maxvalue, &name, &unlink))
|
2010-05-09 12:52:27 -03:00
|
|
|
return 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:
|
2010-05-09 12:52:27 -03:00
|
|
|
if (handle != SEM_FAILED)
|
|
|
|
SEM_CLOSE(handle);
|
2013-08-14 11:35:41 -03:00
|
|
|
PyMem_Free(name_copy);
|
2018-12-07 06:11:30 -04:00
|
|
|
if (!PyErr_Occurred()) {
|
|
|
|
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
|
|
|
}
|
2010-05-09 12:52:27 -03:00
|
|
|
return NULL;
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
semlock_rebuild(PyTypeObject *type, PyObject *args)
|
|
|
|
{
|
2010-05-09 12:52:27 -03:00
|
|
|
SEM_HANDLE handle;
|
|
|
|
int kind, maxvalue;
|
2013-08-14 11:35:41 -03:00
|
|
|
char *name, *name_copy = NULL;
|
2008-06-11 13:44:04 -03:00
|
|
|
|
2013-08-14 11:35:41 -03:00
|
|
|
if (!PyArg_ParseTuple(args, F_SEM_HANDLE "iiz",
|
|
|
|
&handle, &kind, &maxvalue, &name))
|
2010-05-09 12:52:27 -03:00
|
|
|
return 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) {
|
|
|
|
PyMem_Free(name_copy);
|
|
|
|
return PyErr_SetFromErrno(PyExc_OSError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return newsemlockobject(type, handle, kind, maxvalue, name_copy);
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
semlock_dealloc(SemLockObject* 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);
|
2010-05-09 12:52:27 -03:00
|
|
|
PyObject_Del(self);
|
2008-06-11 13:44:04 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
semlock_count(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
semlock_ismine(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
semlock_getvalue(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
semlock_iszero(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2018-04-29 15:59:33 -03:00
|
|
|
semlock_afterfork(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Semaphore methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyMethodDef semlock_methods[] = {
|
2018-11-27 07:27:31 -04:00
|
|
|
{"acquire", (PyCFunction)(void(*)(void))semlock_acquire, METH_VARARGS | METH_KEYWORDS,
|
2010-05-09 12:52:27 -03:00
|
|
|
"acquire the semaphore/lock"},
|
|
|
|
{"release", (PyCFunction)semlock_release, METH_NOARGS,
|
|
|
|
"release the semaphore/lock"},
|
2018-11-27 07:27:31 -04:00
|
|
|
{"__enter__", (PyCFunction)(void(*)(void))semlock_acquire, METH_VARARGS | METH_KEYWORDS,
|
2010-05-09 12:52:27 -03:00
|
|
|
"enter the semaphore/lock"},
|
|
|
|
{"__exit__", (PyCFunction)semlock_release, METH_VARARGS,
|
|
|
|
"exit the semaphore/lock"},
|
|
|
|
{"_count", (PyCFunction)semlock_count, METH_NOARGS,
|
|
|
|
"num of `acquire()`s minus num of `release()`s for this process"},
|
|
|
|
{"_is_mine", (PyCFunction)semlock_ismine, METH_NOARGS,
|
|
|
|
"whether the lock is owned by this thread"},
|
|
|
|
{"_get_value", (PyCFunction)semlock_getvalue, METH_NOARGS,
|
|
|
|
"get the value of the semaphore"},
|
|
|
|
{"_is_zero", (PyCFunction)semlock_iszero, METH_NOARGS,
|
|
|
|
"returns whether semaphore has value zero"},
|
|
|
|
{"_rebuild", (PyCFunction)semlock_rebuild, METH_VARARGS | METH_CLASS,
|
|
|
|
""},
|
|
|
|
{"_after_fork", (PyCFunction)semlock_afterfork, METH_NOARGS,
|
|
|
|
"rezero the net acquisition count after fork()"},
|
|
|
|
{NULL}
|
2008-06-11 13:44:04 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Member table
|
|
|
|
*/
|
|
|
|
|
|
|
|
static PyMemberDef semlock_members[] = {
|
2010-05-09 12:52:27 -03:00
|
|
|
{"handle", T_SEM_HANDLE, offsetof(SemLockObject, handle), READONLY,
|
|
|
|
""},
|
|
|
|
{"kind", T_INT, offsetof(SemLockObject, kind), READONLY,
|
|
|
|
""},
|
|
|
|
{"maxvalue", T_INT, offsetof(SemLockObject, maxvalue), READONLY,
|
|
|
|
""},
|
2013-08-14 11:35:41 -03:00
|
|
|
{"name", T_STRING, offsetof(SemLockObject, name), READONLY,
|
|
|
|
""},
|
2010-05-09 12:52:27 -03:00
|
|
|
{NULL}
|
2008-06-11 13:44:04 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Semaphore type
|
|
|
|
*/
|
|
|
|
|
2012-10-07 14:08:47 -03:00
|
|
|
PyTypeObject _PyMp_SemLockType = {
|
2010-05-09 12:52:27 -03:00
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
/* tp_name */ "_multiprocessing.SemLock",
|
|
|
|
/* tp_basicsize */ sizeof(SemLockObject),
|
|
|
|
/* tp_itemsize */ 0,
|
|
|
|
/* tp_dealloc */ (destructor)semlock_dealloc,
|
2019-05-30 23:13:39 -03:00
|
|
|
/* tp_vectorcall_offset */ 0,
|
2010-05-09 12:52:27 -03:00
|
|
|
/* tp_getattr */ 0,
|
|
|
|
/* tp_setattr */ 0,
|
2019-05-30 23:13:39 -03:00
|
|
|
/* tp_as_async */ 0,
|
2010-05-09 12:52:27 -03:00
|
|
|
/* tp_repr */ 0,
|
|
|
|
/* tp_as_number */ 0,
|
|
|
|
/* tp_as_sequence */ 0,
|
|
|
|
/* tp_as_mapping */ 0,
|
|
|
|
/* tp_hash */ 0,
|
|
|
|
/* tp_call */ 0,
|
|
|
|
/* tp_str */ 0,
|
|
|
|
/* tp_getattro */ 0,
|
|
|
|
/* tp_setattro */ 0,
|
|
|
|
/* tp_as_buffer */ 0,
|
|
|
|
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
/* tp_doc */ "Semaphore/Mutex type",
|
|
|
|
/* tp_traverse */ 0,
|
|
|
|
/* tp_clear */ 0,
|
|
|
|
/* tp_richcompare */ 0,
|
|
|
|
/* tp_weaklistoffset */ 0,
|
|
|
|
/* tp_iter */ 0,
|
|
|
|
/* tp_iternext */ 0,
|
|
|
|
/* tp_methods */ semlock_methods,
|
|
|
|
/* tp_members */ semlock_members,
|
|
|
|
/* tp_getset */ 0,
|
|
|
|
/* tp_base */ 0,
|
|
|
|
/* tp_dict */ 0,
|
|
|
|
/* tp_descr_get */ 0,
|
|
|
|
/* tp_descr_set */ 0,
|
|
|
|
/* tp_dictoffset */ 0,
|
|
|
|
/* tp_init */ 0,
|
|
|
|
/* tp_alloc */ 0,
|
|
|
|
/* tp_new */ semlock_new,
|
2008-06-11 13:44:04 -03:00
|
|
|
};
|
2013-08-14 11:35:41 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Function to unlink semaphore names
|
|
|
|
*/
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
_PyMp_sem_unlink(PyObject *ignore, PyObject *args)
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "s", &name))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (SEM_UNLINK(name) < 0) {
|
|
|
|
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|