static PyObject* rlock_release(RLockObject *self) { PyObject *current, *res; DEBUG("self:%p", self); current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } if (self->owner != current) { PyErr_SetString(PyExc_RuntimeError, "cannot release un-acquired lock"); return NULL; } self->count--; if (self->count <= 0) { Py_CLEAR(self->owner); self->owner = Py_None; Py_INCREF(self->owner); res = semaphore_release((SemaphoreObject*)self->block); Py_XDECREF(res); if (res == NULL) { return NULL; } } Py_RETURN_NONE; }
static PyObject* rlock_acquire(RLockObject *self, PyObject *blocking, long timeout) { PyObject *current, *res; int ret; DEBUG("self:%p", self); current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } if (self->owner == current) { self->count++; Py_RETURN_TRUE; } res = semaphore_acquire((SemaphoreObject*)self->block, blocking, timeout); if (res == NULL) { return NULL; } ret = PyObject_IsTrue(res); if (ret == -1) { return NULL; } if (ret) { self->owner = current; Py_INCREF(self->owner); self->count = 1; } return res; }
static PyObject* get_local_dict(void) { PyObject *current; PyObject *dict = NULL; current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } if (PyObject_HasAttr(current, dict_key) == 0) { dict = PyDict_New(); if (dict == NULL) { return NULL; } if(PyObject_SetAttr(current, dict_key, dict) == -1) { Py_DECREF(dict); return NULL; } } return PyObject_GetAttr(current, dict_key); }
handle current() { #if __S3E__ handle f = (handle)IwFibreGetCurrent(); return f; #elif GREENLETS handle f = (handle)greenlet_getcurrent(); return f; #else OX_ASSERT(!"not implemented"); return 0; #endif }
static PyObject* rlock_is_owned(RLockObject *self) { PyObject *current; DEBUG("self:%p", self); current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } if (self->owner == current) { Py_RETURN_TRUE; } else { Py_RETURN_FALSE; } }
PyObject* semaphore_acquire(SemaphoreObject *self, PyObject *blocking, long timeout) { PyObject *res = NULL; PyObject *current = NULL; PyObject *timer = NULL; LoopObject *loop = NULL; DEBUG("self:%p timeout:%ld counter:%d", self, timeout, self->counter); if (self->counter > 0) { self->counter--; DEBUG("counter decr self:%p", self); Py_RETURN_TRUE; } else if(PyObject_Not(blocking)) { Py_RETURN_FALSE; } loop = get_event_loop(); if (loop == NULL) { return NULL; } current = greenlet_getcurrent(); Py_XDECREF(current); if (current == NULL) { return NULL; } timer = loop_set_timeout(loop, timeout, NULL); if (timer == NULL) { return NULL; } if (PySet_Add(self->waiters, current) == -1) { ((TimerObject*)timer)->cancelled = 1; return NULL; } while (self->counter <= 0) { res = loop_switch(loop); Py_XDECREF(res); if (res == NULL) { if (PyErr_ExceptionMatches(TimeoutException)) { RDEBUG("catch TimeoutException self:%p", self); if (fetch_timer() == timer) { PyErr_Clear(); res = Py_False; goto fin; } } goto fin; } } self->counter--; res = Py_True; fin: ((TimerObject*)timer)->cancelled = 1; if (PySet_Discard(self->waiters, current) == -1) { res = NULL; } Py_DECREF(timer); Py_XINCREF(res); return res; }