xref: /aosp_15_r20/system/chre/platform/tinysys/include/chre/target_platform/condition_variable_base.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2022 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_TINYSYS_CONDITION_VARIABLE_BASE_H_
18 #define CHRE_PLATFORM_TINYSYS_CONDITION_VARIABLE_BASE_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include "FreeRTOS.h"
25 #include "semphr.h"
26 #include "sensorhub/rt_timer.h"
27 
28 #ifdef __cplusplus
29 }  // extern "C"
30 #endif
31 
32 namespace chre {
33 
34 /**
35  * Condition variable implementation based on rt_timer.
36  *
37  * Condition variable is preferred to run the timer callback in the ISR for less
38  * overhead/latency, which is why SystemTimer is not used here because
39  * SystemTimer assumes a callback function takes some time to finish so it
40  * creates a separate thread to run it.
41  */
42 class ConditionVariableBase {
43  protected:
44   /** callback function woken up by the timer */
45   static void conditionVariablTimerCallback(struct rt_timer *rtTimer);
46 
47   /** semaphore implementing the condition variable */
48   SemaphoreHandle_t semaphoreHandle;
49 
50   /** Buffer used to store state used by the semaphore */
51   StaticSemaphore_t mSemaphoreBuffer;
52 
53   /** True if wait_for() times out before semaphoreHandle is given */
54   bool isTimedOut = false;
55 
56   /** settings of the rt_timer */
57   struct rt_timer rtSystemTimer {};
58 };
59 
60 }  // namespace chre
61 
62 #endif  // CHRE_PLATFORM_TINYSYS_CONDITION_VARIABLE_BASE_H_