|
| 1 | +#include <Python.h> |
| 2 | + |
| 3 | +#include "async.h" |
| 4 | +#include "sleep_wrapper.h" |
| 5 | +#include "sonyflake.h" |
| 6 | + |
| 7 | + |
| 8 | +struct async_sonyflake_state { |
| 9 | + PyObject_HEAD |
| 10 | + |
| 11 | + struct sonyflake_state *sf; |
| 12 | + PyObject *sleep; |
| 13 | +}; |
| 14 | + |
| 15 | +static int async_sonyflake_clear(PyObject *py_self) { |
| 16 | + struct async_sonyflake_state *self = (struct async_sonyflake_state *) py_self; |
| 17 | + Py_CLEAR(self->sf); |
| 18 | + Py_CLEAR(self->sleep); |
| 19 | + return 0; |
| 20 | +} |
| 21 | + |
| 22 | +static void async_sonyflake_dealloc(PyObject *py_self) { |
| 23 | + async_sonyflake_clear(py_self); |
| 24 | + |
| 25 | + PyTypeObject *tp = Py_TYPE(py_self); |
| 26 | + freefunc tp_free = PyType_GetSlot(tp, Py_tp_free); |
| 27 | + |
| 28 | + assert(tp_free != NULL); |
| 29 | + |
| 30 | + tp_free(py_self); |
| 31 | + Py_DECREF(tp); |
| 32 | +} |
| 33 | + |
| 34 | +static int async_sonyflake_traverse(PyObject *py_self, visitproc visit, void *arg) { |
| 35 | + struct async_sonyflake_state *self = (struct async_sonyflake_state *) py_self; |
| 36 | + Py_VISIT(self->sf); |
| 37 | + Py_VISIT(self->sleep); |
| 38 | + Py_VISIT(Py_TYPE(py_self)); |
| 39 | + return 0; |
| 40 | +} |
| 41 | + |
| 42 | +static PyObject *async_sonyflake_new(PyTypeObject *type, PyObject *Py_UNUSED(args), PyObject *Py_UNUSED(kwargs)) { |
| 43 | + allocfunc tp_alloc = PyType_GetSlot(type, Py_tp_alloc); |
| 44 | + |
| 45 | + assert(tp_alloc != NULL); |
| 46 | + |
| 47 | + struct async_sonyflake_state *self = (void *) tp_alloc(type, 0); |
| 48 | + |
| 49 | + if (!self) { |
| 50 | + return NULL; |
| 51 | + } |
| 52 | + |
| 53 | + self->sf = NULL; |
| 54 | + self->sleep = NULL; |
| 55 | + |
| 56 | + return (PyObject *) self; |
| 57 | +} |
| 58 | + |
| 59 | +static int async_sonyflake_init(PyObject *py_self, PyObject *args, PyObject *Py_UNUSED(kwargs)) { |
| 60 | + struct async_sonyflake_state *self = (struct async_sonyflake_state *) py_self; |
| 61 | + |
| 62 | + if (!PyArg_ParseTuple(args, "OO", &self->sf, &self->sleep)) { |
| 63 | + return -1; |
| 64 | + } |
| 65 | + |
| 66 | + struct sonyflake_module_state *module = PyType_GetModuleState(Py_TYPE(self)); |
| 67 | + |
| 68 | + assert(module != NULL); |
| 69 | + |
| 70 | + if (!PyObject_IsInstance((PyObject *) self->sf, module->sonyflake_cls)) { |
| 71 | + PyErr_SetString(PyExc_TypeError, "sf must be instance of SonyFlake"); |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + if (!PyCallable_Check(self->sleep)) { |
| 76 | + PyErr_SetString(PyExc_TypeError, "sleep must be callable"); |
| 77 | + return -1; |
| 78 | + } |
| 79 | + |
| 80 | + Py_INCREF(self->sf); |
| 81 | + Py_INCREF(self->sleep); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +static inline PyObject *wrap_into_sleep( |
| 87 | + struct async_sonyflake_state *self, |
| 88 | + struct sonyflake_next_sleep_info *sleep_info, |
| 89 | + PyObject *obj |
| 90 | +) { |
| 91 | + if (!obj) { |
| 92 | + return NULL; |
| 93 | + } |
| 94 | + |
| 95 | + struct sonyflake_module_state *module = PyType_GetModuleState(Py_TYPE(self)); |
| 96 | + |
| 97 | + assert(module); |
| 98 | + assert(sleep_info); |
| 99 | + |
| 100 | + struct timespec diff = sleep_info->future; |
| 101 | + |
| 102 | + sub_diff(&diff, &sleep_info->now); |
| 103 | + |
| 104 | + PyObject *coro = PyObject_CallFunction( |
| 105 | + module->sleep_wrapper_cls, |
| 106 | + "OOd", |
| 107 | + obj, |
| 108 | + self->sleep, |
| 109 | + timespec_to_double(&diff) |
| 110 | + ); |
| 111 | + |
| 112 | + Py_DECREF(obj); |
| 113 | + |
| 114 | + return coro; |
| 115 | +} |
| 116 | + |
| 117 | +static PyObject *async_sonyflake_call(struct async_sonyflake_state *self, PyObject *args) { |
| 118 | + struct sonyflake_next_sleep_info sleep_info; |
| 119 | + |
| 120 | + return wrap_into_sleep(self, &sleep_info, sonyflake_next_py(self->sf, args, &sleep_info)); |
| 121 | +} |
| 122 | + |
| 123 | +static PyObject *async_sonyflake_await(struct async_sonyflake_state *self) { |
| 124 | + struct sonyflake_next_sleep_info sleep_info; |
| 125 | + |
| 126 | + return wrap_into_sleep(self, &sleep_info, sonyflake_next_py(self->sf, NULL, &sleep_info)); |
| 127 | +} |
| 128 | + |
| 129 | +PyType_Slot async_sonyflake_type_slots[] = { |
| 130 | + {Py_tp_alloc, PyType_GenericAlloc}, |
| 131 | + {Py_tp_dealloc, async_sonyflake_dealloc}, |
| 132 | + {Py_tp_traverse, async_sonyflake_traverse}, |
| 133 | + {Py_tp_clear, async_sonyflake_clear}, |
| 134 | + {Py_tp_new, async_sonyflake_new}, |
| 135 | + {Py_tp_init, async_sonyflake_init}, |
| 136 | + {Py_tp_call, async_sonyflake_call}, |
| 137 | + {Py_am_await, async_sonyflake_await}, |
| 138 | + {Py_am_anext, async_sonyflake_await}, |
| 139 | + {Py_am_aiter, PyObject_SelfIter}, |
| 140 | + {0, 0}, |
| 141 | +}; |
| 142 | + |
| 143 | +PyType_Spec async_sonyflake_type_spec = { |
| 144 | + .name = MODULE_NAME ".AsyncSonyFlake", |
| 145 | + .basicsize = sizeof(struct async_sonyflake_state), |
| 146 | + .flags = Py_TPFLAGS_DEFAULT, |
| 147 | + .slots = async_sonyflake_type_slots, |
| 148 | +}; |
0 commit comments