• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- main.cpp ------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // This test is intended to create a situation in which a breakpoint will be
11 // hit in two threads at nearly the same moment.  The expected result is that
12 // the breakpoint in the second thread will be hit while the breakpoint handler
13 // in the first thread is trying to stop all threads.
14 
15 #include <pthread.h>
16 #include <atomic>
17 
18 // Note that although hogging the CPU while waiting for a variable to change
19 // would be terrible in production code, it's great for testing since it
20 // avoids a lot of messy context switching to get multiple threads synchronized.
21 #define do_nothing()
22 
23 #define pseudo_barrier_wait(bar) \
24     --bar;                       \
25     while (bar > 0)              \
26         do_nothing();
27 
28 #define pseudo_barrier_init(bar, count) (bar = count)
29 
30 std::atomic_int g_barrier;
31 
32 volatile int g_test = 0;
33 
34 void *
thread_func(void * input)35 thread_func (void *input)
36 {
37     // Wait until both threads are running
38     pseudo_barrier_wait(g_barrier);
39 
40     // Do something
41     g_test++;       // Set breakpoint here
42 
43     // Return
44     return NULL;
45 }
46 
main()47 int main ()
48 {
49     pthread_t thread_1;
50     pthread_t thread_2;
51 
52     // Don't let either thread do anything until they're both ready.
53     pseudo_barrier_init(g_barrier, 2);
54 
55     // Create two threads
56     pthread_create (&thread_1, NULL, thread_func, NULL);
57     pthread_create (&thread_2, NULL, thread_func, NULL);
58 
59     // Wait for the threads to finish
60     pthread_join(thread_1, NULL);
61     pthread_join(thread_2, NULL);
62 
63     return 0;
64 }
65