From e4c17356b6c819df8effb29dfb4bb903520b8c2b Mon Sep 17 00:00:00 2001 From: herb Date: Tue, 29 Sep 2015 14:38:01 -0700 Subject: [PATCH] Unify ThreadID. BUG=skia: Review URL: https://codereview.chromium.org/1371303003 --- gyp/core.gypi | 2 ++ include/private/SkMutex.h | 31 ++++++++++--------------------- include/private/SkThreadID.h | 19 +++++++++++++++++++ src/core/SkSharedMutex.cpp | 32 ++++++++++++-------------------- src/core/SkThreadID.cpp | 16 ++++++++++++++++ 5 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 include/private/SkThreadID.h create mode 100644 src/core/SkThreadID.cpp diff --git a/gyp/core.gypi b/gyp/core.gypi index d8008dccec..94711b9a3d 100644 --- a/gyp/core.gypi +++ b/gyp/core.gypi @@ -226,6 +226,7 @@ '<(skia_src_path)/core/SkTextMapStateProc.h', '<(skia_src_path)/core/SkTime.cpp', '<(skia_src_path)/core/SkTDPQueue.h', + '<(skia_src_path)/core/SkThreadID.cpp', '<(skia_src_path)/core/SkTLList.h', '<(skia_src_path)/core/SkTLS.cpp', '<(skia_src_path)/core/SkTraceEvent.h', @@ -349,6 +350,7 @@ '<(skia_include_path)/private/SkSemaphore.h', '<(skia_include_path)/private/SkSpinlock.h', '<(skia_include_path)/private/SkTemplates.h', + '<(skia_include_path)/private/SkThreadID.h', # Path ops '<(skia_include_path)/pathops/SkPathOps.h', diff --git a/include/private/SkMutex.h b/include/private/SkMutex.h index 77a00f0235..c3e9919e52 100644 --- a/include/private/SkMutex.h +++ b/include/private/SkMutex.h @@ -12,6 +12,10 @@ #include "../private/SkSemaphore.h" #include "SkTypes.h" +#ifdef SK_DEBUG + #include "../private/SkThreadID.h" +#endif + #define SK_MUTEX_SEMAPHORE_INIT {1, {0}} #ifdef SK_DEBUG @@ -30,35 +34,20 @@ // initialized in a class with this macro. #define SK_DECLARE_STATIC_MUTEX(name) namespace {} static SkBaseMutex name = SK_BASE_MUTEX_INIT; -// TODO(herb): unify this with the ThreadID in SkSharedMutex.cpp. -#ifdef SK_DEBUG - #ifdef SK_BUILD_FOR_WIN - #include - inline int64_t sk_get_thread_id() { return GetCurrentThreadId(); } - #else - #include - inline int64_t sk_get_thread_id() { return (int64_t)pthread_self(); } - #endif -#endif - -typedef int64_t SkThreadID; - -SkDEBUGCODE(static const SkThreadID kNoOwner = 0;) - struct SkBaseMutex { void acquire() { fSemaphore.wait(); - SkDEBUGCODE(fOwner = sk_get_thread_id();) + SkDEBUGCODE(fOwner = SkGetThreadID();) } void release() { this->assertHeld(); - SkDEBUGCODE(fOwner = kNoOwner;) + SkDEBUGCODE(fOwner = kIllegalThreadID;) fSemaphore.signal(); } void assertHeld() { - SkASSERT(fOwner == sk_get_thread_id()); + SkASSERT(fOwner == SkGetThreadID()); } SkBaseSemaphore fSemaphore; @@ -70,7 +59,7 @@ class SkMutex : public SkBaseMutex { public: SkMutex () { fSemaphore = SK_MUTEX_SEMAPHORE_INIT; - SkDEBUGCODE(fOwner = kNoOwner); + SkDEBUGCODE(fOwner = kIllegalThreadID); } ~SkMutex () { fSemaphore.deleteSemaphore(); } SkMutex(const SkMutex&) = delete; @@ -81,7 +70,7 @@ template class SkAutoTAcquire : SkNoncopyable { public: explicit SkAutoTAcquire(Lock& mutex) : fMutex(&mutex) { - SkASSERT(fMutex != NULL); + SkASSERT(fMutex != nullptr); mutex.acquire(); } @@ -102,7 +91,7 @@ public: void release() { if (fMutex) { fMutex->release(); - fMutex = NULL; + fMutex = nullptr; } } diff --git a/include/private/SkThreadID.h b/include/private/SkThreadID.h new file mode 100644 index 0000000000..a210a929f3 --- /dev/null +++ b/include/private/SkThreadID.h @@ -0,0 +1,19 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SkThreadID_DEFINED +#define SkThreadID_DEFINED + +#include "SkTypes.h" + +typedef int64_t SkThreadID; + +SkThreadID SkGetThreadID(); + +const SkThreadID kIllegalThreadID = 0; + +#endif // SkThreadID_DEFINED diff --git a/src/core/SkSharedMutex.cpp b/src/core/SkSharedMutex.cpp index 203a775190..aeaf5776bf 100644 --- a/src/core/SkSharedMutex.cpp +++ b/src/core/SkSharedMutex.cpp @@ -9,7 +9,7 @@ #include "SkAtomics.h" #include "SkTypes.h" -#include "../private/SkSemaphore.h" +#include "SkSemaphore.h" #if defined(THREAD_SANITIZER) @@ -68,21 +68,13 @@ void AnnotateRWLockReleased( #ifdef SK_DEBUG + #include "SkThreadID.h" #include "SkTDArray.h" - #ifdef SK_BUILD_FOR_WIN - #include - static int64_t get_thread_id() { return GetCurrentThreadId(); } - #else - #include - static int64_t get_thread_id() { return (int64_t)pthread_self(); } - #endif - - typedef int64_t ThreadID; class SkSharedMutex::ThreadIDSet { public: // Returns true if threadID is in the set. - bool find(ThreadID threadID) const { + bool find(SkThreadID threadID) const { for (auto& t : fThreadIDs) { if (t == threadID) return true; } @@ -90,7 +82,7 @@ void AnnotateRWLockReleased( } // Returns true if did not already exist. - bool tryAdd(ThreadID threadID) { + bool tryAdd(SkThreadID threadID) { for (auto& t : fThreadIDs) { if (t == threadID) return false; } @@ -98,7 +90,7 @@ void AnnotateRWLockReleased( return true; } // Returns true if already exists in Set. - bool tryRemove(ThreadID threadID) { + bool tryRemove(SkThreadID threadID) { for (int i = 0; i < fThreadIDs.count(); ++i) { if (fThreadIDs[i] == threadID) { fThreadIDs.remove(i); @@ -117,7 +109,7 @@ void AnnotateRWLockReleased( } private: - SkTDArray fThreadIDs; + SkTDArray fThreadIDs; }; SkSharedMutex::SkSharedMutex() @@ -130,7 +122,7 @@ void AnnotateRWLockReleased( SkSharedMutex::~SkSharedMutex() { ANNOTATE_RWLOCK_DESTROY(this); } void SkSharedMutex::acquire() { - ThreadID threadID(get_thread_id()); + SkThreadID threadID(SkGetThreadID()); int currentSharedCount; int waitingExclusiveCount; { @@ -156,7 +148,7 @@ void AnnotateRWLockReleased( // exclusive lock separate from the threads added before. void SkSharedMutex::release() { ANNOTATE_RWLOCK_RELEASED(this, 1); - ThreadID threadID(get_thread_id()); + SkThreadID threadID(SkGetThreadID()); int sharedWaitingCount; int exclusiveWaitingCount; int sharedQueueSelect; @@ -183,14 +175,14 @@ void AnnotateRWLockReleased( } void SkSharedMutex::assertHeld() const { - ThreadID threadID(get_thread_id()); + SkThreadID threadID(SkGetThreadID()); SkAutoMutexAcquire l(&fMu); SkASSERT(0 == fCurrentShared->count()); SkASSERT(fWaitingExclusive->find(threadID)); } void SkSharedMutex::acquireShared() { - ThreadID threadID(get_thread_id()); + SkThreadID threadID(SkGetThreadID()); int exclusiveWaitingCount; int sharedQueueSelect; { @@ -217,7 +209,7 @@ void AnnotateRWLockReleased( void SkSharedMutex::releaseShared() { ANNOTATE_RWLOCK_RELEASED(this, 0); - ThreadID threadID(get_thread_id()); + SkThreadID threadID(SkGetThreadID()); int currentSharedCount; int waitingExclusiveCount; @@ -236,7 +228,7 @@ void AnnotateRWLockReleased( } void SkSharedMutex::assertHeldShared() const { - ThreadID threadID(get_thread_id()); + SkThreadID threadID(SkGetThreadID()); SkAutoMutexAcquire l(&fMu); SkASSERT(fCurrentShared->find(threadID)); } diff --git a/src/core/SkThreadID.cpp b/src/core/SkThreadID.cpp new file mode 100644 index 0000000000..589b4da05a --- /dev/null +++ b/src/core/SkThreadID.cpp @@ -0,0 +1,16 @@ +/* + * Copyright 2015 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkThreadID.h" + +#ifdef SK_BUILD_FOR_WIN + #include + SkThreadID SkGetThreadID() { return GetCurrentThreadId(); } +#else + #include + SkThreadID SkGetThreadID() { return (int64_t)pthread_self(); } +#endif