Have a way to wait until a worker state changes.
authorPierre Habouzit <madcoder@debian.org>
Sun, 13 Jan 2008 12:27:05 +0000 (13:27 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sun, 13 Jan 2008 12:28:16 +0000 (13:28 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
lib-sys/evtloop.c
lib-sys/evtloop.h

index 1d7e252..8e1d361 100644 (file)
@@ -37,7 +37,8 @@ DO_ARRAY_TYPE(job_t, job);
 
 static int epollfd = -1;
 static job_array jobs;
-static pthread_mutex_t mx;
+static pthread_mutex_t el_mx;
+static pthread_cond_t el_cond;
 static pthread_t el_thread;
 
 static int el_job_setemode(job_t *w, el_mode emode)
@@ -113,6 +114,10 @@ job_t *el_job_start(const machine_t *m, void *cfg)
 
 int el_job_release(job_t *w, el_status reason)
 {
+    if (w->cond) {
+        pthread_cond_signal(&el_cond);
+        w->cond = false;
+    }
     w->state = EL_LLP_FINI;
     if (w->m && w->m->finalize) {
         w->m->finalize(w, reason);
@@ -298,12 +303,12 @@ ssize_t el_job_write(job_t *w, buffer_t *buf)
 
 void el_lock(void)
 {
-    pthread_mutex_lock(&mx);
+    pthread_mutex_lock(&el_mx);
 }
 
 void el_unlock(void)
 {
-    pthread_mutex_unlock(&mx);
+    pthread_mutex_unlock(&el_mx);
 }
 
 int el_dispatch(int timeout)
@@ -324,6 +329,10 @@ int el_dispatch(int timeout)
         int event = events[count].events;
         int evt   = 0;
 
+        if (w->cond) {
+            pthread_cond_signal(&el_cond);
+            w->cond = false;
+        }
         gettimeofday(&w->mru, NULL);
         switch (w->state) {
           case EL_LLP_INIT:
@@ -354,6 +363,12 @@ int el_dispatch(int timeout)
     return 0;
 }
 
+void el_wait(job_t *w)
+{
+    w->cond = true;
+    pthread_cond_wait(&el_cond, &el_mx);
+}
+
 static void *el_loop(void *data)
 {
     time_t sec = time(NULL);
@@ -374,6 +389,10 @@ static void *el_loop(void *data)
         for (int i = jobs.len - 1; i >= 0; --i) {
             job_t *w = jobs.arr[i];
             if (timercmp(&now, &w->mru, >)) {
+                if (w->cond) {
+                    pthread_cond_signal(&el_cond);
+                    w->cond = false;
+                }
                 IGNORE(w->m->on_event(w, EL_EVT_WAKEUP));
             }
         }
@@ -387,7 +406,7 @@ void el_initialize(void)
 
     pthread_mutexattr_init(&attr);
     pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
-    pthread_mutex_init(&mx, &attr);
+    pthread_mutex_init(&el_mx, &attr);
     pthread_mutexattr_destroy(&attr);
 
     gnutls_global_init();
@@ -407,5 +426,5 @@ void el_shutdown(void)
     job_array_wipe(&jobs);
     close(epollfd);
     gnutls_global_deinit();
-    pthread_mutex_destroy(&mx);
+    pthread_mutex_destroy(&el_mx);
 }
index 961390a..bb7c50e 100644 (file)
@@ -58,13 +58,14 @@ typedef struct job_t {
     int fd;
     int ssf;
 
-    gnutls_session_t session;
-    gnutls_certificate_credentials_t xcred;
-
+    unsigned cond  : 1;
     el_state state : 2;
     el_mode  mode  : 3;
     el_mode  emode : 3;
 
+    gnutls_session_t session;
+    gnutls_certificate_credentials_t xcred;
+
     struct timeval mru;
     int (*llp)(struct job_t *);
     const struct machine_t *m;
@@ -97,6 +98,7 @@ __must_check__ ssize_t el_job_write(job_t *w, buffer_t *buf);
 
 void el_lock(void);
 void el_unlock(void);
+void el_wait(job_t *w);
 
 int el_dispatch(int timeout);