[mlir][acc] Introduce MappableType interface (#122146)
[llvm-project.git] / lldb / test / API / lang / c / step_over_no_deadlock / locking.cpp
blob8288a668fe86b1b481275c7073a7699a98768eb1
1 #include <stdio.h>
2 #include <thread>
3 #include <mutex>
4 #include <condition_variable>
6 std::mutex contended_mutex;
8 std::mutex control_mutex;
9 std::condition_variable control_condition;
11 std::mutex thread_started_mutex;
12 std::condition_variable thread_started_condition;
14 // This function runs in a thread. The locking dance is to make sure that
15 // by the time the main thread reaches the pthread_join below, this thread
16 // has for sure acquired the contended_mutex. So then the call_me_to_get_lock
17 // function will block trying to get the mutex, and only succeed once it
18 // signals this thread, then lets it run to wake up from the cond_wait and
19 // release the mutex.
21 void
22 lock_acquirer_1 (void)
24 std::unique_lock<std::mutex> contended_lock(contended_mutex);
26 // Grab this mutex, that will ensure that the main thread
27 // is in its cond_wait for it (since that's when it drops the mutex.
29 thread_started_mutex.lock();
30 thread_started_mutex.unlock();
32 // Now signal the main thread that it can continue, we have the contended lock
33 // so the call to call_me_to_get_lock won't make any progress till this
34 // thread gets a chance to run.
36 std::unique_lock<std::mutex> control_lock(control_mutex);
38 thread_started_condition.notify_all();
40 control_condition.wait(control_lock);
44 int
45 call_me_to_get_lock (int ret_val)
47 control_condition.notify_all();
48 contended_mutex.lock();
49 return ret_val;
52 int
53 get_int() {
54 return 567;
57 int main ()
59 std::unique_lock<std::mutex> thread_started_lock(thread_started_mutex);
61 std::thread thread_1(lock_acquirer_1);
63 thread_started_condition.wait(thread_started_lock);
65 control_mutex.lock();
66 control_mutex.unlock();
68 // Break here. At this point the other thread will have the contended_mutex,
69 // and be sitting in its cond_wait for the control condition. So there is
70 // no way that our by-hand calling of call_me_to_get_lock will proceed
71 // without running the first thread at least somewhat.
73 int result = call_me_to_get_lock(get_int());
74 thread_1.join();
76 return 0;