Skip to content

Commit fdb50b0

Browse files
authored
pthread_condvar.h: Fix ConditionVariable::TimedWait() on 32-bit platforms (#1044)
1 parent 33554c8 commit fdb50b0

File tree

3 files changed

+3
-27
lines changed

3 files changed

+3
-27
lines changed

app/src/pthread_condvar.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,11 @@ class ConditionVariable {
5858
// Returns true otherwise
5959
template <class Predicate>
6060
bool TimedWait(pthread_mutex_t* lock, Predicate predicate, int milliseconds) {
61-
timespec end_time, current_time;
62-
clock_gettime(CLOCK_REALTIME, &end_time);
63-
end_time.tv_nsec += milliseconds * kNanosecondsPerMillisecond;
64-
int64_t end_time_ms = TimespecToMs(end_time);
65-
NormalizeTimespec(&end_time);
66-
67-
clock_gettime(CLOCK_REALTIME, &current_time);
68-
int64_t current_time_ms = TimespecToMs(current_time);
61+
int64_t end_time_ms = TimespecToMs(MsToAbsoluteTimespec(milliseconds));
62+
int64_t current_time_ms = TimespecToMs(MsToAbsoluteTimespec(0));
6963
while (!predicate() && current_time_ms < end_time_ms) {
7064
TimedWait(lock, end_time_ms - current_time_ms);
71-
clock_gettime(CLOCK_REALTIME, &current_time);
72-
current_time_ms = TimespecToMs(current_time);
65+
current_time_ms = TimespecToMs(MsToAbsoluteTimespec(0));
7366
}
7467
// If time isn't up, AND the predicate is true, then we return true.
7568
// False otherwise.

app/src/time.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ inline void Sleep(int64_t milliseconds) {
6363
}
6464

6565
#if !FIREBASE_PLATFORM_WINDOWS
66-
// Utility function for normalizing a timespec.
67-
inline void NormalizeTimespec(timespec* t) {
68-
t->tv_sec += t->tv_nsec / kNanosecondsPerSecond;
69-
t->tv_nsec %= kNanosecondsPerSecond;
70-
}
71-
7266
// Utility function, for converting a timespec struct into milliseconds.
7367
inline int64_t TimespecToMs(timespec tm) {
7468
return tm.tv_sec * firebase::internal::kMillisecondsPerSecond +

app/tests/time_test.cc

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@
2424
namespace {
2525

2626
#ifndef WIN32
27-
// Test that the normalize function works, for timespecs
28-
TEST(TimeTests, NormalizeTest) {
29-
timespec t;
30-
t.tv_sec = 2;
31-
t.tv_nsec = firebase::internal::kNanosecondsPerSecond * 5.5;
32-
firebase::internal::NormalizeTimespec(&t);
33-
34-
EXPECT_EQ(t.tv_sec, 7);
35-
EXPECT_EQ(t.tv_nsec, firebase::internal::kNanosecondsPerSecond * 0.5);
36-
}
37-
3827
// Test the various conversions to and from timespecs.
3928
TEST(TimeTests, ConversionTests) {
4029
timespec t;

0 commit comments

Comments
 (0)