2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkThread_DEFINED
|
|
|
|
#define SkThread_DEFINED
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
// SK_ATOMICS_PLATFORM_H must provide inline implementations for the following declarations.
|
|
|
|
|
|
|
|
/** Atomically adds one to the int referenced by addr and returns the previous value.
|
|
|
|
* No additional memory barrier is required; this must act as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static int32_t sk_atomic_inc(int32_t* addr);
|
|
|
|
|
|
|
|
/** Atomically adds inc to the int referenced by addr and returns the previous value.
|
|
|
|
* No additional memory barrier is required; this must act as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static int32_t sk_atomic_add(int32_t* addr, int32_t inc);
|
|
|
|
|
|
|
|
/** Atomically subtracts one from the int referenced by addr and returns the previous value.
|
|
|
|
* This must act as a release (SL/S) memory barrier and as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static int32_t sk_atomic_dec(int32_t* addr);
|
|
|
|
|
2014-01-08 21:15:56 +00:00
|
|
|
/** Atomic compare and set.
|
|
|
|
* If *addr == before, set *addr to after and return true, otherwise return false.
|
|
|
|
* This must act as a release (SL/S) memory barrier and as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static bool sk_atomic_cas(int32_t* addr, int32_t before, int32_t after);
|
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
/** If sk_atomic_dec does not act as an acquire (L/SL) barrier,
|
|
|
|
* this must act as an acquire (L/SL) memory barrier and as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static void sk_membar_acquire__after_atomic_dec();
|
|
|
|
|
|
|
|
/** If sk_atomic_conditional_inc does not act as an acquire (L/SL) barrier,
|
|
|
|
* this must act as an acquire (L/SL) memory barrier and as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static void sk_membar_acquire__after_atomic_conditional_inc();
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
#include SK_ATOMICS_PLATFORM_H
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-05-27 15:00:32 +00:00
|
|
|
/** Atomically adds one to the int referenced by addr iff the referenced int was not 0
|
|
|
|
* and returns the previous value.
|
|
|
|
* No additional memory barrier is required; this must act as a compiler barrier.
|
|
|
|
*/
|
|
|
|
static inline int32_t sk_atomic_conditional_inc(int32_t* addr) {
|
|
|
|
int32_t prev;
|
|
|
|
do {
|
|
|
|
prev = *addr;
|
|
|
|
if (0 == prev) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (!sk_atomic_cas(addr, prev, prev+1));
|
|
|
|
return prev;
|
|
|
|
}
|
|
|
|
|
2014-05-29 18:24:54 +00:00
|
|
|
// SK_BARRIERS_PLATFORM_H must provide implementations for the following declarations:
|
|
|
|
|
|
|
|
/** Prevent the compiler from reordering across this barrier. */
|
|
|
|
static void sk_compiler_barrier();
|
|
|
|
|
|
|
|
/** Read T*, with at least an acquire barrier.
|
|
|
|
*
|
|
|
|
* Only needs to be implemented for T which can be atomically read.
|
|
|
|
*/
|
|
|
|
template <typename T> T sk_acquire_load(T*);
|
|
|
|
|
|
|
|
/** Write T*, with at least a release barrier.
|
|
|
|
*
|
|
|
|
* Only needs to be implemented for T which can be atomically written.
|
|
|
|
*/
|
|
|
|
template <typename T> void sk_release_store(T*, T);
|
|
|
|
|
|
|
|
#include SK_BARRIERS_PLATFORM_H
|
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
/** SK_MUTEX_PLATFORM_H must provide the following (or equivalent) declarations.
|
|
|
|
|
|
|
|
class SkBaseMutex {
|
|
|
|
public:
|
2014-06-09 21:18:02 +00:00
|
|
|
void acquire(); // Block until this thread owns the mutex.
|
|
|
|
void release(); // Assuming this thread owns the mutex, release it.
|
|
|
|
void assertHeld(); // If SK_DEBUG, assert this thread owns the mutex.
|
2013-12-18 15:27:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkMutex : SkBaseMutex {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
|
|
|
SkMutex();
|
|
|
|
~SkMutex();
|
|
|
|
};
|
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
#define SK_DECLARE_STATIC_MUTEX(name) static SkBaseMutex name = ...
|
|
|
|
#define SK_DECLARE_GLOBAL_MUTEX(name) SkBaseMutex name = ...
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include SK_MUTEX_PLATFORM_H
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
class SkAutoMutexAcquire : SkNoncopyable {
|
|
|
|
public:
|
2012-04-23 16:04:22 +00:00
|
|
|
explicit SkAutoMutexAcquire(SkBaseMutex& mutex) : fMutex(&mutex) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkASSERT(fMutex != NULL);
|
|
|
|
mutex.acquire();
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
explicit SkAutoMutexAcquire(SkBaseMutex* mutex) : fMutex(mutex) {
|
2012-04-23 16:04:22 +00:00
|
|
|
if (mutex) {
|
|
|
|
mutex->acquire();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
/** If the mutex has not been released, release it now. */
|
2012-04-23 16:04:22 +00:00
|
|
|
~SkAutoMutexAcquire() {
|
|
|
|
if (fMutex) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fMutex->release();
|
2012-04-23 16:04:22 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-04-23 16:04:22 +00:00
|
|
|
|
2013-12-18 15:27:39 +00:00
|
|
|
/** If the mutex has not been released, release it now. */
|
2012-04-23 16:04:22 +00:00
|
|
|
void release() {
|
|
|
|
if (fMutex) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fMutex->release();
|
|
|
|
fMutex = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-08-23 18:09:54 +00:00
|
|
|
|
2014-06-09 21:18:02 +00:00
|
|
|
/** Assert that we're holding the mutex. */
|
|
|
|
void assertHeld() {
|
|
|
|
SkASSERT(fMutex);
|
|
|
|
fMutex->assertHeld();
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2012-01-26 21:26:40 +00:00
|
|
|
SkBaseMutex* fMutex;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
2013-11-18 16:03:59 +00:00
|
|
|
#define SkAutoMutexAcquire(...) SK_REQUIRE_LOCAL_VAR(SkAutoMutexAcquire)
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
#endif
|