Skip to content

pthread_condvar.h: Fix ConditionVariable::TimedWait() on 32-bit platforms #1044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions app/src/pthread_condvar.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,11 @@ class ConditionVariable {
// Returns true otherwise
template <class Predicate>
bool TimedWait(pthread_mutex_t* lock, Predicate predicate, int milliseconds) {
timespec end_time, current_time;
clock_gettime(CLOCK_REALTIME, &end_time);
end_time.tv_nsec += milliseconds * kNanosecondsPerMillisecond;
int64_t end_time_ms = TimespecToMs(end_time);
NormalizeTimespec(&end_time);

clock_gettime(CLOCK_REALTIME, &current_time);
int64_t current_time_ms = TimespecToMs(current_time);
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);
clock_gettime(CLOCK_REALTIME, &current_time);
current_time_ms = TimespecToMs(current_time);
current_time_ms = TimespecToMs(MsToAbsoluteTimespec(0));
}
// If time isn't up, AND the predicate is true, then we return true.
// False otherwise.
Expand Down
6 changes: 0 additions & 6 deletions app/src/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ inline void Sleep(int64_t milliseconds) {
}

#if !FIREBASE_PLATFORM_WINDOWS
// Utility function for normalizing a timespec.
inline void NormalizeTimespec(timespec* t) {
t->tv_sec += t->tv_nsec / kNanosecondsPerSecond;
t->tv_nsec %= kNanosecondsPerSecond;
}

// Utility function, for converting a timespec struct into milliseconds.
inline int64_t TimespecToMs(timespec tm) {
return tm.tv_sec * firebase::internal::kMillisecondsPerSecond +
Expand Down
11 changes: 0 additions & 11 deletions app/tests/time_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,6 @@
namespace {

#ifndef WIN32
// Test that the normalize function works, for timespecs
TEST(TimeTests, NormalizeTest) {
timespec t;
t.tv_sec = 2;
t.tv_nsec = firebase::internal::kNanosecondsPerSecond * 5.5;
firebase::internal::NormalizeTimespec(&t);

EXPECT_EQ(t.tv_sec, 7);
EXPECT_EQ(t.tv_nsec, firebase::internal::kNanosecondsPerSecond * 0.5);
}

// Test the various conversions to and from timespecs.
TEST(TimeTests, ConversionTests) {
timespec t;
Expand Down