Skip to content

Commit 2a73b7b

Browse files
committed
[NFC][LSAN] Limit the number of concurrent threads is the test
Test still fails with D88184 reverted. The test was flaky on https://bugs.chromium.org/p/chromium/issues/detail?id=1206745 and https://lab.llvm.org/buildbot/#/builders/sanitizer-x86_64-linux Reviewed By: morehouse Differential Revision: https://reviews.llvm.org/D102218
1 parent 731206f commit 2a73b7b

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

compiler-rt/test/lsan/TestCases/many_threads_detach.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// Use `-pthread` so that its driver will DTRT (ie., ignore it).
44
// RUN: %clangxx_lsan %s -o %t -pthread && %run %t
55

6+
#include <assert.h>
7+
#include <dirent.h>
68
#include <pthread.h>
79
#include <stdlib.h>
810
#include <unistd.h>
@@ -12,15 +14,33 @@
1214
// reused.
1315
static const size_t kTestThreads = 10000;
1416

17+
// Limit the number of simultaneous threads to avoid reaching the limit.
18+
static const size_t kTestThreadsBatch = 100;
19+
1520
void *null_func(void *args) {
1621
return NULL;
1722
}
1823

24+
int count_threads() {
25+
DIR *d = opendir("/proc/self/task");
26+
assert(d);
27+
int count = 0;
28+
while (readdir(d))
29+
++count;
30+
closedir(d);
31+
assert(count);
32+
return count;
33+
}
34+
1935
int main(void) {
20-
for (size_t i = 0; i < kTestThreads; i++) {
21-
pthread_t thread;
22-
if (pthread_create(&thread, NULL, null_func, NULL) == 0)
36+
for (size_t i = 0; i < kTestThreads; i += kTestThreadsBatch) {
37+
for (size_t j = 0; j < kTestThreadsBatch; ++j) {
38+
pthread_t thread;
39+
assert(pthread_create(&thread, NULL, null_func, NULL) == 0);
2340
pthread_detach(thread);
41+
}
42+
while (count_threads() > 10)
43+
sched_yield();
2444
}
2545
return 0;
2646
}

0 commit comments

Comments
 (0)