ipc/chromium/src/base/lock_impl_win.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #include "base/lock_impl.h"
michael@0 6 #include "base/logging.h"
michael@0 7
michael@0 8 // NOTE: Although windows critical sections support recursive locks, we do not
michael@0 9 // allow this, and we will commonly fire a DCHECK() if a thread attempts to
michael@0 10 // acquire the lock a second time (while already holding it).
michael@0 11
michael@0 12 LockImpl::LockImpl() {
michael@0 13 #ifndef NDEBUG
michael@0 14 recursion_count_shadow_ = 0;
michael@0 15 recursion_used_ = false;
michael@0 16 owning_thread_id_ = 0;
michael@0 17 #endif // NDEBUG
michael@0 18 // The second parameter is the spin count, for short-held locks it avoid the
michael@0 19 // contending thread from going to sleep which helps performance greatly.
michael@0 20 ::InitializeCriticalSectionAndSpinCount(&os_lock_, 2000);
michael@0 21 }
michael@0 22
michael@0 23 LockImpl::~LockImpl() {
michael@0 24 ::DeleteCriticalSection(&os_lock_);
michael@0 25 }
michael@0 26
michael@0 27 bool LockImpl::Try() {
michael@0 28 if (::TryEnterCriticalSection(&os_lock_) != FALSE) {
michael@0 29 #ifndef NDEBUG
michael@0 30 // ONLY access data after locking.
michael@0 31 owning_thread_id_ = PlatformThread::CurrentId();
michael@0 32 DCHECK_NE(owning_thread_id_, 0);
michael@0 33 recursion_count_shadow_++;
michael@0 34 if (2 == recursion_count_shadow_ && !recursion_used_) {
michael@0 35 recursion_used_ = true;
michael@0 36 DCHECK(false); // Catch accidental redundant lock acquisition.
michael@0 37 }
michael@0 38 #endif
michael@0 39 return true;
michael@0 40 }
michael@0 41 return false;
michael@0 42 }
michael@0 43
michael@0 44 void LockImpl::Lock() {
michael@0 45 ::EnterCriticalSection(&os_lock_);
michael@0 46 #ifndef NDEBUG
michael@0 47 // ONLY access data after locking.
michael@0 48 owning_thread_id_ = PlatformThread::CurrentId();
michael@0 49 DCHECK_NE(owning_thread_id_, 0);
michael@0 50 recursion_count_shadow_++;
michael@0 51 if (2 == recursion_count_shadow_ && !recursion_used_) {
michael@0 52 recursion_used_ = true;
michael@0 53 DCHECK(false); // Catch accidental redundant lock acquisition.
michael@0 54 }
michael@0 55 #endif // NDEBUG
michael@0 56 }
michael@0 57
michael@0 58 void LockImpl::Unlock() {
michael@0 59 #ifndef NDEBUG
michael@0 60 --recursion_count_shadow_; // ONLY access while lock is still held.
michael@0 61 DCHECK(0 <= recursion_count_shadow_);
michael@0 62 owning_thread_id_ = 0;
michael@0 63 #endif // NDEBUG
michael@0 64 ::LeaveCriticalSection(&os_lock_);
michael@0 65 }
michael@0 66
michael@0 67 // In non-debug builds, this method is declared as an empty inline method.
michael@0 68 #ifndef NDEBUG
michael@0 69 void LockImpl::AssertAcquired() const {
michael@0 70 DCHECK(recursion_count_shadow_ > 0);
michael@0 71 DCHECK_EQ(owning_thread_id_, PlatformThread::CurrentId());
michael@0 72 }
michael@0 73 #endif

mercurial