Base RWLockWrapper on rtc::SharedExclusiveLock.

Also moves rtc::Event and rtc::SharedExclusiveLock to rtc_base_approved.

R=tommi@webrtc.org
BUG=

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

Cr-Commit-Position: refs/heads/master@{#8260}
git-svn-id: http://webrtc.googlecode.com/svn/trunk@8260 4adac7df-926f-26a2-2b94-8c16560cd09d
diff --git a/webrtc/base/BUILD.gn b/webrtc/base/BUILD.gn
index e758d86..d62993a 100644
--- a/webrtc/base/BUILD.gn
+++ b/webrtc/base/BUILD.gn
@@ -106,6 +106,8 @@
   sources = [
     "checks.cc",
     "checks.h",
+    "event.cc",
+    "event.h",
     "exp_filter.cc",
     "exp_filter.h",
     "md5.cc",
@@ -115,6 +117,8 @@
     "platform_file.h",
     "safe_conversions.h",
     "safe_conversions_impl.h",
+    "sharedexclusivelock.cc",
+    "sharedexclusivelock.h",
     "stringencode.cc",
     "stringencode.h",
     "stringutils.cc",
@@ -183,8 +187,6 @@
     "cryptstring.h",
     "diskcache.cc",
     "diskcache.h",
-    "event.cc",
-    "event.h",
     "fileutils.cc",
     "fileutils.h",
     "firewallsocketserver.cc",
@@ -360,8 +362,6 @@
       "scopedptrcollection.h",
       "scoped_ref_ptr.h",
       "sec_buffer.h",
-      "sharedexclusivelock.cc",
-      "sharedexclusivelock.h",
       "sslconfig.h",
       "sslroots.h",
       "stringdigest.h",
diff --git a/webrtc/base/base.gyp b/webrtc/base/base.gyp
index 74ba76a..0d1cb05 100644
--- a/webrtc/base/base.gyp
+++ b/webrtc/base/base.gyp
@@ -31,6 +31,8 @@
       'sources': [
         'checks.cc',
         'checks.h',
+        'event.cc',
+        'event.h',
         'exp_filter.cc',
         'exp_filter.h',
         'md5.cc',
@@ -40,6 +42,8 @@
         'platform_file.h',
         'safe_conversions.h',
         'safe_conversions_impl.h',
+        'sharedexclusivelock.cc',
+        'sharedexclusivelock.h',
         'stringencode.cc',
         'stringencode.h',
         'stringutils.cc',
@@ -113,8 +117,6 @@
         'diskcache.h',
         'diskcache_win32.cc',
         'diskcache_win32.h',
-        'event.cc',
-        'event.h',
         'filelock.cc',
         'filelock.h',
         'fileutils.cc',
@@ -227,8 +229,6 @@
         'sha1.cc',
         'sha1.h',
         'sha1digest.h',
-        'sharedexclusivelock.cc',
-        'sharedexclusivelock.h',
         'signalthread.cc',
         'signalthread.h',
         'sigslot.h',
diff --git a/webrtc/base/event.cc b/webrtc/base/event.cc
index 393142e..606d6ad 100644
--- a/webrtc/base/event.cc
+++ b/webrtc/base/event.cc
@@ -10,6 +10,8 @@
 
 #include "webrtc/base/event.h"
 
+#include "webrtc/base/checks.h"
+
 #if defined(WEBRTC_WIN)
 #include <windows.h>
 #elif defined(WEBRTC_POSIX)
@@ -31,7 +33,7 @@
                                 is_manual_reset_,
                                 is_initially_signaled_,
                                 NULL);                // Name.
-  ASSERT(event_handle_ != NULL);
+  CHECK(event_handle_ != NULL);
 }
 
 Event::~Event() {
@@ -56,8 +58,8 @@
 Event::Event(bool manual_reset, bool initially_signaled)
     : is_manual_reset_(manual_reset),
       event_status_(initially_signaled) {
-  VERIFY(pthread_mutex_init(&event_mutex_, NULL) == 0);
-  VERIFY(pthread_cond_init(&event_cond_, NULL) == 0);
+  CHECK(pthread_mutex_init(&event_mutex_, NULL) == 0);
+  CHECK(pthread_cond_init(&event_cond_, NULL) == 0);
 }
 
 Event::~Event() {
diff --git a/webrtc/base/event.h b/webrtc/base/event.h
index f2691a2..7a6209e 100644
--- a/webrtc/base/event.h
+++ b/webrtc/base/event.h
@@ -20,7 +20,6 @@
 #endif
 
 #include "webrtc/base/basictypes.h"
-#include "webrtc/base/common.h"
 
 namespace rtc {
 
diff --git a/webrtc/system_wrappers/BUILD.gn b/webrtc/system_wrappers/BUILD.gn
index c4f1da5..d87ae0a 100644
--- a/webrtc/system_wrappers/BUILD.gn
+++ b/webrtc/system_wrappers/BUILD.gn
@@ -76,12 +76,6 @@
     "source/logging.cc",
     "source/rtp_to_ntp.cc",
     "source/rw_lock.cc",
-    "source/rw_lock_generic.cc",
-    "source/rw_lock_generic.h",
-    "source/rw_lock_posix.cc",
-    "source/rw_lock_posix.h",
-    "source/rw_lock_win.cc",
-    "source/rw_lock_win.h",
     "source/set_thread_name_win.h",
     "source/sleep.cc",
     "source/sort.cc",
diff --git a/webrtc/system_wrappers/interface/rw_lock_wrapper.h b/webrtc/system_wrappers/interface/rw_lock_wrapper.h
index dbe6d6c..94c2c97 100644
--- a/webrtc/system_wrappers/interface/rw_lock_wrapper.h
+++ b/webrtc/system_wrappers/interface/rw_lock_wrapper.h
@@ -13,10 +13,6 @@
 
 #include "webrtc/base/thread_annotations.h"
 
-// Note, Windows pre-Vista version of RW locks are not supported natively. For
-// these OSs regular critical sections have been used to approximate RW lock
-// functionality and will therefore have worse performance.
-
 namespace webrtc {
 
 class LOCKABLE RWLockWrapper {
@@ -31,8 +27,6 @@
   virtual void ReleaseLockShared() UNLOCK_FUNCTION() = 0;
 };
 
-// RAII extensions of the RW lock. Prevents Acquire/Release missmatches and
-// provides more compact locking syntax.
 class SCOPED_LOCKABLE ReadLockScoped {
  public:
   ReadLockScoped(RWLockWrapper& rw_lock) SHARED_LOCK_FUNCTION(rw_lock)
diff --git a/webrtc/system_wrappers/source/rw_lock.cc b/webrtc/system_wrappers/source/rw_lock.cc
index 8b76eb8..ae2a6b7 100644
--- a/webrtc/system_wrappers/source/rw_lock.cc
+++ b/webrtc/system_wrappers/source/rw_lock.cc
@@ -10,28 +10,32 @@
 
 #include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
 
-#include <assert.h>
-
-#if defined(_WIN32)
-#include "webrtc/system_wrappers/source/rw_lock_generic.h"
-#include "webrtc/system_wrappers/source/rw_lock_win.h"
-#else
-#include "webrtc/system_wrappers/source/rw_lock_posix.h"
-#endif
+#include "webrtc/base/sharedexclusivelock.h"
+#include "webrtc/base/thread_annotations.h"
 
 namespace webrtc {
 
-RWLockWrapper* RWLockWrapper::CreateRWLock() {
-#ifdef _WIN32
-  // Native implementation is faster, so use that if available.
-  RWLockWrapper* lock = RWLockWin::Create();
-  if (lock) {
-    return lock;
+class LOCKABLE RwLock : public RWLockWrapper {
+  virtual void AcquireLockExclusive() override EXCLUSIVE_LOCK_FUNCTION() {
+    lock_.LockExclusive();
   }
-  return new RWLockGeneric();
-#else
-  return RWLockPosix::Create();
-#endif
+  virtual void ReleaseLockExclusive() override UNLOCK_FUNCTION() {
+    lock_.UnlockExclusive();
+  }
+
+  virtual void AcquireLockShared() override SHARED_LOCK_FUNCTION() {
+    lock_.LockShared();
+  }
+  virtual void ReleaseLockShared() override UNLOCK_FUNCTION() {
+    lock_.UnlockShared();
+  }
+
+ private:
+  rtc::SharedExclusiveLock lock_;
+};
+
+RWLockWrapper* RWLockWrapper::CreateRWLock() {
+  return new RwLock();
 }
 
 }  // namespace webrtc
diff --git a/webrtc/system_wrappers/source/rw_lock_generic.cc b/webrtc/system_wrappers/source/rw_lock_generic.cc
deleted file mode 100644
index 0ca9518..0000000
--- a/webrtc/system_wrappers/source/rw_lock_generic.cc
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- *  Copyright (c) 2011 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/source/rw_lock_generic.h"
-
-#include "webrtc/system_wrappers/interface/condition_variable_wrapper.h"
-#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
-
-namespace webrtc {
-
-RWLockGeneric::RWLockGeneric()
-    : readers_active_(0),
-      writer_active_(false),
-      readers_waiting_(0),
-      writers_waiting_(0) {
-  critical_section_ = CriticalSectionWrapper::CreateCriticalSection();
-  read_condition_ = ConditionVariableWrapper::CreateConditionVariable();
-  write_condition_ = ConditionVariableWrapper::CreateConditionVariable();
-}
-
-RWLockGeneric::~RWLockGeneric() {
-  delete write_condition_;
-  delete read_condition_;
-  delete critical_section_;
-}
-
-void RWLockGeneric::AcquireLockExclusive() {
-  CriticalSectionScoped cs(critical_section_);
-  if (writer_active_ || readers_active_ > 0) {
-    ++writers_waiting_;
-    while (writer_active_ || readers_active_ > 0) {
-      write_condition_->SleepCS(*critical_section_);
-    }
-    --writers_waiting_;
-  }
-  writer_active_ = true;
-}
-
-void RWLockGeneric::ReleaseLockExclusive() {
-  CriticalSectionScoped cs(critical_section_);
-  writer_active_ = false;
-  if (writers_waiting_ > 0) {
-    write_condition_->Wake();
-  } else if (readers_waiting_ > 0) {
-    read_condition_->WakeAll();
-  }
-}
-
-void RWLockGeneric::AcquireLockShared() {
-  CriticalSectionScoped cs(critical_section_);
-  if (writer_active_ || writers_waiting_ > 0) {
-    ++readers_waiting_;
-
-    while (writer_active_ || writers_waiting_ > 0) {
-      read_condition_->SleepCS(*critical_section_);
-    }
-    --readers_waiting_;
-  }
-  ++readers_active_;
-}
-
-void RWLockGeneric::ReleaseLockShared() {
-  CriticalSectionScoped cs(critical_section_);
-  --readers_active_;
-  if (readers_active_ == 0 && writers_waiting_ > 0) {
-    write_condition_->Wake();
-  }
-}
-
-}  // namespace webrtc
diff --git a/webrtc/system_wrappers/source/rw_lock_generic.h b/webrtc/system_wrappers/source/rw_lock_generic.h
deleted file mode 100644
index dd69f52..0000000
--- a/webrtc/system_wrappers/source/rw_lock_generic.h
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- *  Copyright (c) 2011 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.
- */
-
-#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_GENERIC_H_
-#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_GENERIC_H_
-
-#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
-#include "webrtc/typedefs.h"
-
-namespace webrtc {
-
-class CriticalSectionWrapper;
-class ConditionVariableWrapper;
-
-class RWLockGeneric : public RWLockWrapper {
- public:
-  RWLockGeneric();
-  virtual ~RWLockGeneric();
-
-  virtual void AcquireLockExclusive() OVERRIDE;
-  virtual void ReleaseLockExclusive() OVERRIDE;
-
-  virtual void AcquireLockShared() OVERRIDE;
-  virtual void ReleaseLockShared() OVERRIDE;
-
- private:
-  CriticalSectionWrapper* critical_section_;
-  ConditionVariableWrapper* read_condition_;
-  ConditionVariableWrapper* write_condition_;
-
-  int readers_active_;
-  bool writer_active_;
-  int readers_waiting_;
-  int writers_waiting_;
-};
-
-}  // namespace webrtc
-
-#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_GENERIC_H_
diff --git a/webrtc/system_wrappers/source/rw_lock_posix.cc b/webrtc/system_wrappers/source/rw_lock_posix.cc
deleted file mode 100644
index cdcb7fb..0000000
--- a/webrtc/system_wrappers/source/rw_lock_posix.cc
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- *  Copyright (c) 2011 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/source/rw_lock_posix.h"
-
-namespace webrtc {
-
-RWLockPosix::RWLockPosix() : lock_() {
-}
-
-RWLockPosix::~RWLockPosix() {
-  pthread_rwlock_destroy(&lock_);
-}
-
-RWLockPosix* RWLockPosix::Create() {
-  RWLockPosix* ret_val = new RWLockPosix();
-  if (!ret_val->Init()) {
-    delete ret_val;
-    return NULL;
-  }
-  return ret_val;
-}
-
-bool RWLockPosix::Init() {
-  return pthread_rwlock_init(&lock_, 0) == 0;
-}
-
-void RWLockPosix::AcquireLockExclusive() {
-  pthread_rwlock_wrlock(&lock_);
-}
-
-void RWLockPosix::ReleaseLockExclusive() {
-  pthread_rwlock_unlock(&lock_);
-}
-
-void RWLockPosix::AcquireLockShared() {
-  pthread_rwlock_rdlock(&lock_);
-}
-
-void RWLockPosix::ReleaseLockShared() {
-  pthread_rwlock_unlock(&lock_);
-}
-
-}  // namespace webrtc
diff --git a/webrtc/system_wrappers/source/rw_lock_posix.h b/webrtc/system_wrappers/source/rw_lock_posix.h
deleted file mode 100644
index 6ce2b2d..0000000
--- a/webrtc/system_wrappers/source/rw_lock_posix.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- *  Copyright (c) 2011 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.
- */
-
-#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_POSIX_H_
-#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_POSIX_H_
-
-#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
-#include "webrtc/typedefs.h"
-
-#include <pthread.h>
-
-namespace webrtc {
-
-class RWLockPosix : public RWLockWrapper {
- public:
-  static RWLockPosix* Create();
-  virtual ~RWLockPosix();
-
-  virtual void AcquireLockExclusive() OVERRIDE;
-  virtual void ReleaseLockExclusive() OVERRIDE;
-
-  virtual void AcquireLockShared() OVERRIDE;
-  virtual void ReleaseLockShared() OVERRIDE;
-
- private:
-  RWLockPosix();
-  bool Init();
-
-  pthread_rwlock_t lock_;
-};
-
-}  // namespace webrtc
-
-#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_POSIX_H_
diff --git a/webrtc/system_wrappers/source/rw_lock_win.cc b/webrtc/system_wrappers/source/rw_lock_win.cc
deleted file mode 100644
index aea74fa..0000000
--- a/webrtc/system_wrappers/source/rw_lock_win.cc
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- *  Copyright (c) 2012 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/source/rw_lock_win.h"
-
-#include "webrtc/system_wrappers/interface/trace.h"
-
-namespace webrtc {
-
-static bool native_rw_locks_supported = false;
-static bool module_load_attempted = false;
-static HMODULE library = NULL;
-
-typedef void (WINAPI* InitializeSRWLock)(PSRWLOCK);
-
-typedef void (WINAPI* AcquireSRWLockExclusive)(PSRWLOCK);
-typedef void (WINAPI* ReleaseSRWLockExclusive)(PSRWLOCK);
-
-typedef void (WINAPI* AcquireSRWLockShared)(PSRWLOCK);
-typedef void (WINAPI* ReleaseSRWLockShared)(PSRWLOCK);
-
-InitializeSRWLock       initialize_srw_lock;
-AcquireSRWLockExclusive acquire_srw_lock_exclusive;
-AcquireSRWLockShared    acquire_srw_lock_shared;
-ReleaseSRWLockShared    release_srw_lock_shared;
-ReleaseSRWLockExclusive release_srw_lock_exclusive;
-
-RWLockWin::RWLockWin() {
-  initialize_srw_lock(&lock_);
-}
-
-RWLockWin* RWLockWin::Create() {
-  if (!LoadModule()) {
-    return NULL;
-  }
-  return new RWLockWin();
-}
-
-void RWLockWin::AcquireLockExclusive() {
-  acquire_srw_lock_exclusive(&lock_);
-}
-
-void RWLockWin::ReleaseLockExclusive() {
-  release_srw_lock_exclusive(&lock_);
-}
-
-void RWLockWin::AcquireLockShared() {
-  acquire_srw_lock_shared(&lock_);
-}
-
-void RWLockWin::ReleaseLockShared() {
-  release_srw_lock_shared(&lock_);
-}
-
-bool RWLockWin::LoadModule() {
-  if (module_load_attempted) {
-    return native_rw_locks_supported;
-  }
-  module_load_attempted = true;
-  // Use native implementation if supported (i.e Vista+)
-  library = LoadLibrary(TEXT("Kernel32.dll"));
-  if (!library) {
-    return false;
-  }
-  WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Kernel.dll");
-
-  initialize_srw_lock =
-    (InitializeSRWLock)GetProcAddress(library, "InitializeSRWLock");
-
-  acquire_srw_lock_exclusive =
-    (AcquireSRWLockExclusive)GetProcAddress(library,
-                                            "AcquireSRWLockExclusive");
-  release_srw_lock_exclusive =
-    (ReleaseSRWLockExclusive)GetProcAddress(library,
-                                            "ReleaseSRWLockExclusive");
-  acquire_srw_lock_shared =
-    (AcquireSRWLockShared)GetProcAddress(library, "AcquireSRWLockShared");
-  release_srw_lock_shared =
-    (ReleaseSRWLockShared)GetProcAddress(library, "ReleaseSRWLockShared");
-
-  if (initialize_srw_lock && acquire_srw_lock_exclusive &&
-      release_srw_lock_exclusive && acquire_srw_lock_shared &&
-      release_srw_lock_shared) {
-    WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, "Loaded Native RW Lock");
-    native_rw_locks_supported = true;
-  }
-  return native_rw_locks_supported;
-}
-
-}  // namespace webrtc
diff --git a/webrtc/system_wrappers/source/rw_lock_win.h b/webrtc/system_wrappers/source/rw_lock_win.h
deleted file mode 100644
index 6f7cd33..0000000
--- a/webrtc/system_wrappers/source/rw_lock_win.h
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- *  Copyright (c) 2012 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.
- */
-
-#ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WIN_H_
-#define WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WIN_H_
-
-#include "webrtc/system_wrappers/interface/rw_lock_wrapper.h"
-
-#include <Windows.h>
-
-namespace webrtc {
-
-class RWLockWin : public RWLockWrapper {
- public:
-  static RWLockWin* Create();
-  ~RWLockWin() {}
-
-  virtual void AcquireLockExclusive();
-  virtual void ReleaseLockExclusive();
-
-  virtual void AcquireLockShared();
-  virtual void ReleaseLockShared();
-
- private:
-  RWLockWin();
-  static bool LoadModule();
-
-  SRWLOCK lock_;
-};
-
-}  // namespace webrtc
-
-#endif  // WEBRTC_SYSTEM_WRAPPERS_SOURCE_RW_LOCK_WIN_H_
diff --git a/webrtc/system_wrappers/system_wrappers.gyp b/webrtc/system_wrappers/system_wrappers.gyp
index 3af8897..3af2e02 100644
--- a/webrtc/system_wrappers/system_wrappers.gyp
+++ b/webrtc/system_wrappers/system_wrappers.gyp
@@ -86,12 +86,6 @@
         'source/logging.cc',
         'source/rtp_to_ntp.cc',
         'source/rw_lock.cc',
-        'source/rw_lock_generic.cc',
-        'source/rw_lock_generic.h',
-        'source/rw_lock_posix.cc',
-        'source/rw_lock_posix.h',
-        'source/rw_lock_win.cc',
-        'source/rw_lock_win.h',
         'source/set_thread_name_win.h',
         'source/sleep.cc',
         'source/sort.cc',