blob: 25a53aa230325452917f3515d5a09938adfe0451 [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.
44 */
45class Condition {
46public:
47 enum {
48 PRIVATE = 0,
49 SHARED = 1
50 };
51
Romain Guy31ba37f2013-03-11 14:34:56 -070052 enum WakeUpType {
53 WAKE_UP_ONE = 0,
54 WAKE_UP_ALL = 1
55 };
56
Mathias Agopian2bd99592012-02-25 23:02:14 -080057 Condition();
Chih-Hung Hsieh2a929962016-08-02 12:14:47 -070058 explicit Condition(int type);
Mathias Agopian2bd99592012-02-25 23:02:14 -080059 ~Condition();
60 // Wait on the condition variable. Lock the mutex before calling.
61 status_t wait(Mutex& mutex);
62 // same with relative timeout
63 status_t waitRelative(Mutex& mutex, nsecs_t reltime);
Igor Murashkindb419382014-04-15 15:39:27 -070064 // Signal the condition variable, allowing exactly one thread to continue.
Mathias Agopian2bd99592012-02-25 23:02:14 -080065 void signal();
Romain Guy31ba37f2013-03-11 14:34:56 -070066 // Signal the condition variable, allowing one or all threads to continue.
67 void signal(WakeUpType type) {
68 if (type == WAKE_UP_ONE) {
69 signal();
70 } else {
71 broadcast();
72 }
73 }
Mathias Agopian2bd99592012-02-25 23:02:14 -080074 // Signal the condition variable, allowing all threads to continue.
75 void broadcast();
76
77private:
Yabin Cui4a6e5a32015-01-26 19:48:54 -080078#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080079 pthread_cond_t mCond;
80#else
81 void* mState;
82#endif
83};
84
85// ---------------------------------------------------------------------------
86
Yabin Cui4a6e5a32015-01-26 19:48:54 -080087#if !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -080088
89inline Condition::Condition() {
90 pthread_cond_init(&mCond, NULL);
91}
92inline Condition::Condition(int type) {
93 if (type == SHARED) {
94 pthread_condattr_t attr;
95 pthread_condattr_init(&attr);
96 pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
97 pthread_cond_init(&mCond, &attr);
98 pthread_condattr_destroy(&attr);
99 } else {
100 pthread_cond_init(&mCond, NULL);
101 }
102}
103inline Condition::~Condition() {
104 pthread_cond_destroy(&mCond);
105}
106inline status_t Condition::wait(Mutex& mutex) {
107 return -pthread_cond_wait(&mCond, &mutex.mMutex);
108}
109inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800110 struct timespec ts;
Elliott Hughes76f0a842015-01-09 16:16:53 -0800111#if defined(__linux__)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800112 clock_gettime(CLOCK_REALTIME, &ts);
Elliott Hughes76f0a842015-01-09 16:16:53 -0800113#else // __APPLE__
Elliott Hughesfd376b92016-04-04 14:21:47 -0700114 // Apple doesn't support POSIX clocks.
Mathias Agopian2bd99592012-02-25 23:02:14 -0800115 struct timeval t;
116 gettimeofday(&t, NULL);
117 ts.tv_sec = t.tv_sec;
Elliott Hughesfd376b92016-04-04 14:21:47 -0700118 ts.tv_nsec = t.tv_usec*1000;
Elliott Hughes76f0a842015-01-09 16:16:53 -0800119#endif
Elliott Hughesfd376b92016-04-04 14:21:47 -0700120
121 // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit.
122 int64_t reltime_sec = reltime/1000000000;
123
Colin Cross17b5b822016-09-15 18:15:37 -0700124 ts.tv_nsec += static_cast<long>(reltime%1000000000);
Elliott Hughesfd376b92016-04-04 14:21:47 -0700125 if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) {
Mathias Agopian2bd99592012-02-25 23:02:14 -0800126 ts.tv_nsec -= 1000000000;
Elliott Hughesfd376b92016-04-04 14:21:47 -0700127 ++reltime_sec;
Mathias Agopian2bd99592012-02-25 23:02:14 -0800128 }
Elliott Hughesfd376b92016-04-04 14:21:47 -0700129
130 int64_t time_sec = ts.tv_sec;
131 if (time_sec > INT64_MAX - reltime_sec) {
132 time_sec = INT64_MAX;
133 } else {
134 time_sec += reltime_sec;
135 }
136
Colin Cross17b5b822016-09-15 18:15:37 -0700137 ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec);
Elliott Hughesfd376b92016-04-04 14:21:47 -0700138
Mathias Agopian2bd99592012-02-25 23:02:14 -0800139 return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
Mathias Agopian2bd99592012-02-25 23:02:14 -0800140}
141inline void Condition::signal() {
Igor Murashkindb419382014-04-15 15:39:27 -0700142 /*
143 * POSIX says pthread_cond_signal wakes up "one or more" waiting threads.
144 * However bionic follows the glibc guarantee which wakes up "exactly one"
145 * waiting thread.
146 *
147 * man 3 pthread_cond_signal
148 * pthread_cond_signal restarts one of the threads that are waiting on
149 * the condition variable cond. If no threads are waiting on cond,
150 * nothing happens. If several threads are waiting on cond, exactly one
151 * is restarted, but it is not specified which.
152 */
Mathias Agopian2bd99592012-02-25 23:02:14 -0800153 pthread_cond_signal(&mCond);
154}
155inline void Condition::broadcast() {
156 pthread_cond_broadcast(&mCond);
157}
158
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800159#endif // !defined(_WIN32)
Mathias Agopian2bd99592012-02-25 23:02:14 -0800160
161// ---------------------------------------------------------------------------
162}; // namespace android
163// ---------------------------------------------------------------------------
164
165#endif // _LIBS_UTILS_CONDITON_H