Unify ThreadID.

BUG=skia:

Review URL: https://codereview.chromium.org/1371303003
This commit is contained in:
herb 2015-09-29 14:38:01 -07:00 committed by Commit bot
parent b5d3263534
commit e4c17356b6
5 changed files with 59 additions and 41 deletions

View File

@ -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',

View File

@ -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 <windows.h>
inline int64_t sk_get_thread_id() { return GetCurrentThreadId(); }
#else
#include <pthread.h>
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 <typename Lock>
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;
}
}

View File

@ -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

View File

@ -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 <windows.h>
static int64_t get_thread_id() { return GetCurrentThreadId(); }
#else
#include <pthread.h>
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<ThreadID> fThreadIDs;
SkTDArray<SkThreadID> 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));
}

16
src/core/SkThreadID.cpp Normal file
View File

@ -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 <windows.h>
SkThreadID SkGetThreadID() { return GetCurrentThreadId(); }
#else
#include <pthread.h>
SkThreadID SkGetThreadID() { return (int64_t)pthread_self(); }
#endif