1 // sem_wait and sem_timedwait are cancellation points
7 #define TESTC(c, m) ( (c) || (t_error("%s failed (" m ")\n", #c), 0) )
8 #define TESTR(r, f, m) ( \
9 ((r) = (f)) == 0 || (t_error("%s failed: %s (" m ")\n", #f, strerror(r)), 0) )
11 static sem_t sem1
, sem2
;
15 static void wait_cancel(void *arg
)
17 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, 0);
18 while (sem_wait(&sem1
));
19 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, 0);
23 static void *start_sem_wait(void *arg
)
31 static void *start_sem_timedwait(void *arg
)
34 sem_timedwait(&sem2
, &(struct timespec
){1, 0});
45 TESTR(r
, sem_init(&sem1
, 0, 0), "creating semaphore");
46 TESTR(r
, sem_init(&sem2
, 0, 1), "creating semaphore");
48 /* Cancellation on uncontended sem_wait */
50 TESTR(r
, pthread_create(&td
, 0, start_sem_wait
, 0), "failed to create thread");
51 TESTR(r
, pthread_cancel(td
), "canceling");
53 TESTR(r
, pthread_join(td
, &res
), "joining canceled thread after uncontended sem_wait");
54 TESTC(res
== PTHREAD_CANCELED
, "canceled thread exit status after uncontended sem_wait");
55 TESTC(seqno
== 1, "uncontended sem_wait");
57 /* Cancellation on blocking sem_wait */
60 TESTR(r
, pthread_create(&td
, 0, start_sem_wait
, 0), "failed to create thread");
61 TESTR(r
, pthread_cancel(td
), "canceling");
63 TESTR(r
, pthread_join(td
, &res
), "joining canceled thread after blocking sem_wait");
64 TESTC(res
== PTHREAD_CANCELED
, "canceled thread exit status after blocking sem_wait");
65 TESTC(seqno
== 1, "blocking sem_wait");
67 /* Cancellation on uncontended sem_timedwait */
70 TESTR(r
, pthread_create(&td
, 0, start_sem_timedwait
, 0), "failed to create thread");
71 TESTR(r
, pthread_cancel(td
), "canceling");
73 TESTR(r
, pthread_join(td
, &res
), "joining canceled thread after uncontended sem_timedwait");
74 TESTC(res
== PTHREAD_CANCELED
, "canceled thread exit status after uncontended sem_timedwait");
75 TESTC(seqno
== 1, "uncontended sem_timedwait");
77 /* Cancellation on blocking sem_timedwait */
80 TESTR(r
, pthread_create(&td
, 0, start_sem_timedwait
, 0), "failed to create thread");
81 TESTR(r
, pthread_cancel(td
), "canceling");
83 TESTR(r
, pthread_join(td
, &res
), "joining canceled thread after blocking sem_timedwait");
84 TESTC(res
== PTHREAD_CANCELED
, "canceled thread exit status after blocking sem_timedwait");
85 TESTC(seqno
== 1, "blocking sem_timedwait");