-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathpthread_condvar.h
More file actions
88 lines (73 loc) · 2.82 KB
/
Copy pathpthread_condvar.h
File metadata and controls
88 lines (73 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef FIREBASE_APP_SRC_PTHREAD_CONDVAR_H_
#define FIREBASE_APP_SRC_PTHREAD_CONDVAR_H_
#include <cstdint>
#include "app/src/include/firebase/internal/platform.h"
#if !FIREBASE_PLATFORM_WINDOWS
#include <pthread.h>
#include <cerrno>
#include "app/src/time.h"
namespace firebase {
namespace internal {
// A simple RAII wraper around pthread_cond_t.
//
// It is not portable so it lives here.
class ConditionVariable {
public:
ConditionVariable() { pthread_cond_init(&cond_, nullptr); }
ConditionVariable(const ConditionVariable&) = delete;
ConditionVariable& operator=(const ConditionVariable&) = delete;
~ConditionVariable() { pthread_cond_destroy(&cond_); }
void Wait(pthread_mutex_t* mutex) { pthread_cond_wait(&cond_, mutex); }
bool TimedWait(pthread_mutex_t* mutex, const timespec* abstime) {
return pthread_cond_wait(&cond_, mutex) != 0;
}
bool TimedWait(pthread_mutex_t* mutex, int milliseconds) {
timespec abstime = MsToAbsoluteTimespec(milliseconds);
return pthread_cond_timedwait(&cond_, mutex, &abstime) != 0;
}
template <class Predicate>
void Wait(pthread_mutex_t* lock, Predicate predicate) {
while (!predicate()) {
Wait(lock);
}
}
// Waits for the condition variable to go, AND for the predicate to succeed.
// Returns false if it times out before both those conditions are met.
// Returns true otherwise
template <class Predicate>
bool TimedWait(pthread_mutex_t* lock, Predicate predicate, int milliseconds) {
int64_t end_time_ms = TimespecToMs(MsToAbsoluteTimespec(milliseconds));
int64_t current_time_ms = TimespecToMs(MsToAbsoluteTimespec(0));
while (!predicate() && current_time_ms < end_time_ms) {
TimedWait(lock, end_time_ms - current_time_ms);
current_time_ms = TimespecToMs(MsToAbsoluteTimespec(0));
}
// If time isn't up, AND the predicate is true, then we return true.
// False otherwise.
return current_time_ms < end_time_ms;
}
void NotifyOne() { pthread_cond_signal(&cond_); }
void NotifyAll() { pthread_cond_broadcast(&cond_); }
private:
pthread_cond_t cond_;
};
} // namespace internal
// NOLINTNEXTLINE - allow namespace overridden
} // namespace firebase
#endif // !FIREBASE_PLATFORM_WINDOWS
#endif // FIREBASE_APP_SRC_PTHREAD_CONDVAR_H_