ipc/chromium/src/base/lock_impl.h

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 #ifndef BASE_LOCK_IMPL_H_
michael@0 6 #define BASE_LOCK_IMPL_H_
michael@0 7
michael@0 8 #include "build/build_config.h"
michael@0 9
michael@0 10 #if defined(OS_WIN)
michael@0 11 #include <windows.h>
michael@0 12 #elif defined(OS_POSIX)
michael@0 13 #include <pthread.h>
michael@0 14 #endif
michael@0 15
michael@0 16 #include "base/basictypes.h"
michael@0 17 #include "base/platform_thread.h"
michael@0 18
michael@0 19 // This class implements the underlying platform-specific spin-lock mechanism
michael@0 20 // used for the Lock class. Most users should not use LockImpl directly, but
michael@0 21 // should instead use Lock.
michael@0 22 class LockImpl {
michael@0 23 public:
michael@0 24 #if defined(OS_WIN)
michael@0 25 typedef CRITICAL_SECTION OSLockType;
michael@0 26 #elif defined(OS_POSIX)
michael@0 27 typedef pthread_mutex_t OSLockType;
michael@0 28 #endif
michael@0 29
michael@0 30 LockImpl();
michael@0 31 ~LockImpl();
michael@0 32
michael@0 33 // If the lock is not held, take it and return true. If the lock is already
michael@0 34 // held by something else, immediately return false.
michael@0 35 bool Try();
michael@0 36
michael@0 37 // Take the lock, blocking until it is available if necessary.
michael@0 38 void Lock();
michael@0 39
michael@0 40 // Release the lock. This must only be called by the lock's holder: after
michael@0 41 // a successful call to Try, or a call to Lock.
michael@0 42 void Unlock();
michael@0 43
michael@0 44 // Debug-only method that will DCHECK() if the lock is not acquired by the
michael@0 45 // current thread. In non-debug builds, no check is performed.
michael@0 46 // Because linux and mac condition variables modify the underlyning lock
michael@0 47 // through the os_lock() method, runtime assertions can not be done on those
michael@0 48 // builds.
michael@0 49 #if defined(NDEBUG) || !defined(OS_WIN)
michael@0 50 void AssertAcquired() const {}
michael@0 51 #else
michael@0 52 void AssertAcquired() const;
michael@0 53 #endif
michael@0 54
michael@0 55 // Return the native underlying lock. Not supported for Windows builds.
michael@0 56 // TODO(awalker): refactor lock and condition variables so that this is
michael@0 57 // unnecessary.
michael@0 58 #if !defined(OS_WIN)
michael@0 59 OSLockType* os_lock() { return &os_lock_; }
michael@0 60 #endif
michael@0 61
michael@0 62 private:
michael@0 63 OSLockType os_lock_;
michael@0 64
michael@0 65 #if !defined(NDEBUG) && defined(OS_WIN)
michael@0 66 // All private data is implicitly protected by lock_.
michael@0 67 // Be VERY careful to only access members under that lock.
michael@0 68 PlatformThreadId owning_thread_id_;
michael@0 69 int32_t recursion_count_shadow_;
michael@0 70 bool recursion_used_; // Allow debugging to continued after a DCHECK().
michael@0 71 #endif // NDEBUG
michael@0 72
michael@0 73 DISALLOW_COPY_AND_ASSIGN(LockImpl);
michael@0 74 };
michael@0 75
michael@0 76
michael@0 77 #endif // BASE_LOCK_IMPL_H_

mercurial