1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef ANDROID_HARDWARE_BROADCASTRADIO_V1_1_CALL_BARRIER
17 #define ANDROID_HARDWARE_BROADCASTRADIO_V1_1_CALL_BARRIER
18 
19 #include <chrono>
20 #include <thread>
21 
22 namespace android {
23 namespace hardware {
24 namespace broadcastradio {
25 namespace vts {
26 
27 /**
28  * A barrier for thread synchronization, where one should wait for another to
29  * reach a specific point in execution.
30  */
31 class CallBarrier {
32    public:
33     /**
34      * Notify the other thread it may continue execution.
35      *
36      * This may be called before the other thread starts waiting on the barrier.
37      */
38     void call();
39 
40     /**
41      * Wait for the other thread to reach call() execution point.
42      *
43      * @param timeout a maximum time to wait.
44      * @returns {@code false} if timed out, {@code true} otherwise.
45      */
46     bool waitForCall(std::chrono::milliseconds timeout);
47 
48    private:
49     bool mWasCalled = false;
50     std::mutex mMut;
51     std::condition_variable mCond;
52 };
53 
54 }  // namespace vts
55 }  // namespace broadcastradio
56 }  // namespace hardware
57 }  // namespace android
58 
59 #endif  // ANDROID_HARDWARE_BROADCASTRADIO_V1_1_CALL_BARRIER
60