Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 _LIBS_UTILS_CONDITION_H |
| 18 | #define _LIBS_UTILS_CONDITION_H |
| 19 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | #include <sys/types.h> |
| 23 | #include <time.h> |
| 24 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 25 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 26 | # include <pthread.h> |
| 27 | #endif |
| 28 | |
| 29 | #include <utils/Errors.h> |
| 30 | #include <utils/Mutex.h> |
| 31 | #include <utils/Timers.h> |
| 32 | |
| 33 | // --------------------------------------------------------------------------- |
| 34 | namespace android { |
| 35 | // --------------------------------------------------------------------------- |
| 36 | |
Steven Moreland | b8f152d | 2017-12-18 16:14:13 -0800 | [diff] [blame] | 37 | // DO NOT USE: please use std::condition_variable instead. |
| 38 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 39 | /* |
| 40 | * Condition variable class. The implementation is system-dependent. |
| 41 | * |
| 42 | * Condition variables are paired up with mutexes. Lock the mutex, |
| 43 | * call wait(), then either re-wait() if things aren't quite what you want, |
| 44 | * or unlock the mutex and continue. All threads calling wait() must |
| 45 | * use the same mutex for a given Condition. |
Tom Cherry | f1147f7 | 2017-02-27 17:02:02 -0800 | [diff] [blame] | 46 | * |
| 47 | * On Android and Apple platforms, these are implemented as a simple wrapper |
| 48 | * around pthread condition variables. Care must be taken to abide by |
| 49 | * the pthreads semantics, in particular, a boolean predicate must |
| 50 | * be re-evaluated after a wake-up, as spurious wake-ups may happen. |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 51 | */ |
| 52 | class Condition { |
| 53 | public: |
| 54 | enum { |
| 55 | PRIVATE = 0, |
| 56 | SHARED = 1 |
| 57 | }; |
| 58 | |
Romain Guy | 31ba37f | 2013-03-11 14:34:56 -0700 | [diff] [blame] | 59 | enum WakeUpType { |
| 60 | WAKE_UP_ONE = 0, |
| 61 | WAKE_UP_ALL = 1 |
| 62 | }; |
| 63 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 64 | Condition(); |
Chih-Hung Hsieh | 2a92996 | 2016-08-02 12:14:47 -0700 | [diff] [blame] | 65 | explicit Condition(int type); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 66 | ~Condition(); |
| 67 | // Wait on the condition variable. Lock the mutex before calling. |
Tom Cherry | f1147f7 | 2017-02-27 17:02:02 -0800 | [diff] [blame] | 68 | // Note that spurious wake-ups may happen. |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 69 | status_t wait(Mutex& mutex); |
| 70 | // same with relative timeout |
| 71 | status_t waitRelative(Mutex& mutex, nsecs_t reltime); |
Tom Cherry | f1147f7 | 2017-02-27 17:02:02 -0800 | [diff] [blame] | 72 | // Signal the condition variable, allowing one thread to continue. |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 73 | void signal(); |
Romain Guy | 31ba37f | 2013-03-11 14:34:56 -0700 | [diff] [blame] | 74 | // Signal the condition variable, allowing one or all threads to continue. |
| 75 | void signal(WakeUpType type) { |
| 76 | if (type == WAKE_UP_ONE) { |
| 77 | signal(); |
| 78 | } else { |
| 79 | broadcast(); |
| 80 | } |
| 81 | } |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 82 | // Signal the condition variable, allowing all threads to continue. |
| 83 | void broadcast(); |
| 84 | |
| 85 | private: |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 86 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 87 | pthread_cond_t mCond; |
| 88 | #else |
| 89 | void* mState; |
| 90 | #endif |
| 91 | }; |
| 92 | |
| 93 | // --------------------------------------------------------------------------- |
| 94 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 95 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 96 | |
Tom Cherry | 1fb04ff | 2017-02-22 17:38:00 -0800 | [diff] [blame] | 97 | inline Condition::Condition() : Condition(PRIVATE) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 98 | } |
| 99 | inline Condition::Condition(int type) { |
Tom Cherry | 1fb04ff | 2017-02-22 17:38:00 -0800 | [diff] [blame] | 100 | pthread_condattr_t attr; |
| 101 | pthread_condattr_init(&attr); |
| 102 | #if defined(__linux__) |
| 103 | pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); |
| 104 | #endif |
| 105 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 106 | if (type == SHARED) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 107 | pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 108 | } |
Tom Cherry | 1fb04ff | 2017-02-22 17:38:00 -0800 | [diff] [blame] | 109 | |
| 110 | pthread_cond_init(&mCond, &attr); |
| 111 | pthread_condattr_destroy(&attr); |
| 112 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 113 | } |
| 114 | inline Condition::~Condition() { |
| 115 | pthread_cond_destroy(&mCond); |
| 116 | } |
| 117 | inline status_t Condition::wait(Mutex& mutex) { |
| 118 | return -pthread_cond_wait(&mCond, &mutex.mMutex); |
| 119 | } |
| 120 | inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 121 | struct timespec ts; |
Elliott Hughes | 76f0a84 | 2015-01-09 16:16:53 -0800 | [diff] [blame] | 122 | #if defined(__linux__) |
Tom Cherry | 1fb04ff | 2017-02-22 17:38:00 -0800 | [diff] [blame] | 123 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Elliott Hughes | 76f0a84 | 2015-01-09 16:16:53 -0800 | [diff] [blame] | 124 | #else // __APPLE__ |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 125 | // Apple doesn't support POSIX clocks. |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 126 | struct timeval t; |
| 127 | gettimeofday(&t, NULL); |
| 128 | ts.tv_sec = t.tv_sec; |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 129 | ts.tv_nsec = t.tv_usec*1000; |
Elliott Hughes | 76f0a84 | 2015-01-09 16:16:53 -0800 | [diff] [blame] | 130 | #endif |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 131 | |
| 132 | // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit. |
| 133 | int64_t reltime_sec = reltime/1000000000; |
| 134 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 135 | ts.tv_nsec += static_cast<long>(reltime%1000000000); |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 136 | if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 137 | ts.tv_nsec -= 1000000000; |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 138 | ++reltime_sec; |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 139 | } |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 140 | |
| 141 | int64_t time_sec = ts.tv_sec; |
| 142 | if (time_sec > INT64_MAX - reltime_sec) { |
| 143 | time_sec = INT64_MAX; |
| 144 | } else { |
| 145 | time_sec += reltime_sec; |
| 146 | } |
| 147 | |
Colin Cross | 17b5b82 | 2016-09-15 18:15:37 -0700 | [diff] [blame] | 148 | ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec); |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 149 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 150 | return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 151 | } |
| 152 | inline void Condition::signal() { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 153 | pthread_cond_signal(&mCond); |
| 154 | } |
| 155 | inline void Condition::broadcast() { |
| 156 | pthread_cond_broadcast(&mCond); |
| 157 | } |
| 158 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 159 | #endif // !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 160 | |
| 161 | // --------------------------------------------------------------------------- |
Pirama Arumuga Nainar | 90affce | 2018-04-11 23:10:28 -0700 | [diff] [blame^] | 162 | } // namespace android |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 163 | // --------------------------------------------------------------------------- |
| 164 | |
| 165 | #endif // _LIBS_UTILS_CONDITON_H |