[docs] Fix build-docs.sh
[llvm-project.git] / compiler-rt / test / lsan / TestCases / leak_check_before_thread_started.cpp
blob94084dcb50f54b67f4e391536747136db97efb70
1 // Regression test for http://llvm.org/bugs/show_bug.cgi?id=21621
2 // This test relies on timing between threads, so any failures will be flaky.
3 // RUN: %clangxx_lsan %s -o %t
4 // RUN: %env_lsan_opts="log_pointers=1:log_threads=1" %run %t
5 #include <assert.h>
6 #include <pthread.h>
7 #include <stdio.h>
8 #include <stdlib.h>
10 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
11 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
12 bool flag = false;
14 void *func(void *arg) {
15 // This mutex will never be grabbed.
16 fprintf(stderr, "entered func()\n");
17 pthread_mutex_lock(&mutex);
18 free(arg);
19 pthread_mutex_unlock(&mutex);
20 return 0;
23 void create_detached_thread() {
24 pthread_t thread_id;
25 pthread_attr_t attr;
27 pthread_attr_init(&attr);
28 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
30 void *arg = malloc(1337);
31 assert(arg);
32 // This mutex is never unlocked by the main thread.
33 pthread_mutex_lock(&mutex);
34 int res = pthread_create(&thread_id, &attr, func, arg);
35 assert(res == 0);
38 int main() {
39 create_detached_thread();