8000 port multiprocssing types to heap types · python/cpython@ff5673f · GitHub
[go: up one dir, main page]

Skip to content

Commit ff5673f

Browse files
port multiprocssing types to heap types
1 parent c0453a4 commit ff5673f

File tree

3 files changed

+96
-66
lines changed

3 files changed

+96
-66
lines changed

Modules/_multiprocessing/multiprocessing.c

Lines changed: 63 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ module _multiprocessing
2424

2525
#include "clinic/multiprocessing.c.h"
2626

27+
typedef struct {
28+
PyTypeObject *semlock_type;
29+
} _multiprocessingstate;
30+
31+
static inline
32+
_multiprocessingstate *get_module_state(PyObject *module)
33+
{
34+
void *state = PyModule_GetState(module);
35+
assert(state != NULL);
36+
return (_multiprocessingstate *)state;
37+
}
38+
2739
/*
2840
* Function which raises exceptions based on error codes
2941
*/
@@ -186,35 +198,38 @@ static PyMethodDef module_methods[] = {
186198
static int
187199
multiprocessing_exec(PyObject *module)
188200
{
201+
_multiprocessingstate *state = get_module_state(module);
189202
#ifdef HAVE_MP_SEMAPHORE
190203

191-
/* Add _PyMp_SemLock type to module */
192-
if (PyModule_AddType(module, &_PyMp_SemLockType) < 0) {
204+
state->semlock_type = (PyTypeObject *)PyType_FromModuleAndSpec(
205+
module, &_PyMp_SemLockType_spec, NULL);
206+
207+
if (state->semlock_type == NULL) {
208+
return -1;
209+
}
210+
if (PyModule_AddType(module, state->semlock_type) < 0) {
193211
return -1;
194212
}
195213

196-
{
197-
PyObject *py_sem_value_max;
198-
/* Some systems define SEM_VALUE_MAX as an unsigned value that
199-
* causes it to be negative when used as an int (NetBSD).
200-
*
201-
* Issue #28152: Use (0) instead of 0 to fix a warning on dead code
202-
* when using clang -Wunreachable-code. */
203-
if ((int)(SEM_VALUE_MAX) < (0))
204-
py_sem_value_max = PyLong_FromLong(INT_MAX);
205-
else
206-
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
207-
208-
if (py_sem_value_max == NULL) {
209-
return -1;
210-
}
211-
if (PyDict_SetItemString(_PyMp_SemLockType.tp_dict, "SEM_VALUE_MAX",
212-
py_sem_value_max) < 0) {
213-
Py_DECREF(py_sem_value_max);
214-
return -1;
215-
}
214+
PyObject *py_sem_value_max;
215+
/* Some systems define SEM_VALUE_MAX as an unsigned value that
216+
* causes it to be negative when used as an int (NetBSD).
217+
*
218+
* Issue #28152: Use (0) instead of 0 to fix a warning on dead code
219+
* when using clang -Wunreachable-code. */
220+
if ((int)(SEM_VALUE_MAX) < (0))
221+
py_sem_value_max = PyLong_FromLong(INT_MAX);
222+
else
223+
py_sem_value_max = PyLong_FromLong(SEM_VALUE_MAX);
224+
if (py_sem_value_max == NULL) {
225+
return -1;
226+
}
227+
if (PyDict_SetItemString(state->semlock_type->tp_dict, "SEM_V 6D47 ALUE_MAX",
228+
py_sem_value_max) < 0) {
216229
Py_DECREF(py_sem_value_max);
230+
return -1;
217231
}
232+
Py_DECREF(py_sem_value_max);
218233

219234
#endif
220235

@@ -260,6 +275,28 @@ multiprocessing_exec(PyObject *module)
260275
return 0;
261276
}
262277

278+
static int
279+
multiprocessing_traverse(PyObject *module, visitproc visit, void *arg)
280+
{
281+
_multiprocessingstate *state 9E81 = get_module_state(module);
282+
Py_VISIT(state->semlock_type);
283+
return 0;
284+
}
285+
286+
static int
287+
multiprocessing_clear(PyObject *module)
288+
{
289+
_multiprocessingstate *state = get_module_state(module);
290+
Py_CLEAR(state->semlock_type);
291+
return 0;
292+
}
293+
294+
static void
295+
multiprocessing_dealloc(void *module)
296+
{
297+
(void)multiprocessing_clear((PyObject *)module);
298+
}
299+
263300
static PyModuleDef_Slot multiprocessing_slots[] = {
264301
{Py_mod_exec, multiprocessing_exec},
265302
{0, NULL}
@@ -268,8 +305,12 @@ static PyModuleDef_Slot multiprocessing_slots[] = {
268305
static struct PyModuleDef multiprocessing_module = {
269306
PyModuleDef_HEAD_INIT,
270307
.m_name = "_multiprocessing",
308+
.m_size = sizeof(_multiprocessingstate),
271309
.m_methods = module_methods,
272310
.m_slots = multiprocessing_slots,
311+
.m_traverse = multiprocessing_traverse,
312+
.m_clear = multiprocessing_clear,
313+
.m_free = multiprocessing_dealloc,
273314
};
274315

275316
PyMODINIT_FUNC

Modules/_multiprocessing/multiprocessing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ PyObject *_PyMp_SetError(PyObject *Type, int num);
8989
* Externs - not all will really exist on all platforms
9090
*/
9191

92-
extern PyTypeObject _PyMp_SemLockType;
92+
extern PyType_Spec _PyMp_SemLockType_spec;
9393
extern PyObject *_PyMp_sem_unlink(const char *name);
9494

9595
#endif /* MULTIPROCESSING_H */

Modules/_multiprocessing/semaphore.c

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ static PyObject *
454454
newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
455455
char *name)
456456
{
457-
SemLockObject *self;
458-
459-
self = PyObject_New(SemLockObject, type);
457+
SemLockObject *self = PyObject_GC_New(SemLockObject, type);
460458
if (!self)
461459
return NULL;
462460
self->handle = handle;
@@ -570,10 +568,13 @@ _multiprocessing_SemLock__rebuild_impl(PyTypeObject *type, SEM_HANDLE handle,
570568
static void
571569
semlock_dealloc(SemLockObject* self)
572570
{
571+
PyTypeObject *tp = Py_TYPE(self);
572+
PyObject_GC_UnTrack(self);
573573
if (self->handle != SEM_FAILED)
574574
SEM_CLOSE(self->handle);
575575
PyMem_Free(self->name);
576-
PyObject_Free(self);
576+
tp->tp_free(self);
577+
Py_DECREF(tp);
577578
}
578579

579580
/*[clinic input]
@@ -703,6 +704,13 @@ _multiprocessing_SemLock___exit___impl(SemLockObject *self,
703704
return _multiprocessing_SemLock_release_impl(self);
704705
}
705706

707+
static int
708+
semlock_traverse(SemLockObject *s, visitproc visit, void *arg)
709+
{
710+
Py_VISIT(Py_TYPE(s));
711+
return 0;
712+
}
713+
706714
/*
707715
* Semaphore methods
708716
*/
@@ -741,45 +749,26 @@ static PyMemberDef semlock_members[] = {
741749
* Semaphore type
742750
*/
743751

744-
PyTypeObject _PyMp_SemLockType = {
745-
PyVarObject_HEAD_INIT(NULL, 0)
746-
/* tp_name */ "_multiprocessing.SemLock",
747-
/* tp_basicsize */ sizeof(SemLockObject),
748-
/* tp_itemsize */ 0,
749-
/* tp_dealloc */ (destructor)semlock_dealloc,
750-
/* tp_vectorcall_offset */ 0,
751-
/* tp_getattr */ 0,
752-
/* tp_setattr */ 0,
753-
/* tp_as_async */ 0,
754-
/* tp_repr */ 0,
755-
/* tp_as_number */ 0,
756-
/* tp_as_sequence */ 0,
757-
/* tp_as_mapping */ 0,
758-
/* tp_hash */ 0,
759-
/* tp_call */ 0,
760-
/* tp_str */ 0,
761-
/* tp_getattro */ 0,
762-
/* tp_setattro */ 0,
763-
/* tp_as_buffer */ 0,
764-
/* tp_flags */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
765-
/* tp_doc */ "Semaphore/Mutex type",
766-
/* tp_traverse */ 0,
767-
/* tp_clear */ 0,
768-
/* tp_richcompare */ 0,
769-
/* tp_weaklistoffset */ 0,
770-
/* tp_iter */ 0,
771-
/* tp_iternext */ 0,
772-
/* tp_methods */ semlock_methods,
773-
/* tp_members */ semlock_members,
774-
/* tp_getset */ 0,
775-
/* tp_base */ 0,
776-
/* tp_dict */ 0,
777-
/* tp_descr_get */ 0,
778-
/* tp_descr_set */ 0,
779-
/* tp_dictoffset */ 0,
780-
/* tp_init */ 0,
781-
/* tp_alloc */ 0,
782-
/* tp_new */ _multiprocessing_SemLock,
752+
PyType_Slot _PyMp_SemLockType_slots[] = {
753+
{Py_tp_dealloc, semlock_dealloc},
754+
{Py_tp_getattro, PyObject_GenericGetAttr},
755+
{Py_tp_setattro, PyObject_GenericSetAttr},
756+
{Py_tp_methods, semlock_methods},
757+
{Py_tp_members, semlock_members},
758+
{Py_tp_alloc, PyType_GenericAlloc},
759+
{Py_tp_new, _multiprocessing_SemLock},
760+
{Py_tp_traverse, semlock_traverse},
761+
{Py_tp_free, PyObject_GC_Del},
762+
{Py_tp_doc, (void *)PyDoc_STR("Semaphore/Mutex type")},
763+
{0, 0},
764+
};
765+
766+
PyType_Spec _PyMp_SemLockType_spec = {
767+
.name = "_multiprocessing.SemLock",
768+
.basicsize = sizeof(SemLockObject),
769+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
770+
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
771+
.slots = _PyMp_SemLockType_slots,
783772
};
784773

785774
/*

0 commit comments

Comments
 (0)
0