gfx/skia/trunk/src/ports/SkTLS_win.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/ports/SkTLS_win.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkTLS.h"
    1.12 +#include "SkThread.h"
    1.13 +
    1.14 +static bool gOnce = false;
    1.15 +static DWORD gTlsIndex;
    1.16 +SK_DECLARE_STATIC_MUTEX(gMutex);
    1.17 +
    1.18 +void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) {
    1.19 +    if (!forceCreateTheSlot && !gOnce) {
    1.20 +        return NULL;
    1.21 +    }
    1.22 +
    1.23 +    if (!gOnce) {
    1.24 +        SkAutoMutexAcquire tmp(gMutex);
    1.25 +        if (!gOnce) {
    1.26 +            gTlsIndex = TlsAlloc();
    1.27 +            gOnce = true;
    1.28 +        }
    1.29 +    }
    1.30 +    return TlsGetValue(gTlsIndex);
    1.31 +}
    1.32 +
    1.33 +void SkTLS::PlatformSetSpecific(void* ptr) {
    1.34 +    SkASSERT(gOnce);
    1.35 +    (void)TlsSetValue(gTlsIndex, ptr);
    1.36 +}
    1.37 +
    1.38 +// Call TLS destructors on thread exit. Code based on Chromium's
    1.39 +// base/threading/thread_local_storage_win.cc
    1.40 +#ifdef _WIN64
    1.41 +
    1.42 +#pragma comment(linker, "/INCLUDE:_tls_used")
    1.43 +#pragma comment(linker, "/INCLUDE:skia_tls_callback")
    1.44 +
    1.45 +#else
    1.46 +
    1.47 +#pragma comment(linker, "/INCLUDE:__tls_used")
    1.48 +#pragma comment(linker, "/INCLUDE:_skia_tls_callback")
    1.49 +
    1.50 +#endif
    1.51 +
    1.52 +void NTAPI onTLSCallback(PVOID unused, DWORD reason, PVOID unused2) {
    1.53 +    if ((DLL_THREAD_DETACH == reason || DLL_PROCESS_DETACH == reason) && gOnce) {
    1.54 +        void* ptr = TlsGetValue(gTlsIndex);
    1.55 +        if (ptr != NULL) {
    1.56 +            SkTLS::Destructor(ptr);
    1.57 +            TlsSetValue(gTlsIndex, NULL);
    1.58 +        }
    1.59 +    }
    1.60 +}
    1.61 +
    1.62 +extern "C" {
    1.63 +
    1.64 +#ifdef _WIN64
    1.65 +
    1.66 +#pragma const_seg(".CRT$XLB")
    1.67 +extern const PIMAGE_TLS_CALLBACK skia_tls_callback;
    1.68 +const PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback;
    1.69 +#pragma const_seg()
    1.70 +
    1.71 +#else
    1.72 +
    1.73 +#pragma data_seg(".CRT$XLB")
    1.74 +PIMAGE_TLS_CALLBACK skia_tls_callback = onTLSCallback;
    1.75 +#pragma data_seg()
    1.76 +
    1.77 +#endif
    1.78 +}

mercurial