Skip to content

Commit 91c2007

Browse files
committed
Add module state
1 parent 844b10e commit 91c2007

3 files changed

Lines changed: 55 additions & 18 deletions

File tree

src/sonyflake_turbo/_sonyflake.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11
#include <Python.h>
22
#include <pythread.h>
3-
#include <stdatomic.h>
43
#include <stdbool.h>
54
#include <stdint.h>
6-
#include <time.h>
75

6+
#include "module.h"
87
#include "sonyflake.h"
98
#include "machine_ids.h"
109

10+
static int sonyflake_module_traverse(PyObject *m, visitproc visit, void *arg) {
11+
struct sonyflake_module_state *state = PyModule_GetState(m);
12+
Py_VISIT(state->sonyflake_cls);
13+
Py_VISIT(state->machine_id_lcg_cls);
14+
return 0;
15+
}
16+
17+
static int sonyflake_module_clear(PyObject *m) {
18+
struct sonyflake_module_state *state = PyModule_GetState(m);
19+
Py_CLEAR(state->sonyflake_cls);
20+
Py_CLEAR(state->machine_id_lcg_cls);
21+
return 0;
22+
}
23+
24+
static void sonyflake_module_free(void *m) {
25+
sonyflake_module_clear(m);
26+
}
27+
1128
static int sonyflake_exec(PyObject *module);
1229

1330
PyModuleDef_Slot sonyflake_slots[] = {
@@ -25,8 +42,11 @@ struct PyModuleDef sonyflake_module = {
2542
PyModuleDef_HEAD_INIT,
2643
.m_name = MODULE_NAME,
2744
.m_doc = "",
28-
.m_size = 0,
45+
.m_size = sizeof(struct sonyflake_module_state),
2946
.m_slots = sonyflake_slots,
47+
.m_traverse = sonyflake_module_traverse,
48+
.m_clear = sonyflake_module_clear,
49+
.m_free = sonyflake_module_free,
3050
};
3151

3252
PyMODINIT_FUNC
@@ -36,26 +56,29 @@ PyInit__sonyflake(void)
3656
}
3757

3858
static int sonyflake_exec(PyObject *module) {
39-
PyObject *sonyflake_cls, *machine_id_lcg_cls;
59+
struct sonyflake_module_state *state = PyModule_GetState(module);
60+
61+
state->sonyflake_cls = NULL;
62+
state->machine_id_lcg_cls = NULL;
4063

41-
sonyflake_cls = PyType_FromSpec(&sonyflake_type_spec);
64+
state->sonyflake_cls = PyType_FromModuleAndSpec(module, &sonyflake_type_spec, NULL);
4265

43-
if (!sonyflake_cls) {
66+
if (!state->sonyflake_cls) {
4467
goto err;
4568
}
4669

47-
if (PyModule_AddObject(module, "SonyFlake", sonyflake_cls) < 0) {
48-
goto err_sf;
70+
if (PyModule_AddObjectRef(module, "SonyFlake", state->sonyflake_cls) < 0) {
71+
goto err;
4972
}
5073

51-
machine_id_lcg_cls = PyType_FromSpec(&machine_id_lcg_spec);
74+
state->machine_id_lcg_cls = PyType_FromModuleAndSpec(module, &machine_id_lcg_spec, NULL);
5275

53-
if (!machine_id_lcg_cls) {
54-
goto err_lcg;
76+
if (!state->machine_id_lcg_cls) {
77+
goto err;
5578
}
5679

57-
if (PyModule_AddObject(module, "MachineIDLCG", machine_id_lcg_cls) < 0) {
58-
goto err_lcg;
80+
if (PyModule_AddObjectRef(module, "MachineIDLCG", state->machine_id_lcg_cls) < 0) {
81+
goto err;
5982
}
6083

6184
PyModule_AddIntMacro(module, SONYFLAKE_EPOCH);
@@ -68,10 +91,9 @@ static int sonyflake_exec(PyObject *module) {
6891

6992
return 0;
7093

71-
err_lcg:
72-
Py_DECREF(machine_id_lcg_cls);
73-
err_sf:
74-
Py_DECREF(sonyflake_cls);
7594
err:
95+
Py_CLEAR(state->machine_id_lcg_cls);
96+
Py_CLEAR(state->sonyflake_cls);
97+
7698
return -1;
7799
}

src/sonyflake_turbo/module.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
#include <Python.h>
4+
#include <assert.h>
5+
6+
#define MODULE_NAME "sonyflake_turbo"
7+
#define MODULE(self) ((struct sonyflake_module_state *) PyType_GetModuleState(Py_TYPE(self)))
8+
9+
struct sonyflake_module_state {
10+
PyObject *sonyflake_cls;
11+
PyObject *machine_id_lcg_cls;
12+
PyObject *sleep_wrapper_cls;
13+
};
14+
15+
extern struct PyModuleDef sonyflake_module;

src/sonyflake_turbo/sonyflake.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#include <Python.h>
44
#include <stdint.h>
55
#include <time.h>
6+
#include "module.h"
67

7-
#define MODULE_NAME "sonyflake_turbo"
88
#define SONYFLAKE_EPOCH 1409529600 // 2014-09-01 00:00:00 UTC
99
#define SONYFLAKE_SEQUENCE_BITS 8
1010
#define SONYFLAKE_SEQUENCE_MAX ((1 << SONYFLAKE_SEQUENCE_BITS) - 1)

0 commit comments

Comments
 (0)