xref: /llvm-project/lldb/test/API/commands/process/detach-resumes/main.cpp (revision 5f3e106de3cd5ce6d7ba37fb11f6ad740cb430c5)
1 #include "pseudo_barrier.h"
2 #include <chrono>
3 #include <fcntl.h>
4 #include <fstream>
5 #include <stdio.h>
6 #include <thread>
7 #include <vector>
8 
9 pseudo_barrier_t barrier;
10 
11 constexpr size_t nthreads = 5;
12 volatile bool wait_for_debugger_flag = true;
13 
break_here()14 void break_here() {}
15 
tfunc()16 void tfunc() {
17   pseudo_barrier_wait(barrier);
18 
19   break_here();
20 }
21 
main(int argc,char const * argv[])22 int main(int argc, char const *argv[]) {
23   lldb_enable_attach();
24 
25   if (argc < 3)
26     return 1;
27 
28   // Create a file to signal that this process has started up.
29   std::ofstream(argv[1]).close();
30 
31   // And wait for it to attach.
32   for (int i = 0; i < 100 && wait_for_debugger_flag; ++i)
33     std::this_thread::sleep_for(std::chrono::seconds(1));
34 
35   // Fire up the threads and have them call break_here() simultaneously.
36   pseudo_barrier_init(barrier, nthreads);
37   std::vector<std::thread> threads;
38   for (size_t i = 0; i < nthreads; ++i)
39     threads.emplace_back(tfunc);
40 
41   for (std::thread &t : threads)
42     t.join();
43 
44   // Create the file to let the debugger know we're running.
45   std::ofstream(argv[2]).close();
46 
47   return 0;
48 }
49