• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,
33                           public NonCopyable {
34  public:
35   /**
36    * Allows the platform to do any condition variable initialization at
37    * construction time.
38    */
39   ConditionVariable();
40 
41   /**
42    * Allows the platform to do any condition variable deinitialization at
43    * destruction time.
44    */
45   ~ConditionVariable();
46 
47   /**
48    * Unblock one thread that is waiting on this condition variable.
49    */
50   void notify_one();
51 
52   /**
53    * Causes the current thread to block until the condition variable is
54    * notified. The provided mutex will be unlocked and the thread will be
55    * blocked until the condition variable has notified. The mutex is relocked
56    * prior to this function returning.
57    *
58    * @param The currently locked mutex.
59    */
60   void wait(Mutex& mutex);
61 
62    /**
63    * Same behavior as the wait function, but with a timeout to unblock the
64    * calling thread if not notified within the timeout period.
65    *
66    * @param mutex The currently locked mutex.
67    * @param timeout The timeout duration in nanoseconds.
68    *
69    * @return false if timed out, true if notified.
70    */
71   bool wait_for(Mutex& mutex, Nanoseconds timeout);
72 };
73 
74 }  // namespace chre
75 
76 #include "chre/target_platform/condition_variable_impl.h"
77 
78 #endif  // CHRE_PLATFORM_CONDITION_VARIABLE_H_
79