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/. */
6 
7 #include "mozilla/Assertions.h"
8 
9 #include <float.h>
10 #include <intrin.h>
11 #include <stdlib.h>
12 #include <windows.h>
13 
14 #include "mozilla/PlatformConditionVariable.h"
15 #include "mozilla/PlatformMutex.h"
16 #include "MutexPlatformData_windows.h"
17 
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
25 
26 #if defined(_MSC_VER) && !defined(InterlockedIncrement)
27 #define InterlockedIncrement(addend) \
28   _InterlockedIncrement((volatile long*)(addend))
29 #endif
30 
31 // Wrapper for native condition variable APIs.
32 struct mozilla::detail::ConditionVariableImpl::PlatformData {
33   CONDITION_VARIABLE cv_;
34 };
35 
ConditionVariableImpl()36 mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() {
37   InitializeConditionVariable(&platformData()->cv_);
38 }
39 
notify_one()40 void mozilla::detail::ConditionVariableImpl::notify_one() {
41   WakeConditionVariable(&platformData()->cv_);
42 }
43 
notify_all()44 void mozilla::detail::ConditionVariableImpl::notify_all() {
45   WakeAllConditionVariable(&platformData()->cv_);
46 }
47 
wait(MutexImpl & lock)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);
53 }
54 
wait_for(MutexImpl & lock,const mozilla::TimeDuration & rel_time)55 mozilla::detail::CVStatus mozilla::detail::ConditionVariableImpl::wait_for(
56     MutexImpl& lock, const mozilla::TimeDuration& rel_time) {
57   SRWLOCK* srwlock = &lock.platformData()->lock;
58 
59   // Note that DWORD is unsigned, so we have to be careful to clamp at 0.
60   // If rel_time is Forever, then ToMilliseconds is +inf, which evaluates as
61   // greater than UINT32_MAX, resulting in the correct INFINITE wait.
62   double msecd = rel_time.ToMilliseconds();
63   DWORD msec = msecd < 0.0
64                    ? 0
65                    : msecd > UINT32_MAX ? INFINITE : static_cast<DWORD>(msecd);
66 
67   BOOL r = SleepConditionVariableSRW(&platformData()->cv_, srwlock, msec, 0);
68   if (r) return CVStatus::NoTimeout;
69   MOZ_RELEASE_ASSERT(GetLastError() == ERROR_TIMEOUT);
70   return CVStatus::Timeout;
71 }
72 
~ConditionVariableImpl()73 mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() {
74   // Native condition variables don't require cleanup.
75 }
76 
77 inline mozilla::detail::ConditionVariableImpl::PlatformData*
platformData()78 mozilla::detail::ConditionVariableImpl::platformData() {
79   static_assert(sizeof platformData_ >= sizeof(PlatformData),
80                 "platformData_ is too small");
81   return reinterpret_cast<PlatformData*>(platformData_);
82 }
83