ipc/chromium/src/base/lock_impl_win.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/lock_impl_win.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,73 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/lock_impl.h"
     1.9 +#include "base/logging.h"
    1.10 +
    1.11 +// NOTE: Although windows critical sections support recursive locks, we do not
    1.12 +// allow this, and we will commonly fire a DCHECK() if a thread attempts to
    1.13 +// acquire the lock a second time (while already holding it).
    1.14 +
    1.15 +LockImpl::LockImpl() {
    1.16 +#ifndef NDEBUG
    1.17 +  recursion_count_shadow_ = 0;
    1.18 +  recursion_used_ = false;
    1.19 +  owning_thread_id_ = 0;
    1.20 +#endif  // NDEBUG
    1.21 +  // The second parameter is the spin count, for short-held locks it avoid the
    1.22 +  // contending thread from going to sleep which helps performance greatly.
    1.23 +  ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
    1.24 +}
    1.25 +
    1.26 +LockImpl::~LockImpl() {
    1.27 +  ::DeleteCriticalSection(&os_lock_);
    1.28 +}
    1.29 +
    1.30 +bool LockImpl::Try() {
    1.31 +  if (::TryEnterCriticalSection(&os_lock_) != FALSE) {
    1.32 +#ifndef NDEBUG
    1.33 +    // ONLY access data after locking.
    1.34 +    owning_thread_id_ = PlatformThread::CurrentId();
    1.35 +    DCHECK_NE(owning_thread_id_, 0);
    1.36 +    recursion_count_shadow_++;
    1.37 +    if (2 == recursion_count_shadow_ && !recursion_used_) {
    1.38 +      recursion_used_ = true;
    1.39 +      DCHECK(false);  // Catch accidental redundant lock acquisition.
    1.40 +    }
    1.41 +#endif
    1.42 +    return true;
    1.43 +  }
    1.44 +  return false;
    1.45 +}
    1.46 +
    1.47 +void LockImpl::Lock() {
    1.48 +  ::EnterCriticalSection(&os_lock_);
    1.49 +#ifndef NDEBUG
    1.50 +  // ONLY access data after locking.
    1.51 +  owning_thread_id_ = PlatformThread::CurrentId();
    1.52 +  DCHECK_NE(owning_thread_id_, 0);
    1.53 +  recursion_count_shadow_++;
    1.54 +  if (2 == recursion_count_shadow_ && !recursion_used_) {
    1.55 +    recursion_used_ = true;
    1.56 +    DCHECK(false);  // Catch accidental redundant lock acquisition.
    1.57 +  }
    1.58 +#endif  // NDEBUG
    1.59 +}
    1.60 +
    1.61 +void LockImpl::Unlock() {
    1.62 +#ifndef NDEBUG
    1.63 +  --recursion_count_shadow_;  // ONLY access while lock is still held.
    1.64 +  DCHECK(0 <= recursion_count_shadow_);
    1.65 +  owning_thread_id_ = 0;
    1.66 +#endif  // NDEBUG
    1.67 +  ::LeaveCriticalSection(&os_lock_);
    1.68 +}
    1.69 +
    1.70 +// In non-debug builds, this method is declared as an empty inline method.
    1.71 +#ifndef NDEBUG
    1.72 +void LockImpl::AssertAcquired() const {
    1.73 +  DCHECK(recursion_count_shadow_ > 0);
    1.74 +  DCHECK_EQ(owning_thread_id_, PlatformThread::CurrentId());
    1.75 +}
    1.76 +#endif

mercurial