Save new folder
This commit is contained in:
@ -0,0 +1,133 @@
|
||||
/*[clinic input]
|
||||
preserve
|
||||
[clinic start generated code]*/
|
||||
|
||||
#if defined(HAVE_SHM_OPEN)
|
||||
|
||||
PyDoc_STRVAR(_posixshmem_shm_open__doc__,
|
||||
"shm_open($module, /, path, flags, mode=511)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Open a shared memory object. Returns a file descriptor (integer).");
|
||||
|
||||
#define _POSIXSHMEM_SHM_OPEN_METHODDEF \
|
||||
{"shm_open", (PyCFunction)(void(*)(void))_posixshmem_shm_open, METH_FASTCALL|METH_KEYWORDS, _posixshmem_shm_open__doc__},
|
||||
|
||||
static int
|
||||
_posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
|
||||
int mode);
|
||||
|
||||
static PyObject *
|
||||
_posixshmem_shm_open(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
static const char * const _keywords[] = {"path", "flags", "mode", NULL};
|
||||
static _PyArg_Parser _parser = {NULL, _keywords, "shm_open", 0};
|
||||
PyObject *argsbuf[3];
|
||||
Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
|
||||
PyObject *path;
|
||||
int flags;
|
||||
int mode = 511;
|
||||
int _return_value;
|
||||
|
||||
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
|
||||
if (!args) {
|
||||
goto exit;
|
||||
}
|
||||
if (!PyUnicode_Check(args[0])) {
|
||||
_PyArg_BadArgument("shm_open", "argument 'path'", "str", args[0]);
|
||||
goto exit;
|
||||
}
|
||||
if (PyUnicode_READY(args[0]) == -1) {
|
||||
goto exit;
|
||||
}
|
||||
path = args[0];
|
||||
if (PyFloat_Check(args[1])) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"integer argument expected, got float" );
|
||||
goto exit;
|
||||
}
|
||||
flags = _PyLong_AsInt(args[1]);
|
||||
if (flags == -1 && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
if (!noptargs) {
|
||||
goto skip_optional_pos;
|
||||
}
|
||||
if (PyFloat_Check(args[2])) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"integer argument expected, got float" );
|
||||
goto exit;
|
||||
}
|
||||
mode = _PyLong_AsInt(args[2]);
|
||||
if (mode == -1 && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
skip_optional_pos:
|
||||
_return_value = _posixshmem_shm_open_impl(module, path, flags, mode);
|
||||
if ((_return_value == -1) && PyErr_Occurred()) {
|
||||
goto exit;
|
||||
}
|
||||
return_value = PyLong_FromLong((long)_return_value);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
#endif /* defined(HAVE_SHM_OPEN) */
|
||||
|
||||
#if defined(HAVE_SHM_UNLINK)
|
||||
|
||||
PyDoc_STRVAR(_posixshmem_shm_unlink__doc__,
|
||||
"shm_unlink($module, /, path)\n"
|
||||
"--\n"
|
||||
"\n"
|
||||
"Remove a shared memory object (similar to unlink()).\n"
|
||||
"\n"
|
||||
"Remove a shared memory object name, and, once all processes have unmapped\n"
|
||||
"the object, de-allocates and destroys the contents of the associated memory\n"
|
||||
"region.");
|
||||
|
||||
#define _POSIXSHMEM_SHM_UNLINK_METHODDEF \
|
||||
{"shm_unlink", (PyCFunction)(void(*)(void))_posixshmem_shm_unlink, METH_FASTCALL|METH_KEYWORDS, _posixshmem_shm_unlink__doc__},
|
||||
|
||||
static PyObject *
|
||||
_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path);
|
||||
|
||||
static PyObject *
|
||||
_posixshmem_shm_unlink(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||
{
|
||||
PyObject *return_value = NULL;
|
||||
static const char * const _keywords[] = {"path", NULL};
|
||||
static _PyArg_Parser _parser = {NULL, _keywords, "shm_unlink", 0};
|
||||
PyObject *argsbuf[1];
|
||||
PyObject *path;
|
||||
|
||||
args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
|
||||
if (!args) {
|
||||
goto exit;
|
||||
}
|
||||
if (!PyUnicode_Check(args[0])) {
|
||||
_PyArg_BadArgument("shm_unlink", "argument 'path'", "str", args[0]);
|
||||
goto exit;
|
||||
}
|
||||
if (PyUnicode_READY(args[0]) == -1) {
|
||||
goto exit;
|
||||
}
|
||||
path = args[0];
|
||||
return_value = _posixshmem_shm_unlink_impl(module, path);
|
||||
|
||||
exit:
|
||||
return return_value;
|
||||
}
|
||||
|
||||
#endif /* defined(HAVE_SHM_UNLINK) */
|
||||
|
||||
#ifndef _POSIXSHMEM_SHM_OPEN_METHODDEF
|
||||
#define _POSIXSHMEM_SHM_OPEN_METHODDEF
|
||||
#endif /* !defined(_POSIXSHMEM_SHM_OPEN_METHODDEF) */
|
||||
|
||||
#ifndef _POSIXSHMEM_SHM_UNLINK_METHODDEF
|
||||
#define _POSIXSHMEM_SHM_UNLINK_METHODDEF
|
||||
#endif /* !defined(_POSIXSHMEM_SHM_UNLINK_METHODDEF) */
|
||||
/*[clinic end generated code: output=9132861c61d8c2d8 input=a9049054013a1b77]*/
|
||||
@ -0,0 +1,219 @@
|
||||
/*
|
||||
* Extension module used by multiprocessing package
|
||||
*
|
||||
* multiprocessing.c
|
||||
*
|
||||
* Copyright (c) 2006-2008, R Oudkerk
|
||||
* Licensed to PSF under a Contributor Agreement.
|
||||
*/
|
||||
|
||||
#include "multiprocessing.h"
|
||||
|
||||
|
||||
/*
|
||||
* Function which raises exceptions based on error codes
|
||||
*/
|
||||
|
||||
PyObject *
|
||||
_PyMp_SetError(PyObject *Type, int num)
|
||||
{
|
||||
switch (num) {
|
||||
#ifdef MS_WINDOWS
|
||||
case MP_STANDARD_ERROR:
|
||||
if (Type == NULL)
|
||||
Type = PyExc_OSError;
|
||||
PyErr_SetExcFromWindowsErr(Type, 0);
|
||||
break;
|
||||
case MP_SOCKET_ERROR:
|
||||
if (Type == NULL)
|
||||
Type = PyExc_OSError;
|
||||
PyErr_SetExcFromWindowsErr(Type, WSAGetLastError());
|
||||
break;
|
||||
#else /* !MS_WINDOWS */
|
||||
case MP_STANDARD_ERROR:
|
||||
case MP_SOCKET_ERROR:
|
||||
if (Type == NULL)
|
||||
Type = PyExc_OSError;
|
||||
PyErr_SetFromErrno(Type);
|
||||
break;
|
||||
#endif /* !MS_WINDOWS */
|
||||
case MP_MEMORY_ERROR:
|
||||
PyErr_NoMemory();
|
||||
break;
|
||||
case MP_EXCEPTION_HAS_BEEN_SET:
|
||||
break;
|
||||
default:
|
||||
PyErr_Format(PyExc_RuntimeError,
|
||||
"unknown error number %d", num);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
static PyObject *
|
||||
multiprocessing_closesocket(PyObject *self, PyObject *args)
|
||||
{
|
||||
HANDLE handle;
|
||||
int ret;
|
||||
|
||||
if (!PyArg_ParseTuple(args, F_HANDLE ":closesocket" , &handle))
|
||||
return NULL;
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
ret = closesocket((SOCKET) handle);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (ret)
|
||||
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
multiprocessing_recv(PyObject *self, PyObject *args)
|
||||
{
|
||||
HANDLE handle;
|
||||
int size, nread;
|
||||
PyObject *buf;
|
||||
|
||||
if (!PyArg_ParseTuple(args, F_HANDLE "i:recv" , &handle, &size))
|
||||
return NULL;
|
||||
|
||||
buf = PyBytes_FromStringAndSize(NULL, size);
|
||||
if (!buf)
|
||||
return NULL;
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
nread = recv((SOCKET) handle, PyBytes_AS_STRING(buf), size, 0);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
if (nread < 0) {
|
||||
Py_DECREF(buf);
|
||||
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
|
||||
}
|
||||
_PyBytes_Resize(&buf, nread);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
multiprocessing_send(PyObject *self, PyObject *args)
|
||||
{
|
||||
HANDLE handle;
|
||||
Py_buffer buf;
|
||||
int ret, length;
|
||||
|
||||
if (!PyArg_ParseTuple(args, F_HANDLE "y*:send" , &handle, &buf))
|
||||
return NULL;
|
||||
|
||||
length = (int)Py_MIN(buf.len, INT_MAX);
|
||||
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
ret = send((SOCKET) handle, buf.buf, length, 0);
|
||||
Py_END_ALLOW_THREADS
|
||||
|
||||
PyBuffer_Release(&buf);
|
||||
if (ret < 0)
|
||||
return PyErr_SetExcFromWindowsErr(PyExc_OSError, WSAGetLastError());
|
||||
return PyLong_FromLong(ret);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Function table
|
||||
*/
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
#ifdef MS_WINDOWS
|
||||
{"closesocket", multiprocessing_closesocket, METH_VARARGS, ""},
|
||||
{"recv", multiprocessing_recv, METH_VARARGS, ""},
|
||||
{"send", multiprocessing_send, METH_VARARGS, ""},
|
||||
#endif
|
||||
#if !defined(POSIX_SEMAPHORES_NOT_ENABLED) && !defined(__ANDROID__)
|
||||
{"sem_unlink", _PyMp_sem_unlink, METH_VARARGS, ""},
|
||||
#endif
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Initialize
|
||||
*/
|
||||
|
||||
static struct PyModuleDef multiprocessing_module = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"_multiprocessing",
|
||||
NULL,
|
||||
-1,
|
||||
module_methods,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL
|
||||
};
|
||||
|
||||
|
||||
PyMODINIT_FUNC
|
||||
PyInit__multiprocessing(void)
|
||||
{
|
||||
PyObject *module, *temp, *value = NULL;
|
||||
|
||||
/* Initialize module */
|
||||
module = PyModule_Create(&multiprocessing_module);
|
||||
if (!module)
|
||||
return NULL;
|
||||
|
||||
#if defined(MS_WINDOWS) || \
|
||||
(defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED))
|
||||
/* Add _PyMp_SemLock type to module */
|
||||
if (PyType_Ready(&_PyMp_SemLockType) < 0)
|
||||
return NULL;
|
||||
Py_INCREF(&_PyMp_SemLockType);
|
||||
{
|
||||
PyObject *py_sem_value_max;
|
||||
/* Some systems define SEM_VALUE_MAX as an unsigned value that
|
||||
* causes it to be negative when used as an int (NetBSD).
|
||||
*
|
||||
* Issue #28152: Use (0) instead of 0 to fix a warning on dead code
|
||||
* when using clang -Wunreachable-code. */
|
||||
if ((int)(SEM_VALUE_MAX) < (0))
|
||||
py_sem_value_max = PyLong_FromLong(INT_MAX);
|
||||
else
|
||||
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
|
||||
if (py_sem_value_max == NULL)
|
||||
return NULL;
|
||||
PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
|
||||
py_sem_value_max);
|
||||
}
|
||||
PyModule_AddObject(module, "SemLock", (PyObject*)&_PyMp_SemLockType);
|
||||
#endif
|
||||
|
||||
/* Add configuration macros */
|
||||
temp = PyDict_New();
|
||||
if (!temp)
|
||||
return NULL;
|
||||
|
||||
#define ADD_FLAG(name) \
|
||||
value = Py_BuildValue("i", name); \
|
||||
if (value == NULL) { Py_DECREF(temp); return NULL; } \
|
||||
if (PyDict_SetItemString(temp, #name, value) < 0) { \
|
||||
Py_DECREF(temp); Py_DECREF(value); return NULL; } \
|
||||
Py_DECREF(value)
|
||||
|
||||
#if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
|
||||
ADD_FLAG(HAVE_SEM_OPEN);
|
||||
#endif
|
||||
#ifdef HAVE_SEM_TIMEDWAIT
|
||||
ADD_FLAG(HAVE_SEM_TIMEDWAIT);
|
||||
#endif
|
||||
#ifdef HAVE_BROKEN_SEM_GETVALUE
|
||||
ADD_FLAG(HAVE_BROKEN_SEM_GETVALUE);
|
||||
#endif
|
||||
#ifdef HAVE_BROKEN_SEM_UNLINK
|
||||
ADD_FLAG(HAVE_BROKEN_SEM_UNLINK);
|
||||
#endif
|
||||
|
||||
if (PyModule_AddObject(module, "flags", temp) < 0)
|
||||
return NULL;
|
||||
|
||||
return module;
|
||||
}
|
||||
@ -0,0 +1,93 @@
|
||||
#ifndef MULTIPROCESSING_H
|
||||
#define MULTIPROCESSING_H
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
|
||||
#include "Python.h"
|
||||
#include "structmember.h"
|
||||
#include "pythread.h"
|
||||
|
||||
/*
|
||||
* Platform includes and definitions
|
||||
*/
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
# include <windows.h>
|
||||
# include <winsock2.h>
|
||||
# include <process.h> /* getpid() */
|
||||
# ifdef Py_DEBUG
|
||||
# include <crtdbg.h>
|
||||
# endif
|
||||
# define SEM_HANDLE HANDLE
|
||||
# define SEM_VALUE_MAX LONG_MAX
|
||||
#else
|
||||
# include <fcntl.h> /* O_CREAT and O_EXCL */
|
||||
# if defined(HAVE_SEM_OPEN) && !defined(POSIX_SEMAPHORES_NOT_ENABLED)
|
||||
# include <semaphore.h>
|
||||
typedef sem_t *SEM_HANDLE;
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Issue 3110 - Solaris does not define SEM_VALUE_MAX
|
||||
*/
|
||||
#ifndef SEM_VALUE_MAX
|
||||
#if defined(HAVE_SYSCONF) && defined(_SC_SEM_VALUE_MAX)
|
||||
# define SEM_VALUE_MAX sysconf(_SC_SEM_VALUE_MAX)
|
||||
#elif defined(_SEM_VALUE_MAX)
|
||||
# define SEM_VALUE_MAX _SEM_VALUE_MAX
|
||||
#elif defined(_POSIX_SEM_VALUE_MAX)
|
||||
# define SEM_VALUE_MAX _POSIX_SEM_VALUE_MAX
|
||||
#else
|
||||
# define SEM_VALUE_MAX INT_MAX
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
* Format codes
|
||||
*/
|
||||
|
||||
#if SIZEOF_VOID_P == SIZEOF_LONG
|
||||
# define F_POINTER "k"
|
||||
# define T_POINTER T_ULONG
|
||||
#elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
|
||||
# define F_POINTER "K"
|
||||
# define T_POINTER T_ULONGLONG
|
||||
#else
|
||||
# error "can't find format code for unsigned integer of same size as void*"
|
||||
#endif
|
||||
|
||||
#ifdef MS_WINDOWS
|
||||
# define F_HANDLE F_POINTER
|
||||
# define T_HANDLE T_POINTER
|
||||
# define F_SEM_HANDLE F_HANDLE
|
||||
# define T_SEM_HANDLE T_HANDLE
|
||||
#else
|
||||
# define F_HANDLE "i"
|
||||
# define T_HANDLE T_INT
|
||||
# define F_SEM_HANDLE F_POINTER
|
||||
# define T_SEM_HANDLE T_POINTER
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Error codes which can be returned by functions called without GIL
|
||||
*/
|
||||
|
||||
#define MP_SUCCESS (0)
|
||||
#define MP_STANDARD_ERROR (-1)
|
||||
#define MP_MEMORY_ERROR (-1001)
|
||||
#define MP_SOCKET_ERROR (-1002)
|
||||
#define MP_EXCEPTION_HAS_BEEN_SET (-1003)
|
||||
|
||||
PyObject *_PyMp_SetError(PyObject *Type, int num);
|
||||
|
||||
/*
|
||||
* Externs - not all will really exist on all platforms
|
||||
*/
|
||||
|
||||
extern PyTypeObject _PyMp_SemLockType;
|
||||
extern PyObject *_PyMp_sem_unlink(PyObject *ignore, PyObject *args);
|
||||
|
||||
#endif /* MULTIPROCESSING_H */
|
||||
@ -0,0 +1,130 @@
|
||||
/*
|
||||
posixshmem - A Python extension that provides shm_open() and shm_unlink()
|
||||
*/
|
||||
|
||||
#define PY_SSIZE_T_CLEAN
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
// for shm_open() and shm_unlink()
|
||||
#ifdef HAVE_SYS_MMAN_H
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
/*[clinic input]
|
||||
module _posixshmem
|
||||
[clinic start generated code]*/
|
||||
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a416734e49164bf8]*/
|
||||
|
||||
/*
|
||||
*
|
||||
* Module-level functions & meta stuff
|
||||
*
|
||||
*/
|
||||
|
||||
#ifdef HAVE_SHM_OPEN
|
||||
/*[clinic input]
|
||||
_posixshmem.shm_open -> int
|
||||
path: unicode
|
||||
flags: int
|
||||
mode: int = 0o777
|
||||
|
||||
# "shm_open(path, flags, mode=0o777)\n\n\
|
||||
|
||||
Open a shared memory object. Returns a file descriptor (integer).
|
||||
|
||||
[clinic start generated code]*/
|
||||
|
||||
static int
|
||||
_posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
|
||||
int mode)
|
||||
/*[clinic end generated code: output=8d110171a4fa20df input=e83b58fa802fac25]*/
|
||||
{
|
||||
int fd;
|
||||
int async_err = 0;
|
||||
const char *name = PyUnicode_AsUTF8(path);
|
||||
if (name == NULL) {
|
||||
return -1;
|
||||
}
|
||||
do {
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
fd = shm_open(name, flags, mode);
|
||||
Py_END_ALLOW_THREADS
|
||||
} while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
||||
|
||||
if (fd < 0) {
|
||||
if (!async_err)
|
||||
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
#endif /* HAVE_SHM_OPEN */
|
||||
|
||||
#ifdef HAVE_SHM_UNLINK
|
||||
/*[clinic input]
|
||||
_posixshmem.shm_unlink
|
||||
path: unicode
|
||||
|
||||
Remove a shared memory object (similar to unlink()).
|
||||
|
||||
Remove a shared memory object name, and, once all processes have unmapped
|
||||
the object, de-allocates and destroys the contents of the associated memory
|
||||
region.
|
||||
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
|
||||
/*[clinic end generated code: output=42f8b23d134b9ff5 input=8dc0f87143e3b300]*/
|
||||
{
|
||||
int rv;
|
||||
int async_err = 0;
|
||||
const char *name = PyUnicode_AsUTF8(path);
|
||||
if (name == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
do {
|
||||
Py_BEGIN_ALLOW_THREADS
|
||||
rv = shm_unlink(name);
|
||||
Py_END_ALLOW_THREADS
|
||||
} while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
|
||||
|
||||
if (rv < 0) {
|
||||
if (!async_err)
|
||||
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
#endif /* HAVE_SHM_UNLINK */
|
||||
|
||||
#include "clinic/posixshmem.c.h"
|
||||
|
||||
static PyMethodDef module_methods[ ] = {
|
||||
_POSIXSHMEM_SHM_OPEN_METHODDEF
|
||||
_POSIXSHMEM_SHM_UNLINK_METHODDEF
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
|
||||
static struct PyModuleDef this_module = {
|
||||
PyModuleDef_HEAD_INIT, // m_base
|
||||
"_posixshmem", // m_name
|
||||
"POSIX shared memory module", // m_doc
|
||||
-1, // m_size (space allocated for module globals)
|
||||
module_methods, // m_methods
|
||||
};
|
||||
|
||||
/* Module init function */
|
||||
PyMODINIT_FUNC
|
||||
PyInit__posixshmem(void) {
|
||||
PyObject *module;
|
||||
module = PyModule_Create(&this_module);
|
||||
if (!module) {
|
||||
return NULL;
|
||||
}
|
||||
return module;
|
||||
}
|
||||
@ -0,0 +1,690 @@
|
||||
/*
|
||||
* A type which wraps a semaphore
|
||||
*
|
||||
* semaphore.c
|
||||
*
|
||||
* Copyright (c) 2006-2008, R Oudkerk
|
||||
* Licensed to PSF under a Contributor Agreement.
|
||||
*/
|
||||
|
||||
#include "multiprocessing.h"
|
||||
|
||||
enum { RECURSIVE_MUTEX, SEMAPHORE };
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
SEM_HANDLE handle;
|
||||
unsigned long last_tid;
|
||||
int count;
|
||||
int maxvalue;
|
||||
int kind;
|
||||
char *name;
|
||||
} 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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
int blocking = 1;
|
||||
double timeout;
|
||||
PyObject *timeout_obj = Py_None;
|
||||
DWORD res, full_msecs, nhandles;
|
||||
HANDLE handles[2], sigint_event;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_release(SemLockObject *self, PyObject *args)
|
||||
{
|
||||
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 */
|
||||
|
||||
static PyObject *
|
||||
semlock_acquire(SemLockObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
int blocking = 1, res, err = 0;
|
||||
PyObject *timeout_obj = Py_None;
|
||||
struct timespec deadline = {0};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_release(SemLockObject *self, PyObject *args)
|
||||
{
|
||||
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;
|
||||
|
||||
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;
|
||||
self->name = name;
|
||||
return (PyObject*)self;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
SEM_HANDLE handle = SEM_FAILED;
|
||||
int kind, maxvalue, value, unlink;
|
||||
PyObject *result;
|
||||
char *name, *name_copy = NULL;
|
||||
static char *kwlist[] = {"kind", "value", "maxvalue", "name", "unlink",
|
||||
NULL};
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iiisi", kwlist,
|
||||
&kind, &value, &maxvalue, &name, &unlink))
|
||||
return 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 (handle != SEM_FAILED)
|
||||
SEM_CLOSE(handle);
|
||||
PyMem_Free(name_copy);
|
||||
if (!PyErr_Occurred()) {
|
||||
_PyMp_SetError(NULL, MP_STANDARD_ERROR);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_rebuild(PyTypeObject *type, PyObject *args)
|
||||
{
|
||||
SEM_HANDLE handle;
|
||||
int kind, maxvalue;
|
||||
char *name, *name_copy = NULL;
|
||||
|
||||
if (!PyArg_ParseTuple(args, F_SEM_HANDLE "iiz",
|
||||
&handle, &kind, &maxvalue, &name))
|
||||
return 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) {
|
||||
PyMem_Free(name_copy);
|
||||
return PyErr_SetFromErrno(PyExc_OSError);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return newsemlockobject(type, handle, kind, maxvalue, name_copy);
|
||||
}
|
||||
|
||||
static void
|
||||
semlock_dealloc(SemLockObject* self)
|
||||
{
|
||||
if (self->handle != SEM_FAILED)
|
||||
SEM_CLOSE(self->handle);
|
||||
PyMem_Free(self->name);
|
||||
PyObject_Del(self);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_count(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
return PyLong_FromLong((long)self->count);
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_ismine(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
/* only makes sense for a lock */
|
||||
return PyBool_FromLong(ISMINE(self));
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_getvalue(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
#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
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_iszero(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
#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
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
semlock_afterfork(SemLockObject *self, PyObject *Py_UNUSED(ignored))
|
||||
{
|
||||
self->count = 0;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
/*
|
||||
* Semaphore methods
|
||||
*/
|
||||
|
||||
static PyMethodDef semlock_methods[] = {
|
||||
{"acquire", (PyCFunction)(void(*)(void))semlock_acquire, METH_VARARGS | METH_KEYWORDS,
|
||||
"acquire the semaphore/lock"},
|
||||
{"release", (PyCFunction)semlock_release, METH_NOARGS,
|
||||
"release the semaphore/lock"},
|
||||
{"__enter__", (PyCFunction)(void(*)(void))semlock_acquire, METH_VARARGS | METH_KEYWORDS,
|
||||
"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}
|
||||
};
|
||||
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
PyTypeObject _PyMp_SemLockType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
/* tp_name */ "_multiprocessing.SemLock",
|
||||
/* tp_basicsize */ sizeof(SemLockObject),
|
||||
/* tp_itemsize */ 0,
|
||||
/* tp_dealloc */ (destructor)semlock_dealloc,
|
||||
/* tp_vectorcall_offset */ 0,
|
||||
/* tp_getattr */ 0,
|
||||
/* tp_setattr */ 0,
|
||||
/* tp_as_async */ 0,
|
||||
/* 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,
|
||||
};
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
Reference in New Issue
Block a user