1 /*
2  * Copyright (C) 2020 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 CHPP_PLATFORM_SYNC_H_
18 #define CHPP_PLATFORM_SYNC_H_
19 
20 #include <pthread.h>
21 #include <stdint.h>
22 
23 #include "chpp/mutex.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 struct ChppNotifier {
30   pthread_cond_t cond;     // Condition variable
31   struct ChppMutex mutex;  // Platform-specific mutex
32   uint32_t signal;
33 };
34 
35 /**
36  * Platform implementation of chppNotifierInit()
37  */
38 void chppPlatformNotifierInit(struct ChppNotifier *notifier);
39 
40 /**
41  * Platform implementation of chppNotifierDeinit()
42  */
43 void chppPlatformNotifierDeinit(struct ChppNotifier *notifier);
44 
45 /**
46  * Platform implementation of chppNotifierGetSignal()
47  */
48 uint32_t chppPlatformNotifierGetSignal(struct ChppNotifier *notifier);
49 
50 /**
51  * Platform implementation of chppNotifierWait()
52  */
53 uint32_t chppPlatformNotifierWait(struct ChppNotifier *notifier);
54 
55 /**
56  * Platform implementation of chppNotifierTimedWait()
57  */
58 uint32_t chppPlatformNotifierTimedWait(struct ChppNotifier *notifier,
59                                        uint64_t timeoutNs);
60 
61 /**
62  * Platform implementation of chppNotifierSignal()
63  */
64 void chppPlatformNotifierSignal(struct ChppNotifier *notifier, uint32_t signal);
65 
chppNotifierInit(struct ChppNotifier * notifier)66 static inline void chppNotifierInit(struct ChppNotifier *notifier) {
67   chppPlatformNotifierInit(notifier);
68 }
69 
chppNotifierDeinit(struct ChppNotifier * notifier)70 static inline void chppNotifierDeinit(struct ChppNotifier *notifier) {
71   chppPlatformNotifierDeinit(notifier);
72 }
73 
chppNotifierGetSignal(struct ChppNotifier * notifier)74 static inline uint32_t chppNotifierGetSignal(struct ChppNotifier *notifier) {
75   return chppPlatformNotifierGetSignal(notifier);
76 }
77 
chppNotifierWait(struct ChppNotifier * notifier)78 static inline uint32_t chppNotifierWait(struct ChppNotifier *notifier) {
79   return chppPlatformNotifierWait(notifier);
80 }
81 
chppNotifierTimedWait(struct ChppNotifier * notifier,uint64_t timeoutNs)82 static inline uint32_t chppNotifierTimedWait(struct ChppNotifier *notifier,
83                                              uint64_t timeoutNs) {
84   return chppPlatformNotifierTimedWait(notifier, timeoutNs);
85 }
86 
chppNotifierSignal(struct ChppNotifier * notifier,uint32_t signal)87 static inline void chppNotifierSignal(struct ChppNotifier *notifier,
88                                       uint32_t signal) {
89   chppPlatformNotifierSignal(notifier, signal);
90 }
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #endif  // CHPP_PLATFORM_SYNC_H_
97