Path: blob/main/Modules/_multiprocessing/posixshmem.c
12 views
/*1posixshmem - A Python extension that provides shm_open() and shm_unlink()2*/34#include <Python.h>56// for shm_open() and shm_unlink()7#ifdef HAVE_SYS_MMAN_H8#include <sys/mman.h>9#endif1011/*[clinic input]12module _posixshmem13[clinic start generated code]*/14/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a416734e49164bf8]*/1516/*17*18* Module-level functions & meta stuff19*20*/2122#ifdef HAVE_SHM_OPEN23/*[clinic input]24_posixshmem.shm_open -> int25path: unicode26flags: int27mode: int = 0o7772829# "shm_open(path, flags, mode=0o777)\n\n\3031Open a shared memory object. Returns a file descriptor (integer).3233[clinic start generated code]*/3435static int36_posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,37int mode)38/*[clinic end generated code: output=8d110171a4fa20df input=e83b58fa802fac25]*/39{40int fd;41int async_err = 0;42const char *name = PyUnicode_AsUTF8(path);43if (name == NULL) {44return -1;45}46do {47Py_BEGIN_ALLOW_THREADS48fd = shm_open(name, flags, mode);49Py_END_ALLOW_THREADS50} while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));5152if (fd < 0) {53if (!async_err)54PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);55return -1;56}5758return fd;59}60#endif /* HAVE_SHM_OPEN */6162#ifdef HAVE_SHM_UNLINK63/*[clinic input]64_posixshmem.shm_unlink65path: unicode6667Remove a shared memory object (similar to unlink()).6869Remove a shared memory object name, and, once all processes have unmapped70the object, de-allocates and destroys the contents of the associated memory71region.7273[clinic start generated code]*/7475static PyObject *76_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)77/*[clinic end generated code: output=42f8b23d134b9ff5 input=8dc0f87143e3b300]*/78{79int rv;80int async_err = 0;81const char *name = PyUnicode_AsUTF8(path);82if (name == NULL) {83return NULL;84}85do {86Py_BEGIN_ALLOW_THREADS87rv = shm_unlink(name);88Py_END_ALLOW_THREADS89} while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));9091if (rv < 0) {92if (!async_err)93PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);94return NULL;95}9697Py_RETURN_NONE;98}99#endif /* HAVE_SHM_UNLINK */100101#include "clinic/posixshmem.c.h"102103static PyMethodDef module_methods[ ] = {104_POSIXSHMEM_SHM_OPEN_METHODDEF105_POSIXSHMEM_SHM_UNLINK_METHODDEF106{NULL} /* Sentinel */107};108109110static PyModuleDef_Slot module_slots[] = {111{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},112{0, NULL}113};114115116static struct PyModuleDef _posixshmemmodule = {117PyModuleDef_HEAD_INIT,118.m_name = "_posixshmem",119.m_doc = "POSIX shared memory module",120.m_size = 0,121.m_methods = module_methods,122.m_slots = module_slots,123};124125/* Module init function */126PyMODINIT_FUNC127PyInit__posixshmem(void)128{129return PyModuleDef_Init(&_posixshmemmodule);130}131132133