From 423c21bee223e2f7a22e1976cdff8e944293a9a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Tue, 24 Dec 2013 02:30:22 +0100 Subject: [PATCH] Use a separate variable for the exit condition --- test/iomux_wake.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/test/iomux_wake.c b/test/iomux_wake.c index 086f2c9..31c7535 100644 --- a/test/iomux_wake.c +++ b/test/iomux_wake.c @@ -8,21 +8,24 @@ pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; static int counter = 0; +static int please_exit = 0; void wake_cb(struct IOMuxWaker *wk) { int local_counter; + int exit_now; int rc; rc = pthread_mutex_lock(&lock); assert(rc == 0); local_counter = counter; + exit_now = please_exit; printf("counter is %d\n", local_counter); rc = pthread_mutex_unlock(&lock); assert(rc == 0); - if (local_counter == 13) { + if (exit_now) { rc = uc_iomux_waker_destroy(wk); assert(rc == 0); } @@ -59,6 +62,15 @@ void *waker(void *arg) rc = uc_iomux_waker_signal(s); assert(rc == 0); } + rc = pthread_mutex_lock(&lock); + assert(rc == 0); + please_exit = 1; + //must wake it inside the mutex, else we risk a race + //when the waker is destroyed + rc = uc_iomux_waker_signal(s); + assert(rc == 0); + + rc = pthread_mutex_unlock(&lock); return NULL; }