| 1 |
// RUN: %clangxx -pthread %s -o %t |
| 2 |
|
| 3 |
// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 0 2>&1 | FileCheck %s |
| 4 |
// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 1 2>&1 | FileCheck %s |
| 5 |
// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 2 2>&1 | FileCheck %s |
| 6 |
// RUN: %env_tool_opts=detect_invalid_join=true not %run %t 3 2>&1 | FileCheck %s --check-prefix=DETACH |
| 7 |
|
| 8 |
// REQUIRES: glibc && (asan || hwasan || lsan) |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <ctime> |
| 12 |
#include <errno.h> |
| 13 |
#include <pthread.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <unistd.h> |
| 17 |
|
| 18 |
static void *fn(void *args) { |
| 19 |
sleep(1); |
| 20 |
return nullptr; |
| 21 |
} |
| 22 |
|
| 23 |
int main(int argc, char **argv) { |
| 24 |
int n = atoi(argv[1]); |
| 25 |
pthread_t thread; |
| 26 |
assert(!pthread_create(&thread, nullptr, fn, nullptr)); |
| 27 |
void *res; |
| 28 |
if (n == 0) { |
| 29 |
while (pthread_tryjoin_np(thread, &res)) |
| 30 |
sleep(1); |
| 31 |
pthread_tryjoin_np(thread, &res); |
| 32 |
} else if (n == 1) { |
| 33 |
timespec tm = {0, 1}; |
| 34 |
while (pthread_timedjoin_np(thread, &res, &tm)) |
| 35 |
sleep(1); |
| 36 |
pthread_timedjoin_np(thread, &res, &tm); |
| 37 |
} else if (n == 2) { |
| 38 |
assert(!pthread_join(thread, &res)); |
| 39 |
pthread_join(thread, &res); |
| 40 |
} else if (n == 3) { |
| 41 |
assert(!pthread_detach(thread)); |
| 42 |
pthread_join(thread, &res); |
| 43 |
} |
| 44 |
// CHECK: Joining already joined thread |
| 45 |
// DETACH: Joining detached thread |
| 46 |
return 0; |
| 47 |
} |