Blame SOURCES/0007-Add-missing-windows-conditions-variable.patch

14ed36
From 6ad2f51e9e94daf6b5925590c4cc08459a2e0833 Mon Sep 17 00:00:00 2001
14ed36
From: Nicolas Dufresne <nicolas.dufresne@collabora.com>
14ed36
Date: Mon, 4 Jul 2016 22:12:20 -0400
14ed36
Subject: [PATCH 07/16] Add missing windows conditions variable
14ed36
14ed36
Those are used by generic RW lock implementation.
14ed36
14ed36
https://bugs.freedesktop.org/show_bug.cgi?id=96754
14ed36
---
14ed36
 webrtc/system_wrappers/Makefile.am                 |  41 ++++-
14ed36
 .../include/condition_variable_wrapper.h           |  42 +++++
14ed36
 .../system_wrappers/source/condition_variable.cc   |  41 +++++
14ed36
 .../source/condition_variable_event_win.cc         | 195 +++++++++++++++++++++
14ed36
 .../source/condition_variable_event_win.h          |  46 +++++
14ed36
 .../source/condition_variable_native_win.cc        | 104 +++++++++++
14ed36
 .../source/condition_variable_native_win.h         |  54 ++++++
14ed36
 7 files changed, 514 insertions(+), 9 deletions(-)
14ed36
 create mode 100644 webrtc/system_wrappers/include/condition_variable_wrapper.h
14ed36
 create mode 100644 webrtc/system_wrappers/source/condition_variable.cc
14ed36
 create mode 100644 webrtc/system_wrappers/source/condition_variable_event_win.cc
14ed36
 create mode 100644 webrtc/system_wrappers/source/condition_variable_event_win.h
14ed36
 create mode 100644 webrtc/system_wrappers/source/condition_variable_native_win.cc
14ed36
 create mode 100644 webrtc/system_wrappers/source/condition_variable_native_win.h
14ed36
14ed36
diff --git a/webrtc/system_wrappers/Makefile.am b/webrtc/system_wrappers/Makefile.am
14ed36
index 8215013..09973a7 100644
14ed36
--- a/webrtc/system_wrappers/Makefile.am
14ed36
+++ b/webrtc/system_wrappers/Makefile.am
14ed36
@@ -2,6 +2,7 @@ noinst_LTLIBRARIES = libsystem_wrappers.la
14ed36
 
14ed36
 noinst_HEADERS = include/aligned_array.h \
14ed36
 		 include/asm_defines.h \
14ed36
+		 include/condition_variable_wrapper.h \
14ed36
 		 include/compile_assert_c.h \
14ed36
 		 include/event_wrapper.h \
14ed36
 		 include/scoped_vector.h \
14ed36
@@ -42,27 +43,49 @@ libsystem_wrappers_la_SOURCES = include/aligned_malloc.h \
14ed36
 				source/trace_impl.h \
14ed36
 				source/trace_posix.h \
14ed36
 				source/trace_win.h
14ed36
+
14ed36
+EXTRA_DIST = BUILD.gn
14ed36
+
14ed36
 if HAVE_POSIX
14ed36
 libsystem_wrappers_la_SOURCES += source/critical_section_posix.cc \
14ed36
 				 source/event_timer_posix.cc \
14ed36
 				 source/rw_lock_posix.cc \
14ed36
 				 source/thread_posix.cc \
14ed36
 				 source/trace_posix.cc
14ed36
+else
14ed36
+EXTRA_DIST += source/critical_section_posix.cc \
14ed36
+			  source/event_timer_posix.cc \
14ed36
+			  source/rw_lock_posix.cc \
14ed36
+			  source/thread_posix.cc \
14ed36
+			  source/trace_posix.cc
14ed36
 endif
14ed36
+
14ed36
 if HAVE_WIN
14ed36
-libsystem_wrappers_la_SOURCES += source/critical_section_win.cc \
14ed36
+libsystem_wrappers_la_SOURCES += include/fix_interlocked_exchange_pointer_win.h \
14ed36
+				 source/critical_section_win.cc \
14ed36
+				 source/condition_variable.cc \
14ed36
+				 source/condition_variable_event_win.cc \
14ed36
+				 source/condition_variable_event_win.h \
14ed36
+				 source/condition_variable_native_win.cc \
14ed36
+				 source/condition_variable_native_win.h \
14ed36
 				 source/event_timer_win.cc \
