michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef SkThread_DEFINED michael@0: #define SkThread_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: // SK_ATOMICS_PLATFORM_H must provide inline implementations for the following declarations. michael@0: michael@0: /** Atomically adds one to the int referenced by addr and returns the previous value. michael@0: * No additional memory barrier is required; this must act as a compiler barrier. michael@0: */ michael@0: static int32_t sk_atomic_inc(int32_t* addr); michael@0: michael@0: /** Atomically adds inc to the int referenced by addr and returns the previous value. michael@0: * No additional memory barrier is required; this must act as a compiler barrier. michael@0: */ michael@0: static int32_t sk_atomic_add(int32_t* addr, int32_t inc); michael@0: michael@0: /** Atomically subtracts one from the int referenced by addr and returns the previous value. michael@0: * This must act as a release (SL/S) memory barrier and as a compiler barrier. michael@0: */ michael@0: static int32_t sk_atomic_dec(int32_t* addr); michael@0: michael@0: /** Atomically adds one to the int referenced by addr iff the referenced int was not 0 michael@0: * and returns the previous value. michael@0: * No additional memory barrier is required; this must act as a compiler barrier. michael@0: */ michael@0: static int32_t sk_atomic_conditional_inc(int32_t* addr); michael@0: michael@0: /** Atomic compare and set. michael@0: * If *addr == before, set *addr to after and return true, otherwise return false. michael@0: * This must act as a release (SL/S) memory barrier and as a compiler barrier. michael@0: */ michael@0: static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after); michael@0: michael@0: /** If sk_atomic_dec does not act as an acquire (L/SL) barrier, michael@0: * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. michael@0: */ michael@0: static void sk_membar_acquire__after_atomic_dec(); michael@0: michael@0: /** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier, michael@0: * this must act as an acquire (L/SL) memory barrier and as a compiler barrier. michael@0: */ michael@0: static void sk_membar_acquire__after_atomic_conditional_inc(); michael@0: michael@0: #include SK_ATOMICS_PLATFORM_H michael@0: michael@0: /** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations. michael@0: michael@0: class SkBaseMutex { michael@0: public: michael@0: void acquire(); michael@0: void release(); michael@0: }; michael@0: michael@0: class SkMutex : SkBaseMutex { michael@0: public: michael@0: SkMutex(); michael@0: ~SkMutex(); michael@0: }; michael@0: michael@0: #define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = ... michael@0: #define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = ... michael@0: */ michael@0: michael@0: #include SK_MUTEX_PLATFORM_H michael@0: michael@0: michael@0: class SkAutoMutexAcquire : SkNoncopyable { michael@0: public: michael@0: explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) { michael@0: SkASSERT(fMutex != NULL); michael@0: mutex.acquire(); michael@0: } michael@0: michael@0: explicit SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) { michael@0: if (mutex) { michael@0: mutex->acquire(); michael@0: } michael@0: } michael@0: michael@0: /** If the mutex has not been released, release it now. */ michael@0: ~SkAutoMutexAcquire() { michael@0: if (fMutex) { michael@0: fMutex->release(); michael@0: } michael@0: } michael@0: michael@0: /** If the mutex has not been released, release it now. */ michael@0: void release() { michael@0: if (fMutex) { michael@0: fMutex->release(); michael@0: fMutex = NULL; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: SkBaseMutex* fMutex; michael@0: }; michael@0: #define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire) michael@0: michael@0: #endif