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_CONDITION_VARIABLE_H_
18 #define CHPP_PLATFORM_CONDITION_VARIABLE_H_
19
20 #include <pthread.h>
21 #include <stdbool.h>
22
23 #include "chpp/macros.h"
24 #include "chpp/mutex.h"
25 #include "time.h"
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 struct ChppConditionVariable {
32 pthread_cond_t cond; // Condition variable
33 };
34
chppConditionVariableInit(struct ChppConditionVariable * cv)35 static inline void chppConditionVariableInit(struct ChppConditionVariable *cv) {
36 pthread_cond_init(&cv->cond, NULL);
37 }
38
chppConditionVariableDeinit(struct ChppConditionVariable * cv)39 static inline void chppConditionVariableDeinit(
40 struct ChppConditionVariable *cv) {
41 pthread_cond_destroy(&cv->cond);
42 }
43
chppConditionVariableWait(struct ChppConditionVariable * cv,struct ChppMutex * mutex)44 static inline bool chppConditionVariableWait(struct ChppConditionVariable *cv,
45 struct ChppMutex *mutex) {
46 return pthread_cond_wait(&cv->cond, &mutex->lock) == 0;
47 }
48
chppConditionVariableTimedWait(struct ChppConditionVariable * cv,struct ChppMutex * mutex,uint64_t timeoutNs)49 static inline bool chppConditionVariableTimedWait(
50 struct ChppConditionVariable *cv, struct ChppMutex *mutex,
51 uint64_t timeoutNs) {
52 uint64_t timeoutS = timeoutNs / CHPP_NSEC_PER_SEC;
53 timeoutNs = timeoutNs % CHPP_NSEC_PER_SEC;
54
55 struct timespec now;
56 clock_gettime(CLOCK_REALTIME, &now);
57 now.tv_sec += timeoutS;
58 now.tv_nsec += timeoutNs;
59 return pthread_cond_timedwait(&cv->cond, &mutex->lock, &now) == 0;
60 }
61
chppConditionVariableSignal(struct ChppConditionVariable * cv)62 static inline void chppConditionVariableSignal(
63 struct ChppConditionVariable *cv) {
64 pthread_cond_signal(&cv->cond);
65 }
66
67 #ifdef __cplusplus
68 }
69 #endif
70
71 #endif // CHPP_PLATFORM_CONDITION_VARIABLE_H_
72