blob: 8bc55a0f2cfe9c04bf15fd4015321773b400a31a [file] [log] [blame]
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
mtklein1ee76512015-11-02 10:20:27 -08007#include "SkTypes.h"
8#if defined(SK_BUILD_FOR_WIN32)
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +00009
10#include "SkTLS.h"
mtklein1b249332015-07-07 12:21:21 -070011#include "SkMutex.h"
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +000012
13static bool gOnce = false;
14static DWORD gTlsIndex;
reed086eea92016-05-04 17:12:46 -070015SK_DECLARE_STATIC_MUTEX(gMutex);
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +000016
17void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) {
18 if (!forceCreateTheSlot && !gOnce) {
halcanary96fcdcc2015-08-27 07:41:13 -070019 return nullptr;
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +000020 }
21
22 if (!gOnce) {
23 SkAutoMutexAcquire tmp(gMutex);
24 if (!gOnce) {
25 gTlsIndex = TlsAlloc();
26 gOnce = true;
27 }
28 }
29 return TlsGetValue(gTlsIndex);
30}
31
32void SkTLS::PlatformSetSpecific(void* ptr) {
33 SkASSERT(gOnce);
34 (void)TlsSetValue(gTlsIndex, ptr);
35}
36
37// Call TLS destructors on thread exit. Code based on Chromium's
38// base/threading/thread_local_storage_win.cc
39#ifdef _WIN64
40
41#pragma comment(linker, "/INCLUDE:_tls_used")
42#pragma comment(linker, "/INCLUDE:skia_tls_callback")
43
44#else
45
46#pragma comment(linker, "/INCLUDE:__tls_used")
47#pragma comment(linker, "/INCLUDE:_skia_tls_callback")
48
49#endif
50
51void NTAPI onTLSCallback(PVOID unused, DWORD reason, PVOID unused2) {
52 if ((DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason) && gOnce) {
53 void* ptr = TlsGetValue(gTlsIndex);
halcanary96fcdcc2015-08-27 07:41:13 -070054 if (ptr != nullptr) {
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +000055 SkTLS::Destructor(ptr);
halcanary96fcdcc2015-08-27 07:41:13 -070056 TlsSetValue(gTlsIndex, nullptr);
bungeman@google.comf2e7dbb2013-07-16 14:59:24 +000057 }
58 }
59}
60
61extern "C" {
62
63#ifdef _WIN64
64
65#pragma const_seg(".CRT$XLB")
66extern const PIMAGE_TLS_CALLBACK skia_tls_callback;
67const PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback;
68#pragma const_seg()
69
70#else
71
72#pragma data_seg(".CRT$XLB")
73PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback;
74#pragma data_seg()
75
76#endif
77}
mtklein1ee76512015-11-02 10:20:27 -080078
79#endif//defined(SK_BUILD_FOR_WIN32)