diff options
author | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-07-16 14:59:24 +0000 |
---|---|---|
committer | bungeman@google.com <bungeman@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2013-07-16 14:59:24 +0000 |
commit | f2e7dbb09fabc31fe5b16f6dc1a16593f518ca7d (patch) | |
tree | 5e0cc6a0bf8e2b89d57c99ef54f34e26395df548 | |
parent | 883fe7f8b13be4ae3e3f8d8f8a6d6e94d13f9392 (diff) |
Split TLS implementation into its own translation unit.
SkTLS has it's own header separate from SkThread, and having SkThread own
the platform implementation of SkTLS is problematic with Chromium. The
simplest way to clean this up is to put the implementation in its own set
of files, where it is also more easily found.
R=robertphillips@google.com
Review URL: https://codereview.chromium.org/19240007
git-svn-id: http://skia.googlecode.com/svn/trunk@10105 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | gyp/ports.gyp | 19 | ||||
-rw-r--r-- | src/ports/SkTLS_none.cpp | 18 | ||||
-rw-r--r-- | src/ports/SkTLS_pthread.cpp | 30 | ||||
-rw-r--r-- | src/ports/SkTLS_win.cpp | 75 | ||||
-rw-r--r-- | src/ports/SkThread_none.cpp | 15 | ||||
-rw-r--r-- | src/ports/SkThread_pthread.cpp | 23 | ||||
-rw-r--r-- | src/ports/SkThread_win.cpp | 71 |
7 files changed, 134 insertions, 117 deletions
diff --git a/gyp/ports.gyp b/gyp/ports.gyp index 526e92032d..2641bc9641 100644 --- a/gyp/ports.gyp +++ b/gyp/ports.gyp @@ -27,15 +27,19 @@ '../src/ports/SkFontHost_win.cpp', '../src/ports/SkFontHost_win_dw.cpp', '../src/ports/SkGlobalInitialization_default.cpp', - '../src/ports/SkPurgeableMemoryBlock_none.cpp', - '../src/ports/SkThread_win.cpp', - '../src/ports/SkMemory_malloc.cpp', '../src/ports/SkOSFile_posix.cpp', '../src/ports/SkOSFile_stdio.cpp', '../src/ports/SkOSFile_win.cpp', + '../src/ports/SkPurgeableMemoryBlock_none.cpp', + #'../src/ports/SkThread_none.cpp', + '../src/ports/SkThread_pthread.cpp', + '../src/ports/SkThread_win.cpp', '../src/ports/SkTime_Unix.cpp', '../src/ports/SkTime_win.cpp', + #'../src/ports/SkTLS_none.cpp', + '../src/ports/SkTLS_pthread.cpp', + '../src/ports/SkTLS_win.cpp', '../src/ports/SkXMLParser_empty.cpp', ], 'conditions': [ @@ -58,7 +62,6 @@ '../src/ports/SkFontHost_FreeType_common.cpp', '../src/ports/SkFontHost_fontconfig.cpp', '../src/ports/SkFontConfigInterface_direct.cpp', - '../src/ports/SkThread_pthread.cpp', ], }], [ 'skia_os == "nacl"', { @@ -79,7 +82,6 @@ '../src/ports/SkFontHost_FreeType.cpp', '../src/ports/SkFontHost_FreeType_common.cpp', '../src/ports/SkFontHost_linux.cpp', - '../src/ports/SkThread_pthread.cpp', ], 'sources!': [ '../src/ports/SkDebug_stdio.cpp', @@ -99,7 +101,6 @@ # '../src/ports/SkFontHost_FreeType.cpp', # '../src/ports/SkFontHost_FreeType_common.cpp', '../src/ports/SkPurgeableMemoryBlock_mac.cpp', - '../src/ports/SkThread_pthread.cpp', '../src/utils/mac/SkStream_mac.cpp', ], 'sources!': [ @@ -115,7 +116,6 @@ 'sources': [ '../src/ports/SkFontHost_mac.cpp', '../src/ports/SkPurgeableMemoryBlock_mac.cpp', - '../src/ports/SkThread_pthread.cpp', '../src/utils/mac/SkStream_mac.cpp', ], 'sources!': [ @@ -141,8 +141,10 @@ ], 'sources!': [ # these are used everywhere but windows '../src/ports/SkDebug_stdio.cpp', - '../src/ports/SkTime_Unix.cpp', '../src/ports/SkOSFile_posix.cpp', + '../src/ports/SkThread_pthread.cpp', + '../src/ports/SkTime_Unix.cpp', + '../src/ports/SkTLS_pthread.cpp', ], }, { # else !win 'sources!': [ @@ -152,6 +154,7 @@ '../src/ports/SkOSFile_win.cpp', '../src/ports/SkThread_win.cpp', '../src/ports/SkTime_win.cpp', + '../src/ports/SkTLS_win.cpp', ], }], [ 'skia_os == "android"', { diff --git a/src/ports/SkTLS_none.cpp b/src/ports/SkTLS_none.cpp new file mode 100644 index 0000000000..95f6e37037 --- /dev/null +++ b/src/ports/SkTLS_none.cpp @@ -0,0 +1,18 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTLS.h" + +static void* gSpecific = NULL; + +void* SkTLS::PlatformGetSpecific(bool) { + return gSpecific; +} + +void SkTLS::PlatformSetSpecific(void* ptr) { + gSpecific = ptr; +} diff --git a/src/ports/SkTLS_pthread.cpp b/src/ports/SkTLS_pthread.cpp new file mode 100644 index 0000000000..4264890217 --- /dev/null +++ b/src/ports/SkTLS_pthread.cpp @@ -0,0 +1,30 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTLS.h" + +#include <pthread.h> + +static pthread_key_t gSkTLSKey; +static pthread_once_t gSkTLSKey_Once = PTHREAD_ONCE_INIT; + +static void sk_tls_make_key() { + (void)pthread_key_create(&gSkTLSKey, SkTLS::Destructor); +} + +void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) { + // should we use forceCreateTheSlot to potentially skip calling pthread_once + // and just return NULL if we've never been called with + // forceCreateTheSlot==true ? + + (void)pthread_once(&gSkTLSKey_Once, sk_tls_make_key); + return pthread_getspecific(gSkTLSKey); +} + +void SkTLS::PlatformSetSpecific(void* ptr) { + (void)pthread_setspecific(gSkTLSKey, ptr); +} diff --git a/src/ports/SkTLS_win.cpp b/src/ports/SkTLS_win.cpp new file mode 100644 index 0000000000..dea8cd4d6b --- /dev/null +++ b/src/ports/SkTLS_win.cpp @@ -0,0 +1,75 @@ +/* + * Copyright 2013 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SkTLS.h" +#include "SkThread.h" + +static bool gOnce = false; +static DWORD gTlsIndex; +SK_DECLARE_STATIC_MUTEX(gMutex); + +void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) { + if (!forceCreateTheSlot && !gOnce) { + return NULL; + } + + if (!gOnce) { + SkAutoMutexAcquire tmp(gMutex); + if (!gOnce) { + gTlsIndex = TlsAlloc(); + gOnce = true; + } + } + return TlsGetValue(gTlsIndex); +} + +void SkTLS::PlatformSetSpecific(void* ptr) { + SkASSERT(gOnce); + (void)TlsSetValue(gTlsIndex, ptr); +} + +// Call TLS destructors on thread exit. Code based on Chromium's +// base/threading/thread_local_storage_win.cc +#ifdef _WIN64 + +#pragma comment(linker, "/INCLUDE:_tls_used") +#pragma comment(linker, "/INCLUDE:skia_tls_callback") + +#else + +#pragma comment(linker, "/INCLUDE:__tls_used") +#pragma comment(linker, "/INCLUDE:_skia_tls_callback") + +#endif + +void NTAPI onTLSCallback(PVOID unused, DWORD reason, PVOID unused2) { + if ((DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason) && gOnce) { + void* ptr = TlsGetValue(gTlsIndex); + if (ptr != NULL) { + SkTLS::Destructor(ptr); + TlsSetValue(gTlsIndex, NULL); + } + } +} + +extern "C" { + +#ifdef _WIN64 + +#pragma const_seg(".CRT$XLB") +extern const PIMAGE_TLS_CALLBACK skia_tls_callback; +const PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback; +#pragma const_seg() + +#else + +#pragma data_seg(".CRT$XLB") +PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback; +#pragma data_seg() + +#endif +} diff --git a/src/ports/SkThread_none.cpp b/src/ports/SkThread_none.cpp index 1122c959d3..638d7d017d 100644 --- a/src/ports/SkThread_none.cpp +++ b/src/ports/SkThread_none.cpp @@ -1,4 +1,3 @@ - /* * Copyright 2006 The Android Open Source Project * @@ -6,9 +5,7 @@ * found in the LICENSE file. */ - #include "SkThread.h" -#include "SkTLS.h" int32_t sk_atomic_inc(int32_t* addr) { int32_t value = *addr; @@ -44,15 +41,3 @@ SkMutex::~SkMutex() {} void SkMutex::acquire() {} void SkMutex::release() {} #endif - -////////////////////////////////////////////////////////////////////////// - -static void* gSpecific; - -void* SkTLS::PlatformGetSpecific(bool) { - return gSpecific; -} - -void SkTLS::PlatformSetSpecific(void* ptr) { - gSpecific = ptr; -} diff --git a/src/ports/SkThread_pthread.cpp b/src/ports/SkThread_pthread.cpp index 370f673d86..a78c7b2f14 100644 --- a/src/ports/SkThread_pthread.cpp +++ b/src/ports/SkThread_pthread.cpp @@ -6,7 +6,6 @@ * found in the LICENSE file. */ #include "SkThread.h" -#include "SkTLS.h" #include <pthread.h> #include <errno.h> @@ -196,25 +195,3 @@ void SkMutex::release() { } #endif // !SK_USE_POSIX_THREADS - -/////////////////////////////////////////////////////////////////////////////// - -static pthread_key_t gSkTLSKey; -static pthread_once_t gSkTLSKey_Once = PTHREAD_ONCE_INIT; - -static void sk_tls_make_key() { - (void)pthread_key_create(&gSkTLSKey, SkTLS::Destructor); -} - -void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) { - // should we use forceCreateTheSlot to potentially skip calling pthread_once - // and just return NULL if we've never been called with - // forceCreateTheSlot==true ? - - (void)pthread_once(&gSkTLSKey_Once, sk_tls_make_key); - return pthread_getspecific(gSkTLSKey); -} - -void SkTLS::PlatformSetSpecific(void* ptr) { - (void)pthread_setspecific(gSkTLSKey, ptr); -} diff --git a/src/ports/SkThread_win.cpp b/src/ports/SkThread_win.cpp index c69fc4506d..708db24ab9 100644 --- a/src/ports/SkThread_win.cpp +++ b/src/ports/SkThread_win.cpp @@ -1,4 +1,3 @@ - /* * Copyright 2008 The Android Open Source Project * @@ -6,11 +5,9 @@ * found in the LICENSE file. */ - #include <windows.h> #include <intrin.h> #include "SkThread.h" -#include "SkTLS.h" //MSDN says in order to declare an interlocked function for use as an //intrinsic, include intrin.h and put the function in a #pragma intrinsic @@ -66,71 +63,3 @@ void SkMutex::acquire() { void SkMutex::release() { LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); } - -/////////////////////////////////////////////////////////////////////////// - -static bool gOnce; -static DWORD gTlsIndex; -SK_DECLARE_STATIC_MUTEX(gMutex); - -void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) { - if (!forceCreateTheSlot && !gOnce) { - return NULL; - } - - if (!gOnce) { - SkAutoMutexAcquire tmp(gMutex); - if (!gOnce) { - gTlsIndex = TlsAlloc(); - gOnce = true; - } - } - return TlsGetValue(gTlsIndex); -} - -void SkTLS::PlatformSetSpecific(void* ptr) { - SkASSERT(gOnce); - (void)TlsSetValue(gTlsIndex, ptr); -} - -// Call TLS destructors on thread exit. Code based on Chromium's -// base/threading/thread_local_storage_win.cc -#ifdef _WIN64 - -#pragma comment(linker, "/INCLUDE:_tls_used") -#pragma comment(linker, "/INCLUDE:skia_tls_callback") - -#else - -#pragma comment(linker, "/INCLUDE:__tls_used") -#pragma comment(linker, "/INCLUDE:_skia_tls_callback") - -#endif - -void NTAPI onTLSCallback(PVOID unused, DWORD reason, PVOID unused2) { - if ((DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason) && gOnce) { - void* ptr = TlsGetValue(gTlsIndex); - if (ptr != NULL) { - SkTLS::Destructor(ptr); - TlsSetValue(gTlsIndex, NULL); - } - } -} - -extern "C" { - -#ifdef _WIN64 - -#pragma const_seg(".CRT$XLB") -extern const PIMAGE_TLS_CALLBACK skia_tls_callback; -const PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback; -#pragma const_seg() - -#else - -#pragma data_seg(".CRT$XLB") -PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback; -#pragma data_seg() - -#endif -} |