Blame src/regression/pthread_cond_wait-cancel_ignored.c
|
Szabolcs Nagy |
83e2fb |
// commit 76ca7a5446a8aec2b671a401d5e1878c4704754e
|
|
Szabolcs Nagy |
83e2fb |
// pthread_cond_wait should act on cancellation arriving after unlocking mutex
|
|
Szabolcs Nagy |
83e2fb |
|
|
Szabolcs Nagy |
83e2fb |
#include <pthread.h>
|
|
Szabolcs Nagy |
83e2fb |
#include <errno.h>
|
|
Szabolcs Nagy |
83e2fb |
#include <time.h>
|
|
Szabolcs Nagy |
83e2fb |
|
|
Szabolcs Nagy |
83e2fb |
#include "test.h"
|
|
Szabolcs Nagy |
83e2fb |
|
|
Szabolcs Nagy |
83e2fb |
static pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
|
|
Szabolcs Nagy |
83e2fb |
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
|
|
Szabolcs Nagy |
83e2fb |
static int waiting;
|
|
Szabolcs Nagy |
83e2fb |
|
|
Szabolcs Nagy |
83e2fb |
static void cleanup(void *p)
|
|
Szabolcs Nagy |
83e2fb |
{
|
|
Szabolcs Nagy |
83e2fb |
waiting = 0;
|
|
Szabolcs Nagy |
83e2fb |
pthread_cond_signal(&cv;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_mutex_unlock(&mx;;
|
|
Szabolcs Nagy |
83e2fb |
}
|
|
Szabolcs Nagy |
83e2fb |
|
|
Szabolcs Nagy |
83e2fb |
static void *waiter(void *p)
|
|
Szabolcs Nagy |
83e2fb |
{
|
|
Szabolcs Nagy |
83e2fb |
pthread_mutex_lock(&mx;;
|
|
Szabolcs Nagy |
83e2fb |
waiting = 1;
|
|
Szabolcs Nagy |
83e2fb |
pthread_cond_signal(&cv;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_cleanup_push(cleanup, 0);
|
|
Szabolcs Nagy |
83e2fb |
while (waiting) pthread_cond_wait(&cv, &mx;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_cleanup_pop(1);
|
|
Szabolcs Nagy |
83e2fb |
return 0;
|
|
Szabolcs Nagy |
83e2fb |
}
|
|
Szabolcs Nagy |
83e2fb |
|
|
Szabolcs Nagy |
83e2fb |
int main(void)
|
|
Szabolcs Nagy |
83e2fb |
{
|
|
Szabolcs Nagy |
83e2fb |
pthread_t td;
|
|
Szabolcs Nagy |
83e2fb |
struct timespec ts;
|
|
Szabolcs Nagy |
83e2fb |
void *rv;
|
|
Szabolcs Nagy |
83e2fb |
pthread_mutex_lock(&mx;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_create(&td, 0, waiter, 0);
|
|
Szabolcs Nagy |
83e2fb |
while (!waiting) pthread_cond_wait(&cv, &mx;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_cancel(td);
|
|
Szabolcs Nagy |
83e2fb |
clock_gettime(CLOCK_REALTIME, &ts);
|
|
Szabolcs Nagy |
83e2fb |
if ((ts.tv_nsec+=30000000) >= 1000000000) {
|
|
Szabolcs Nagy |
83e2fb |
ts.tv_sec++;
|
|
Szabolcs Nagy |
83e2fb |
ts.tv_nsec -= 1000000000;
|
|
Szabolcs Nagy |
83e2fb |
}
|
|
Szabolcs Nagy |
83e2fb |
while (waiting && !pthread_cond_timedwait(&cv, &mx, &ts);;
|
|
Szabolcs Nagy |
83e2fb |
waiting = 0;
|
|
Szabolcs Nagy |
83e2fb |
pthread_cond_signal(&cv;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_mutex_unlock(&mx;;
|
|
Szabolcs Nagy |
83e2fb |
pthread_join(td, &rv;;
|
|
Szabolcs Nagy |
83e2fb |
if (rv != PTHREAD_CANCELED)
|
|
Szabolcs Nagy |
83e2fb |
t_error("pthread_cond_wait did not act on cancellation\n");
|
|
Szabolcs Nagy |
83e2fb |
return t_status;
|
|
Szabolcs Nagy |
83e2fb |
}
|