epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include <windows.h> |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 9 | #include <intrin.h> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkThread.h" |
| 11 | |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 12 | //MSDN says in order to declare an interlocked function for use as an |
| 13 | //intrinsic, include intrin.h and put the function in a #pragma intrinsic |
| 14 | //directive. |
| 15 | //The pragma appears to be unnecessary, but doesn't hurt. |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 16 | #pragma intrinsic(_InterlockedIncrement, _InterlockedExchangeAdd, _InterlockedDecrement) |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 17 | #pragma intrinsic(_InterlockedCompareExchange) |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 18 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 19 | int32_t sk_atomic_inc(int32_t* addr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | // InterlockedIncrement returns the new value, we want to return the old. |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 21 | return _InterlockedIncrement(reinterpret_cast<LONG*>(addr)) - 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | } |
| 23 | |
scroggo@google.com | 50ccb0a | 2012-07-16 16:51:28 +0000 | [diff] [blame] | 24 | int32_t sk_atomic_add(int32_t* addr, int32_t inc) { |
| 25 | return _InterlockedExchangeAdd(reinterpret_cast<LONG*>(addr), |
| 26 | static_cast<LONG>(inc)); |
| 27 | } |
| 28 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 29 | int32_t sk_atomic_dec(int32_t* addr) { |
bungeman@google.com | eee6661 | 2012-02-17 22:00:07 +0000 | [diff] [blame] | 30 | return _InterlockedDecrement(reinterpret_cast<LONG*>(addr)) + 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | } |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 32 | void sk_membar_aquire__after_atomic_dec() { } |
| 33 | |
| 34 | int32_t sk_atomic_conditional_inc(int32_t* addr) { |
| 35 | while (true) { |
bungeman@google.com | 0afc6bb | 2013-06-20 17:54:26 +0000 | [diff] [blame] | 36 | LONG value = static_cast<int32_t const volatile&>(*addr); |
bungeman@google.com | a02bc15 | 2012-05-16 18:21:56 +0000 | [diff] [blame] | 37 | if (value == 0) { |
| 38 | return 0; |
| 39 | } |
| 40 | if (_InterlockedCompareExchange(reinterpret_cast<LONG*>(addr), |
| 41 | value + 1, |
| 42 | value) == value) { |
| 43 | return value; |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | void sk_membar_aquire__after_atomic_conditional_inc() { } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 49 | SkMutex::SkMutex() { |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 50 | SK_COMPILE_ASSERT(sizeof(fStorage) > sizeof(CRITICAL_SECTION), |
| 51 | NotEnoughSizeForCriticalSection); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | InitializeCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 53 | } |
| 54 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 55 | SkMutex::~SkMutex() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | DeleteCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 57 | } |
| 58 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 59 | void SkMutex::acquire() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | EnterCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 61 | } |
| 62 | |
reed@google.com | dcbd6e3 | 2012-01-12 15:21:16 +0000 | [diff] [blame] | 63 | void SkMutex::release() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | LeaveCriticalSection(reinterpret_cast<CRITICAL_SECTION*>(&fStorage)); |
| 65 | } |