|
Szabolcs Nagy |
891282 |
// sem_wait and sem_timedwait are cancellation points
|
|
Szabolcs Nagy |
891282 |
#include <pthread.h>
|
|
Szabolcs Nagy |
891282 |
#include <semaphore.h>
|
|
Szabolcs Nagy |
891282 |
#include <string.h>
|
|
Szabolcs Nagy |
891282 |
#include "test.h"
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
#define TESTC(c, m) ( (c) || (t_error("%s failed (" m ")\n", #c), 0) )
|
|
Szabolcs Nagy |
891282 |
#define TESTR(r, f, m) ( \
|
|
Szabolcs Nagy |
891282 |
((r) = (f)) == 0 || (t_error("%s failed: %s (" m ")\n", #f, strerror(r)), 0) )
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
static sem_t sem1, sem2;
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
static int seqno;
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
static void wait_cancel(void *arg)
|
|
Szabolcs Nagy |
891282 |
{
|
|
Szabolcs Nagy |
891282 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
|
|
Szabolcs Nagy |
891282 |
while (sem_wait(&sem1));
|
|
Szabolcs Nagy |
891282 |
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
|
|
Szabolcs Nagy |
891282 |
seqno = 1;
|
|
Szabolcs Nagy |
891282 |
}
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
static void *start_sem_wait(void *arg)
|
|
Szabolcs Nagy |
891282 |
{
|
|
Szabolcs Nagy |
891282 |
wait_cancel(arg);
|
|
Szabolcs Nagy |
891282 |
sem_wait(&sem2;;
|
|
Szabolcs Nagy |
891282 |
seqno = 2;
|
|
Szabolcs Nagy |
891282 |
return 0;
|
|
Szabolcs Nagy |
891282 |
}
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
static void *start_sem_timedwait(void *arg)
|
|
Szabolcs Nagy |
891282 |
{
|
|
Szabolcs Nagy |
891282 |
wait_cancel(arg);
|
|
Szabolcs Nagy |
891282 |
sem_timedwait(&sem2, &(struct timespec){1, 0});
|
|
Szabolcs Nagy |
891282 |
seqno = 2;
|
|
Szabolcs Nagy |
891282 |
return 0;
|
|
Szabolcs Nagy |
891282 |
}
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
int main(void)
|
|
Szabolcs Nagy |
891282 |
{
|
|
Szabolcs Nagy |
891282 |
pthread_t td;
|
|
Szabolcs Nagy |
891282 |
int r;
|
|
Szabolcs Nagy |
891282 |
void *res;
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
TESTR(r, sem_init(&sem1, 0, 0), "creating semaphore");
|
|
Szabolcs Nagy |
891282 |
TESTR(r, sem_init(&sem2, 0, 1), "creating semaphore");
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
/* Cancellation on uncontended sem_wait */
|
|
Szabolcs Nagy |
891282 |
seqno = 0;
|
|
Szabolcs Nagy |
891282 |
TESTR(r, pthread_create(&td, 0, start_sem_wait, 0), "failed to create thread");
|
|
Szabolcs Nagy |
891282 |
TESTR(r, pthread_cancel(td), "canceling");
|
|
Szabolcs Nagy |
891282 |
sem_post(&sem1;;
|
|
Szabolcs Nagy |
3bd0bf |
TESTR(r, pthread_join(td, &res), "joining canceled thread after uncontended sem_wait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(res == PTHREAD_CANCELED, "canceled thread exit status after uncontended sem_wait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(seqno == 1, "uncontended sem_wait");
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
/* Cancellation on blocking sem_wait */
|
|
Szabolcs Nagy |
891282 |
seqno = 0;
|
|
Szabolcs Nagy |
891282 |
sem_trywait(&sem2;;
|
|
Szabolcs Nagy |
891282 |
TESTR(r, pthread_create(&td, 0, start_sem_wait, 0), "failed to create thread");
|
|
Szabolcs Nagy |
891282 |
TESTR(r, pthread_cancel(td), "canceling");
|
|
Szabolcs Nagy |
891282 |
sem_post(&sem1;;
|
|
Szabolcs Nagy |
3bd0bf |
TESTR(r, pthread_join(td, &res), "joining canceled thread after blocking sem_wait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(res == PTHREAD_CANCELED, "canceled thread exit status after blocking sem_wait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(seqno == 1, "blocking sem_wait");
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
/* Cancellation on uncontended sem_timedwait */
|
|
Szabolcs Nagy |
891282 |
seqno = 0;
|
|
Szabolcs Nagy |
891282 |
sem_post(&sem2;;
|
|
Szabolcs Nagy |
3bd0bf |
TESTR(r, pthread_create(&td, 0, start_sem_timedwait, 0), "failed to create thread");
|
|
Szabolcs Nagy |
891282 |
TESTR(r, pthread_cancel(td), "canceling");
|
|
Szabolcs Nagy |
891282 |
sem_post(&sem1;;
|
|
Szabolcs Nagy |
3bd0bf |
TESTR(r, pthread_join(td, &res), "joining canceled thread after uncontended sem_timedwait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(res == PTHREAD_CANCELED, "canceled thread exit status after uncontended sem_timedwait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(seqno == 1, "uncontended sem_timedwait");
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
/* Cancellation on blocking sem_timedwait */
|
|
Szabolcs Nagy |
891282 |
seqno = 0;
|
|
Szabolcs Nagy |
891282 |
sem_trywait(&sem2;;
|
|
Szabolcs Nagy |
3bd0bf |
TESTR(r, pthread_create(&td, 0, start_sem_timedwait, 0), "failed to create thread");
|
|
Szabolcs Nagy |
891282 |
TESTR(r, pthread_cancel(td), "canceling");
|
|
Szabolcs Nagy |
891282 |
sem_post(&sem1;;
|
|
Szabolcs Nagy |
3bd0bf |
TESTR(r, pthread_join(td, &res), "joining canceled thread after blocking sem_timedwait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(res == PTHREAD_CANCELED, "canceled thread exit status after blocking sem_timedwait");
|
|
Szabolcs Nagy |
3bd0bf |
TESTC(seqno == 1, "blocking sem_timedwait");
|
|
Szabolcs Nagy |
891282 |
|
|
Szabolcs Nagy |
891282 |
return t_status;
|
|
Szabolcs Nagy |
891282 |
}
|