]>
Commit | Line | Data |
---|---|---|
964d3577 A |
1 | #include <pthread.h> |
2 | #include <stdio.h> | |
3 | #include <string.h> | |
4 | #include <unistd.h> | |
5 | #include <os/assumes.h> | |
6 | ||
a0619f9c A |
7 | #include "darwintest_defaults.h" |
8 | ||
2546420a A |
9 | |
10 | static uintptr_t stackaddr; | |
11 | static const size_t stacksize = 4096 * 8; | |
12 | ||
13 | static void *function(void *arg) { | |
964d3577 A |
14 | // Use the stack... |
15 | char buffer[BUFSIZ]; | |
16 | strlcpy(buffer, arg, sizeof(buffer)); | |
17 | strlcat(buffer, arg, sizeof(buffer)); | |
18 | ||
2546420a A |
19 | T_ASSERT_LT((uintptr_t)__builtin_frame_address(0), stackaddr, NULL); |
20 | T_ASSERT_GT((uintptr_t)__builtin_frame_address(0), stackaddr - stacksize, NULL); | |
21 | ||
964d3577 A |
22 | return (void *)(uintptr_t)strlen(buffer); |
23 | } | |
24 | ||
2546420a A |
25 | T_DECL(custom_stack, "creating a pthread with a custom stack", |
26 | T_META_ALL_VALID_ARCHS(YES)){ | |
964d3577 | 27 | char *arg = "This is a test and only a test of the pthread stackaddr system.\n"; |
2546420a | 28 | stackaddr = (uintptr_t)valloc(stacksize); |
964d3577 A |
29 | stackaddr += stacksize; // address starts at top of stack. |
30 | ||
31 | pthread_t thread; | |
32 | pthread_attr_t attr; | |
33 | ||
2546420a A |
34 | T_ASSERT_POSIX_ZERO(pthread_attr_init(&attr), NULL); |
35 | T_ASSERT_POSIX_ZERO(pthread_attr_setstacksize(&attr, stacksize), NULL); | |
36 | T_ASSERT_POSIX_ZERO(pthread_attr_setstackaddr(&attr, (void *)stackaddr), NULL); | |
964d3577 | 37 | |
2546420a | 38 | T_ASSERT_POSIX_ZERO(pthread_create(&thread, &attr, function, arg), NULL); |
964d3577 A |
39 | |
40 | void *result; | |
2546420a A |
41 | T_ASSERT_POSIX_ZERO(pthread_join(thread, &result), NULL); |
42 | T_ASSERT_EQ((uintptr_t)result, (uintptr_t)strlen(arg)*2, "thread should return correct value"); | |
964d3577 | 43 | |
2546420a | 44 | free((void*)(stackaddr - stacksize)); |
964d3577 | 45 | } |