+ struct thread *nh, *ct = current_thread;
+
+ if (ct && evmgr && evmgr_release_if(evmgr, ct)) {
+ nh = ct->nholder;
+ ct->nholder = 0;
+ if (nh) {
+ evmgr_try_hold(evmgr, nh);
+ pthread_cond_signal(nh->cwhold);
+ }
+ }
+}
+
+/**
+ * get the eventloop for the current thread
+ */
+static int evloop_get()
+{
+ return evmgr && evmgr_try_hold(evmgr, current_thread);
+}
+
+/**
+ * acquire the eventloop for the current thread
+ */
+static void evloop_acquire()
+{
+ struct thread *pwait, *ct;
+ pthread_cond_t cond;
+
+ /* try to get the evloop */
+ if (!evloop_get()) {
+ /* failed, init waiting state */
+ ct = current_thread;
+ ct->nholder = NULL;
+ ct->cwhold = &cond;
+ pthread_cond_init(&cond, NULL);
+
+ /* queue current thread in holder list */
+ pwait = evmgr_holder(evmgr);
+ while (pwait->nholder)
+ pwait = pwait->nholder;
+ pwait->nholder = ct;
+
+ /* wake up the evloop */
+ evloop_wakeup();
+
+ /* wait to acquire the evloop */
+ pthread_cond_wait(&cond, &mutex);
+ pthread_cond_destroy(&cond);
+ }
+}