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"
14 #include "mozilla/PlatformConditionVariable.h"
15 #include "mozilla/PlatformMutex.h"
16 #include "MutexPlatformData_posix.h"
18 using mozilla::CheckedInt
;
19 using mozilla::TimeDuration
;
21 static const long NanoSecPerSec
= 1000000000;
23 // Android 32-bit & macOS 10.12 has the clock functions, but not
24 // pthread_condattr_setclock.
25 #if defined(HAVE_CLOCK_MONOTONIC) && \
26 !(defined(__ANDROID__) && !defined(__LP64__)) && !defined(__APPLE__)
27 # define CV_USE_CLOCK_API
30 #ifdef CV_USE_CLOCK_API
31 // The C++ specification defines std::condition_variable::wait_for in terms of
32 // std::chrono::steady_clock, which is closest to CLOCK_MONOTONIC.
33 static const clockid_t WhichClock
= CLOCK_MONOTONIC
;
35 // While timevaladd is widely available to work with timevals, the newer
36 // timespec structure is largely lacking such conveniences. Thankfully, the
37 // utilities available in MFBT make implementing our own quite easy.
38 static void moz_timespecadd(struct timespec
* lhs
, struct timespec
* rhs
,
39 struct timespec
* result
) {
40 // Add nanoseconds. This may wrap, but not above 2 billion.
41 MOZ_RELEASE_ASSERT(lhs
->tv_nsec
< NanoSecPerSec
);
42 MOZ_RELEASE_ASSERT(rhs
->tv_nsec
< NanoSecPerSec
);
43 result
->tv_nsec
= lhs
->tv_nsec
+ rhs
->tv_nsec
;
45 // Add seconds, checking for overflow in the platform specific time_t type.
46 CheckedInt
<time_t> sec
= CheckedInt
<time_t>(lhs
->tv_sec
) + rhs
->tv_sec
;
48 // If nanoseconds overflowed, carry the result over into seconds.
49 if (result
->tv_nsec
>= NanoSecPerSec
) {
50 MOZ_RELEASE_ASSERT(result
->tv_nsec
< 2 * NanoSecPerSec
);
51 result
->tv_nsec
-= NanoSecPerSec
;
55 // Extracting the value asserts that there was no overflow.
56 MOZ_RELEASE_ASSERT(sec
.isValid());
57 result
->tv_sec
= sec
.value();
61 struct mozilla::detail::ConditionVariableImpl::PlatformData
{
62 pthread_cond_t ptCond
;
65 mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() {
66 pthread_cond_t
* ptCond
= &platformData()->ptCond
;
68 #ifdef CV_USE_CLOCK_API
69 pthread_condattr_t attr
;
70 int r0
= pthread_condattr_init(&attr
);
71 MOZ_RELEASE_ASSERT(!r0
);
73 int r1
= pthread_condattr_setclock(&attr
, WhichClock
);
74 MOZ_RELEASE_ASSERT(!r1
);
76 int r2
= pthread_cond_init(ptCond
, &attr
);
77 MOZ_RELEASE_ASSERT(!r2
);
79 int r3
= pthread_condattr_destroy(&attr
);
80 MOZ_RELEASE_ASSERT(!r3
);
82 int r
= pthread_cond_init(ptCond
, NULL
);
83 MOZ_RELEASE_ASSERT(!r
);
87 mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() {
88 int r
= pthread_cond_destroy(&platformData()->ptCond
);
89 MOZ_RELEASE_ASSERT(r
== 0);
92 void mozilla::detail::ConditionVariableImpl::notify_one() {
93 int r
= pthread_cond_signal(&platformData()->ptCond
);
94 MOZ_RELEASE_ASSERT(r
== 0);
97 void mozilla::detail::ConditionVariableImpl::notify_all() {
98 int r
= pthread_cond_broadcast(&platformData()->ptCond
);
99 MOZ_RELEASE_ASSERT(r
== 0);
102 void mozilla::detail::ConditionVariableImpl::wait(MutexImpl
& lock
) {
103 pthread_cond_t
* ptCond
= &platformData()->ptCond
;
104 pthread_mutex_t
* ptMutex
= &lock
.platformData()->ptMutex
;
106 int r
= pthread_cond_wait(ptCond
, ptMutex
);
107 MOZ_RELEASE_ASSERT(r
== 0);
110 mozilla::CVStatus
mozilla::detail::ConditionVariableImpl::wait_for(
111 MutexImpl
& lock
, const TimeDuration
& a_rel_time
) {
112 if (a_rel_time
== TimeDuration::Forever()) {
114 return CVStatus::NoTimeout
;
117 pthread_cond_t
* ptCond
= &platformData()->ptCond
;
118 pthread_mutex_t
* ptMutex
= &lock
.platformData()->ptMutex
;
121 // Clamp to 0, as time_t is unsigned.
122 TimeDuration rel_time
= a_rel_time
< TimeDuration::FromSeconds(0)
123 ? TimeDuration::FromSeconds(0)
126 // Convert the duration to a timespec.
127 struct timespec rel_ts
;
128 rel_ts
.tv_sec
= static_cast<time_t>(rel_time
.ToSeconds());
130 static_cast<uint64_t>(rel_time
.ToMicroseconds() * 1000.0) % NanoSecPerSec
;
132 #ifdef CV_USE_CLOCK_API
133 struct timespec now_ts
;
134 r
= clock_gettime(WhichClock
, &now_ts
);
135 MOZ_RELEASE_ASSERT(!r
);
137 struct timespec abs_ts
;
138 moz_timespecadd(&now_ts
, &rel_ts
, &abs_ts
);
140 r
= pthread_cond_timedwait(ptCond
, ptMutex
, &abs_ts
);
142 // Our non-clock-supporting platforms, OS X and Android, do support waiting
143 // on a condition variable with a relative timeout.
144 r
= pthread_cond_timedwait_relative_np(ptCond
, ptMutex
, &rel_ts
);
148 return CVStatus::NoTimeout
;
150 MOZ_RELEASE_ASSERT(r
== ETIMEDOUT
);
151 return CVStatus::Timeout
;
154 mozilla::detail::ConditionVariableImpl::PlatformData
*
155 mozilla::detail::ConditionVariableImpl::platformData() {
156 static_assert(sizeof platformData_
>= sizeof(PlatformData
),
157 "platformData_ is too small");
158 return reinterpret_cast<PlatformData
*>(platformData_
);