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::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;
60   }
61 
62   SRWLOCK* srwlock = &lock.platformData()->lock;
63 
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;
80     }
81   }
82 
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;
87 }
88 
~ConditionVariableImpl()89 mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() {
90   // Native condition variables don't require cleanup.
91 }
92 
93 inline mozilla::detail::ConditionVariableImpl::PlatformData*
platformData()94 mozilla::detail::ConditionVariableImpl::platformData() {
95   static_assert(sizeof platformData_ >= sizeof(PlatformData),
96                 "platformData_ is too small");
97   return reinterpret_cast<PlatformData*>(platformData_);
98 }
99