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 | |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <time.h> |
| 23 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 24 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 25 | # include <pthread.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <utils/Errors.h> |
| 29 | #include <utils/Mutex.h> |
| 30 | #include <utils/Timers.h> |
| 31 | |
| 32 | // --------------------------------------------------------------------------- |
| 33 | namespace android { |
| 34 | // --------------------------------------------------------------------------- |
| 35 | |
| 36 | /* |
| 37 | * Condition variable class. The implementation is system-dependent. |
| 38 | * |
| 39 | * Condition variables are paired up with mutexes. Lock the mutex, |
| 40 | * call wait(), then either re-wait() if things aren't quite what you want, |
| 41 | * or unlock the mutex and continue. All threads calling wait() must |
| 42 | * use the same mutex for a given Condition. |
| 43 | */ |
| 44 | class Condition { |
| 45 | public: |
| 46 | enum { |
| 47 | PRIVATE = 0, |
| 48 | SHARED = 1 |
| 49 | }; |
| 50 | |
Romain Guy | 31ba37f | 2013-03-11 14:34:56 -0700 | [diff] [blame] | 51 | enum WakeUpType { |
| 52 | WAKE_UP_ONE = 0, |
| 53 | WAKE_UP_ALL = 1 |
| 54 | }; |
| 55 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 56 | Condition(); |
| 57 | Condition(int type); |
| 58 | ~Condition(); |
| 59 | // Wait on the condition variable. Lock the mutex before calling. |
| 60 | status_t wait(Mutex& mutex); |
| 61 | // same with relative timeout |
| 62 | status_t waitRelative(Mutex& mutex, nsecs_t reltime); |
Igor Murashkin | db41938 | 2014-04-15 15:39:27 -0700 | [diff] [blame] | 63 | // Signal the condition variable, allowing exactly one thread to continue. |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 64 | void signal(); |
Romain Guy | 31ba37f | 2013-03-11 14:34:56 -0700 | [diff] [blame] | 65 | // Signal the condition variable, allowing one or all threads to continue. |
| 66 | void signal(WakeUpType type) { |
| 67 | if (type == WAKE_UP_ONE) { |
| 68 | signal(); |
| 69 | } else { |
| 70 | broadcast(); |
| 71 | } |
| 72 | } |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 73 | // Signal the condition variable, allowing all threads to continue. |
| 74 | void broadcast(); |
| 75 | |
| 76 | private: |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 77 | #if !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 78 | pthread_cond_t mCond; |
| 79 | #else |
| 80 | void* mState; |
| 81 | #endif |
| 82 | }; |
| 83 | |
| 84 | // --------------------------------------------------------------------------- |
| 85 | |
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 | |
| 88 | inline Condition::Condition() { |
| 89 | pthread_cond_init(&mCond, NULL); |
| 90 | } |
| 91 | inline Condition::Condition(int type) { |
| 92 | if (type == SHARED) { |
| 93 | pthread_condattr_t attr; |
| 94 | pthread_condattr_init(&attr); |
| 95 | pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
| 96 | pthread_cond_init(&mCond, &attr); |
| 97 | pthread_condattr_destroy(&attr); |
| 98 | } else { |
| 99 | pthread_cond_init(&mCond, NULL); |
| 100 | } |
| 101 | } |
| 102 | inline Condition::~Condition() { |
| 103 | pthread_cond_destroy(&mCond); |
| 104 | } |
| 105 | inline status_t Condition::wait(Mutex& mutex) { |
| 106 | return -pthread_cond_wait(&mCond, &mutex.mMutex); |
| 107 | } |
| 108 | inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 109 | struct timespec ts; |
Elliott Hughes | 76f0a84 | 2015-01-09 16:16:53 -0800 | [diff] [blame] | 110 | #if defined(__linux__) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 111 | clock_gettime(CLOCK_REALTIME, &ts); |
Elliott Hughes | 76f0a84 | 2015-01-09 16:16:53 -0800 | [diff] [blame] | 112 | #else // __APPLE__ |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 113 | // Apple doesn't support POSIX clocks. |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 114 | struct timeval t; |
| 115 | gettimeofday(&t, NULL); |
| 116 | ts.tv_sec = t.tv_sec; |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 117 | ts.tv_nsec = t.tv_usec*1000; |
Elliott Hughes | 76f0a84 | 2015-01-09 16:16:53 -0800 | [diff] [blame] | 118 | #endif |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 119 | |
| 120 | // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit. |
| 121 | int64_t reltime_sec = reltime/1000000000; |
| 122 | |
| 123 | ts.tv_nsec += reltime%1000000000; |
| 124 | if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) { |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 125 | ts.tv_nsec -= 1000000000; |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 126 | ++reltime_sec; |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 127 | } |
Elliott Hughes | fd376b9 | 2016-04-04 14:21:47 -0700 | [diff] [blame] | 128 | |
| 129 | int64_t time_sec = ts.tv_sec; |
| 130 | if (time_sec > INT64_MAX - reltime_sec) { |
| 131 | time_sec = INT64_MAX; |
| 132 | } else { |
| 133 | time_sec += reltime_sec; |
| 134 | } |
| 135 | |
| 136 | #if defined(__LP64__) |
| 137 | ts.tv_sec = time_sec; |
| 138 | #else |
| 139 | ts.tv_sec = (time_sec > INT32_MAX) ? INT32_MAX : time_sec; |
| 140 | #endif |
| 141 | |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 142 | return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts); |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 143 | } |
| 144 | inline void Condition::signal() { |
Igor Murashkin | db41938 | 2014-04-15 15:39:27 -0700 | [diff] [blame] | 145 | /* |
| 146 | * POSIX says pthread_cond_signal wakes up "one or more" waiting threads. |
| 147 | * However bionic follows the glibc guarantee which wakes up "exactly one" |
| 148 | * waiting thread. |
| 149 | * |
| 150 | * man 3 pthread_cond_signal |
| 151 | * pthread_cond_signal restarts one of the threads that are waiting on |
| 152 | * the condition variable cond. If no threads are waiting on cond, |
| 153 | * nothing happens. If several threads are waiting on cond, exactly one |
| 154 | * is restarted, but it is not specified which. |
| 155 | */ |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 156 | pthread_cond_signal(&mCond); |
| 157 | } |
| 158 | inline void Condition::broadcast() { |
| 159 | pthread_cond_broadcast(&mCond); |
| 160 | } |
| 161 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 162 | #endif // !defined(_WIN32) |
Mathias Agopian | 2bd9959 | 2012-02-25 23:02:14 -0800 | [diff] [blame] | 163 | |
| 164 | // --------------------------------------------------------------------------- |
| 165 | }; // namespace android |
| 166 | // --------------------------------------------------------------------------- |
| 167 | |
| 168 | #endif // _LIBS_UTILS_CONDITON_H |