Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
allendowney
GitHub Repository: allendowney/cpython
Path: blob/main/Modules/_multiprocessing/posixshmem.c
12 views
1
/*
2
posixshmem - A Python extension that provides shm_open() and shm_unlink()
3
*/
4
5
#include <Python.h>
6
7
// for shm_open() and shm_unlink()
8
#ifdef HAVE_SYS_MMAN_H
9
#include <sys/mman.h>
10
#endif
11
12
/*[clinic input]
13
module _posixshmem
14
[clinic start generated code]*/
15
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=a416734e49164bf8]*/
16
17
/*
18
*
19
* Module-level functions & meta stuff
20
*
21
*/
22
23
#ifdef HAVE_SHM_OPEN
24
/*[clinic input]
25
_posixshmem.shm_open -> int
26
path: unicode
27
flags: int
28
mode: int = 0o777
29
30
# "shm_open(path, flags, mode=0o777)\n\n\
31
32
Open a shared memory object. Returns a file descriptor (integer).
33
34
[clinic start generated code]*/
35
36
static int
37
_posixshmem_shm_open_impl(PyObject *module, PyObject *path, int flags,
38
int mode)
39
/*[clinic end generated code: output=8d110171a4fa20df input=e83b58fa802fac25]*/
40
{
41
int fd;
42
int async_err = 0;
43
const char *name = PyUnicode_AsUTF8(path);
44
if (name == NULL) {
45
return -1;
46
}
47
do {
48
Py_BEGIN_ALLOW_THREADS
49
fd = shm_open(name, flags, mode);
50
Py_END_ALLOW_THREADS
51
} while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
52
53
if (fd < 0) {
54
if (!async_err)
55
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
56
return -1;
57
}
58
59
return fd;
60
}
61
#endif /* HAVE_SHM_OPEN */
62
63
#ifdef HAVE_SHM_UNLINK
64
/*[clinic input]
65
_posixshmem.shm_unlink
66
path: unicode
67
68
Remove a shared memory object (similar to unlink()).
69
70
Remove a shared memory object name, and, once all processes have unmapped
71
the object, de-allocates and destroys the contents of the associated memory
72
region.
73
74
[clinic start generated code]*/
75
76
static PyObject *
77
_posixshmem_shm_unlink_impl(PyObject *module, PyObject *path)
78
/*[clinic end generated code: output=42f8b23d134b9ff5 input=8dc0f87143e3b300]*/
79
{
80
int rv;
81
int async_err = 0;
82
const char *name = PyUnicode_AsUTF8(path);
83
if (name == NULL) {
84
return NULL;
85
}
86
do {
87
Py_BEGIN_ALLOW_THREADS
88
rv = shm_unlink(name);
89
Py_END_ALLOW_THREADS
90
} while (rv < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
91
92
if (rv < 0) {
93
if (!async_err)
94
PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
95
return NULL;
96
}
97
98
Py_RETURN_NONE;
99
}
100
#endif /* HAVE_SHM_UNLINK */
101
102
#include "clinic/posixshmem.c.h"
103
104
static PyMethodDef module_methods[ ] = {
105
_POSIXSHMEM_SHM_OPEN_METHODDEF
106
_POSIXSHMEM_SHM_UNLINK_METHODDEF
107
{NULL} /* Sentinel */
108
};
109
110
111
static PyModuleDef_Slot module_slots[] = {
112
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
113
{0, NULL}
114
};
115
116
117
static struct PyModuleDef _posixshmemmodule = {
118
PyModuleDef_HEAD_INIT,
119
.m_name = "_posixshmem",
120
.m_doc = "POSIX shared memory module",
121
.m_size = 0,
122
.m_methods = module_methods,
123
.m_slots = module_slots,
124
};
125
126
/* Module init function */
127
PyMODINIT_FUNC
128
PyInit__posixshmem(void)
129
{
130
return PyModuleDef_Init(&_posixshmemmodule);
131
}
132
133