1 /* 2 * Copyright (C) 2016 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 17 #ifndef CHRE_PLATFORM_CONDITION_VARIABLE_H_ 18 #define CHRE_PLATFORM_CONDITION_VARIABLE_H_ 19 20 #include "chre/platform/mutex.h" 21 #include "chre/target_platform/condition_variable_base.h" 22 #include "chre/util/non_copyable.h" 23 #include "chre/util/time.h" 24 25 namespace chre { 26 27 /** 28 * Provides an implementation of a Condition Variable. The public API is 29 * similar to std::condition_variable. ConditionVariableBase is subclassed here 30 * to allow platforms to inject their own storage for their implementation. 31 */ 32 class ConditionVariable : public ConditionVariableBase, public NonCopyable { 33 public: 34 /** 35 * Allows the platform to do any condition variable initialization at 36 * construction time. 37 */ 38 ConditionVariable(); 39 40 /** 41 * Allows the platform to do any condition variable deinitialization at 42 * destruction time. 43 */ 44 ~ConditionVariable(); 45 46 /** 47 * Unblock one thread that is waiting on this condition variable. 48 */ 49 void notify_one(); 50 51 /** 52 * Causes the current thread to block until the condition variable is 53 * notified. The provided mutex will be unlocked and the thread will be 54 * blocked until the condition variable has notified. The mutex is relocked 55 * prior to this function returning. Note that while std::condition_variable 56 * allows for multiple threads to wait on the same condition variable object 57 * concurrently, the CHRE platform implementation is only required to allow 58 * for a single waiting thread. The calling code must also be equipped to 59 * handle spurious wakeups. 60 * 61 * @param The currently locked mutex. 62 */ 63 void wait(Mutex &mutex); 64 65 /** 66 * Same behavior as the wait function, but with a timeout to unblock the 67 * calling thread if not notified within the timeout period. 68 * 69 * @param mutex The currently locked mutex. 70 * @param timeout The timeout duration in nanoseconds. 71 * 72 * @return false if timed out, true if notified. 73 */ 74 bool wait_for(Mutex &mutex, Nanoseconds timeout); 75 }; 76 77 } // namespace chre 78 79 #include "chre/target_platform/condition_variable_impl.h" 80 81 #endif // CHRE_PLATFORM_CONDITION_VARIABLE_H_ 82