1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/CheckedInt.h"
16 #include "mozilla/PlatformConditionVariable.h"
17 #include "mozilla/PlatformMutex.h"
18 #include "MutexPlatformData_posix.h"
20 using mozilla::CheckedInt
;
21 using mozilla::TimeDuration
;
22 using mozilla::TimeStamp
;
24 static const long NanoSecPerSec
= 1000000000;
26 // Android 32-bit & macOS 10.12 has the clock functions, but not pthread_condattr_setclock.
27 #if defined(HAVE_CLOCK_MONOTONIC) && \
28 !(defined(__ANDROID__) && !defined(__LP64__)) && !defined(__APPLE__)
29 # define CV_USE_CLOCK_API
32 #ifdef CV_USE_CLOCK_API
33 // The C++ specification defines std::condition_variable::wait_for in terms of
34 // std::chrono::steady_clock, which is closest to CLOCK_MONOTONIC.
35 static const clockid_t WhichClock
= CLOCK_MONOTONIC
;
37 // While timevaladd is widely available to work with timevals, the newer
38 // timespec structure is largely lacking such conveniences. Thankfully, the
39 // utilities available in MFBT make implementing our own quite easy.
41 moz_timespecadd(struct timespec
* lhs
, struct timespec
* rhs
, struct timespec
* result
)
43 // Add nanoseconds. This may wrap, but not above 2 billion.
44 MOZ_RELEASE_ASSERT(lhs
->tv_nsec
< NanoSecPerSec
);
45 MOZ_RELEASE_ASSERT(rhs
->tv_nsec
< NanoSecPerSec
);
46 result
->tv_nsec
= lhs
->tv_nsec
+ rhs
->tv_nsec
;
48 // Add seconds, checking for overflow in the platform specific time_t type.
49 CheckedInt
<time_t> sec
= CheckedInt
<time_t>(lhs
->tv_sec
) + rhs
->tv_sec
;
51 // If nanoseconds overflowed, carry the result over into seconds.
52 if (result
->tv_nsec
>= NanoSecPerSec
) {
53 MOZ_RELEASE_ASSERT(result
->tv_nsec
< 2 * NanoSecPerSec
);
54 result
->tv_nsec
-= NanoSecPerSec
;
58 // Extracting the value asserts that there was no overflow.
59 MOZ_RELEASE_ASSERT(sec
.isValid());
60 result
->tv_sec
= sec
.value();
64 struct mozilla::detail::ConditionVariableImpl::PlatformData
66 pthread_cond_t ptCond
;
69 mozilla::detail::ConditionVariableImpl::ConditionVariableImpl()
71 pthread_cond_t
* ptCond
= &platformData()->ptCond
;
73 #ifdef CV_USE_CLOCK_API
74 pthread_condattr_t attr
;
75 int r0
= pthread_condattr_init(&attr
);
76 MOZ_RELEASE_ASSERT(!r0
);
78 int r1
= pthread_condattr_setclock(&attr
, WhichClock
);
79 MOZ_RELEASE_ASSERT(!r1
);
81 int r2
= pthread_cond_init(ptCond
, &attr
);
82 MOZ_RELEASE_ASSERT(!r2
);
84 int r3
= pthread_condattr_destroy(&attr
);
85 MOZ_RELEASE_ASSERT(!r3
);
87 int r
= pthread_cond_init(ptCond
, NULL
);
88 MOZ_RELEASE_ASSERT(!r
);
92 mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl()
94 int r
= pthread_cond_destroy(&platformData()->ptCond
);
95 MOZ_RELEASE_ASSERT(r
== 0);
99 mozilla::detail::ConditionVariableImpl::notify_one()
101 int r
= pthread_cond_signal(&platformData()->ptCond
);
102 MOZ_RELEASE_ASSERT(r
== 0);
106 mozilla::detail::ConditionVariableImpl::notify_all()
108 int r
= pthread_cond_broadcast(&platformData()->ptCond
);
109 MOZ_RELEASE_ASSERT(r
== 0);
113 mozilla::detail::ConditionVariableImpl::wait(MutexImpl
& lock
)
115 pthread_cond_t
* ptCond
= &platformData()->ptCond
;
116 pthread_mutex_t
* ptMutex
= &lock
.platformData()->ptMutex
;
118 int r
= pthread_cond_wait(ptCond
, ptMutex
);
119 MOZ_RELEASE_ASSERT(r
== 0);
123 mozilla::detail::ConditionVariableImpl::wait_for(MutexImpl
& lock
,
124 const TimeDuration
& a_rel_time
)
126 if (a_rel_time
== TimeDuration::Forever()) {
128 return CVStatus::NoTimeout
;
131 pthread_cond_t
* ptCond
= &platformData()->ptCond
;
132 pthread_mutex_t
* ptMutex
= &lock
.platformData()->ptMutex
;
135 // Clamp to 0, as time_t is unsigned.
136 TimeDuration rel_time
= a_rel_time
< TimeDuration::FromSeconds(0)
137 ? TimeDuration::FromSeconds(0)
140 // Convert the duration to a timespec.
141 struct timespec rel_ts
;
142 rel_ts
.tv_sec
= static_cast<time_t>(rel_time
.ToSeconds());
143 rel_ts
.tv_nsec
= static_cast<uint64_t>(rel_time
.ToMicroseconds() * 1000.0) % NanoSecPerSec
;
145 #ifdef CV_USE_CLOCK_API
146 struct timespec now_ts
;
147 r
= clock_gettime(WhichClock
, &now_ts
);
148 MOZ_RELEASE_ASSERT(!r
);
150 struct timespec abs_ts
;
151 moz_timespecadd(&now_ts
, &rel_ts
, &abs_ts
);
153 r
= pthread_cond_timedwait(ptCond
, ptMutex
, &abs_ts
);
155 // Our non-clock-supporting platforms, OS X and Android, do support waiting
156 // on a condition variable with a relative timeout.
157 r
= pthread_cond_timedwait_relative_np(ptCond
, ptMutex
, &rel_ts
);
161 return CVStatus::NoTimeout
;
163 MOZ_RELEASE_ASSERT(r
== ETIMEDOUT
);
164 return CVStatus::Timeout
;
167 mozilla::detail::ConditionVariableImpl::PlatformData
*
168 mozilla::detail::ConditionVariableImpl::platformData()
170 static_assert(sizeof platformData_
>= sizeof(PlatformData
),
171 "platformData_ is too small");
172 return reinterpret_cast<PlatformData
*>(platformData_
);