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