| OLD | NEW |
| 1 | |
| 2 /* | 1 /* |
| 3 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
| 4 * | 3 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 7 */ | 6 */ |
| 8 | 7 |
| 9 | |
| 10 #ifndef SkThread_DEFINED | 8 #ifndef SkThread_DEFINED |
| 11 #define SkThread_DEFINED | 9 #define SkThread_DEFINED |
| 12 | 10 |
| 13 #include "SkTypes.h" | 11 #include "SkTypes.h" |
| 14 #include "SkThread_platform.h" | |
| 15 | 12 |
| 16 /****** SkThread_platform needs to define the following... | 13 // SK_ATOMICS_PLATFORM_H must provide inline implementations for the following d
eclarations. |
| 17 | 14 |
| 18 int32_t sk_atomic_inc(int32_t*); | 15 /** Atomically adds one to the int referenced by addr and returns the previous v
alue. |
| 19 int32_t sk_atomic_add(int32_t*, int32_t); | 16 * No additional memory barrier is required; this must act as a compiler barrie
r. |
| 20 int32_t sk_atomic_dec(int32_t*); | 17 */ |
| 21 int32_t sk_atomic_conditional_inc(int32_t*); | 18 static int32_t sk_atomic_inc(int32_t* addr); |
| 22 | 19 |
| 23 class SkMutex { | 20 /** Atomically adds inc to the int referenced by addr and returns the previous v
alue. |
| 21 * No additional memory barrier is required; this must act as a compiler barrie
r. |
| 22 */ |
| 23 static int32_t sk_atomic_add(int32_t* addr, int32_t inc); |
| 24 |
| 25 /** Atomically subtracts one from the int referenced by addr and returns the pre
vious value. |
| 26 * This must act as a release (SL/S) memory barrier and as a compiler barrier. |
| 27 */ |
| 28 static int32_t sk_atomic_dec(int32_t* addr); |
| 29 |
| 30 /** Atomically adds one to the int referenced by addr iff the referenced int was
not 0 |
| 31 * and returns the previous value. |
| 32 * No additional memory barrier is required; this must act as a compiler barrie
r. |
| 33 */ |
| 34 static int32_t sk_atomic_conditional_inc(int32_t* addr); |
| 35 |
| 36 /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, |
| 37 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
| 38 */ |
| 39 static void sk_membar_acquire__after_atomic_dec(); |
| 40 |
| 41 /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, |
| 42 * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. |
| 43 */ |
| 44 static void sk_membar_acquire__after_atomic_conditional_inc(); |
| 45 |
| 46 #include SK_ATOMICS_PLATFORM_H |
| 47 |
| 48 |
| 49 /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. |
| 50 |
| 51 class SkBaseMutex { |
| 52 public: |
| 53 void acquire(); |
| 54 void release(); |
| 55 }; |
| 56 |
| 57 class SkMutex : SkBaseMutex { |
| 24 public: | 58 public: |
| 25 SkMutex(); | 59 SkMutex(); |
| 26 ~SkMutex(); | 60 ~SkMutex(); |
| 27 | |
| 28 void acquire(); | |
| 29 void release(); | |
| 30 }; | 61 }; |
| 31 | 62 |
| 32 ****************/ | 63 #define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = ... |
| 64 #define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = ... |
| 65 */ |
| 66 |
| 67 #include SK_MUTEX_PLATFORM_H |
| 68 |
| 33 | 69 |
| 34 class SkAutoMutexAcquire : SkNoncopyable { | 70 class SkAutoMutexAcquire : SkNoncopyable { |
| 35 public: | 71 public: |
| 36 explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) { | 72 explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) { |
| 37 SkASSERT(fMutex != NULL); | 73 SkASSERT(fMutex != NULL); |
| 38 mutex.acquire(); | 74 mutex.acquire(); |
| 39 } | 75 } |
| 40 | 76 |
| 41 SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) { | 77 explicit SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) { |
| 42 if (mutex) { | 78 if (mutex) { |
| 43 mutex->acquire(); | 79 mutex->acquire(); |
| 44 } | 80 } |
| 45 } | 81 } |
| 46 | 82 |
| 47 /** If the mutex has not been release, release it now. | 83 /** If the mutex has not been released, release it now. */ |
| 48 */ | |
| 49 ~SkAutoMutexAcquire() { | 84 ~SkAutoMutexAcquire() { |
| 50 if (fMutex) { | 85 if (fMutex) { |
| 51 fMutex->release(); | 86 fMutex->release(); |
| 52 } | 87 } |
| 53 } | 88 } |
| 54 | 89 |
| 55 /** If the mutex has not been release, release it now. | 90 /** If the mutex has not been released, release it now. */ |
| 56 */ | |
| 57 void release() { | 91 void release() { |
| 58 if (fMutex) { | 92 if (fMutex) { |
| 59 fMutex->release(); | 93 fMutex->release(); |
| 60 fMutex = NULL; | 94 fMutex = NULL; |
| 61 } | 95 } |
| 62 } | 96 } |
| 63 | 97 |
| 64 private: | 98 private: |
| 65 SkBaseMutex* fMutex; | 99 SkBaseMutex* fMutex; |
| 66 }; | 100 }; |
| 67 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) | 101 #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) |
| 68 | 102 |
| 69 #endif | 103 #endif |
| OLD | NEW |