xref: /haiku/src/tests/system/libroot/posix/pthread_attr_stack_test.cpp (revision a72f3582be00f2151800fa7da036d7adc14e3272)
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <pthread.h>
6 
7 #define panic(str) if (ret != 0) { errno = ret; perror(str); return 1; }
8 
9 
10 void*
11 threadFunction(void*)
12 {
13 	int i = 0;
14 	printf("variable addr: %p\n", &i);
15 	pthread_exit(0);
16 	return 0;
17 }
18 
19 
20 int
21 main(int argc, char **argv)
22 {
23 	int ret;
24 	pthread_attr_t attr;
25 	ret = pthread_attr_init(&attr);
26 	panic("init");
27 	void* stackAddress;
28 	size_t stackSize;
29 	ret = pthread_attr_getstack(&attr, &stackAddress, &stackSize);
30 	panic("getstack");
31 	printf("stackAddress: %p, stackSize: %lu\n", stackAddress, stackSize);
32 	stackSize = PTHREAD_STACK_MIN;
33 	ret = posix_memalign(&stackAddress, sysconf(_SC_PAGE_SIZE),
34 		stackSize);
35 	panic("memalign");
36 	ret = pthread_attr_setstack(&attr, stackAddress, stackSize);
37 	panic("setstack");
38 	ret = pthread_attr_getstack(&attr, &stackAddress, &stackSize);
39 	panic("getstack");
40 	printf("stackAddress: %p, stackSize: %lu\n", stackAddress, stackSize);
41 
42 	pthread_t thread;
43 	ret = pthread_create(&thread, &attr, threadFunction, NULL);
44 	panic("create");
45 	ret = pthread_join(thread, NULL);
46 	panic("join");
47 
48 	ret = pthread_attr_destroy(&attr);
49 	panic("destroy");
50 
51 	return 0;
52 }
53