ipc/chromium/src/base/lock_impl.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/lock_impl.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     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 +#ifndef BASE_LOCK_IMPL_H_
     1.9 +#define BASE_LOCK_IMPL_H_
    1.10 +
    1.11 +#include "build/build_config.h"
    1.12 +
    1.13 +#if defined(OS_WIN)
    1.14 +#include <windows.h>
    1.15 +#elif defined(OS_POSIX)
    1.16 +#include <pthread.h>
    1.17 +#endif
    1.18 +
    1.19 +#include "base/basictypes.h"
    1.20 +#include "base/platform_thread.h"
    1.21 +
    1.22 +// This class implements the underlying platform-specific spin-lock mechanism
    1.23 +// used for the Lock class.  Most users should not use LockImpl directly, but
    1.24 +// should instead use Lock.
    1.25 +class LockImpl {
    1.26 + public:
    1.27 +#if defined(OS_WIN)
    1.28 +  typedef CRITICAL_SECTION OSLockType;
    1.29 +#elif defined(OS_POSIX)
    1.30 +  typedef pthread_mutex_t OSLockType;
    1.31 +#endif
    1.32 +
    1.33 +  LockImpl();
    1.34 +  ~LockImpl();
    1.35 +
    1.36 +  // If the lock is not held, take it and return true.  If the lock is already
    1.37 +  // held by something else, immediately return false.
    1.38 +  bool Try();
    1.39 +
    1.40 +  // Take the lock, blocking until it is available if necessary.
    1.41 +  void Lock();
    1.42 +
    1.43 +  // Release the lock.  This must only be called by the lock's holder: after
    1.44 +  // a successful call to Try, or a call to Lock.
    1.45 +  void Unlock();
    1.46 +
    1.47 +  // Debug-only method that will DCHECK() if the lock is not acquired by the
    1.48 +  // current thread.  In non-debug builds, no check is performed.
    1.49 +  // Because linux and mac condition variables modify the underlyning lock
    1.50 +  // through the os_lock() method, runtime assertions can not be done on those
    1.51 +  // builds.
    1.52 +#if defined(NDEBUG) || !defined(OS_WIN)
    1.53 +  void AssertAcquired() const {}
    1.54 +#else
    1.55 +  void AssertAcquired() const;
    1.56 +#endif
    1.57 +
    1.58 +  // Return the native underlying lock.  Not supported for Windows builds.
    1.59 +  // TODO(awalker): refactor lock and condition variables so that this is
    1.60 +  // unnecessary.
    1.61 +#if !defined(OS_WIN)
    1.62 +  OSLockType* os_lock() { return &os_lock_; }
    1.63 +#endif
    1.64 +
    1.65 + private:
    1.66 +  OSLockType os_lock_;
    1.67 +
    1.68 +#if !defined(NDEBUG) && defined(OS_WIN)
    1.69 +  // All private data is implicitly protected by lock_.
    1.70 +  // Be VERY careful to only access members under that lock.
    1.71 +  PlatformThreadId owning_thread_id_;
    1.72 +  int32_t recursion_count_shadow_;
    1.73 +  bool recursion_used_;      // Allow debugging to continued after a DCHECK().
    1.74 +#endif  // NDEBUG
    1.75 +
    1.76 +  DISALLOW_COPY_AND_ASSIGN(LockImpl);
    1.77 +};
    1.78 +
    1.79 +
    1.80 +#endif  // BASE_LOCK_IMPL_H_

mercurial