Add a skia method to perform an atomic add.
Complements sk_atomic_inc for when you want to increase by more than one. This time, use the correct atomic add function on Windows. Reviewed at https://codereview.appspot.com/6399050/ Review URL: https://codereview.appspot.com/6407048 git-svn-id: http://skia.googlecode.com/svn/trunk@4623 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
parent
70d1be5f94
commit
50ccb0a738
@ -18,6 +18,7 @@
|
||||
'sources': [
|
||||
'../tests/AAClipTest.cpp',
|
||||
'../tests/AnnotationTest.cpp',
|
||||
'../tests/AtomicTest.cpp',
|
||||
'../tests/BitmapCopyTest.cpp',
|
||||
'../tests/BitmapGetColorTest.cpp',
|
||||
'../tests/BitSetTest.cpp',
|
||||
|
@ -16,6 +16,7 @@
|
||||
/****** SkThread_platform needs to define the following...
|
||||
|
||||
int32_t sk_atomic_inc(int32_t*);
|
||||
int32_t sk_atomic_add(int32_t*, int32_t);
|
||||
int32_t sk_atomic_dec(int32_t*);
|
||||
int32_t sk_atomic_conditional_inc(int32_t*);
|
||||
|
||||
|
@ -23,6 +23,10 @@ static inline __attribute__((always_inline)) int32_t sk_atomic_inc(int32_t *addr
|
||||
return __sync_fetch_and_add(addr, 1);
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) int32_t sk_atomic_add(int32_t *addr, int32_t inc) {
|
||||
return __sync_fetch_and_add(addr, inc);
|
||||
}
|
||||
|
||||
static inline __attribute__((always_inline)) int32_t sk_atomic_dec(int32_t *addr) {
|
||||
return __sync_fetch_and_add(addr, -1);
|
||||
}
|
||||
@ -54,8 +58,9 @@ static inline __attribute__((always_inline)) void sk_membar_aquire__after_atomic
|
||||
*/
|
||||
#include <utils/Atomic.h>
|
||||
|
||||
#define sk_atomic_inc(addr) android_atomic_inc(addr)
|
||||
#define sk_atomic_dec(addr) android_atomic_dec(addr)
|
||||
#define sk_atomic_inc(addr) android_atomic_inc(addr)
|
||||
#define sk_atomic_add(addr, inc) android_atomic_add(inc, addr)
|
||||
#define sk_atomic_dec(addr) android_atomic_dec(addr)
|
||||
void sk_membar_aquire__after_atomic_dec() {
|
||||
//HACK: Android is actually using full memory barriers.
|
||||
// Should this change, uncomment below.
|
||||
@ -92,6 +97,14 @@ void sk_membar_aquire__after_atomic_conditional_inc() {
|
||||
*/
|
||||
SK_API int32_t sk_atomic_inc(int32_t* addr);
|
||||
|
||||
/** Implemented by the porting layer, this function adds inc to the int
|
||||
specified by the address (in a thread-safe manner), and returns the
|
||||
previous value.
|
||||
No additional memory barrier is required.
|
||||
This must act as a compiler barrier.
|
||||
*/
|
||||
SK_API int32_t sk_atomic_add(int32_t* addr, int32_t inc);
|
||||
|
||||
/** Implemented by the porting layer, this function subtracts one from the int
|
||||
specified by the address (in a thread-safe manner), and returns the
|
||||
previous value.
|
||||
|
@ -16,6 +16,12 @@ int32_t sk_atomic_inc(int32_t* addr) {
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t sk_atomic_add(int32_t* addr, int32_t inc) {
|
||||
int32_t value = *addr;
|
||||
*addr = value + inc;
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t sk_atomic_dec(int32_t* addr) {
|
||||
int32_t value = *addr;
|
||||
*addr = value - 1;
|
||||
|
@ -35,6 +35,11 @@ int32_t sk_atomic_inc(int32_t* addr)
|
||||
return __sync_fetch_and_add(addr, 1);
|
||||
}
|
||||
|
||||
int32_t sk_atomic_add(int32_t* addr, int32_t inc)
|
||||
{
|
||||
return __sync_fetch_and_add(addr, inc);
|
||||
}
|
||||
|
||||
int32_t sk_atomic_dec(int32_t* addr)
|
||||
{
|
||||
return __sync_fetch_and_add(addr, -1);
|
||||
@ -74,6 +79,15 @@ int32_t sk_atomic_inc(int32_t* addr)
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t sk_atomic_add(int32_t* addr, int32_t inc)
|
||||
{
|
||||
SkAutoMutexAcquire ac(gAtomicMutex);
|
||||
|
||||
int32_t value = *addr;
|
||||
*addr = value + inc;
|
||||
return value;
|
||||
}
|
||||
|
||||
int32_t sk_atomic_dec(int32_t* addr)
|
||||
{
|
||||
SkAutoMutexAcquire ac(gAtomicMutex);
|
||||
|
@ -16,7 +16,7 @@
|
||||
//intrinsic, include intrin.h and put the function in a #pragma intrinsic
|
||||
//directive.
|
||||
//The pragma appears to be unnecessary, but doesn't hurt.
|
||||
#pragma intrinsic(_InterlockedIncrement, _InterlockedDecrement)
|
||||
#pragma intrinsic(_InterlockedIncrement, _InterlockedExchangeAdd, _InterlockedDecrement)
|
||||
#pragma intrinsic(_InterlockedCompareExchange)
|
||||
|
||||
int32_t sk_atomic_inc(int32_t* addr) {
|
||||
@ -24,6 +24,11 @@ int32_t sk_atomic_inc(int32_t* addr) {
|
||||
return _InterlockedIncrement(reinterpret_cast<LONG*>(addr)) - 1;
|
||||
}
|
||||
|
||||
int32_t sk_atomic_add(int32_t* addr, int32_t inc) {
|
||||
return _InterlockedExchangeAdd(reinterpret_cast<LONG*>(addr),
|
||||
static_cast<LONG>(inc));
|
||||
}
|
||||
|
||||
int32_t sk_atomic_dec(int32_t* addr) {
|
||||
return _InterlockedDecrement(reinterpret_cast<LONG*>(addr)) + 1;
|
||||
}
|
||||
|
61
tests/AtomicTest.cpp
Normal file
61
tests/AtomicTest.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright 2012 Google Inc.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "SkThread.h"
|
||||
#include "SkThreadUtils.h"
|
||||
#include "SkTypes.h"
|
||||
#include "Test.h"
|
||||
|
||||
struct AddInfo {
|
||||
int32_t valueToAdd;
|
||||
int timesToAdd;
|
||||
unsigned int processorAffinity;
|
||||
};
|
||||
|
||||
static int32_t base = 0;
|
||||
|
||||
static AddInfo gAdds[] = {
|
||||
{ 3, 100, 23 },
|
||||
{ 2, 200, 2 },
|
||||
{ 7, 150, 17 },
|
||||
};
|
||||
|
||||
static void addABunchOfTimes(void* data) {
|
||||
AddInfo* addInfo = static_cast<AddInfo*>(data);
|
||||
for (int i = 0; i < addInfo->timesToAdd; i++) {
|
||||
sk_atomic_add(&base, addInfo->valueToAdd);
|
||||
}
|
||||
}
|
||||
|
||||
static void test_atomicAddTests(skiatest::Reporter* reporter) {
|
||||
int32_t total = base;
|
||||
SkThread* threads[SK_ARRAY_COUNT(gAdds)];
|
||||
for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
|
||||
total += gAdds[i].valueToAdd * gAdds[i].timesToAdd;
|
||||
}
|
||||
// Start the threads
|
||||
for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
|
||||
threads[i] = new SkThread(addABunchOfTimes, &gAdds[i]);
|
||||
threads[i]->setProcessorAffinity(gAdds[i].processorAffinity);
|
||||
threads[i]->start();
|
||||
}
|
||||
|
||||
// Now end the threads
|
||||
for (size_t i = 0; i < SK_ARRAY_COUNT(gAdds); i++) {
|
||||
threads[i]->join();
|
||||
delete threads[i];
|
||||
}
|
||||
REPORTER_ASSERT(reporter, total == base);
|
||||
// Ensure that the returned value from sk_atomic_add is correct.
|
||||
int32_t valueToModify = 3;
|
||||
const int32_t originalValue = valueToModify;
|
||||
REPORTER_ASSERT(reporter, originalValue == sk_atomic_add(&valueToModify, 7));
|
||||
}
|
||||
|
||||
#include "TestClassDef.h"
|
||||
DEFINE_TESTCLASS("AtomicAdd", AtomicAddTestClass, test_atomicAddTests)
|
||||
|
Loading…
Reference in New Issue
Block a user