aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/linux/pthread_attr.c
blob: 4c1907c054a5eda7ec9e44656902e2f5aa86cb0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>

static void test_affinity() {
	pthread_attr_t attr;
	cpu_set_t set = {0};
	assert(!pthread_attr_init(&attr));
	assert(!pthread_attr_setaffinity_np(&attr, 1, &set));

	cpu_set_t other_set = {0};
	assert(!pthread_attr_getaffinity_np(&attr, 1, &set));

	assert(!memcmp(&set, &other_set, sizeof(cpu_set_t)));

	pthread_attr_destroy(&attr);
}

#if !defined(USE_HOST_LIBC) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)
static void test_sigmask() {
	pthread_attr_t attr;
	sigset_t set;
	sigemptyset(&set);
	sigaddset(&set, SIGUSR1);
#ifndef USE_HOST_LIBC
	sigaddset(&set, SIGCANCEL);
#endif

	assert(!pthread_attr_init(&attr));
	assert(!pthread_attr_setsigmask_np(&attr, &set));

	sigset_t other_set;
	sigemptyset(&other_set);

	assert(!pthread_attr_getsigmask_np(&attr, &other_set));

	assert(sigismember(&other_set, SIGUSR1));
#ifndef USE_HOST_LIBC
	// Test whether internal signals get filtered properly.
	assert(!sigismember(&other_set, SIGCANCEL));
#endif

	pthread_attr_destroy(&attr);
}

static void *getattr_worker(void *arg) {
	(void)arg;
	return NULL;
}

static void test_getattrnp() {
	pthread_attr_t attr;
	size_t stacksize = PTHREAD_STACK_MIN;
	assert(!pthread_attr_init(&attr));
	assert(!pthread_attr_setstacksize(&attr, stacksize));

	pthread_t thread;
	assert(!pthread_create(&thread, &attr, getattr_worker, NULL));
	assert(!pthread_getattr_np(thread, &attr));
	size_t other_stacksize;
	assert(!pthread_attr_getstacksize(&attr, &other_stacksize));
	assert(other_stacksize == stacksize);
	assert(!pthread_join(thread, NULL));

	pthread_t own_thread = pthread_self();
	void *stack;
	assert(!pthread_getattr_np(own_thread, &attr));
	assert(!pthread_attr_getstack(&attr, &stack, &other_stacksize));
	assert(stack);
	assert(other_stacksize);
	// Check that we can read from the highest byte returned.
	// pthread_getattr_np() should return the lowest byte
	// of the stack.
	printf("highest byte: %hhu\n", *(char *)(stack + other_stacksize - 1));

	pthread_attr_destroy(&attr);
}
#endif // !defined(USE_HOST_LIBC) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)

int main() {
	test_affinity();
#if !defined(USE_HOST_LIBC) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 32)
	test_sigmask();
	test_getattrnp();
#endif

	return 0;
}