[docs] Fix build-docs.sh
[llvm-project.git] / compiler-rt / test / lsan / TestCases / Linux / fork_threaded.cpp
blob62702b4dfe47ba9cb6a1f5d2f113e5f8ff2e680f
1 // Test that thread local data is handled correctly after forking without
2 // exec(). In this test leak checking is initiated from a non-main thread.
3 // RUN: %clangxx_lsan %s -o %t
4 // RUN: %run %t 2>&1
6 #include <assert.h>
7 #include <pthread.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <sys/wait.h>
11 #include <unistd.h>
13 __thread void *thread_local_var;
15 void *exit_thread_func(void *arg) {
16 exit(0);
19 void ExitFromThread() {
20 pthread_t tid;
21 int res;
22 res = pthread_create(&tid, 0, exit_thread_func, 0);
23 assert(res == 0);
24 pthread_join(tid, 0);
27 int main() {
28 int status = 0;
29 thread_local_var = malloc(1337);
30 pid_t pid = fork();
31 assert(pid >= 0);
32 if (pid > 0) {
33 waitpid(pid, &status, 0);
34 assert(WIFEXITED(status));
35 return WEXITSTATUS(status);
36 } else {
37 // Spawn a thread and call exit() from there, to check that we track main
38 // thread's pid correctly even if leak checking is initiated from another
39 // thread.
40 ExitFromThread();
42 return 0;