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

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