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"
14 #include "mozilla/PlatformConditionVariable.h"
15 #include "mozilla/PlatformMutex.h"
16 #include "MutexPlatformData_windows.h"
18 // Some versions of the Windows SDK have a bug where some interlocked functions
19 // are not redefined as compiler intrinsics. Fix that for the interlocked
20 // functions that are used in this file.
21 #if defined(_MSC_VER) && !defined(InterlockedExchangeAdd)
22 # define InterlockedExchangeAdd(addend, value) \
23 _InterlockedExchangeAdd((volatile long*)(addend), (long)(value))
26 #if defined(_MSC_VER) && !defined(InterlockedIncrement)
27 # define InterlockedIncrement(addend) \
28 _InterlockedIncrement((volatile long*)(addend))
31 // Wrapper for native condition variable APIs.
32 struct mozilla::detail::ConditionVariableImpl::PlatformData
{
33 CONDITION_VARIABLE cv_
;
36 mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() {
37 InitializeConditionVariable(&platformData()->cv_
);
40 void mozilla::detail::ConditionVariableImpl::notify_one() {
41 WakeConditionVariable(&platformData()->cv_
);
44 void mozilla::detail::ConditionVariableImpl::notify_all() {
45 WakeAllConditionVariable(&platformData()->cv_
);
48 void mozilla::detail::ConditionVariableImpl::wait(MutexImpl
& lock
) {
49 SRWLOCK
* srwlock
= &lock
.platformData()->lock
;
51 SleepConditionVariableSRW(&platformData()->cv_
, srwlock
, INFINITE
, 0);
52 MOZ_RELEASE_ASSERT(r
);
55 mozilla::CVStatus
mozilla::detail::ConditionVariableImpl::wait_for(
56 MutexImpl
& lock
, const mozilla::TimeDuration
& rel_time
) {
57 if (rel_time
== mozilla::TimeDuration::Forever()) {
59 return CVStatus::NoTimeout
;
62 SRWLOCK
* srwlock
= &lock
.platformData()->lock
;
64 // Note that DWORD is unsigned, so we have to be careful to clamp at 0. If
65 // rel_time is Forever, then ToMilliseconds is +inf, which evaluates as
66 // greater than UINT32_MAX, resulting in the correct INFINITE wait. We also
67 // don't want to round sub-millisecond waits to 0, as that wastes energy (see
68 // bug 1437167 comment 6), so we instead round submillisecond waits to 1ms.
69 double msecd
= rel_time
.ToMilliseconds();
73 } else if (msecd
> UINT32_MAX
) {
76 msec
= static_cast<DWORD
>(msecd
);
77 // Round submillisecond waits to 1ms.
78 if (msec
== 0 && !rel_time
.IsZero()) {
83 BOOL r
= SleepConditionVariableSRW(&platformData()->cv_
, srwlock
, msec
, 0);
84 if (r
) return CVStatus::NoTimeout
;
85 MOZ_RELEASE_ASSERT(GetLastError() == ERROR_TIMEOUT
);
86 return CVStatus::Timeout
;
89 mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() {
90 // Native condition variables don't require cleanup.
93 inline mozilla::detail::ConditionVariableImpl::PlatformData
*
94 mozilla::detail::ConditionVariableImpl::platformData() {
95 static_assert(sizeof platformData_
>= sizeof(PlatformData
),
96 "platformData_ is too small");
97 return reinterpret_cast<PlatformData
*>(platformData_
);