From 5ac62b17bce8dae24c6cd9ee2c89a4e7b32484b6 Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Thu, 2 Dec 2021 21:58:13 +0100 Subject: [PATCH] Python coro under-the-hood improvements. - Change python coroutine id to a uint64_t. - Add kore.task_id() to return active coro its id. --- include/kore/python_methods.h | 4 +++- src/python.c | 21 +++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h index 8ca9c62..1160130 100644 --- a/include/kore/python_methods.h +++ b/include/kore/python_methods.h @@ -18,7 +18,7 @@ #define CORO_STATE_SUSPENDED 2 struct python_coro { - u_int32_t id; + u_int64_t id; int state; int killed; PyObject *obj; @@ -45,6 +45,7 @@ static PyObject *python_kore_queue(PyObject *, PyObject *); static PyObject *python_kore_worker(PyObject *, PyObject *); static PyObject *python_kore_tracer(PyObject *, PyObject *); static PyObject *python_kore_fatalx(PyObject *, PyObject *); +static PyObject *python_kore_task_id(PyObject *, PyObject *); static PyObject *python_kore_setname(PyObject *, PyObject *); static PyObject *python_kore_suspend(PyObject *, PyObject *); static PyObject *python_kore_shutdown(PyObject *, PyObject *); @@ -96,6 +97,7 @@ static struct PyMethodDef pykore_methods[] = { METHOD("tracer", python_kore_tracer, METH_VARARGS), METHOD("fatal", python_kore_fatal, METH_VARARGS), METHOD("fatalx", python_kore_fatalx, METH_VARARGS), + METHOD("task_id", python_kore_task_id, METH_NOARGS), METHOD("setname", python_kore_setname, METH_VARARGS), METHOD("suspend", python_kore_suspend, METH_VARARGS), METHOD("shutdown", python_kore_shutdown, METH_NOARGS), diff --git a/src/python.c b/src/python.c index 0c84d42..55ec7ab 100644 --- a/src/python.c +++ b/src/python.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -1173,7 +1174,7 @@ python_coro_trace(const char *label, struct python_coro *coro) kore_log(LOG_NOTICE, "coro '%s' %s <%s> @ [%s:%d]", coro->name, label, func, fname, line); } else { - kore_log(LOG_NOTICE, "coro %u %s <%s> @ [%s:%d]", + kore_log(LOG_NOTICE, "coro %" PRIu64 " %s <%s> @ [%s:%d]", coro->id, label, func, fname, line); } } @@ -1945,16 +1946,28 @@ python_kore_task_create(PyObject *self, PyObject *args) coro = python_coro_create(obj, NULL); Py_INCREF(obj); - return (PyLong_FromUnsignedLong(coro->id)); + return (PyLong_FromUnsignedLongLong(coro->id)); +} + +static PyObject * +python_kore_task_id(PyObject *self, PyObject *args) +{ + if (coro_running == NULL) { + PyErr_SetString(PyExc_RuntimeError, + "no coroutine active"); + return (NULL); + } + + return (PyLong_FromUnsignedLongLong(coro_running->id)); } static PyObject * python_kore_task_kill(PyObject *self, PyObject *args) { - u_int32_t id; + u_int64_t id; struct python_coro *coro, *active; - if (!PyArg_ParseTuple(args, "I", &id)) + if (!PyArg_ParseTuple(args, "K", &id)) return (NULL); if (coro_running != NULL && coro_running->id == id) {