Skip to content

Commit 34e47c0

Browse files
MarcusCalhoun-Lopezmascguy
authored andcommitted
add test for bug in pthread_get_stacksize_np
see rust-lang/rust#43347 see https://trac.macports.org/ticket/68015
1 parent 6e59675 commit 34e47c0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <pthread.h>
2+
#include <assert.h>
3+
#include <stdio.h>
4+
#include <stddef.h>
5+
#include <unistd.h>
6+
7+
size_t stack_size_main = 0;
8+
size_t stack_size_thread = 0;
9+
size_t stack_size_detached = 0;
10+
11+
void *thread_function(void *arg) {
12+
assert( !pthread_main_np() && "Unknown error, thread should *not* be main" );
13+
stack_size_thread = pthread_get_stacksize_np(pthread_self());
14+
assert( stack_size_thread && "Unknown error, stack size should *not* be zero" );
15+
return NULL;
16+
}
17+
18+
void *detached_function(void *arg) {
19+
assert( !pthread_main_np() && "Unknown error, thread should *not* be main" );
20+
stack_size_detached = pthread_get_stacksize_np(pthread_self());
21+
assert( stack_size_detached && "Unknown error, stack size should *not* be zero" );
22+
return NULL;
23+
}
24+
25+
int main()
26+
{
27+
pthread_t thread;
28+
pthread_t thread_detached;
29+
pthread_attr_t attr;
30+
31+
assert( pthread_main_np() && "Unknown error, thread should be main." );
32+
stack_size_main = pthread_get_stacksize_np(pthread_self());
33+
34+
assert( !pthread_create(&thread, NULL, thread_function, NULL) && "Unknown error, cannot create first thread" );
35+
pthread_join(thread, NULL);
36+
37+
assert( !pthread_attr_init(&attr) && "Unknown error, cannot initialises attribute" );
38+
assert( !pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) && "Unknown error, cannot set detached state" );
39+
assert( !pthread_create(&thread_detached, &attr, detached_function, NULL) && "Unknown error, cannot create second thread" );
40+
assert( !pthread_attr_destroy(&attr) && "Unknown error, cannot destroy attribute" );
41+
42+
do {
43+
usleep(1000);
44+
} while( !stack_size_detached );
45+
46+
assert( stack_size_thread == stack_size_detached && "Unknown Error, non-main threads are assumed to have the same stack size" );
47+
assert( stack_size_main > stack_size_thread && "Default stack size for main thread is the same as the default stack size of non-main threads" );
48+
49+
printf("Stack size for the non-main thread (%zu) is larger than for non-main threads (%zu), which is expected.\n", stack_size_main, stack_size_thread);
50+
51+
return 0;
52+
}

0 commit comments

Comments
 (0)