commit d99d0b2d775ad92fc0429b29a0f7445c4ad4a1be
parent 06fd5ca2f25728687abe0f497780cf28a56bc039
Author: Joris Vink <joris@coders.se>
Date: Fri, 20 Sep 2019 10:03:43 +0200
add lock.trylock() and make lock.release() sync.
Diffstat:
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/include/kore/python_methods.h b/include/kore/python_methods.h
@@ -357,10 +357,13 @@ struct pylock {
static PyObject *pylock_aexit(struct pylock *, PyObject *);
static PyObject *pylock_aenter(struct pylock *, PyObject *);
+static PyObject *pylock_release(struct pylock *, PyObject *);
+static PyObject *pylock_trylock(struct pylock *, PyObject *);
static PyMethodDef pylock_methods[] = {
METHOD("aqcuire", pylock_aenter, METH_NOARGS),
- METHOD("release", pylock_aexit, METH_NOARGS),
+ METHOD("release", pylock_release, METH_NOARGS),
+ METHOD("trylock", pylock_trylock, METH_NOARGS),
METHOD("__aexit__", pylock_aexit, METH_VARARGS),
METHOD("__aenter__", pylock_aenter, METH_NOARGS),
METHOD(NULL, NULL, -1)
diff --git a/src/python.c b/src/python.c
@@ -2834,6 +2834,35 @@ pylock_dealloc(struct pylock *lock)
}
static PyObject *
+pylock_trylock(struct pylock *lock, PyObject *args)
+{
+ if (lock->owner != NULL)
+ Py_RETURN_FALSE;
+
+ lock->owner = coro_running;
+
+ Py_RETURN_TRUE;
+}
+
+static PyObject *
+pylock_release(struct pylock *lock, PyObject *args)
+{
+ if (lock->owner == NULL) {
+ PyErr_SetString(PyExc_RuntimeError, "no lock owner set");
+ return (NULL);
+ }
+
+ if (lock->owner->id != coro_running->id) {
+ PyErr_SetString(PyExc_RuntimeError, "lock not owned by caller");
+ return (NULL);
+ }
+
+ pylock_do_release(lock);
+
+ Py_RETURN_NONE;
+}
+
+static PyObject *
pylock_aenter(struct pylock *lock, PyObject *args)
{
struct pylock_op *op;