Python coro under-the-hood improvements.

- Change python coroutine id to a uint64_t.
- Add kore.task_id() to return active coro its id.
This commit is contained in:
Joris Vink 2021-12-02 21:58:13 +01:00
parent 86ecb85f03
commit 5ac62b17bc
2 changed files with 20 additions and 5 deletions

View File

@ -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),

View File

@ -24,6 +24,7 @@
#include <ctype.h>
#include <libgen.h>
#include <inttypes.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
@ -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) {