forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move base/lock and base/condition_variable to base/synchronization/
I kept a base/lock.h in place with a using statement to avoid updating all callers in one CL. TEST=it compiles BUG=none Review URL: http://codereview.chromium.org/6018013 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70363 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
brettw@chromium.org
committed
Jan 1, 2011
1 parent
10f33b1
commit bc581a6
Showing
48 changed files
with
427 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,18 @@ | ||
// Copyright (c) 2010 The Chromium Authors. All rights reserved. | ||
// Copyright (c) 2011 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef BASE_LOCK_H_ | ||
#define BASE_LOCK_H_ | ||
#pragma once | ||
|
||
#include "base/lock_impl.h" | ||
#include "base/threading/platform_thread.h" | ||
// This is a temporary forwarding file so not every user of lock needs to | ||
// be updated at once. | ||
// TODO(brettw) remove this and fix everybody up to using the new location. | ||
#include "base/synchronization/lock.h" | ||
|
||
// A convenient wrapper for an OS specific critical section. The only real | ||
// intelligence in this class is in debug mode for the support for the | ||
// AssertAcquired() method. | ||
|
||
class Lock { | ||
public: | ||
#if defined(NDEBUG) // Optimized wrapper implementation | ||
Lock() : lock_() {} | ||
~Lock() {} | ||
void Acquire() { lock_.Lock(); } | ||
void Release() { lock_.Unlock(); } | ||
|
||
// If the lock is not held, take it and return true. If the lock is already | ||
// held by another thread, immediately return false. This must not be called | ||
// by a thread already holding the lock (what happens is undefined and an | ||
// assertion may fail). | ||
bool Try() { return lock_.Try(); } | ||
|
||
// Null implementation if not debug. | ||
void AssertAcquired() const {} | ||
#else | ||
Lock(); | ||
~Lock() {} | ||
|
||
// NOTE: Although windows critical sections support recursive locks, we do not | ||
// allow this, and we will commonly fire a DCHECK() if a thread attempts to | ||
// acquire the lock a second time (while already holding it). | ||
void Acquire() { | ||
lock_.Lock(); | ||
CheckUnheldAndMark(); | ||
} | ||
void Release() { | ||
CheckHeldAndUnmark(); | ||
lock_.Unlock(); | ||
} | ||
|
||
bool Try() { | ||
bool rv = lock_.Try(); | ||
if (rv) { | ||
CheckUnheldAndMark(); | ||
} | ||
return rv; | ||
} | ||
|
||
void AssertAcquired() const; | ||
#endif // NDEBUG | ||
|
||
#if defined(OS_POSIX) | ||
// The posix implementation of ConditionVariable needs to be able | ||
// to see our lock and tweak our debugging counters, as it releases | ||
// and acquires locks inside of pthread_cond_{timed,}wait. | ||
// Windows doesn't need to do this as it calls the Lock::* methods. | ||
friend class ConditionVariable; | ||
#endif | ||
|
||
private: | ||
#if !defined(NDEBUG) | ||
// Members and routines taking care of locks assertions. | ||
// Note that this checks for recursive locks and allows them | ||
// if the variable is set. This is allowed by the underlying implementation | ||
// on windows but not on Posix, so we're doing unneeded checks on Posix. | ||
// It's worth it to share the code. | ||
void CheckHeldAndUnmark(); | ||
void CheckUnheldAndMark(); | ||
|
||
// All private data is implicitly protected by lock_. | ||
// Be VERY careful to only access members under that lock. | ||
|
||
// Determines validity of owning_thread_id_. Needed as we don't have | ||
// a null owning_thread_id_ value. | ||
bool owned_by_thread_; | ||
base::PlatformThreadId owning_thread_id_; | ||
#endif // NDEBUG | ||
|
||
LockImpl lock_; // Platform specific underlying lock implementation. | ||
|
||
DISALLOW_COPY_AND_ASSIGN(Lock); | ||
}; | ||
|
||
// A helper class that acquires the given Lock while the AutoLock is in scope. | ||
class AutoLock { | ||
public: | ||
explicit AutoLock(Lock& lock) : lock_(lock) { | ||
lock_.Acquire(); | ||
} | ||
|
||
~AutoLock() { | ||
lock_.AssertAcquired(); | ||
lock_.Release(); | ||
} | ||
|
||
private: | ||
Lock& lock_; | ||
DISALLOW_COPY_AND_ASSIGN(AutoLock); | ||
}; | ||
|
||
// AutoUnlock is a helper that will Release() the |lock| argument in the | ||
// constructor, and re-Acquire() it in the destructor. | ||
class AutoUnlock { | ||
public: | ||
explicit AutoUnlock(Lock& lock) : lock_(lock) { | ||
// We require our caller to have the lock. | ||
lock_.AssertAcquired(); | ||
lock_.Release(); | ||
} | ||
|
||
~AutoUnlock() { | ||
lock_.Acquire(); | ||
} | ||
|
||
private: | ||
Lock& lock_; | ||
DISALLOW_COPY_AND_ASSIGN(AutoUnlock); | ||
}; | ||
using base::AutoLock; | ||
using base::AutoUnlock; | ||
using base::Lock; | ||
|
||
#endif // BASE_LOCK_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.