LCOV - code coverage report
Current view: top level - ipc/chromium/src/base - lock.h (source / functions) Hit Total Coverage
Test: output.info Lines: 12 13 92.3 %
Date: 2017-07-14 16:53:18 Functions: 7 8 87.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2011 The Chromium Authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #ifndef BASE_LOCK_H_
       6             : #define BASE_LOCK_H_
       7             : 
       8             : #include "base/basictypes.h"
       9             : #include "base/lock_impl.h"
      10             : #include "base/platform_thread.h"
      11             : #include "build/build_config.h"
      12             : 
      13             : // A convenient wrapper for an OS specific critical section.
      14             : class Lock {
      15             :  public:
      16             :    // Optimized wrapper implementation
      17         929 :   Lock() : lock_() {}
      18         786 :   ~Lock() {}
      19       18144 :   void Acquire() { lock_.Lock(); }
      20       18148 :   void Release() { lock_.Unlock(); }
      21             : 
      22             :   // If the lock is not held, take it and return true. If the lock is already
      23             :   // held by another thread, immediately return false. This must not be called
      24             :   // by a thread already holding the lock (what happens is undefined and an
      25             :   // assertion may fail).
      26           0 :   bool Try() { return lock_.Try(); }
      27             : 
      28             :   // Null implementation if not debug.
      29       15008 :   void AssertAcquired() const {}
      30             : 
      31             :   // Whether Lock mitigates priority inversion when used from different thread
      32             :   // priorities.
      33             :   static bool HandlesMultipleThreadPriorities() {
      34             : #if defined(OS_POSIX)
      35             :     // POSIX mitigates priority inversion by setting the priority of a thread
      36             :     // holding a Lock to the maximum priority of any other thread waiting on it.
      37             :     return base::internal::LockImpl::PriorityInheritanceAvailable();
      38             : #elif defined(OS_WIN)
      39             :     // Windows mitigates priority inversion by randomly boosting the priority of
      40             :     // ready threads.
      41             :     // https://msdn.microsoft.com/library/windows/desktop/ms684831.aspx
      42             :     return true;
      43             : #else
      44             : #error Unsupported platform
      45             : #endif
      46             :   }
      47             : 
      48             : #if defined(OS_POSIX) || defined(OS_WIN)
      49             :   // Both Windows and POSIX implementations of ConditionVariable need to be
      50             :   // able to see our lock and tweak our debugging counters, as they release and
      51             :   // acquire locks inside of their condition variable APIs.
      52             :   friend class ConditionVariable;
      53             : #endif
      54             : 
      55             :  private:
      56             :   // Platform specific underlying lock implementation.
      57             :   ::base::internal::LockImpl lock_;
      58             : 
      59             :   DISALLOW_COPY_AND_ASSIGN(Lock);
      60             : };
      61             : 
      62             : // A helper class that acquires the given Lock while the AutoLock is in scope.
      63             : class AutoLock {
      64             :  public:
      65             :   struct AlreadyAcquired {};
      66             : 
      67       15003 :   explicit AutoLock(Lock& lock) : lock_(lock) {
      68       15003 :     lock_.Acquire();
      69       15008 :   }
      70             : 
      71             :   AutoLock(Lock& lock, const AlreadyAcquired&) : lock_(lock) {
      72             :     lock_.AssertAcquired();
      73             :   }
      74             : 
      75       30017 :   ~AutoLock() {
      76       15008 :     lock_.AssertAcquired();
      77       15007 :     lock_.Release();
      78       15009 :   }
      79             : 
      80             :  private:
      81             :   Lock& lock_;
      82             :   DISALLOW_COPY_AND_ASSIGN(AutoLock);
      83             : };
      84             : 
      85             : // AutoUnlock is a helper that will Release() the |lock| argument in the
      86             : // constructor, and re-Acquire() it in the destructor.
      87             : class AutoUnlock {
      88             :  public:
      89             :   explicit AutoUnlock(Lock& lock) : lock_(lock) {
      90             :     // We require our caller to have the lock.
      91             :     lock_.AssertAcquired();
      92             :     lock_.Release();
      93             :   }
      94             : 
      95             :   ~AutoUnlock() {
      96             :     lock_.Acquire();
      97             :   }
      98             : 
      99             :  private:
     100             :   Lock& lock_;
     101             :   DISALLOW_COPY_AND_ASSIGN(AutoUnlock);
     102             : };
     103             : 
     104             : #endif  // BASE_LOCK_H_

Generated by: LCOV version 1.13