Blame src/functional/pthread.c

nsz bcf192
#include <pthread.h>
nsz 8f27a3
#include <semaphore.h>
nsz bcf192
#include <stdlib.h>
nsz bcf192
#include <unistd.h>
nsz bcf192
#include <stdio.h>
nsz bcf192
#include <errno.h>
nsz bcf192
#include <string.h>
nsz 7f409d
#include <signal.h>
nsz bcf192
#include "test.h"
nsz bcf192
nsz bcf192
#define TEST(r, f, x, m) ( \
nsz 7f409d
	msg = #f, ((r) = (f)) == (x) || \
nsz bcf192
	(error("%s failed (" m ")\n", #f, r, x), 0) )
nsz bcf192
nsz bcf192
#define TEST_S(s, x, m) ( \
nsz bcf192
	!strcmp((s),(x)) || \
nsz bcf192
	(error("[%s] != [%s] (%s)\n", s, x, m), 0) )
nsz bcf192
nsz 7f409d
static volatile char *msg = "";
nsz 7f409d
nsz 7f409d
static void alarmhandler(int sig) {
nsz 7f409d
	error("timeout in %s\n", msg);
nsz 7f409d
	_Exit(1);
nsz 7f409d
}
nsz 7f409d
nsz bcf192
static pthread_key_t k1, k2;
nsz bcf192
nsz bcf192
static void dtor(void *p)
nsz bcf192
{
nsz bcf192
	*(int *)p = 1;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start1(void *arg)
nsz bcf192
{
nsz bcf192
	return arg;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start2(void *arg)
nsz bcf192
{
nsz bcf192
	int *p = arg;
nsz bcf192
	if (pthread_setspecific(k1, p) || pthread_setspecific(k2, p+1))
nsz bcf192
		return arg;
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start3(void *arg)
nsz bcf192
{
nsz bcf192
	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, 0);
nsz 8f27a3
	sem_post(arg);
nsz bcf192
	for (;;);
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void cleanup4(void *arg)
nsz bcf192
{
nsz bcf192
	*(int *)arg = 1;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start4(void *arg)
nsz bcf192
{
nsz bcf192
	pthread_cleanup_push(cleanup4, arg);
nsz bcf192
	sleep(3);
nsz bcf192
	pthread_cleanup_pop(0);
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void cleanup4a2(void *arg)
nsz bcf192
{
nsz bcf192
	*(int *)arg += 2;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void cleanup4a3(void *arg)
nsz bcf192
{
nsz bcf192
	*(int *)arg += 3;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void cleanup4a4(void *arg)
nsz bcf192
{
nsz bcf192
	*(int *)arg += 4;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start4a(void *arg)
nsz bcf192
{
nsz bcf192
	int *foo = arg;
nsz bcf192
	pthread_cleanup_push(cleanup4, foo);
nsz bcf192
	pthread_cleanup_push(cleanup4a2, foo+1);
nsz bcf192
	pthread_cleanup_push(cleanup4a3, foo+2);
nsz bcf192
	pthread_cleanup_push(cleanup4a4, foo+3);
nsz bcf192
	sleep(3);
nsz bcf192
	pthread_cleanup_pop(0);
nsz bcf192
	pthread_cleanup_pop(0);
nsz bcf192
	pthread_cleanup_pop(0);
nsz bcf192
	pthread_cleanup_pop(0);
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start5(void *arg)
nsz bcf192
{
nsz bcf192
	pthread_mutex_lock(arg);
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start6(void *arg)
nsz bcf192
{
nsz bcf192
	void **args = arg;
nsz bcf192
	pthread_mutex_lock(args[1]);
nsz bcf192
	pthread_barrier_wait(args[0]);
nsz bcf192
	nanosleep(&(struct timespec){ .tv_nsec = 10000000 }, 0);
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz bcf192
static void *start7(void *arg)
nsz bcf192
{
nsz bcf192
	void **args = arg;
nsz bcf192
	pthread_mutex_lock(args[1]);
nsz bcf192
	pthread_cond_signal(args[0]);
nsz bcf192
	pthread_mutex_unlock(args[1]);
nsz bcf192
	return 0;
nsz bcf192
}
nsz bcf192
nsz 8f27a3
static void *start8(void *arg)
nsz 8f27a3
{
nsz 8f27a3
	void **args = arg;
nsz 8f27a3
	pthread_mutex_t *m = args[1];
nsz 8f27a3
	pthread_cond_t *c = args[0];
nsz 8f27a3
	int *x = args[2];
nsz 8f27a3
nsz 8f27a3
	pthread_mutex_lock(m);
nsz 8f27a3
	while (*x) pthread_cond_wait(c, m);
nsz 8f27a3
	pthread_mutex_unlock(m);
nsz 8f27a3
nsz 8f27a3
	return 0;
nsz 8f27a3
}
nsz 8f27a3
nsz 462b4f
int main(void)
nsz bcf192
{
nsz 8f27a3
	pthread_t td, td1, td2, td3;
nsz bcf192
	int r;
nsz bcf192
	void *res;
nsz bcf192
	int foo[4], bar[2];
nsz bcf192
	pthread_barrier_t barrier2;
nsz 8f27a3
	pthread_mutexattr_t mtx_a;
nsz 8f27a3
	sem_t sem1;
nsz bcf192
	pthread_mutex_t mtx;
nsz bcf192
	pthread_cond_t cond;
nsz bcf192
nsz 7f409d
	signal(SIGALRM, alarmhandler);
nsz 7f409d
	alarm(10);
nsz 7f409d
nsz bcf192
	TEST(r, pthread_barrier_init(&barrier2, 0, 2), 0, "creating barrier");
nsz 8f27a3
	TEST(r, sem_init(&sem1, 0, 0), 0, "creating semaphore");
nsz bcf192
nsz bcf192
	/* Test basic thread creation and joining */
nsz bcf192
	TEST(r, pthread_create(&td, 0, start1, &res), 0, "failed to create thread");
nsz bcf192
	res = 0;
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "failed to join");
nsz bcf192
	TEST(r, (res==&res), 1, "wrong result from join");
nsz bcf192
nsz bcf192
	/* Test POSIX thread-specific data */
nsz bcf192
	TEST(r, pthread_key_create(&k1, dtor), 0, "failed to create key");
nsz bcf192
	TEST(r, pthread_key_create(&k2, dtor), 0, "failed to create key");
nsz bcf192
	foo[0] = foo[1] = 0;
nsz bcf192
	TEST(r, pthread_setspecific(k1, bar), 0, "failed to set tsd");
nsz bcf192
	TEST(r, pthread_setspecific(k2, bar+1), 0, "failed to set tsd");
nsz bcf192
	TEST(r, pthread_create(&td, 0, start2, foo), 0, "failed to create thread");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "failed to join");
nsz bcf192
	TEST(res, res, 0, "pthread_setspecific failed in thread");
nsz bcf192
	TEST(r, foo[0], 1, "dtor failed to run");
nsz bcf192
	TEST(r, foo[1], 1, "dtor failed to run");
nsz bcf192
	TEST(res, pthread_getspecific(k1), bar, "tsd corrupted");
nsz bcf192
	TEST(res, pthread_getspecific(k2), bar+1, "tsd corrupted");
nsz bcf192
	TEST(r, pthread_setspecific(k1, 0), 0, "failed to clear tsd");
nsz bcf192
	TEST(r, pthread_setspecific(k2, 0), 0, "failed to clear tsd");
nsz bcf192
	TEST(r, pthread_key_delete(k1), 0, "failed to destroy key");
nsz bcf192
	TEST(r, pthread_key_delete(k2), 0, "failed to destroy key");
nsz bcf192
nsz bcf192
	/* Asynchronous cancellation */
nsz 8f27a3
	TEST(r, pthread_create(&td, 0, start3, &sem1), 0, "failed to create thread");
nsz 8f27a3
	while (sem_wait(&sem1));
nsz bcf192
	TEST(r, pthread_cancel(td), 0, "canceling");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
nsz bcf192
	TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
nsz bcf192
nsz bcf192
	/* Cancellation cleanup handlers */
nsz bcf192
	foo[0] = 0;
nsz bcf192
	TEST(r, pthread_create(&td, 0, start4, foo), 0, "failed to create thread");
nsz bcf192
	TEST(r, pthread_cancel(td), 0, "cancelling");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
nsz bcf192
	TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
nsz bcf192
	TEST(r, foo[0], 1, "cleanup handler failed to run");
nsz bcf192
nsz bcf192
	/* Nested cleanup handlers */
nsz bcf192
	memset(foo, 0, sizeof foo);
nsz bcf192
	TEST(r, pthread_create(&td, 0, start4a, foo), 0, "failed to create thread");
nsz bcf192
	TEST(r, pthread_cancel(td), 0, "cancelling");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "joining canceled thread");
nsz 8f27a3
	TEST(res, res, PTHREAD_CANCELED, "canceled thread exit status");
nsz bcf192
	TEST(r, foo[0], 1, "cleanup handler failed to run");
nsz bcf192
	TEST(r, foo[1], 2, "cleanup handler failed to run");
nsz bcf192
	TEST(r, foo[2], 3, "cleanup handler failed to run");
nsz bcf192
	TEST(r, foo[3], 4, "cleanup handler failed to run");
nsz bcf192
nsz bcf192
	/* Robust mutexes */
nsz bcf192
	TEST(r, pthread_mutexattr_init(&mtx_a), 0, "initializing mutex attr");
nsz bcf192
	TEST(r, pthread_mutexattr_setrobust(&mtx_a, PTHREAD_MUTEX_ROBUST), 0, "setting robust attribute");
nsz bcf192
	TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), 0, "locking robust mutex");
nsz bcf192
	TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking robust mutex");
nsz bcf192
	TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "joining thread");
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), ENOTRECOVERABLE, "re-locking orphaned robust mutex %d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying unrecoverable mutex %d!=%d");
nsz bcf192
nsz bcf192
	TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "initializing robust mutex");
nsz bcf192
	TEST(r, pthread_create(&td, 0, start5, &mtx), 0, "failed to create thread");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "joining thread");
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "locking orphaned robust mutex %d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_consistent(&mtx), 0, "%d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_unlock(&mtx), 0, "unlocking orphaned robust mutex %d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), 0, "re-locking orphaned robust mutex %d!=%d");
nsz bcf192
	TEST(r, pthread_mutex_destroy(&mtx), 0, "destroying mutex %d!=%d");
nsz bcf192
nsz bcf192
	TEST(r, pthread_mutex_init(&mtx, &mtx_a), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_create(&td, 0, start6, (void *[]){ &barrier2, &mtx }), 0, "%d != %d");
nsz bcf192
	pthread_barrier_wait(&barrier2);
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), EOWNERDEAD, "%d != %d");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_mutex_consistent(&mtx), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
nsz 8f27a3
nsz bcf192
	//TEST(r, (fd=open("/dev/zero", O_RDWR))>=0, 1, "opening zero page file");
nsz bcf192
	//TEST(r, 
nsz bcf192
nsz bcf192
	/* Condition variables */
nsz bcf192
	TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_create(&td, 0, start7, (void *[]){ &cond, &mtx }), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_cond_wait(&cond, &mtx), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_join(td, &res), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
nsz bcf192
	TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
nsz 8f27a3
nsz 8f27a3
	/* Condition variables with multiple waiters */
nsz 8f27a3
	TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz 8f27a3
	foo[0] = 1;
nsz 8f27a3
	TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz 8f27a3
	nanosleep(&(struct timespec){.tv_nsec=1000000}, 0);
nsz 8f27a3
	foo[0] = 0;
nsz 8f27a3
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_signal(&cond), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_signal(&cond), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_signal(&cond), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_join(td1, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_join(td2, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_join(td3, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
nsz 8f27a3
nsz 8f27a3
	/* Condition variables with broadcast signals */
nsz 8f27a3
	TEST(r, pthread_mutex_init(&mtx, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_init(&cond, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz 8f27a3
	foo[0] = 1;
nsz 8f27a3
	TEST(r, pthread_create(&td1, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_create(&td2, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_create(&td3, 0, start8, (void *[]){ &cond, &mtx, foo }), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz 8f27a3
	nanosleep(&(struct timespec){.tv_nsec=1000000}, 0);
nsz 8f27a3
	TEST(r, pthread_mutex_lock(&mtx), 0, "%d != %d");
nsz 8f27a3
	foo[0] = 0;
nsz 8f27a3
	TEST(r, pthread_mutex_unlock(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_broadcast(&cond), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_join(td1, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_join(td2, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_join(td3, 0), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_mutex_destroy(&mtx), 0, "%d != %d");
nsz 8f27a3
	TEST(r, pthread_cond_destroy(&cond), 0, "%d != %d");
nsz 462b4f
	return test_status;
nsz bcf192
}