diff options
-rw-r--r-- | base/base.xcodeproj/project.pbxproj | 16 | ||||
-rw-r--r-- | base/build/base.vcproj | 4 | ||||
-rw-r--r-- | base/condition_variable.h | 77 | ||||
-rw-r--r-- | base/condition_variable_posix.cc | 85 | ||||
-rw-r--r-- | base/condition_variable_win.cc (renamed from base/condition_variable.cc) | 8 | ||||
-rw-r--r-- | base/lock.h | 15 | ||||
-rw-r--r-- | base/lock_impl.h | 37 | ||||
-rw-r--r-- | base/lock_impl_posix.cc (renamed from base/lock_impl_mac.cc) | 21 | ||||
-rw-r--r-- | base/lock_impl_win.cc | 10 |
9 files changed, 195 insertions, 78 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index 529183c..d17a22e 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -49,7 +49,7 @@ 824652C10DC12044007C2BAA /* hash_tables.h in Headers */ = {isa = PBXBuildFile; fileRef = 824652C00DC12044007C2BAA /* hash_tables.h */; }; 824653450DC1230B007C2BAA /* lock.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403250D92D2090006B936 /* lock.cc */; }; 824653460DC1230C007C2BAA /* lock.h in Headers */ = {isa = PBXBuildFile; fileRef = 825403260D92D2090006B936 /* lock.h */; }; - 824653680DC12CEC007C2BAA /* condition_variable_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = 824653670DC12CEC007C2BAA /* condition_variable_mac.cc */; }; + 824653680DC12CEC007C2BAA /* condition_variable_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 824653670DC12CEC007C2BAA /* condition_variable_posix.cc */; }; 824653690DC12CF2007C2BAA /* condition_variable.h in Headers */ = {isa = PBXBuildFile; fileRef = 825402EB0D92D1940006B936 /* condition_variable.h */; }; 824653740DC12D0E007C2BAA /* shared_memory_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 824653730DC12D0E007C2BAA /* shared_memory_posix.cc */; }; 824653750DC12D0F007C2BAA /* shared_memory.h in Headers */ = {isa = PBXBuildFile; fileRef = 825403760D92D2CF0006B936 /* shared_memory.h */; }; @@ -85,7 +85,7 @@ 829E31340DBFDB6F00819EBF /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 829E31330DBFDB6F00819EBF /* CoreGraphics.framework */; }; 829E35D50DC0DC9400819EBF /* logging.cc in Sources */ = {isa = PBXBuildFile; fileRef = 825403270D92D2090006B936 /* logging.cc */; }; 829E35D60DC0DC9400819EBF /* logging.h in Headers */ = {isa = PBXBuildFile; fileRef = 825403280D92D2090006B936 /* logging.h */; }; - 829E35DA0DC0DD0400819EBF /* lock_impl_mac.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BEFC29D0D99832D000829AD /* lock_impl_mac.cc */; }; + 829E35DA0DC0DD0400819EBF /* lock_impl_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7BEFC29D0D99832D000829AD /* lock_impl_posix.cc */; }; 829E35DB0DC0DD0500819EBF /* lock_impl.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BEFC29C0D99832D000829AD /* lock_impl.h */; }; 829E36420DC0F69D00819EBF /* string16.h in Headers */ = {isa = PBXBuildFile; fileRef = 821B91680DAABD7F00F350D7 /* string16.h */; }; 829E36430DC0F69E00819EBF /* string_util_mac.h in Headers */ = {isa = PBXBuildFile; fileRef = 7BEE52C10D9D84FD0067FF23 /* string_util_mac.h */; }; @@ -308,7 +308,7 @@ 7BEB834D0D9C4BE0009BA8DD /* string_util_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = string_util_mac.cc; sourceTree = "<group>"; }; 7BEE52C10D9D84FD0067FF23 /* string_util_mac.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = string_util_mac.h; sourceTree = "<group>"; }; 7BEFC29C0D99832D000829AD /* lock_impl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lock_impl.h; sourceTree = "<group>"; }; - 7BEFC29D0D99832D000829AD /* lock_impl_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lock_impl_mac.cc; sourceTree = "<group>"; }; + 7BEFC29D0D99832D000829AD /* lock_impl_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lock_impl_posix.cc; sourceTree = "<group>"; }; 820EB4EB0E3A60FE009668FC /* idle_timer.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = idle_timer.cc; sourceTree = "<group>"; }; 820EB4EC0E3A60FE009668FC /* idle_timer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = idle_timer.h; sourceTree = "<group>"; }; 820EB4EF0E3A610A009668FC /* linked_ptr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = linked_ptr.h; sourceTree = "<group>"; }; @@ -326,7 +326,7 @@ 821B91680DAABD7F00F350D7 /* string16.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = string16.h; sourceTree = "<group>"; }; 822BDC710DF739F000034F4C /* message_loop_mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = message_loop_mac.mm; sourceTree = "<group>"; }; 824652C00DC12044007C2BAA /* hash_tables.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hash_tables.h; sourceTree = "<group>"; }; - 824653670DC12CEC007C2BAA /* condition_variable_mac.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = condition_variable_mac.cc; sourceTree = "<group>"; }; + 824653670DC12CEC007C2BAA /* condition_variable_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = condition_variable_posix.cc; sourceTree = "<group>"; }; 824653730DC12D0E007C2BAA /* shared_memory_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = shared_memory_posix.cc; sourceTree = "<group>"; }; 824654900DC25A8C007C2BAA /* time.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = time.cc; sourceTree = "<group>"; }; 825402BB0D92D0FA0006B936 /* libbase.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libbase.a; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -561,7 +561,7 @@ E4A133490E37A41D00110AA2 /* command_line.cc */, E4A1334A0E37A41D00110AA2 /* command_line.h */, 825402EB0D92D1940006B936 /* condition_variable.h */, - 824653670DC12CEC007C2BAA /* condition_variable_mac.cc */, + 824653670DC12CEC007C2BAA /* condition_variable_posix.cc */, 825402F30D92D1AC0006B936 /* debug_on_start.h */, 825402F40D92D1AC0006B936 /* debug_on_start.cc */, 825402F10D92D1AC0006B936 /* debug_util.h */, @@ -595,7 +595,7 @@ 825403250D92D2090006B936 /* lock.cc */, 825403260D92D2090006B936 /* lock.h */, 7BEFC29C0D99832D000829AD /* lock_impl.h */, - 7BEFC29D0D99832D000829AD /* lock_impl_mac.cc */, + 7BEFC29D0D99832D000829AD /* lock_impl_posix.cc */, 825403270D92D2090006B936 /* logging.cc */, 825403280D92D2090006B936 /* logging.h */, 825403290D92D2090006B936 /* md5.cc */, @@ -1091,13 +1091,13 @@ buildActionMask = 2147483647; files = ( 829E35D50DC0DC9400819EBF /* logging.cc in Sources */, - 829E35DA0DC0DD0400819EBF /* lock_impl_mac.cc in Sources */, + 829E35DA0DC0DD0400819EBF /* lock_impl_posix.cc in Sources */, 829E36440DC0F69F00819EBF /* string_util_mac.cc in Sources */, 829E36460DC0F6AC00819EBF /* string_util.cc in Sources */, 829E365F0DC0FB1C00819EBF /* stats_table.cc in Sources */, 829E36730DC0FBAD00819EBF /* thread_local_storage_posix.cc in Sources */, 824653450DC1230B007C2BAA /* lock.cc in Sources */, - 824653680DC12CEC007C2BAA /* condition_variable_mac.cc in Sources */, + 824653680DC12CEC007C2BAA /* condition_variable_posix.cc in Sources */, 824653740DC12D0E007C2BAA /* shared_memory_posix.cc in Sources */, 824654530DC25633007C2BAA /* time_mac.cc in Sources */, 8246548C0DC259DB007C2BAA /* revocable_store.cc in Sources */, diff --git a/base/build/base.vcproj b/base/build/base.vcproj index a5257cb..3e2cd15 100644 --- a/base/build/base.vcproj +++ b/base/build/base.vcproj @@ -234,11 +234,11 @@ > </File> <File - RelativePath="..\condition_variable.cc" + RelativePath="..\condition_variable.h" > </File> <File - RelativePath="..\condition_variable.h" + RelativePath="..\condition_variable_win.cc" > </File> <File diff --git a/base/condition_variable.h b/base/condition_variable.h index bdb9a19..f30ffba 100644 --- a/base/condition_variable.h +++ b/base/condition_variable.h @@ -27,37 +27,35 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// ConditionVariable is a reasonable attempt at simulating -// the newer Posix and Vista-only construct for condition variable -// synchronization. This functionality is very helpful for having several -// threads wait for an event, as is common with a thread pool -// managed by a master. The meaning of such an event in the -// (worker) thread pool scenario is that additional tasks are -// now available for processing. It is used in Chrome in the -// DNS prefetching system to notify worker threads that a queue -// now has items (tasks) which need to be tended to. -// A related use would have a pool manager waiting on a -// ConditionVariable, waiting for a thread in the pool to announce -// (signal) that there is now more room in a (bounded size) communications -// queue for the manager to deposit tasks, or, as a second example, that -// the queue of tasks is completely empty and all workers are waiting. - +// ConditionVariable wraps pthreads condition variable synchronization or, on +// Windows, simulates it. This functionality is very helpful for having +// several threads wait for an event, as is common with a thread pool managed +// by a master. The meaning of such an event in the (worker) thread pool +// scenario is that additional tasks are now available for processing. It is +// used in Chrome in the DNS prefetching system to notify worker threads that +// a queue now has items (tasks) which need to be tended to. A related use +// would have a pool manager waiting on a ConditionVariable, waiting for a +// thread in the pool to announce (signal) that there is now more room in a +// (bounded size) communications queue for the manager to deposit tasks, or, +// as a second example, that the queue of tasks is completely empty and all +// workers are waiting. +// // USAGE NOTE 1: spurious signal events are possible with this and // most implementations of condition variables. As a result, be // *sure* to retest your condition before proceeding. The following // is a good example of doing this correctly: - +// // while (!work_to_be_done()) Wait(...); - +// // In contrast do NOT do the following: - +// // if (!work_to_be_done()) Wait(...); // Don't do this. - +// // Especially avoid the above if you are relying on some other thread only // issuing a signal up *if* there is work-to-do. There can/will // be spurious signals. Recheck state on waiting thread before // assuming the signal was intentional. Caveat caller ;-). - +// // USAGE NOTE 2: Broadcast() frees up all waiting threads at once, // which leads to contention for the locks they all held when they // called Wait(). This results in POOR performance. A much better @@ -65,11 +63,11 @@ // thread (upon exiting Wait()) call Signal() to free up another // Wait'ing thread. Look at condition_variable_unittest.cc for // both examples. - +// // Broadcast() can be used nicely during teardown, as it gets the job // done, and leaves no sleeping threads... and performance is less // critical at that point. - +// // The semantics of Broadcast() are carefully crafted so that *all* // threads that were waiting when the request was made will indeed // get signaled. Some implementations mess up, and don't signal them @@ -77,7 +75,7 @@ // for a while while waiting threads come around). This implementation // appears correct, as it will not "lose" any signals, and will guarantee // that all threads get signaled by Broadcast(). - +// // This implementation offers support for "performance" in its selection of // which thread to revive. Performance, in direct contrast with "fairness," // assures that the thread that most recently began to Wait() is selected by @@ -85,20 +83,18 @@ // thread that has Wait()ed the longest is selected. The default policy // may improve performance, as the selected thread may have a greater chance of // having some of its stack data in various CPU caches. - +// // For a discussion of the many very subtle implementation details, see the FAQ -// at the end of condition_variable.cc. +// at the end of condition_variable_win.cc. -#ifndef BASE_CONDITION_VARIABLE_H__ -#define BASE_CONDITION_VARIABLE_H__ +#ifndef BASE_CONDITION_VARIABLE_H_ +#define BASE_CONDITION_VARIABLE_H_ #include "base/lock.h" #include "base/logging.h" #include "base/scoped_ptr.h" #include "base/time.h" -class Lock; - class ConditionVariable { public: // Construct a cv for use with ONLY one user lock. @@ -108,12 +104,8 @@ class ConditionVariable { // Wait() releases the caller's critical section atomically as it starts to // sleep, and the reacquires it when it is signaled. + void Wait(); void TimedWait(const TimeDelta& max_time); - void Wait() { - // Default to "wait forever" timing, which means have to get a Signal() - // or Broadcast() to come out of this wait state. - TimedWait(TimeDelta::FromMilliseconds(INFINITE)); - } // Broadcast() revives all waiting threads. void Broadcast(); @@ -121,6 +113,9 @@ class ConditionVariable { void Signal(); private: + +#if defined(OS_WIN) + // Define Event class that is used to form circularly linked lists. // The list container is an element with NULL as its handle_ value. // The actual list elements have a non-zero handle_ value. @@ -161,7 +156,7 @@ class ConditionVariable { HANDLE handle_; Event* next_; Event* prev_; - DISALLOW_EVIL_CONSTRUCTORS(Event); + DISALLOW_COPY_AND_ASSIGN(Event); }; // Note that RUNNING is an unlikely number to have in RAM by accident. @@ -176,6 +171,7 @@ class ConditionVariable { // Private critical section for access to member data. Lock internal_lock_; + // Lock that is acquired before calling Wait(). Lock& user_lock_; @@ -189,7 +185,14 @@ class ConditionVariable { // The number of allocated, but not yet deleted events. int allocation_counter_; - DISALLOW_EVIL_CONSTRUCTORS(ConditionVariable); +#elif defined(OS_POSIX) + + pthread_cond_t condition_; + pthread_mutex_t* user_mutex_; + +#endif + + DISALLOW_COPY_AND_ASSIGN(ConditionVariable); }; -#endif // BASE_CONDITION_VARIABLE_H__ +#endif // BASE_CONDITION_VARIABLE_H_ diff --git a/base/condition_variable_posix.cc b/base/condition_variable_posix.cc new file mode 100644 index 0000000..e234ece --- /dev/null +++ b/base/condition_variable_posix.cc @@ -0,0 +1,85 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "base/condition_variable.h" + +#include <errno.h> +#include <sys/time.h> + +#include "base/lock.h" +#include "base/lock_impl.h" +#include "base/logging.h" + +ConditionVariable::ConditionVariable(Lock* user_lock) + : user_mutex_(user_lock->lock_impl()->os_lock()) { + int rv = pthread_cond_init(&condition_, NULL); + DCHECK(rv == 0); +} + +ConditionVariable::~ConditionVariable() { + int rv = pthread_cond_destroy(&condition_); + DCHECK(rv == 0); +} + +void ConditionVariable::Wait() { + int rv = pthread_cond_wait(&condition_, user_mutex_); + DCHECK(rv == 0); +} + +void ConditionVariable::TimedWait(const TimeDelta& max_time) { + int64 usecs = max_time.InMicroseconds(); + + // The timeout argument to pthread_cond_timedwait is in absolute time. + struct timeval now; + gettimeofday(&now, NULL); + + const int kMicrosPerUnit = 1000000; + const int kNanosPerMicro = 1000; + const int kNanosPerUnit = kMicrosPerUnit * kNanosPerMicro; + + struct timespec abstime; + abstime.tv_sec = now.tv_sec + (usecs / kMicrosPerUnit); + abstime.tv_nsec = (now.tv_usec + (usecs % kMicrosPerUnit)) * kNanosPerMicro; + abstime.tv_sec += abstime.tv_nsec / kNanosPerUnit; + abstime.tv_nsec %= kNanosPerUnit; + DCHECK(abstime.tv_sec >= now.tv_sec); // Overflow paranoia + + int rv = pthread_cond_timedwait(&condition_, user_mutex_, &abstime); + DCHECK(rv == 0 || rv == ETIMEDOUT); +} + +void ConditionVariable::Broadcast() { + int rv = pthread_cond_broadcast(&condition_); + DCHECK(rv == 0); +} + +void ConditionVariable::Signal() { + int rv = pthread_cond_signal(&condition_); + DCHECK(rv == 0); +} diff --git a/base/condition_variable.cc b/base/condition_variable_win.cc index 0e4f7c8..9b17261 100644 --- a/base/condition_variable.cc +++ b/base/condition_variable_win.cc @@ -65,8 +65,12 @@ ConditionVariable::~ConditionVariable() { DCHECK_EQ(recycling_list_size_, allocation_counter_); } -// Wait() atomically releases the caller's lock as it starts to Wait, and then -// reacquires it when it is signaled. +void ConditionVariable::Wait() { + // Default to "wait forever" timing, which means have to get a Signal() + // or Broadcast() to come out of this wait state. + TimedWait(TimeDelta::FromMilliseconds(INFINITE)); +} + void ConditionVariable::TimedWait(const TimeDelta& max_time) { Event* waiting_event; HANDLE handle; diff --git a/base/lock.h b/base/lock.h index 59b0aea..249dae3 100644 --- a/base/lock.h +++ b/base/lock.h @@ -27,8 +27,8 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef BASE_LOCK_H__ -#define BASE_LOCK_H__ +#ifndef BASE_LOCK_H_ +#define BASE_LOCK_H_ #include "base/lock_impl.h" @@ -50,6 +50,11 @@ class Lock { // held by something else, immediately return false. bool Try(); + // Return the underlying lock implementation. + // TODO(awalker): refactor lock and condition variables so that this is + // unnecessary. + LockImpl* lock_impl() { return &lock_; } + private: LockImpl lock_; // User-supplied underlying lock implementation. @@ -69,7 +74,7 @@ class Lock { int32 contention_count_; #endif // NDEBUG - DISALLOW_EVIL_CONSTRUCTORS(Lock); + DISALLOW_COPY_AND_ASSIGN(Lock); }; // A helper class that acquires the given Lock while the AutoLock is in scope. @@ -85,7 +90,7 @@ class AutoLock { private: Lock& lock_; - DISALLOW_EVIL_CONSTRUCTORS(AutoLock); + DISALLOW_COPY_AND_ASSIGN(AutoLock); }; // AutoUnlock is a helper class for ConditionVariable instances @@ -111,4 +116,4 @@ class AutoUnlock { int release_count_; }; -#endif // BASE_LOCK_H__ +#endif // BASE_LOCK_H_ diff --git a/base/lock_impl.h b/base/lock_impl.h index 331bdc6..a33f107 100644 --- a/base/lock_impl.h +++ b/base/lock_impl.h @@ -27,22 +27,30 @@ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#ifndef BASE_LOCK_IMPL_H__ -#define BASE_LOCK_IMPL_H__ +#ifndef BASE_LOCK_IMPL_H_ +#define BASE_LOCK_IMPL_H_ -#include "base/basictypes.h" +#include "build/build_config.h" -#if defined(WIN32) -#include <Windows.h> -#elif defined(__APPLE__) -#include <libkern/OSAtomic.h> +#if defined(OS_WIN) +#include <windows.h> +#elif defined(OS_POSIX) +#include <pthread.h> #endif +#include "base/basictypes.h" + // This class implements the underlying platform-specific spin-lock mechanism // used for the Lock class. Most users should not use LockImpl directly, but // should instead use Lock. class LockImpl { public: +#if defined(OS_WIN) + typedef CRITICAL_SECTION OSLockType; +#elif defined(OS_POSIX) + typedef pthread_mutex_t OSLockType; +#endif + LockImpl(); ~LockImpl(); @@ -57,14 +65,15 @@ class LockImpl { // a successful call to Try, or a call to Lock. void Unlock(); + // Return the native underlying lock. + // TODO(awalker): refactor lock and condition variables so that this is + // unnecessary. + OSLockType* os_lock() { return &os_lock_; } + private: -#if defined(WIN32) - CRITICAL_SECTION critical_section_; -#elif defined(__APPLE__) - OSSpinLock spin_lock_; -#endif + OSLockType os_lock_; - DISALLOW_EVIL_CONSTRUCTORS(LockImpl); + DISALLOW_COPY_AND_ASSIGN(LockImpl); }; class AutoLockImpl { @@ -84,4 +93,4 @@ class AutoLockImpl { LockImpl* lock_impl_; }; -#endif // BASE_LOCK_IMPL_H__ +#endif // BASE_LOCK_IMPL_H_ diff --git a/base/lock_impl_mac.cc b/base/lock_impl_posix.cc index fcb630d..aceb414 100644 --- a/base/lock_impl_mac.cc +++ b/base/lock_impl_posix.cc @@ -29,21 +29,32 @@ #include "base/lock_impl.h" -LockImpl::LockImpl() - : spin_lock_(0) { +#include <errno.h> + +#include "base/logging.h" + +LockImpl::LockImpl() { + int rv = pthread_mutex_init(&os_lock_, NULL); + DCHECK(rv == 0); } LockImpl::~LockImpl() { + int rv = pthread_mutex_destroy(&os_lock_); + DCHECK(rv == 0); } bool LockImpl::Try() { - return ::OSSpinLockTry(&spin_lock_); + int rv = pthread_mutex_trylock(&os_lock_); + DCHECK(rv == 0 || rv == EBUSY); + return rv == 0; } void LockImpl::Lock() { - ::OSSpinLockLock(&spin_lock_); + int rv = pthread_mutex_lock(&os_lock_); + DCHECK(rv == 0); } void LockImpl::Unlock() { - ::OSSpinLockUnlock(&spin_lock_); + int rv = pthread_mutex_unlock(&os_lock_); + DCHECK(rv == 0); } diff --git a/base/lock_impl_win.cc b/base/lock_impl_win.cc index a45cbdd..5298176 100644 --- a/base/lock_impl_win.cc +++ b/base/lock_impl_win.cc @@ -30,21 +30,21 @@ #include "base/lock_impl.h" LockImpl::LockImpl() { - ::InitializeCriticalSection(&critical_section_); + ::InitializeCriticalSection(&os_lock_); } LockImpl::~LockImpl() { - ::DeleteCriticalSection(&critical_section_); + ::DeleteCriticalSection(&os_lock_); } bool LockImpl::Try() { - return ::TryEnterCriticalSection(&critical_section_) != FALSE; + return ::TryEnterCriticalSection(&os_lock_) != FALSE; } void LockImpl::Lock() { - ::EnterCriticalSection(&critical_section_); + ::EnterCriticalSection(&os_lock_); } void LockImpl::Unlock() { - ::LeaveCriticalSection(&critical_section_); + ::LeaveCriticalSection(&os_lock_); } |