14ed36
 				 source/rw_lock_win.cc \
14ed36
 				 source/rw_lock_generic.cc \
14ed36
 				 source/thread_win.cc \
14ed36
 				 source/trace_win.cc
14ed36
+else
14ed36
+EXTRA_DIST += include/fix_interlocked_exchange_pointer_win.h \
14ed36
+	      source/critical_section_win.cc \
14ed36
+	      source/condition_variable.cc \
14ed36
+	      source/condition_variable_event_win.cc \
14ed36
+	      source/condition_variable_event_win.h \
14ed36
+	      source/condition_variable_native_win.cc \
14ed36
+	      source/condition_variable_native_win.h \
14ed36
+	      source/event_timer_win.cc \
14ed36
+	      source/rw_lock_generic.cc \
14ed36
+	      source/rw_lock_win.cc \
14ed36
+	      source/thread_win.cc \
14ed36
+	      source/trace_win.cc
14ed36
 endif
14ed36
-libsystem_wrappers_la_CXXFLAGS = $(AM_CXXFLAGS) $(COMMON_CXXFLAGS)
14ed36
 
14ed36
-EXTRA_DIST = BUILD.gn \
14ed36
-	     source/critical_section_win.cc \
14ed36
-	     source/event_timer_win.cc \
14ed36
-	     source/rw_lock_generic.cc \
14ed36
-	     source/rw_lock_win.cc \
14ed36
-	     source/thread_win.cc \
14ed36
-	     source/trace_win.cc
14ed36
+libsystem_wrappers_la_CXXFLAGS = $(AM_CXXFLAGS) $(COMMON_CXXFLAGS)
14ed36
diff --git a/webrtc/system_wrappers/include/condition_variable_wrapper.h b/webrtc/system_wrappers/include/condition_variable_wrapper.h
14ed36
new file mode 100644
14ed36
index 0000000..37ca30f
14ed36
--- /dev/null
14ed36
+++ b/webrtc/system_wrappers/include/condition_variable_wrapper.h
14ed36
@@ -0,0 +1,42 @@
14ed36
+/*
14ed36
+ *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
14ed36
+ *
14ed36
+ *  Use of this source code is governed by a BSD-style license
14ed36
+ *  that can be found in the LICENSE file in the root of the source
14ed36
+ *  tree. An additional intellectual property rights grant can be found
14ed36
+ *  in the file PATENTS.  All contributing project authors may
14ed36
+ *  be found in the AUTHORS file in the root of the source tree.
14ed36
+ */
14ed36
+
14ed36
+#ifndef WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CONDITION_VARIABLE_WRAPPER_H_
14ed36
+#define WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CONDITION_VARIABLE_WRAPPER_H_
14ed36
+
14ed36
+namespace webrtc {
14ed36
+
14ed36
+class CriticalSectionWrapper;
14ed36
+
14ed36
+class ConditionVariableWrapper {
14ed36
+ public:
14ed36
+  // Factory method, constructor disabled.
14ed36
+  static ConditionVariableWrapper* CreateConditionVariable();
14ed36
+
14ed36
+  virtual ~ConditionVariableWrapper() {}
14ed36
+
14ed36
+  // Calling thread will atomically release crit_sect and wait until next
14ed36
+  // some other thread calls Wake() or WakeAll().
14ed36
+  virtual void SleepCS(CriticalSectionWrapper& crit_sect) = 0;
14ed36
+
14ed36
+  // Same as above but with a timeout.
14ed36
+  virtual bool SleepCS(CriticalSectionWrapper& crit_sect,
14ed36
+                       unsigned long max_time_in_ms) = 0;
14ed36
+
14ed36
+  // Wakes one thread calling SleepCS().
14ed36
+  virtual void Wake() = 0;
14ed36
+
14ed36
+  // Wakes all threads calling SleepCS().
14ed36
+  virtual void WakeAll() = 0;
14ed36
+};
14ed36
+
14ed36
+}  // namespace webrtc
14ed36
+
14ed36
+#endif  // WEBRTC_SYSTEM_WRAPPERS_INCLUDE_CONDITION_VARIABLE_WRAPPER_H_
14ed36
diff --git a/webrtc/system_wrappers/source/condition_variable.cc b/webrtc/system_wrappers/source/condition_variable.cc
14ed36
new file mode 100644
14ed36
index 0000000..f5ae93a
14ed36
--- /dev/null
14ed36
+++ b/webrtc/system_wrappers/source/condition_variable.cc
14ed36
@@ -0,0 +1,41 @@
14ed36
+/*
14ed36
+ *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
14ed36
+ *
14ed36
+ *  Use of this source code is governed by a BSD-style license
14ed36
+ *  that can be found in the LICENSE file in the root of the source
14ed36
+ *  tree. An additional intellectual property rights grant can be found
14ed36
+ *  in the file PATENTS.  All contributing project authors may
14ed36
+ *  be found in the AUTHORS file in the root of the source tree.
14ed36
+ */
14ed36
+
14ed36
+#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
14ed36
+
14ed36
+#if defined(_WIN32)
14ed36
+#include <windows.h>
14ed36
+#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
14ed36
+#include "webrtc/system_wrappers/source/condition_variable_native_win.h"
14ed36
+#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
14ed36
+#include <pthread.h>
14ed36
+#include "webrtc/system_wrappers/source/condition_variable_posix.h"
14ed36
+#endif
14ed36
+
14ed36
+namespace webrtc {
14ed36
+
14ed36
+ConditionVariableWrapper* ConditionVariableWrapper::CreateConditionVariable() {
14ed36
+#if defined(_WIN32)
14ed36
+  // Try to create native condition variable implementation.
14ed36
+  ConditionVariableWrapper* ret_val = ConditionVariableNativeWin::Create();
14ed36
+  if (!ret_val) {
14ed36
+    // Native condition variable implementation does not exist. Create generic
14ed36
+    // condition variable based on events.
14ed36
+    ret_val = new ConditionVariableEventWin();
14ed36
+  }
14ed36
+  return ret_val;
14ed36
+#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
14ed36
+  return ConditionVariablePosix::Create();
14ed36
+#else
14ed36
+  return NULL;
14ed36
+#endif
14ed36
+}
14ed36
+
14ed36
+}  // namespace webrtc
14ed36
diff --git a/webrtc/system_wrappers/source/condition_variable_event_win.cc b/webrtc/system_wrappers/source/condition_variable_event_win.cc
14ed36
new file mode 100644
14ed36
index 0000000..41b019d
14ed36
--- /dev/null
14ed36
+++ b/webrtc/system_wrappers/source/condition_variable_event_win.cc
14ed36
@@ -0,0 +1,195 @@
14ed36
+/*
14ed36
+Source:
14ed36
+http://www1.cse.wustl.edu/~schmidt/ACE-copying.html
14ed36
+
14ed36
+License:
14ed36
+Copyright and Licensing Information for ACE(TM), TAO(TM), CIAO(TM), DAnCE(TM),
14ed36
+and CoSMIC(TM)
14ed36
+
14ed36
+ACE(TM), TAO(TM), CIAO(TM), DAnCE>(TM), and CoSMIC(TM) (henceforth referred to
14ed36
+as "DOC software") are copyrighted by Douglas C. Schmidt and his research
14ed36
+group at Washington University, University of California, Irvine, and
14ed36
+Vanderbilt University, Copyright (c) 1993-2009, all rights reserved. Since DOC
14ed36
+software is open-source, freely available software, you are free to use,
14ed36
+modify, copy, and distribute--perpetually and irrevocably--the DOC software
14ed36
+source code and object code produced from the source, as well as copy and
14ed36
+distribute modified versions of this software. You must, however, include this
14ed36
+copyright statement along with any code built using DOC software that you
14ed36
+release. No copyright statement needs to be provided if you just ship binary
14ed36
+executables of your software products.
14ed36
+You can use DOC software in commercial and/or binary software releases and are
14ed36
+under no obligation to redistribute any of your source code that is built
14ed36
+using DOC software. Note, however, that you may not misappropriate the DOC
14ed36
+software code, such as copyrighting it yourself or claiming authorship of the
14ed36
+DOC software code, in a way that will prevent DOC software from being
14ed36
+distributed freely using an open-source development model. You needn't inform
14ed36
+anyone that you're using DOC software in your software, though we encourage
14ed36
+you to let us know so we can promote your project in the DOC software success
14ed36
+stories.
14ed36
+
14ed36
+The ACE, TAO, CIAO, DAnCE, and CoSMIC web sites are maintained by the DOC
14ed36
+Group at the Institute for Software Integrated Systems (ISIS) and the Center
14ed36
+for Distributed Object Computing of Washington University, St. Louis for the
14ed36
+development of open-source software as part of the open-source software
14ed36
+community. Submissions are provided by the submitter ``as is'' with no
14ed36
+warranties whatsoever, including any warranty of merchantability,
14ed36
+noninfringement of third party intellectual property, or fitness for any
14ed36
+particular purpose. In no event shall the submitter be liable for any direct,
14ed36
+indirect, special, exemplary, punitive, or consequential damages, including
14ed36
+without limitation, lost profits, even if advised of the possibility of such
14ed36
+damages. Likewise, DOC software is provided as is with no warranties of any
14ed36
+kind, including the warranties of design, merchantability, and fitness for a
14ed36
+particular purpose, noninfringement, or arising from a course of dealing,
14ed36
+usage or trade practice. Washington University, UC Irvine, Vanderbilt
14ed36
+University, their employees, and students shall have no liability with respect
14ed36
+to the infringement of copyrights, trade secrets or any patents by DOC
14ed36
+software or any part thereof. Moreover, in no event will Washington
14ed36
+University, UC Irvine, or Vanderbilt University, their employees, or students
14ed36
+be liable for any lost revenue or profits or other special, indirect and
14ed36
+consequential damages.
14ed36
+
14ed36
+DOC software is provided with no support and without any obligation on the
14ed36
+part of Washington University, UC Irvine, Vanderbilt University, their
14ed36
+employees, or students to assist in its use, correction, modification, or
14ed36
+enhancement. A number of companies around the world provide commercial support
14ed36
+for DOC software, however. DOC software is Y2K-compliant, as long as the
14ed36
+underlying OS platform is Y2K-compliant. Likewise, DOC software is compliant
14ed36
+with the new US daylight savings rule passed by Congress as "The Energy Policy
14ed36
+Act of 2005," which established new daylight savings times (DST) rules for the
14ed36
+United States that expand DST as of March 2007. Since DOC software obtains
14ed36
+time/date and calendaring information from operating systems users will not be
14ed36
+affected by the new DST rules as long as they upgrade their operating systems
14ed36
+accordingly.
14ed36
+
14ed36
+The names ACE(TM), TAO(TM), CIAO(TM), DAnCE(TM), CoSMIC(TM), Washington
14ed36
+University, UC Irvine, and Vanderbilt University, may not be used to endorse
14ed36
+or promote products or services derived from this source without express
14ed36
+written permission from Washington University, UC Irvine, or Vanderbilt
14ed36
+University. This license grants no permission to call products or services
14ed36
+derived from this source ACE(TM), TAO(TM), CIAO(TM), DAnCE(TM), or CoSMIC(TM),
14ed36
+nor does it grant permission for the name Washington University, UC Irvine, or
14ed36
+Vanderbilt University to appear in their names.
14ed36
+*/
14ed36
+
14ed36
+/*
14ed36
+ *  This source code contain modifications to the original source code
14ed36
+ *  which can be found here:
14ed36
+ *  http://www.cs.wustl.edu/~schmidt/win32-cv-1.html (section 3.2).
14ed36
+ *  Modifications:
14ed36
+ *  1) Dynamic detection of native support for condition variables.
14ed36
+ *  2) Use of WebRTC defined types and classes. Renaming of some functions.
14ed36
+ *  3) Introduction of a second event for wake all functionality. This prevents
14ed36
+ *     a thread from spinning on the same condition variable, preventing other
14ed36
+ *     threads from waking up.
14ed36
+ */
14ed36
+
14ed36
+#include "webrtc/system_wrappers/source/condition_variable_event_win.h"
14ed36
+#include "webrtc/system_wrappers/source/critical_section_win.h"
14ed36
+
14ed36
+namespace webrtc {
14ed36
+
14ed36
+ConditionVariableEventWin::ConditionVariableEventWin() : eventID_(WAKEALL_0) {
14ed36
+  memset(&num_waiters_[0], 0, sizeof(num_waiters_));
14ed36
+
14ed36
+  InitializeCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  events_[WAKEALL_0] = CreateEvent(NULL,  // no security attributes
14ed36
+                                   TRUE,  // manual-reset, sticky event
14ed36
+                                   FALSE,  // initial state non-signaled
14ed36
+                                   NULL);  // no name for event
14ed36
+
14ed36
+  events_[WAKEALL_1] = CreateEvent(NULL,  // no security attributes
14ed36
+                                   TRUE,  // manual-reset, sticky event
14ed36
+                                   FALSE,  // initial state non-signaled
14ed36
+                                   NULL);  // no name for event
14ed36
+
14ed36
+  events_[WAKE] = CreateEvent(NULL,  // no security attributes
14ed36
+                              FALSE,  // auto-reset, sticky event
14ed36
+                              FALSE,  // initial state non-signaled
14ed36
+                              NULL);  // no name for event
14ed36
+}
14ed36
+
14ed36
+ConditionVariableEventWin::~ConditionVariableEventWin() {
14ed36
+  CloseHandle(events_[WAKE]);
14ed36
+  CloseHandle(events_[WAKEALL_1]);
14ed36
+  CloseHandle(events_[WAKEALL_0]);
14ed36
+
14ed36
+  DeleteCriticalSection(&num_waiters_crit_sect_);
14ed36
+}
14ed36
+
14ed36
+void ConditionVariableEventWin::SleepCS(CriticalSectionWrapper& crit_sect) {
14ed36
+  SleepCS(crit_sect, INFINITE);
14ed36
+}
14ed36
+
14ed36
+bool ConditionVariableEventWin::SleepCS(CriticalSectionWrapper& crit_sect,
14ed36
+                                        unsigned long max_time_in_ms) {
14ed36
+  EnterCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  // Get the eventID for the event that will be triggered by next
14ed36
+  // WakeAll() call and start waiting for it.
14ed36
+  const EventWakeUpType eventID =
14ed36
+      (WAKEALL_0 == eventID_) ? WAKEALL_1 : WAKEALL_0;
14ed36
+
14ed36
+  ++(num_waiters_[eventID]);
14ed36
+  LeaveCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  CriticalSectionWindows* cs =
14ed36
+      static_cast<CriticalSectionWindows*>(&crit_sect);
14ed36
+  LeaveCriticalSection(&cs->crit);
14ed36
+  HANDLE events[2];
14ed36
+  events[0] = events_[WAKE];
14ed36
+  events[1] = events_[eventID];
14ed36
+  const DWORD result = WaitForMultipleObjects(2,  // Wait on 2 events.
14ed36
+                                              events,
14ed36
+                                              FALSE,  // Wait for either.
14ed36
+                                              max_time_in_ms);
14ed36
+
14ed36
+  const bool ret_val = (result != WAIT_TIMEOUT);
14ed36
+
14ed36
+  EnterCriticalSection(&num_waiters_crit_sect_);
14ed36
+  --(num_waiters_[eventID]);
14ed36
+
14ed36
+  // Last waiter should only be true for WakeAll(). WakeAll() correspond
14ed36
+  // to position 1 in events[] -> (result == WAIT_OBJECT_0 + 1)
14ed36
+  const bool last_waiter = (result == WAIT_OBJECT_0 + 1) &&
14ed36
+      (num_waiters_[eventID] == 0);
14ed36
+  LeaveCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  if (last_waiter) {
14ed36
+    // Reset/unset the WakeAll() event since all threads have been
14ed36
+    // released.
14ed36
+    ResetEvent(events_[eventID]);
14ed36
+  }
14ed36
+
14ed36
+  EnterCriticalSection(&cs->crit);
14ed36
+  return ret_val;
14ed36
+}
14ed36
+
14ed36
+void ConditionVariableEventWin::Wake() {
14ed36
+  EnterCriticalSection(&num_waiters_crit_sect_);
14ed36
+  const bool have_waiters = (num_waiters_[WAKEALL_0] > 0) ||
14ed36
+      (num_waiters_[WAKEALL_1] > 0);
14ed36
+  LeaveCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  if (have_waiters) {
14ed36
+    SetEvent(events_[WAKE]);
14ed36
+  }
14ed36
+}
14ed36
+
14ed36
+void ConditionVariableEventWin::WakeAll() {
14ed36
+  EnterCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  // Update current WakeAll() event
14ed36
+  eventID_ = (WAKEALL_0 == eventID_) ? WAKEALL_1 : WAKEALL_0;
14ed36
+
14ed36
+  // Trigger current event
14ed36
+  const EventWakeUpType eventID = eventID_;
14ed36
+  const bool have_waiters = num_waiters_[eventID] > 0;
14ed36
+  LeaveCriticalSection(&num_waiters_crit_sect_);
14ed36
+
14ed36
+  if (have_waiters) {
14ed36
+    SetEvent(events_[eventID]);
14ed36
+  }
14ed36
+}
14ed36
+
14ed36
+}  // namespace webrtc
14ed36
diff --git a/webrtc/system_wrappers/source/condition_variable_event_win.h b/webrtc/system_wrappers/source/condition_variable_event_win.h
14ed36
new file mode 100644
14ed36
index 0000000..cdcef7d
14ed36
--- /dev/null
14ed36
+++ b/webrtc/system_wrappers/source/condition_variable_event_win.h
14ed36
@@ -0,0 +1,46 @@
14ed36
+/*
14ed36
+ *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
14ed36
+ *
14ed36
+ *  Use of this source code is governed by a BSD-style license
14ed36
+ *  that can be found in the LICENSE file in the root of the source
14ed36
+ *  tree. An additional intellectual property rights grant can be found
14ed36
+ *  in the file PATENTS.  All contributing project authors may
14ed36
+ *  be found in the AUTHORS file in the root of the source tree.
14ed36
+ */
14ed36
+
14ed36
+#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_EVENT_WIN_H_
14ed36
+#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_EVENT_WIN_H_
14ed36
+
14ed36
+#include <windows.h>
14ed36
+
14ed36
+#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
14ed36
+
14ed36
+namespace webrtc {
14ed36
+
14ed36
+class ConditionVariableEventWin : public ConditionVariableWrapper {
14ed36
+ public:
14ed36
+  ConditionVariableEventWin();
14ed36
+  virtual ~ConditionVariableEventWin();
14ed36
+
14ed36
+  void SleepCS(CriticalSectionWrapper& crit_sect);
14ed36
+  bool SleepCS(CriticalSectionWrapper& crit_sect, unsigned long max_time_inMS);
14ed36
+  void Wake();
14ed36
+  void WakeAll();
14ed36
+
14ed36
+ private:
14ed36
+  enum EventWakeUpType {
14ed36
+    WAKEALL_0   = 0,
14ed36
+    WAKEALL_1   = 1,
14ed36
+    WAKE        = 2,
14ed36
+    EVENT_COUNT = 3
14ed36
+  };
14ed36
+
14ed36
+  unsigned int     num_waiters_[2];
14ed36
+  EventWakeUpType  eventID_;
14ed36
+  CRITICAL_SECTION num_waiters_crit_sect_;
14ed36
+  HANDLE           events_[EVENT_COUNT];
14ed36
+};
14ed36
+
14ed36
+}  // namespace webrtc
14ed36
+
14ed36
+#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_EVENT_WIN_H_
14ed36
diff --git a/webrtc/system_wrappers/source/condition_variable_native_win.cc b/webrtc/system_wrappers/source/condition_variable_native_win.cc
14ed36
new file mode 100644
14ed36
index 0000000..45225f2
14ed36
--- /dev/null
14ed36
+++ b/webrtc/system_wrappers/source/condition_variable_native_win.cc
14ed36
@@ -0,0 +1,104 @@
14ed36
+/*
14ed36
+ *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
14ed36
+ *
14ed36
+ *  Use of this source code is governed by a BSD-style license
14ed36
+ *  that can be found in the LICENSE file in the root of the source
14ed36
+ *  tree. An additional intellectual property rights grant can be found
14ed36
+ *  in the file PATENTS.  All contributing project authors may
14ed36
+ *  be found in the AUTHORS file in the root of the source tree.
14ed36
+ */
14ed36
+
14ed36
+#include "webrtc/system_wrappers/include/trace.h"
14ed36
+#include "webrtc/system_wrappers/source/condition_variable_native_win.h"
14ed36
+#include "webrtc/system_wrappers/source/critical_section_win.h"
14ed36
+
14ed36
+namespace webrtc {
14ed36
+
14ed36
+static HMODULE library = NULL;
14ed36
+static bool win_support_condition_variables_primitive = false;
14ed36
+
14ed36
+PInitializeConditionVariable  PInitializeConditionVariable_;
14ed36
+PSleepConditionVariableCS     PSleepConditionVariableCS_;
14ed36
+PWakeConditionVariable        PWakeConditionVariable_;
14ed36
+PWakeAllConditionVariable     PWakeAllConditionVariable_;
14ed36
+
14ed36
+typedef void (WINAPI *PInitializeConditionVariable)(PCONDITION_VARIABLE);
14ed36
+typedef BOOL (WINAPI *PSleepConditionVariableCS)(PCONDITION_VARIABLE,
14ed36
+                                                 PCRITICAL_SECTION, DWORD);
14ed36
+typedef void (WINAPI *PWakeConditionVariable)(PCONDITION_VARIABLE);
14ed36
+typedef void (WINAPI *PWakeAllConditionVariable)(PCONDITION_VARIABLE);
14ed36
+
14ed36
+ConditionVariableNativeWin::ConditionVariableNativeWin() {
14ed36
+}
14ed36
+
14ed36
+ConditionVariableNativeWin::~ConditionVariableNativeWin() {
14ed36
+}
14ed36
+
14ed36
+ConditionVariableWrapper* ConditionVariableNativeWin::Create() {
14ed36
+  ConditionVariableNativeWin* ret_val = new ConditionVariableNativeWin();
14ed36
+  if (!ret_val->Init()) {
14ed36
+    delete ret_val;
14ed36
+    return NULL;
14ed36
+  }
14ed36
+  return ret_val;
14ed36
+}
14ed36
+
14ed36
+bool ConditionVariableNativeWin::Init() {
14ed36
+  if (!library) {
14ed36
+    // Native implementation is supported on Vista+.
14ed36
+    library = LoadLibrary(TEXT("Kernel32.dll"));
14ed36
+    // TODO(henrike): this code results in an attempt to load the above dll
14ed36
+    // every time the previous attempt failed. Only try to load once.
14ed36
+    if (library) {
14ed36
+      // TODO(henrike): not thread safe as reading and writing to library is not
14ed36
+      // serialized. Fix.
14ed36
+      WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Kernel.dll");
14ed36
+
14ed36
+      PInitializeConditionVariable_ =
14ed36
+          (PInitializeConditionVariable) GetProcAddress(
14ed36
+              library, "InitializeConditionVariable");
14ed36
+      PSleepConditionVariableCS_ = (PSleepConditionVariableCS) GetProcAddress(
14ed36
+          library, "SleepConditionVariableCS");
14ed36
+      PWakeConditionVariable_ = (PWakeConditionVariable) GetProcAddress(
14ed36
+          library, "WakeConditionVariable");
14ed36
+      PWakeAllConditionVariable_ = (PWakeAllConditionVariable) GetProcAddress(
14ed36
+          library, "WakeAllConditionVariable");
14ed36
+
14ed36
+      if (PInitializeConditionVariable_ && PSleepConditionVariableCS_
14ed36
+          && PWakeConditionVariable_ && PWakeAllConditionVariable_) {
14ed36
+        WEBRTC_TRACE(
14ed36
+            kTraceStateInfo, kTraceUtility, -1,
14ed36
+            "Loaded native condition variables");
14ed36
+        win_support_condition_variables_primitive = true;
14ed36
+      }
14ed36
+    }
14ed36
+  }
14ed36
+  if (!win_support_condition_variables_primitive) {
14ed36
+    return false;
14ed36
+  }
14ed36
+  PInitializeConditionVariable_(&condition_variable_);
14ed36
+  return true;
14ed36
+}
14ed36
+
14ed36
+void ConditionVariableNativeWin::SleepCS(CriticalSectionWrapper& crit_sect) {
14ed36
+  SleepCS(crit_sect, INFINITE);
14ed36
+}
14ed36
+
14ed36
+bool ConditionVariableNativeWin::SleepCS(CriticalSectionWrapper& crit_sect,
14ed36
+                                         unsigned long max_time_in_ms) {
14ed36
+  CriticalSectionWindows* cs =
14ed36
+      static_cast<CriticalSectionWindows*>(&crit_sect);
14ed36
+  BOOL ret_val = PSleepConditionVariableCS_(&condition_variable_,
14ed36
+                                            &(cs->crit), max_time_in_ms);
14ed36
+  return ret_val != 0;
14ed36
+}
14ed36
+
14ed36
+void ConditionVariableNativeWin::Wake() {
14ed36
+  PWakeConditionVariable_(&condition_variable_);
14ed36
+}
14ed36
+
14ed36
+void ConditionVariableNativeWin::WakeAll() {
14ed36
+  PWakeAllConditionVariable_(&condition_variable_);
14ed36
+}
14ed36
+
14ed36
+}  // namespace webrtc
14ed36
diff --git a/webrtc/system_wrappers/source/condition_variable_native_win.h b/webrtc/system_wrappers/source/condition_variable_native_win.h
14ed36
new file mode 100644
14ed36
index 0000000..c22787f
14ed36
--- /dev/null
14ed36
+++ b/webrtc/system_wrappers/source/condition_variable_native_win.h
14ed36
@@ -0,0 +1,54 @@
14ed36
+/*
14ed36
+ *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
14ed36
+ *
14ed36
+ *  Use of this source code is governed by a BSD-style license
14ed36
+ *  that can be found in the LICENSE file in the root of the source
14ed36
+ *  tree. An additional intellectual property rights grant can be found
14ed36
+ *  in the file PATENTS.  All contributing project authors may
14ed36
+ *  be found in the AUTHORS file in the root of the source tree.
14ed36
+ */
14ed36
+
14ed36
+#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_NATIVE_WIN_H_
14ed36
+#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_NATIVE_WIN_H_
14ed36
+
14ed36
+#include <windows.h>
14ed36
+
14ed36
+#include "webrtc/system_wrappers/include/condition_variable_wrapper.h"
14ed36
+
14ed36
+namespace webrtc {
14ed36
+
14ed36
+#if !defined CONDITION_VARIABLE_INIT
14ed36
+typedef struct RTL_CONDITION_VARIABLE_ {
14ed36
+  void* Ptr;
14ed36
+} RTL_CONDITION_VARIABLE, *PRTL_CONDITION_VARIABLE;
14ed36
+
14ed36
+typedef RTL_CONDITION_VARIABLE CONDITION_VARIABLE, *PCONDITION_VARIABLE;
14ed36
+#endif
14ed36
+
14ed36
+typedef void (WINAPI* PInitializeConditionVariable)(PCONDITION_VARIABLE);
14ed36
+typedef BOOL (WINAPI* PSleepConditionVariableCS)(PCONDITION_VARIABLE,
14ed36
+                                                 PCRITICAL_SECTION, DWORD);
14ed36
+typedef void (WINAPI* PWakeConditionVariable)(PCONDITION_VARIABLE);
14ed36
+typedef void (WINAPI* PWakeAllConditionVariable)(PCONDITION_VARIABLE);
14ed36
+
14ed36
+class ConditionVariableNativeWin : public ConditionVariableWrapper {
14ed36
+ public:
14ed36
+  static ConditionVariableWrapper* Create();
14ed36
+  virtual ~ConditionVariableNativeWin();
14ed36
+
14ed36
+  void SleepCS(CriticalSectionWrapper& crit_sect);
14ed36
+  bool SleepCS(CriticalSectionWrapper& crit_sect, unsigned long max_time_inMS);
14ed36
+  void Wake();
14ed36
+  void WakeAll();
14ed36
+
14ed36
+ private:
14ed36
+  ConditionVariableNativeWin();
14ed36
+
14ed36
+  bool Init();
14ed36
+
14ed36
+  CONDITION_VARIABLE condition_variable_;
14ed36
+};
14ed36
+
14ed36
+}  // namespace webrtc
14ed36
+
14ed36
+#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_CONDITION_VARIABLE_NATIVE_WIN_H_
14ed36
-- 
14ed36
2.14.3
14ed36