OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "third_party/skia/include/core/SkThread.h" | 5 #include "third_party/skia/include/core/SkThread.h" |
6 | 6 |
7 #include <new> | 7 #include <new> |
8 | 8 |
9 #include "base/atomicops.h" | 9 #include "base/atomicops.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
13 | 13 |
| 14 /** Adds one to the int specified by the address (in a thread-safe manner), and |
| 15 returns the previous value. |
| 16 No additional memory barrier is required. |
| 17 This must act as a compiler barrier. |
| 18 */ |
14 int32_t sk_atomic_inc(int32_t* addr) { | 19 int32_t sk_atomic_inc(int32_t* addr) { |
15 // sk_atomic_inc is expected to return the old value, Barrier_AtomicIncrement | 20 // sk_atomic_inc is expected to return the old value, |
16 // returns the new value. | 21 // Barrier_AtomicIncrement returns the new value. |
17 return base::subtle::Barrier_AtomicIncrement(addr, 1) - 1; | 22 return base::subtle::NoBarrier_AtomicIncrement(addr, 1) - 1; |
18 } | 23 } |
19 | 24 |
| 25 /* Subtracts one from the int specified by the address (in a thread-safe |
| 26 manner), and returns the previous value. |
| 27 Expected to act as a release (SL/S) memory barrier and a compiler barrier. |
| 28 */ |
20 int32_t sk_atomic_dec(int32_t* addr) { | 29 int32_t sk_atomic_dec(int32_t* addr) { |
21 // sk_atomic_dec is expected to return the old value, Barrier_AtomicIncrement | 30 // sk_atomic_dec is expected to return the old value, |
22 // returns the new value. | 31 // Barrier_AtomicIncrement returns the new value. |
23 return base::subtle::Barrier_AtomicIncrement(addr, -1) + 1; | 32 return base::subtle::Barrier_AtomicIncrement(addr, -1) + 1; |
24 } | 33 } |
| 34 /** If sk_atomic_dec does not act as an aquire (L/SL) barrier, this is expected |
| 35 to act as an aquire (L/SL) memory barrier and as a compiler barrier. |
| 36 */ |
| 37 void sk_membar_aquire__after_atomic_dec() { } |
| 38 |
| 39 /** Adds one to the int specified by the address iff the int specified by the |
| 40 address is not zero (in a thread-safe manner), and returns the previous |
| 41 value. |
| 42 No additional memory barrier is required. |
| 43 This must act as a compiler barrier. |
| 44 */ |
| 45 int32_t sk_atomic_conditional_inc(int32_t* addr) { |
| 46 int32_t value = *addr; |
| 47 |
| 48 while (true) { |
| 49 if (value == 0) { |
| 50 return 0; |
| 51 } |
| 52 |
| 53 int32_t before; |
| 54 before = base::subtle::Aquire_CompareAndSwap(addr, value, value + 1); |
| 55 |
| 56 if (before == value) { |
| 57 return value; |
| 58 } else { |
| 59 value = before; |
| 60 } |
| 61 } |
| 62 } |
| 63 /** If sk_atomic_conditional_inc does not act as an aquire (L/SL) barrier, this |
| 64 is expected to act as an aquire (L/SL) memory barrier and as a compiler |
| 65 barrier. |
| 66 */ |
| 67 void sk_membar_aquire__after_atomic_conditional_inc() { } |
25 | 68 |
26 SkMutex::SkMutex() { | 69 SkMutex::SkMutex() { |
27 COMPILE_ASSERT(sizeof(base::Lock) <= sizeof(fStorage), Lock_is_too_big_for_SkM
utex); | 70 COMPILE_ASSERT(sizeof(base::Lock) <= sizeof(fStorage), Lock_is_too_big_for_SkM
utex); |
28 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); | 71 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); |
29 new(lock) base::Lock(); | 72 new(lock) base::Lock(); |
30 } | 73 } |
31 | 74 |
32 SkMutex::~SkMutex() { | 75 SkMutex::~SkMutex() { |
33 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); | 76 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); |
34 lock->~Lock(); | 77 lock->~Lock(); |
35 } | 78 } |
36 | 79 |
37 void SkMutex::acquire() { | 80 void SkMutex::acquire() { |
38 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); | 81 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); |
39 lock->Acquire(); | 82 lock->Acquire(); |
40 } | 83 } |
41 | 84 |
42 void SkMutex::release() { | 85 void SkMutex::release() { |
43 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); | 86 base::Lock* lock = reinterpret_cast<base::Lock*>(fStorage); |
44 lock->Release(); | 87 lock->Release(); |
45 } | 88 } |
OLD | NEW |