|
Szabolcs Nagy |
1cc8c4 |
// testing cancellation points
|
|
Szabolcs Nagy |
1cc8c4 |
#include <errno.h>
|
|
Szabolcs Nagy |
1cc8c4 |
#include <pthread.h>
|
|
Szabolcs Nagy |
1cc8c4 |
#include <semaphore.h>
|
|
Szabolcs Nagy |
1cc8c4 |
#include <string.h>
|
|
Szabolcs Nagy |
1cc8c4 |
#include <sys/mman.h>
|
|
Szabolcs Nagy |
1cc8c4 |
#include "test.h"
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
#define TESTC(c, m) ( (c) || (t_error(#c " failed (%s, " m ")\n", cdescr), 0) )
|
|
Szabolcs Nagy |
1cc8c4 |
#define TESTR(f, m) do {int r; \
|
|
Szabolcs Nagy |
1cc8c4 |
if ((r = (f))) t_error(#f " failed: %s (%s, " m ")\n", strerror(r), cdescr); } while (0)
|
|
Szabolcs Nagy |
1cc8c4 |
#define TESTE(f, m) do { \
|
|
Szabolcs Nagy |
1cc8c4 |
if ((f)==-1) t_error(#f " failed: %s (%s, " m ")\n", strerror(errno), cdescr); } while (0)
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static sem_t sem_seq, sem_test;
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static int seqno;
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static const char *cdescr = "global initialization";
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void prepare_sem(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(sem_init(&sem_test, 0, (long)arg), "creating semaphore");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void cleanup_sem(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(sem_destroy(&sem_test), "destroying semaphore");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void execute_sem_wait(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(sem_wait(&sem_test), "waiting on semaphore in the canceled thread");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void execute_sem_timedwait(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
struct timespec ts;
|
|
Szabolcs Nagy |
1cc8c4 |
clock_gettime(CLOCK_REALTIME, &ts);
|
|
Szabolcs Nagy |
1cc8c4 |
ts.tv_sec += 1;
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(sem_timedwait(&sem_test, &ts), "timed-waiting on semaphore in the canceled thread");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static pthread_t td_test;
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void *run_test(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
while (sem_wait(&sem_test));
|
|
Szabolcs Nagy |
1cc8c4 |
return 0;
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void prepare_thread(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
prepare_sem(arg);
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(pthread_create(&td_test, 0, run_test, 0), "creating auxiliary thread");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void cleanup_thread(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
void *res;
|
|
Szabolcs Nagy |
700e06 |
if (td_test) {
|
|
Szabolcs Nagy |
700e06 |
TESTR(sem_post(&sem_test), "posting semaphore");
|
|
Szabolcs Nagy |
700e06 |
TESTR(pthread_join(td_test, &res), "joining auxiliary thread");
|
|
Szabolcs Nagy |
700e06 |
TESTC(res == 0, "auxiliary thread exit status");
|
|
Szabolcs Nagy |
700e06 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
cleanup_sem(arg);
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void execute_thread_join(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(pthread_join(td_test, 0), "joining in the canceled thread");
|
|
Szabolcs Nagy |
1cc8c4 |
td_test = 0;
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void prepare_dummy(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void execute_shm_open(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
int *fd = arg;
|
|
Szabolcs Nagy |
1cc8c4 |
TESTE(*fd = shm_open("/testshm", O_RDWR|O_CREAT, 0666), "");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void cleanup_shm(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
int *fd = arg;
|
|
Szabolcs Nagy |
700e06 |
if (*fd != -1)
|
|
Szabolcs Nagy |
700e06 |
TESTE(close(*fd), "shm fd");
|
|
Szabolcs Nagy |
1cc8c4 |
TESTE(shm_unlink("/testshm"), "");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static struct {
|
|
Szabolcs Nagy |
1cc8c4 |
int want_cancel;
|
|
Szabolcs Nagy |
1cc8c4 |
void (*prepare)(void *);
|
|
Szabolcs Nagy |
1cc8c4 |
void (*execute)(void *);
|
|
Szabolcs Nagy |
1cc8c4 |
void (*cleanup)(void *);
|
|
Szabolcs Nagy |
1cc8c4 |
void *arg;
|
|
Szabolcs Nagy |
1cc8c4 |
const char *descr;
|
|
Szabolcs Nagy |
1cc8c4 |
} scenarios[] = {
|
|
Szabolcs Nagy |
1cc8c4 |
{1, prepare_sem, execute_sem_wait, cleanup_sem, 0, "blocking sem_wait"},
|
|
Szabolcs Nagy |
1cc8c4 |
{1, prepare_sem, execute_sem_wait, cleanup_sem, (void*)1, "non-blocking sem_wait"},
|
|
Szabolcs Nagy |
1cc8c4 |
{1, prepare_sem, execute_sem_timedwait, cleanup_sem, 0, "blocking sem_timedwait"},
|
|
Szabolcs Nagy |
1cc8c4 |
{1, prepare_sem, execute_sem_timedwait, cleanup_sem, (void*)1, "non-blocking sem_timedwait"},
|
|
Szabolcs Nagy |
1cc8c4 |
{1, prepare_thread, execute_thread_join, cleanup_thread, 0, "blocking pthread_join"},
|
|
Szabolcs Nagy |
1cc8c4 |
{1, prepare_thread, execute_thread_join, cleanup_thread, (void*)1, "non-blocking pthread_join"},
|
|
Szabolcs Nagy |
1cc8c4 |
{0, prepare_dummy, execute_shm_open, cleanup_shm, &(int){0}, "shm_open"},
|
|
Szabolcs Nagy |
1cc8c4 |
{ 0 }
|
|
Szabolcs Nagy |
1cc8c4 |
}, *cur_sc = scenarios;
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
static void *run_execute(void *arg)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0);
|
|
Szabolcs Nagy |
1cc8c4 |
while (sem_wait(&sem_seq));
|
|
Szabolcs Nagy |
1cc8c4 |
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, 0);
|
|
Szabolcs Nagy |
1cc8c4 |
seqno = 1;
|
|
Szabolcs Nagy |
1cc8c4 |
cur_sc->execute(cur_sc->arg);
|
|
Szabolcs Nagy |
1cc8c4 |
seqno = 2;
|
|
Szabolcs Nagy |
1cc8c4 |
return 0;
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
int main(void)
|
|
Szabolcs Nagy |
1cc8c4 |
{
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(sem_init(&sem_seq, 0, 0), "creating semaphore");
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
for (; cur_sc->prepare; cur_sc++) {
|
|
Szabolcs Nagy |
1cc8c4 |
pthread_t td;
|
|
Szabolcs Nagy |
1cc8c4 |
void *res;
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
cdescr = cur_sc->descr;
|
|
Szabolcs Nagy |
1cc8c4 |
cur_sc->prepare(cur_sc->arg);
|
|
Szabolcs Nagy |
1cc8c4 |
seqno = 0;
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(pthread_create(&td, 0, run_execute, 0), "creating thread to be canceled");
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(pthread_cancel(td), "canceling");
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(sem_post(&sem_seq), "unblocking canceled thread");
|
|
Szabolcs Nagy |
1cc8c4 |
TESTR(pthread_join(td, &res), "joining canceled thread");
|
|
Szabolcs Nagy |
1cc8c4 |
if (cur_sc->want_cancel) {
|
|
Szabolcs Nagy |
1cc8c4 |
TESTC(res == PTHREAD_CANCELED, "canceled thread exit status")
|
|
Szabolcs Nagy |
1cc8c4 |
&& TESTC(seqno == 1, "seqno");
|
|
Szabolcs Nagy |
1cc8c4 |
} else {
|
|
Szabolcs Nagy |
1cc8c4 |
TESTC(res != PTHREAD_CANCELED, "canceled thread exit status")
|
|
Szabolcs Nagy |
1cc8c4 |
&& TESTC(seqno == 2, "seqno");
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
cur_sc->cleanup(cur_sc->arg);
|
|
Szabolcs Nagy |
1cc8c4 |
}
|
|
Szabolcs Nagy |
1cc8c4 |
|
|
Szabolcs Nagy |
1cc8c4 |
return t_status;
|
|
Szabolcs Nagy |
1cc8c4 |
}
|