epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2008 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include <windows.h> |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 11 | #include <intrin.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | #include "SkThread.h" |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 13 | #include "SkTLS.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 15 | //MSDN says in order to declare an interlocked function for use as an |
| 16 | //intrinsic, include intrin.h and put the function in a #pragma intrinsic |
| 17 | //directive. |
| 18 | //The pragma appears to be unnecessary, but doesn't hurt. |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 19 | #pragma intrinsic(_InterlockedIncrement, _InterlockedExchangeAdd, _InterlockedDecrement) |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 20 | #pragma intrinsic(_InterlockedCompareExchange) |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 21 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 22 | int32_t sk_atomic_inc(int32_t* addr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | // InterlockedIncrement returns the new value, we want to return the old. |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 24 | return _InterlockedIncrement(reinterpret_cast<LONG*>(addr)) - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | } |
| 26 | |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 27 | int32_t sk_atomic_add(int32_t* addr, int32_t inc) { |
| 28 | return _InterlockedExchangeAdd(reinterpret_cast<LONG*>(addr), |
| 29 | static_cast<LONG>(inc)); |
| 30 | } |
| 31 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 32 | int32_t sk_atomic_dec(int32_t* addr) { |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 33 | return _InterlockedDecrement(reinterpret_cast<LONG*>(addr)) + 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 35 | void sk_membar_aquire__after_atomic_dec() { } |
| 36 | |
| 37 | int32_t sk_atomic_conditional_inc(int32_t* addr) { |
| 38 | while (true) { |
| 39 | LONG value = static_cast<LONG const volatile&>(*addr); |
| 40 | if (value == 0) { |
| 41 | return 0; |
| 42 | } |
| 43 | if (_InterlockedCompareExchange(reinterpret_cast<LONG*>(addr), |
| 44 | value + 1, |
| 45 | value) == value) { |
| 46 | return value; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | void sk_membar_aquire__after_atomic_conditional_inc() { } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 52 | SkMutex::SkMutex() { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 53 | SK_COMPILE_ASSERT(sizeof(fStorage) > sizeof(CRITICAL_SECTION), |
| 54 | NotEnoughSizeForCriticalSection); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | InitializeCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 56 | } |
| 57 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 58 | SkMutex::~SkMutex() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | DeleteCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 60 | } |
| 61 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 62 | void SkMutex::acquire() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | EnterCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 64 | } |
| 65 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 66 | void SkMutex::release() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 68 | } |
| 69 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | static bool gOnce; |
| 73 | static DWORD gTlsIndex; |
| 74 | SK_DECLARE_STATIC_MUTEX(gMutex); |
| 75 | |
reed@google.com | 331e2dc | 2012-05-08 21:45:03 +0000 | [diff] [blame] | 76 | void* SkTLS::PlatformGetSpecific(bool forceCreateTheSlot) { |
| 77 | if (!forceCreateTheSlot && !gOnce) { |
| 78 | return NULL; |
| 79 | } |
| 80 | |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 81 | if (!gOnce) { |
| 82 | SkAutoMutexAcquire tmp(gMutex); |
reed@google.com | 331e2dc | 2012-05-08 21:45:03 +0000 | [diff] [blame] | 83 | if (!gOnce) { |
| 84 | gTlsIndex = TlsAlloc(); |
| 85 | gOnce = true; |
| 86 | } |
reed@google.com | 48ca7e3 | 2012-05-07 20:23:27 +0000 | [diff] [blame] | 87 | } |
| 88 | return TlsGetValue(gTlsIndex); |
| 89 | } |
| 90 | |
| 91 | void SkTLS::PlatformSetSpecific(void* ptr) { |
| 92 | SkASSERT(gOnce); |
| 93 | (void)TlsSetValue(gTlsIndex, ptr); |
| 94 | } |
| 95 | |