blob: ba1c67ac679fe4a14cfa60a8313ea67d8dd3da7a [file] [log] [blame]
fei.dengb9a1a572023-09-13 01:33:57 +00001/*
2 * Copyright (C) 2021 Amlogic Corporation.
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#ifndef _TOOLS_CONDITION_H_
17#define _TOOLS_CONDITION_H_
18
19#include <limits.h>
20#include <stdint.h>
21#include <sys/types.h>
22#include <time.h>
23#include <pthread.h>
24#include "Mutex.h"
25
26namespace Tls {
27class Condition {
28public:
29 enum {
30 PRIVATE = 0,
31 SHARED = 1
32 };
33
34 enum WakeUpType {
35 WAKE_UP_ONE = 0,
36 WAKE_UP_ALL = 1
37 };
38
39 Condition();
40 explicit Condition(int type);
41 ~Condition();
42 // Wait on the condition variable. Lock the mutex before calling.
43 // Note that spurious wake-ups may happen.
44 int wait(Mutex& mutex);
45 // same with relative timeout , microsecond time
46 int waitRelative(Mutex& mutex, int64_t reltime);
47 // same with relative timeout , us time
48 int waitRelativeUs(Mutex& mutex, int64_t reltime/*us*/);
49 // Signal the condition variable, allowing one thread to continue.
50 void signal();
51 // Signal the condition variable, allowing one or all threads to continue.
52 void signal(WakeUpType type) {
53 if (type == WAKE_UP_ONE) {
54 signal();
55 } else {
56 broadcast();
57 }
58 }
59 // Signal the condition variable, allowing all threads to continue.
60 void broadcast();
61
62private:
63 pthread_cond_t mCond;
64};
65
66inline Condition::Condition() : Condition(PRIVATE) {
67}
68inline Condition::Condition(int type) {
69 pthread_condattr_t attr;
70 pthread_condattr_init(&attr);
71 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
72
73 if (type == SHARED) {
74 pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
75 }
76
77 pthread_cond_init(&mCond, &attr);
78 pthread_condattr_destroy(&attr);
79
80}
81inline Condition::~Condition() {
82 pthread_cond_destroy(&mCond);
83}
84
85/**
86 * wait relative nanosecond time
87 * return 0 if wait success, non 0 if timeout or other
88*/
89inline int Condition::wait(Mutex& mutex) {
90 return -pthread_cond_wait(&mCond, &mutex.mMutex);
91}
92
93/**
94 * wait relative microsecond time
95 * return 0 if wait success, non 0 if timeout or other
96 * ETIMEDOUT timeout,
97*/
98inline int Condition::waitRelative(Mutex& mutex, int64_t reltime/*microsecnd*/) {
99 struct timespec ts;
100 clock_gettime(CLOCK_MONOTONIC, &ts);
101
102
103 // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit.
104 int64_t reltime_sec = reltime/1000;
105
106 ts.tv_nsec += static_cast<long>(reltime*1000*1000%1000000000);
107 if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) {
108 ts.tv_nsec -= 1000000000;
109 ++reltime_sec;
110 }
111
112 int64_t time_sec = ts.tv_sec;
113 if (time_sec > INT64_MAX - reltime_sec) {
114 time_sec = INT64_MAX;
115 } else {
116 time_sec += reltime_sec;
117 }
118
119 ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec);
120
121 return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
122}
123
124/**
125 * wait relative us time
126 * return 0 if wait success, non 0 if timeout or other
127 * ETIMEDOUT timeout,
128*/
129inline int Condition::waitRelativeUs(Mutex& mutex, int64_t reltime/*us*/) {
130 struct timespec ts;
131 clock_gettime(CLOCK_MONOTONIC, &ts);
132
133
134 // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit.
135 int64_t reltime_sec = reltime/1000000;
136
137 ts.tv_nsec += static_cast<long>(reltime*1000%1000000000);
138 if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) {
139 ts.tv_nsec -= 1000000000;
140 ++reltime_sec;
141 }
142
143 int64_t time_sec = ts.tv_sec;
144 if (time_sec > INT64_MAX - reltime_sec) {
145 time_sec = INT64_MAX;
146 } else {
147 time_sec += reltime_sec;
148 }
149
150 ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec);
151
152 return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
153}
154
155inline void Condition::signal() {
156 pthread_cond_signal(&mCond);
157}
158inline void Condition::broadcast() {
159 pthread_cond_broadcast(&mCond);
160}
161
162}
163
164#endif // _TOOLS_CONDITION_H_