webrtc_m130/webrtc/system_wrappers/source/condition_variable_native_win.cc
henrike@webrtc.org 686001dd96 Split condition_variable_win.cc into native (for Vista and newer OS versions) and generic implementation (based on events).
Note that this means that there is no new code. The code has been taken directly from condition_variable_win.cc/h compensating minimally to be able to split up the two code paths.

Tested by:
1) Disabling native implementation and send to try bots.
2) Only return native implementation (i.e. if native implementation returns NULL there will be a crash when using the condition variable) and send to try bots.
3) The final cl sent to trybots.
All tests pass.

The changes are due to static analyzer code complaints.

BUG=N/A

Review URL: https://webrtc-codereview.appspot.com/1191004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@3728 4adac7df-926f-26a2-2b94-8c16560cd09d
2013-03-26 14:16:05 +00:00

106 lines
3.8 KiB
C++

/*
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/system_wrappers/interface/trace.h"
#include "webrtc/system_wrappers/source/condition_variable_native_win.h"
#include "webrtc/system_wrappers/source/critical_section_win.h"
namespace webrtc {
static HMODULE library = NULL;
PInitializeConditionVariable PInitializeConditionVariable_;
PSleepConditionVariableCS PSleepConditionVariableCS_;
PWakeConditionVariable PWakeConditionVariable_;
PWakeAllConditionVariable PWakeAllConditionVariable_;
typedef void (WINAPI *PInitializeConditionVariable)(PCONDITION_VARIABLE);
typedef BOOL (WINAPI *PSleepConditionVariableCS)(PCONDITION_VARIABLE,
PCRITICAL_SECTION, DWORD);
typedef void (WINAPI *PWakeConditionVariable)(PCONDITION_VARIABLE);
typedef void (WINAPI *PWakeAllConditionVariable)(PCONDITION_VARIABLE);
ConditionVariableNativeWin::ConditionVariableNativeWin() {
}
ConditionVariableNativeWin::~ConditionVariableNativeWin() {
}
ConditionVariableWrapper* ConditionVariableNativeWin::Create() {
ConditionVariableNativeWin* ret_val = new ConditionVariableNativeWin();
if (!ret_val->Init()) {
delete ret_val;
return NULL;
}
return ret_val;
}
bool ConditionVariableNativeWin::Init() {
bool win_support_condition_variables_primitive = true;
if (!library) {
// Native implementation is supported on Vista+.
library = LoadLibrary(TEXT("Kernel32.dll"));
// TODO(henrike): this code results in an attempt to load the above dll
// every time the previous attempt failed. Only try to load once.
if (library) {
// TODO(henrike): not thread safe as reading and writing to library is not
// serialized. Fix.
WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Kernel.dll");
PInitializeConditionVariable_ =
(PInitializeConditionVariable) GetProcAddress(
library, "InitializeConditionVariable");
PSleepConditionVariableCS_ = (PSleepConditionVariableCS) GetProcAddress(
library, "SleepConditionVariableCS");
PWakeConditionVariable_ = (PWakeConditionVariable) GetProcAddress(
library, "WakeConditionVariable");
PWakeAllConditionVariable_ = (PWakeAllConditionVariable) GetProcAddress(
library, "WakeAllConditionVariable");
if (PInitializeConditionVariable_ && PSleepConditionVariableCS_
&& PWakeConditionVariable_ && PWakeAllConditionVariable_) {
WEBRTC_TRACE(
kTraceStateInfo, kTraceUtility, -1,
"Loaded native condition variables");
} else {
win_support_condition_variables_primitive = false;
}
}
}
if (!win_support_condition_variables_primitive) {
return false;
}
PInitializeConditionVariable_(&condition_variable_);
return true;
}
void ConditionVariableNativeWin::SleepCS(CriticalSectionWrapper& crit_sect) {
SleepCS(crit_sect, INFINITE);
}
bool ConditionVariableNativeWin::SleepCS(CriticalSectionWrapper& crit_sect,
unsigned long max_time_in_ms) {
CriticalSectionWindows* cs =
static_cast<CriticalSectionWindows*>(&crit_sect);
BOOL ret_val = PSleepConditionVariableCS_(&condition_variable_,
&(cs->crit), max_time_in_ms);
return ret_val != 0;
}
void ConditionVariableNativeWin::Wake() {
PWakeConditionVariable_(&condition_variable_);
}
void ConditionVariableNativeWin::WakeAll() {
PWakeAllConditionVariable_(&condition_variable_);
}
} // namespace webrtc