2014-01-20 05:58:00 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#include <intrin.h>
|
|
|
|
#include <stdlib.h>
|
2016-05-05 15:03:19 +03:00
|
|
|
#include <windows.h>
|
2014-01-20 05:58:00 +04:00
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
#include "mozilla/PlatformConditionVariable.h"
|
|
|
|
#include "mozilla/PlatformMutex.h"
|
|
|
|
#include "MutexPlatformData_windows.h"
|
2014-01-20 05:58:00 +04:00
|
|
|
|
|
|
|
// Some versions of the Windows SDK have a bug where some interlocked functions
|
|
|
|
// are not redefined as compiler intrinsics. Fix that for the interlocked
|
|
|
|
// functions that are used in this file.
|
|
|
|
#if defined(_MSC_VER) && !defined(InterlockedExchangeAdd)
|
|
|
|
# define InterlockedExchangeAdd(addend, value) \
|
|
|
|
_InterlockedExchangeAdd((volatile long*)(addend), (long)(value))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(_MSC_VER) && !defined(InterlockedIncrement)
|
|
|
|
# define InterlockedIncrement(addend) \
|
|
|
|
_InterlockedIncrement((volatile long*)(addend))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Wrapper for native condition variable APIs.
|
2017-02-09 04:52:55 +03:00
|
|
|
struct mozilla::detail::ConditionVariableImpl::PlatformData {
|
2017-01-28 01:10:18 +03:00
|
|
|
CONDITION_VARIABLE cv_;
|
2014-01-20 05:58:00 +04:00
|
|
|
};
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
mozilla::detail::ConditionVariableImpl::ConditionVariableImpl() {
|
2017-01-28 01:10:18 +03:00
|
|
|
InitializeConditionVariable(&platformData()->cv_);
|
2014-01-20 05:58:00 +04:00
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
void mozilla::detail::ConditionVariableImpl::notify_one() {
|
2017-01-28 01:10:18 +03:00
|
|
|
WakeConditionVariable(&platformData()->cv_);
|
2014-01-20 05:58:00 +04:00
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
void mozilla::detail::ConditionVariableImpl::notify_all() {
|
2017-01-28 01:10:18 +03:00
|
|
|
WakeAllConditionVariable(&platformData()->cv_);
|
2014-01-20 05:58:00 +04:00
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
void mozilla::detail::ConditionVariableImpl::wait(MutexImpl& lock) {
|
2018-04-28 02:48:47 +03:00
|
|
|
SRWLOCK* srwlock = &lock.platformData()->lock;
|
|
|
|
bool r =
|
|
|
|
SleepConditionVariableSRW(&platformData()->cv_, srwlock, INFINITE, 0);
|
2014-01-20 05:58:00 +04:00
|
|
|
MOZ_RELEASE_ASSERT(r);
|
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
mozilla::CVStatus mozilla::detail::ConditionVariableImpl::wait_for(
|
|
|
|
MutexImpl& lock, const mozilla::TimeDuration& rel_time) {
|
2018-02-10 00:44:18 +03:00
|
|
|
if (rel_time == mozilla::TimeDuration::Forever()) {
|
|
|
|
wait(lock);
|
|
|
|
return CVStatus::NoTimeout;
|
|
|
|
}
|
|
|
|
|
2018-04-28 02:48:47 +03:00
|
|
|
SRWLOCK* srwlock = &lock.platformData()->lock;
|
2014-01-20 05:58:00 +04:00
|
|
|
|
2018-02-10 00:44:18 +03:00
|
|
|
// Note that DWORD is unsigned, so we have to be careful to clamp at 0. If
|
|
|
|
// rel_time is Forever, then ToMilliseconds is +inf, which evaluates as
|
|
|
|
// greater than UINT32_MAX, resulting in the correct INFINITE wait. We also
|
|
|
|
// don't want to round sub-millisecond waits to 0, as that wastes energy (see
|
|
|
|
// bug 1437167 comment 6), so we instead round submillisecond waits to 1ms.
|
2014-01-20 05:58:00 +04:00
|
|
|
double msecd = rel_time.ToMilliseconds();
|
2018-02-10 00:44:18 +03:00
|
|
|
DWORD msec;
|
|
|
|
if (msecd < 0.0) {
|
|
|
|
msec = 0;
|
|
|
|
} else if (msecd > UINT32_MAX) {
|
|
|
|
msec = INFINITE;
|
|
|
|
} else {
|
|
|
|
msec = static_cast<DWORD>(msecd);
|
|
|
|
// Round submillisecond waits to 1ms.
|
|
|
|
if (msec == 0 && !rel_time.IsZero()) {
|
|
|
|
msec = 1;
|
|
|
|
}
|
|
|
|
}
|
2014-01-20 05:58:00 +04:00
|
|
|
|
2018-04-28 02:48:47 +03:00
|
|
|
BOOL r = SleepConditionVariableSRW(&platformData()->cv_, srwlock, msec, 0);
|
2014-01-20 05:58:00 +04:00
|
|
|
if (r) return CVStatus::NoTimeout;
|
|
|
|
MOZ_RELEASE_ASSERT(GetLastError() == ERROR_TIMEOUT);
|
|
|
|
return CVStatus::Timeout;
|
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
mozilla::detail::ConditionVariableImpl::~ConditionVariableImpl() {
|
2017-01-28 01:10:18 +03:00
|
|
|
// Native condition variables don't require cleanup.
|
2014-01-20 05:58:00 +04:00
|
|
|
}
|
|
|
|
|
2017-02-09 04:52:55 +03:00
|
|
|
inline mozilla::detail::ConditionVariableImpl::PlatformData*
|
|
|
|
mozilla::detail::ConditionVariableImpl::platformData() {
|
2014-01-20 05:58:00 +04:00
|
|
|
static_assert(sizeof platformData_ >= sizeof(PlatformData),
|
|
|
|
"platformData_ is too small");
|
|
|
|
return reinterpret_cast<PlatformData*>(platformData_);
|
|
|
|
}
|