Blame src/functional/pthread_mutex_pi.c

Rich Felker 2064be
/* testing pthread mutex behaviour with various attributes */
Rich Felker 2064be
#include <pthread.h>
Rich Felker 2064be
#include <semaphore.h>
Rich Felker 2064be
#include <stdio.h>
Rich Felker 2064be
#include <errno.h>
Rich Felker 2064be
#include <string.h>
Rich Felker 2064be
#include "test.h"
Rich Felker 2064be
Rich Felker 2064be
#define T(f) if ((r=(f))) t_error(#f " failed: %s\n", strerror(r))
Rich Felker 2064be
#define E(f) if (f) t_error(#f " failed: %s\n", strerror(errno))
Rich Felker 2064be
Rich Felker 2064be
static void *relock(void *arg)
Rich Felker 2064be
{
Rich Felker 2064be
	void **a = arg;
Rich Felker 2064be
	int r;
Rich Felker 2064be
Rich Felker 2064be
	T(pthread_mutex_lock(a[0]));
Rich Felker 2064be
	E(sem_post(a[1]));
Rich Felker 2064be
	*(int*)a[2] = pthread_mutex_lock(a[0]);
Rich Felker 2064be
	E(sem_post(a[1]));
Rich Felker 2064be
Rich Felker 2064be
	T(pthread_mutex_unlock(a[0]));
Rich Felker 2064be
	if (*(int*)a[2] == 0)
Rich Felker 2064be
		T(pthread_mutex_unlock(a[0]));
Rich Felker 2064be
	return 0;
Rich Felker 2064be
}
Rich Felker 2064be
Rich Felker 2064be
static int test_relock(int mtype)
Rich Felker 2064be
{
Rich Felker 2064be
	struct timespec ts;
Rich Felker 2064be
	pthread_t t;
Rich Felker 2064be
	pthread_mutex_t m;
Rich Felker 2064be
	pthread_mutexattr_t ma;
Rich Felker 2064be
	sem_t s;
Rich Felker 2064be
	int i;
Rich Felker 2064be
	int r;
Rich Felker 2064be
	void *p;
Rich Felker 2064be
	void *a[] = {&m,&s,&i};
Rich Felker 2064be
Rich Felker 2064be
	T(pthread_mutexattr_init(&ma);;
Rich Felker 2064be
	T(pthread_mutexattr_settype(&ma, mtype));
Rich Felker 2064be
	T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
Rich Felker 2064be
	T(pthread_mutex_init(a[0], &ma);;
Rich Felker 2064be
	T(pthread_mutexattr_destroy(&ma);;
Rich Felker 2064be
	E(sem_init(a[1], 0, 0));
Rich Felker 2064be
	T(pthread_create(&t, 0, relock, a));
Rich Felker 2064be
	E(sem_wait(a[1]));
Rich Felker 2064be
	E(clock_gettime(CLOCK_REALTIME, &ts);;
Rich Felker 2064be
	ts.tv_nsec += 100*1000*1000;
Rich Felker 2064be
	if (ts.tv_nsec >= 1000*1000*1000) {
Rich Felker 2064be
		ts.tv_nsec -= 1000*1000*1000;
Rich Felker 2064be
		ts.tv_sec += 1;
Rich Felker 2064be
	}
Rich Felker 2064be
	r = sem_timedwait(a[1],&ts);
Rich Felker 2064be
	if (r == -1) {
Rich Felker 2064be
		if (errno != ETIMEDOUT)
Rich Felker 2064be
			t_error("sem_timedwait failed with unexpected error: %s\n", strerror(errno));
Rich Felker 2064be
		/* leave the deadlocked relock thread running */
Rich Felker 2064be
		return -1;
Rich Felker 2064be
	}
Rich Felker 2064be
	T(pthread_join(t, &p);;
Rich Felker 2064be
	T(pthread_mutex_destroy(a[0]));
Rich Felker 2064be
	E(sem_destroy(a[1]));
Rich Felker 2064be
	return i;
Rich Felker 2064be
}
Rich Felker 2064be
Rich Felker 2064be
static void *unlock(void *arg)
Rich Felker 2064be
{
Rich Felker 2064be
	void **a = arg;
Rich Felker 2064be
Rich Felker 2064be
	*(int*)a[1] = pthread_mutex_unlock(a[0]);
Rich Felker 2064be
	return 0;
Rich Felker 2064be
}
Rich Felker 2064be
Rich Felker 2064be
static int test_unlock(int mtype)
Rich Felker 2064be
{
Rich Felker 2064be
	pthread_t t;
Rich Felker 2064be
	pthread_mutex_t m;
Rich Felker 2064be
	pthread_mutexattr_t ma;
Rich Felker 2064be
	int i;
Rich Felker 2064be
	int r;
Rich Felker 2064be
	void *p;
Rich Felker 2064be
	void *a[] = {&m,&i};
Rich Felker 2064be
Rich Felker 2064be
	T(pthread_mutexattr_init(&ma);;
Rich Felker 2064be
	T(pthread_mutexattr_settype(&ma, mtype));
Rich Felker 2064be
	T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
Rich Felker 2064be
	T(pthread_mutex_init(a[0], &ma);;
Rich Felker 2064be
	T(pthread_mutexattr_destroy(&ma);;
Rich Felker 2064be
	T(pthread_create(&t, 0, unlock, a));
Rich Felker 2064be
	T(pthread_join(t, &p);;
Rich Felker 2064be
	T(pthread_mutex_destroy(a[0]));
Rich Felker 2064be
	return i;
Rich Felker 2064be
}
Rich Felker 2064be
Rich Felker 2064be
static int test_unlock_other(int mtype)
Rich Felker 2064be
{
Rich Felker 2064be
	pthread_t t;
Rich Felker 2064be
	pthread_mutex_t m;
Rich Felker 2064be
	pthread_mutexattr_t ma;
Rich Felker 2064be
	int i;
Rich Felker 2064be
	int r;
Rich Felker 2064be
	void *p;
Rich Felker 2064be
	void *a[] = {&m,&i};
Rich Felker 2064be
Rich Felker 2064be
	T(pthread_mutexattr_init(&ma);;
Rich Felker 2064be
	T(pthread_mutexattr_settype(&ma, mtype));
Rich Felker 2064be
	T(pthread_mutexattr_setprotocol(&ma, PTHREAD_PRIO_INHERIT));
Rich Felker 2064be
	T(pthread_mutex_init(a[0], &ma);;
Rich Felker 2064be
	T(pthread_mutexattr_destroy(&ma);;
Rich Felker 2064be
	T(pthread_mutex_lock(a[0]));
Rich Felker 2064be
	T(pthread_create(&t, 0, unlock, a));
Rich Felker 2064be
	T(pthread_join(t, &p);;
Rich Felker 2064be
	T(pthread_mutex_unlock(a[0]));
Rich Felker 2064be
	T(pthread_mutex_destroy(a[0]));
Rich Felker 2064be
	return i;
Rich Felker 2064be
}
Rich Felker 2064be
Rich Felker 2064be
static void test_mutexattr()
Rich Felker 2064be
{
Rich Felker 2064be
	pthread_mutex_t m;
Rich Felker 2064be
	pthread_mutexattr_t a;
Rich Felker 2064be
	int r;
Rich Felker 2064be
	int i;
Rich Felker 2064be
Rich Felker 2064be
	T(pthread_mutexattr_init(&a);;
Rich Felker 2064be
	T(pthread_mutexattr_gettype(&a, &i);;
Rich Felker 2064be
	if (i != PTHREAD_MUTEX_DEFAULT)
Rich Felker 2064be
		t_error("default mutex type is %d, wanted PTHREAD_MUTEX_DEFAULT (%d)\n", i, PTHREAD_MUTEX_DEFAULT);
Rich Felker 2064be
	T(pthread_mutexattr_settype(&a, PTHREAD_MUTEX_ERRORCHECK));
Rich Felker 2064be
	T(pthread_mutexattr_gettype(&a, &i);;
Rich Felker 2064be
	if (i != PTHREAD_MUTEX_ERRORCHECK)
Rich Felker 2064be
		t_error("setting error check mutex type failed failed: got %d, wanted %d\n", i, PTHREAD_MUTEX_ERRORCHECK);
Rich Felker 2064be
	T(pthread_mutexattr_destroy(&a);;
Rich Felker 2064be
}
Rich Felker 2064be
Rich Felker 2064be
int main(void)
Rich Felker 2064be
{
Rich Felker 2064be
	int i;
Rich Felker 2064be
Rich Felker 2064be
	test_mutexattr();
Rich Felker 2064be
Rich Felker 2064be
	i = test_relock(PTHREAD_MUTEX_NORMAL);
Rich Felker 2064be
	if (i != -1)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_NORMAL relock did not deadlock, got %s\n", strerror(i));
Rich Felker 2064be
	i = test_relock(PTHREAD_MUTEX_ERRORCHECK);
Rich Felker 2064be
	if (i != EDEADLK)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_ERRORCHECK relock did not return EDEADLK, got %s\n", i==-1?"deadlock":strerror(i));
Rich Felker 2064be
	i = test_relock(PTHREAD_MUTEX_RECURSIVE);
Rich Felker 2064be
	if (i != 0)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_RECURSIVE relock did not succed, got %s\n", i==-1?"deadlock":strerror(i));
Rich Felker 2064be
Rich Felker 2064be
	i = test_unlock(PTHREAD_MUTEX_ERRORCHECK);
Rich Felker 2064be
	if (i != EPERM)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
Rich Felker 2064be
	i = test_unlock(PTHREAD_MUTEX_RECURSIVE);
Rich Felker 2064be
	if (i != EPERM)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));
Rich Felker 2064be
Rich Felker 2064be
	i = test_unlock_other(PTHREAD_MUTEX_ERRORCHECK);
Rich Felker 2064be
	if (i != EPERM)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_ERRORCHECK unlock did not return EPERM, got %s\n", strerror(i));
Rich Felker 2064be
	i = test_unlock_other(PTHREAD_MUTEX_RECURSIVE);
Rich Felker 2064be
	if (i != EPERM)
Rich Felker 2064be
		t_error("PTHREAD_MUTEX_RECURSIVE unlock did not return EPERM, got %s\n", strerror(i));
Rich Felker 2064be
Rich Felker 2064be
	return t_status;
Rich Felker 2064be
}