Skip to content

Commit

Permalink
Move base/lock and base/condition_variable to base/synchronization/
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 48 changed files with 427 additions and 377 deletions.
6 changes: 4 additions & 2 deletions app/resource_bundle.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
namespace app {
class DataPack;
}
namespace base {
class Lock;
}
#if defined(USE_X11)
typedef struct _GdkPixbuf GdkPixbuf;
#endif
namespace gfx {
class Font;
}
class Lock;
class SkBitmap;
typedef uint32 SkColor;
namespace base {
Expand Down Expand Up @@ -244,7 +246,7 @@ class ResourceBundle {

// Class level lock. Used to protect internal data structures that may be
// accessed from other threads (e.g., skia_images_).
scoped_ptr<Lock> lock_;
scoped_ptr<base::Lock> lock_;

// Handles for data sources.
DataHandle resources_data_;
Expand Down
6 changes: 3 additions & 3 deletions base/base.gyp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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.

Expand Down Expand Up @@ -67,7 +67,6 @@
'callback_unittest.cc',
'cancellation_flag_unittest.cc',
'command_line_unittest.cc',
'condition_variable_unittest.cc',
'crypto/encryptor_unittest.cc',
'crypto/rsa_private_key_unittest.cc',
'crypto/rsa_private_key_nss_unittest.cc',
Expand Down Expand Up @@ -97,7 +96,6 @@
'lazy_instance_unittest.cc',
'linked_list_unittest.cc',
'linked_ptr_unittest.cc',
'lock_unittest.cc',
'logging_unittest.cc',
'mac/mac_util_unittest.mm',
'message_loop_proxy_impl_unittest.cc',
Expand Down Expand Up @@ -132,6 +130,8 @@
'string_util_unittest.cc',
'stringize_macros_unittest.cc',
'stringprintf_unittest.cc',
'synchronization/condition_variable_unittest.cc',
'synchronization/lock_unittest.cc',
'sys_info_unittest.cc',
'sys_string_conversions_mac_unittest.mm',
'sys_string_conversions_unittest.cc',
Expand Down
17 changes: 9 additions & 8 deletions base/base.gypi
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 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.

Expand Down Expand Up @@ -44,9 +44,6 @@
'command_line.cc',
'command_line.h',
'compiler_specific.h',
'condition_variable.h',
'condition_variable_posix.cc',
'condition_variable_win.cc',
'cpu.cc',
'cpu.h',
'debug/debug_on_start_win.cc',
Expand Down Expand Up @@ -103,11 +100,7 @@
'lazy_instance.h',
'linked_list.h',
'linked_ptr.h',
'lock.cc',
'lock.h',
'lock_impl.h',
'lock_impl_posix.cc',
'lock_impl_win.cc',
'logging.cc',
'logging.h',
'logging_win.cc',
Expand Down Expand Up @@ -216,6 +209,14 @@
'stringize_macros.h',
'stringprintf.cc',
'stringprintf.h',
'synchronization/condition_variable.h',
'synchronization/condition_variable_posix.cc',
'synchronization/condition_variable_win.cc',
'synchronization/lock.cc',
'synchronization/lock.h',
'synchronization/lock_impl.h',
'synchronization/lock_impl_posix.cc',
'synchronization/lock_impl_win.cc',
'sys_info.h',
'sys_info_chromeos.cc',
'sys_info_freebsd.cc',
Expand Down
8 changes: 4 additions & 4 deletions base/crypto/cssm_init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include <Security/SecBase.h>

#include "base/lock.h"
#include "base/logging.h"
#include "base/singleton.h"
#include "base/synchronization/lock.h"
#include "base/sys_string_conversions.h"

// When writing crypto code for Mac OS X, you may find the following
Expand Down Expand Up @@ -92,15 +92,15 @@ class SecurityServicesSingleton {

~SecurityServicesSingleton() {}

Lock& lock() { return lock_; }
base::Lock& lock() { return lock_; }

private:
friend class Singleton<SecurityServicesSingleton>;
friend struct DefaultSingletonTraits<SecurityServicesSingleton>;

SecurityServicesSingleton() {}

Lock lock_;
base::Lock lock_;

DISALLOW_COPY_AND_ASSIGN(SecurityServicesSingleton);
};
Expand Down Expand Up @@ -154,7 +154,7 @@ void LogCSSMError(const char *fn_name, CSSM_RETURN err) {
}
}

Lock& GetMacSecurityServicesLock() {
base::Lock& GetMacSecurityServicesLock() {
return SecurityServicesSingleton::GetInstance()->lock();
}

Expand Down
4 changes: 2 additions & 2 deletions base/crypto/cssm_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

#include "base/scoped_ptr.h"

class Lock;

namespace base {

class Lock;

// Initialize CSSM if it isn't already initialized. This must be called before
// any other CSSM functions. This function is thread-safe, and CSSM will only
// ever be initialized once. CSSM will be properly shut down on program exit.
Expand Down
125 changes: 8 additions & 117 deletions base/lock.h
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_
8 changes: 4 additions & 4 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ typedef pthread_mutex_t* MutexHandle;
#include "base/debug/debugger.h"
#include "base/debug/stack_trace.h"
#include "base/eintr_wrapper.h"
#include "base/lock_impl.h"
#include "base/string_piece.h"
#include "base/synchronization/lock_impl.h"
#include "base/utf_string_conversions.h"
#include "base/vlog.h"
#if defined(OS_POSIX)
Expand Down Expand Up @@ -243,7 +243,7 @@ class LoggingLock {
}
#endif
} else {
log_lock = new LockImpl();
log_lock = new base::internal::LockImpl();
}
initialized = true;
}
Expand Down Expand Up @@ -282,7 +282,7 @@ class LoggingLock {
// The lock is used if log file locking is false. It helps us avoid problems
// with multiple threads writing to the log file at the same time. Use
// LockImpl directly instead of using Lock, because Lock makes logging calls.
static LockImpl* log_lock;
static base::internal::LockImpl* log_lock;

// When we don't use a lock, we are using a global mutex. We need to do this
// because LockFileEx is not thread safe.
Expand All @@ -299,7 +299,7 @@ class LoggingLock {
// static
bool LoggingLock::initialized = false;
// static
LockImpl* LoggingLock::log_lock = NULL;
base::internal::LockImpl* LoggingLock::log_lock = NULL;
// static
LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE;

Expand Down
Loading

0 comments on commit bc581a6

Please sign in to comment.