blob: 3019a21d699cce2f96926ab15aac1e6b21787748 [file] [log] [blame]
Mathias Agopian2bd99592012-02-25 23:02:14 -08001/*
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 Cross17b5b822016-09-15 18:15:37 -070020#include <limits.h>
Mathias Agopian2bd99592012-02-25 23:02:14 -080021#include <stdint.h>
22#include <sys/types.h>
23#include <time.h>
24
Yabin Cui4a6e5a32015-01-26 19:48:54 -080025#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080026# include <pthread.h>
27#endif
28
29#include <utils/Errors.h>
30#include <utils/Mutex.h>
31#include <utils/Timers.h>
32
33// ---------------------------------------------------------------------------
34namespace android {
35// ---------------------------------------------------------------------------
36
37/*
38 * Condition variable class. The implementation is system-dependent.
39 *
40 * Condition variables are paired up with mutexes. Lock the mutex,
41 * call wait(), then either re-wait() if things aren't quite what you want,
42 * or unlock the mutex and continue. All threads calling wait() must
43 * use the same mutex for a given Condition.
Tom Cherryf1147f72017-02-27 17:02:02 -080044 *
45 * On Android and Apple platforms, these are implemented as a simple wrapper
46 * around pthread condition variables. Care must be taken to abide by
47 * the pthreads semantics, in particular, a boolean predicate must
48 * be re-evaluated after a wake-up, as spurious wake-ups may happen.
Mathias Agopian2bd99592012-02-25 23:02:14 -080049 */
50class Condition {
51public:
52 enum {
53 PRIVATE = 0,
54 SHARED = 1
55 };
56
Romain Guy31ba37f2013-03-11 14:34:56 -070057 enum WakeUpType {
58 WAKE_UP_ONE = 0,
59 WAKE_UP_ALL = 1
60 };
61
Mathias Agopian2bd99592012-02-25 23:02:14 -080062 Condition();
Chih-Hung Hsieh2a929962016-08-02 12:14:47 -070063 explicit Condition(int type);
Mathias Agopian2bd99592012-02-25 23:02:14 -080064 ~Condition();
65 // Wait on the condition variable. Lock the mutex before calling.
Tom Cherryf1147f72017-02-27 17:02:02 -080066 // Note that spurious wake-ups may happen.
Mathias Agopian2bd99592012-02-25 23:02:14 -080067 status_t wait(Mutex& mutex);
68 // same with relative timeout
69 status_t waitRelative(Mutex& mutex, nsecs_t reltime);
Tom Cherryf1147f72017-02-27 17:02:02 -080070 // Signal the condition variable, allowing one thread to continue.
Mathias Agopian2bd99592012-02-25 23:02:14 -080071 void signal();
Romain Guy31ba37f2013-03-11 14:34:56 -070072 // Signal the condition variable, allowing one or all threads to continue.
73 void signal(WakeUpType type) {
74 if (type == WAKE_UP_ONE) {
75 signal();
76 } else {
77 broadcast();
78 }
79 }
Mathias Agopian2bd99592012-02-25 23:02:14 -080080 // Signal the condition variable, allowing all threads to continue.
81 void broadcast();
82
83private:
Yabin Cui4a6e5a32015-01-26 19:48:54 -080084#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080085 pthread_cond_t mCond;
86#else
87 void* mState;
88#endif
89};
90
91// ---------------------------------------------------------------------------
92
Yabin Cui4a6e5a32015-01-26 19:48:54 -080093#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080094
Tom Cherry1fb04ff2017-02-22 17:38:00 -080095inline Condition::Condition() : Condition(PRIVATE) {
Mathias Agopian2bd99592012-02-25 23:02:14 -080096}
97inline Condition::Condition(int type) {
Tom Cherry1fb04ff2017-02-22 17:38:00 -080098 pthread_condattr_t attr;
99 pthread_condattr_init(&attr);
100#if defined(__linux__)
101 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
102#endif
103
Mathias Agopian2bd99592012-02-25 23:02:14 -0800104 if (type == SHARED) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800105 pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800106 }
Tom Cherry1fb04ff2017-02-22 17:38:00 -0800107
108 pthread_cond_init(&mCond, &attr);
109 pthread_condattr_destroy(&attr);
110
Mathias Agopian2bd99592012-02-25 23:02:14 -0800111}
112inline Condition::~Condition() {
113 pthread_cond_destroy(&mCond);
114}
115inline status_t Condition::wait(Mutex& mutex) {
116 return -pthread_cond_wait(&mCond, &mutex.mMutex);
117}
118inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800119 struct timespec ts;
Elliott Hughes76f0a842015-01-09 16:16:53 -0800120#if defined(__linux__)
Tom Cherry1fb04ff2017-02-22 17:38:00 -0800121 clock_gettime(CLOCK_MONOTONIC, &ts);
Elliott Hughes76f0a842015-01-09 16:16:53 -0800122#else // __APPLE__
Elliott Hughesfd376b92016-04-04 14:21:47 -0700123 // Apple doesn't support POSIX clocks.
Mathias Agopian2bd99592012-02-25 23:02:14 -0800124 struct timeval t;
125 gettimeofday(&t, NULL);
126 ts.tv_sec = t.tv_sec;
Elliott Hughesfd376b92016-04-04 14:21:47 -0700127 ts.tv_nsec = t.tv_usec*1000;
Elliott Hughes76f0a842015-01-09 16:16:53 -0800128#endif
Elliott Hughesfd376b92016-04-04 14:21:47 -0700129
130 // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit.
131 int64_t reltime_sec = reltime/1000000000;
132
Colin Cross17b5b822016-09-15 18:15:37 -0700133 ts.tv_nsec += static_cast<long>(reltime%1000000000);
Elliott Hughesfd376b92016-04-04 14:21:47 -0700134 if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800135 ts.tv_nsec -= 1000000000;
Elliott Hughesfd376b92016-04-04 14:21:47 -0700136 ++reltime_sec;
Mathias Agopian2bd99592012-02-25 23:02:14 -0800137 }
Elliott Hughesfd376b92016-04-04 14:21:47 -0700138
139 int64_t time_sec = ts.tv_sec;
140 if (time_sec > INT64_MAX - reltime_sec) {
141 time_sec = INT64_MAX;
142 } else {
143 time_sec += reltime_sec;
144 }
145
Colin Cross17b5b822016-09-15 18:15:37 -0700146 ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec);
Elliott Hughesfd376b92016-04-04 14:21:47 -0700147
Mathias Agopian2bd99592012-02-25 23:02:14 -0800148 return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800149}
150inline void Condition::signal() {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800151 pthread_cond_signal(&mCond);
152}
153inline void Condition::broadcast() {
154 pthread_cond_broadcast(&mCond);
155}
156
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800157#endif // !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800158
159// ---------------------------------------------------------------------------
160}; // namespace android
161// ---------------------------------------------------------------------------
162
163#endif // _LIBS_UTILS_CONDITON_H