]>
Commit | Line | Data |
---|---|---|
214d78a2 A |
1 | #include <pthread.h> |
2 | #include <stdio.h> | |
3 | #include <stdlib.h> | |
4 | #include "darwintest_defaults.h" | |
5 | ||
6 | #define PTHREAD_T_OFFSET (0) | |
7 | ||
8 | static void * | |
9 | function(void *arg) | |
10 | { | |
11 | size_t expected_size = (size_t)(uintptr_t)arg; | |
12 | T_ASSERT_EQ(pthread_get_stacksize_np(pthread_self()), expected_size, | |
13 | "saw expected pthread_get_stacksize_np"); | |
14 | return NULL; | |
15 | } | |
16 | ||
17 | T_DECL(stack_size_default, "stack size of default pthread", | |
18 | T_META_ALL_VALID_ARCHS(YES)) | |
19 | { | |
20 | static const size_t dflsize = 512 * 1024; | |
21 | pthread_t thread; | |
22 | pthread_attr_t attr; | |
23 | ||
24 | T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), NULL); | |
25 | T_ASSERT_POSIX_ZERO(pthread_create(&thread, &attr, function, | |
26 | (void *)(dflsize + PTHREAD_T_OFFSET)), NULL); | |
27 | T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL); | |
28 | } | |
29 | ||
30 | T_DECL(stack_size_customsize, "stack size of thread with custom stack size", | |
31 | T_META_ALL_VALID_ARCHS(YES)) | |
32 | { | |
33 | static const size_t stksize = 768 * 1024; | |
34 | pthread_t thread; | |
35 | pthread_attr_t attr; | |
36 | ||
37 | T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), NULL); | |
38 | T_ASSERT_POSIX_ZERO(pthread_attr_setstacksize(&attr, stksize), NULL); | |
39 | T_ASSERT_POSIX_ZERO(pthread_create(&thread, &attr, function, | |
40 | (void *)(stksize + PTHREAD_T_OFFSET)), NULL); | |
41 | T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL); | |
42 | } | |
43 | ||
44 | T_DECL(stack_size_customaddr, "stack size of thread with custom stack addr", | |
45 | T_META_ALL_VALID_ARCHS(YES)) | |
46 | { | |
47 | static const size_t stksize = 512 * 1024; | |
48 | pthread_t thread; | |
49 | pthread_attr_t attr; | |
50 | ||
51 | uintptr_t stackaddr = (uintptr_t)valloc(stksize); | |
52 | stackaddr += stksize; // address is top of stack | |
53 | ||
54 | T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), NULL); | |
55 | T_ASSERT_POSIX_ZERO(pthread_attr_setstackaddr(&attr, (void *)stackaddr), | |
56 | NULL); | |
57 | T_ASSERT_POSIX_ZERO(pthread_create(&thread, &attr, function, | |
58 | (void *)stksize), NULL); | |
59 | T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL); | |
60 | free((void *)(stackaddr - stksize)); | |
61 | } | |
62 | ||
63 | T_DECL(stack_size_custom, "stack size of thread with custom stack addr+size", | |
64 | T_META_ALL_VALID_ARCHS(YES)) | |
65 | { | |
66 | static const size_t stksize = 768 * 1024; | |
67 | pthread_t thread; | |
68 | pthread_attr_t attr; | |
69 | ||
70 | uintptr_t stackaddr = (uintptr_t)valloc(stksize); | |
71 | stackaddr += stksize; // address is top of stack | |
72 | ||
73 | T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), NULL); | |
74 | T_ASSERT_POSIX_ZERO(pthread_attr_setstackaddr(&attr, (void *)stackaddr), | |
75 | NULL); | |
76 | T_ASSERT_POSIX_ZERO(pthread_attr_setstacksize(&attr, stksize), NULL); | |
77 | T_ASSERT_POSIX_ZERO(pthread_create(&thread, &attr, function, | |
78 | (void *)stksize), NULL); | |
79 | T_ASSERT_POSIX_ZERO(pthread_join(thread, NULL), NULL); | |
80 | free((void *)(stackaddr - stksize)); | |
81 | } |