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 two threads are stopped
11// at a breakpoint and the debugger issues a step-out command.
12
13#include <pthread.h>
14#include <atomic>
15
16// Note that although hogging the CPU while waiting for a variable to change
17// would be terrible in production code, it's great for testing since it
18// avoids a lot of messy context switching to get multiple threads synchronized.
19#define do_nothing()
20
21#define pseudo_barrier_wait(bar) \
22    --bar;                       \
23    while (bar > 0)              \
24        do_nothing();
25
26#define pseudo_barrier_init(bar, count) (bar = count)
27
28std::atomic_int g_barrier;
29
30volatile int g_test = 0;
31
32void step_out_of_here() {
33  g_test += 5; // Set breakpoint here
34}
35
36void *
37thread_func (void *input)
38{
39    // Wait until both threads are running
40    pseudo_barrier_wait(g_barrier);
41
42    // Do something
43    step_out_of_here(); // Expect to stop here after step-out (clang)
44
45    // Return
46    return NULL;  // Expect to stop here after step-out (icc and gcc)
47}
48
49int main ()
50{
51    pthread_t thread_1;
52    pthread_t thread_2;
53
54    // Don't let either thread do anything until they're both ready.
55    pseudo_barrier_init(g_barrier, 2);
56
57    // Create two threads
58    pthread_create (&thread_1, NULL, thread_func, NULL);
59    pthread_create (&thread_2, NULL, thread_func, NULL);
60
61    // Wait for the threads to finish
62    pthread_join(thread_1, NULL);
63    pthread_join(thread_2, NULL);
64
65    return 0;
66}
67