1 // commit 76ca7a5446a8aec2b671a401d5e1878c4704754e
2 // pthread_cond_wait should act on cancellation arriving after unlocking mutex
10 static pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
11 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
14 static void cleanup(void *p)
17 pthread_cond_signal(&cv);
18 pthread_mutex_unlock(&mx);
21 static void *waiter(void *p)
23 pthread_mutex_lock(&mx);
25 pthread_cond_signal(&cv);
26 pthread_cleanup_push(cleanup, 0);
27 while (waiting) pthread_cond_wait(&cv, &mx);
28 pthread_cleanup_pop(1);
37 pthread_mutex_lock(&mx);
38 pthread_create(&td, 0, waiter, 0);
39 while (!waiting) pthread_cond_wait(&cv, &mx);
41 clock_gettime(CLOCK_REALTIME, &ts);
42 if ((ts.tv_nsec+=30000000) >= 1000000000) {
44 ts.tv_nsec -= 1000000000;
46 while (waiting && !pthread_cond_timedwait(&cv, &mx, &ts));
48 pthread_cond_signal(&cv);
49 pthread_mutex_unlock(&mx);
50 pthread_join(td, &rv);
51 if (rv != PTHREAD_CANCELED)
52 t_error("pthread_cond_wait did not act on cancellation\n");