op_runfbapp: fix thread and fbapp race
authorGrazvydas Ignotas <notasas@gmail.com>
Mon, 9 Apr 2012 18:39:44 +0000 (21:39 +0300)
committerGrazvydas Ignotas <notasas@gmail.com>
Mon, 9 Apr 2012 18:39:44 +0000 (21:39 +0300)
op_runfbapp.c

index 8488aaa..495b493 100644 (file)
 
 static struct termios g_kbd_termios_saved;
 static int g_kbdfd;
+static pthread_cond_t g_start_cond = PTHREAD_COND_INITIALIZER;
+static pthread_mutex_t g_start_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void signal_main_thread(void)
+{
+       pthread_mutex_lock(&g_start_mutex);
+       pthread_cond_signal(&g_start_cond);
+       pthread_mutex_unlock(&g_start_mutex);
+}
 
 static Cursor transparent_cursor(Display *display, Window win)
 {
@@ -74,6 +83,7 @@ static void *x11_handler(void *arg)
        {
                fprintf(stderr, PFX "(not hiding X): Can't open display: %s\n",
                        XDisplayName(NULL));
+               signal_main_thread();
                return NULL;
        }
 
@@ -95,14 +105,17 @@ static void *x11_handler(void *arg)
        XSelectInput(display, win, ExposureMask);
        XMapWindow(display, win);
        XGrabKeyboard(display, win, False, GrabModeAsync, GrabModeAsync, CurrentTime);
+       XSync(display, False);
 
        while (1)
        {
                XNextEvent(display, &report);
 
-               if (report.type == Expose)
+               if (report.type == Expose) {
+                       signal_main_thread();
                        while (XCheckTypedEvent(display, Expose, &report))
                                ;
+               }
        }
 
        return NULL;
@@ -196,6 +209,8 @@ int main(int argc, char *argv[])
        pthread_t tid;
        int ret;
 
+       pthread_mutex_lock(&g_start_mutex);
+
        ret = pthread_create(&tid, NULL, x11_handler, NULL);
        if (ret != 0) {
                fprintf(stderr, PFX "pthread_create: %d\n", ret);
@@ -203,6 +218,9 @@ int main(int argc, char *argv[])
        }
        pthread_detach(tid);
 
+       pthread_cond_wait(&g_start_cond, &g_start_mutex);
+       pthread_mutex_unlock(&g_start_mutex);
+
        hidecon_start();
 
        do_exec(argv + 1);