no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / mozglue / misc / ConditionVariable_windows.cpp
blob0c0151f1d3f9e5c7d3ed81f985dd043147b2ff96
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"
9 #include <float.h>
10 #include <intrin.h>
11 #include <stdlib.h>
12 #include <windows.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))
24 #endif
26 #if defined(_MSC_VER) && !defined(InterlockedIncrement)
27 # define InterlockedIncrement(addend) \
28 _InterlockedIncrement((volatile long*)(addend))
29 #endif
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;
50 bool r =
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()) {
58 wait(lock);
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();
70 DWORD msec;
71 if (msecd < 0.0) {
72 msec = 0;
73 } else if (msecd > UINT32_MAX) {
74 msec = INFINITE;
75 } else {
76 msec = static_cast<DWORD>(msecd);
77 // Round submillisecond waits to 1ms.
78 if (msec == 0 && !rel_time.IsZero()) {
79 msec = 1;
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_);