aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/ports
diff options
context:
space:
mode:
authorGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-07-16 13:13:29 +0000
committerGravatar scroggo@google.com <scroggo@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>2012-07-16 13:13:29 +0000
commite3f84f3911d6ab1c99030fef3200199755251d51 (patch)
tree6b49e6c64b5ee84be0e3c032062eb14f7fb0fa90 /src/ports
parentfde2c0af2fd5aae19ab6c8b5228debd5b6209856 (diff)
Add a skia method to perform an atomic add.
Complements sk_atomic_inc for when you want to increase by more than one. Review URL: https://codereview.appspot.com/6350106 git-svn-id: http://skia.googlecode.com/svn/trunk@4614 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/ports')
-rw-r--r--src/ports/SkThread_none.cpp6
-rw-r--r--src/ports/SkThread_pthread.cpp14
-rw-r--r--src/ports/SkThread_win.cpp8
3 files changed, 27 insertions, 1 deletions
diff --git a/src/ports/SkThread_none.cpp b/src/ports/SkThread_none.cpp
index 56bbbae3b3..1122c959d3 100644
--- a/src/ports/SkThread_none.cpp
+++ b/src/ports/SkThread_none.cpp
@@ -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;
diff --git a/src/ports/SkThread_pthread.cpp b/src/ports/SkThread_pthread.cpp
index d0bb3acbad..a1c7b24f66 100644
--- a/src/ports/SkThread_pthread.cpp
+++ b/src/ports/SkThread_pthread.cpp
@@ -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 value)
+{
+ return __sync_fetch_and_add(addr, value);
+}
+
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);
diff --git a/src/ports/SkThread_win.cpp b/src/ports/SkThread_win.cpp
index e833314843..7d091d2a67 100644
--- a/src/ports/SkThread_win.cpp
+++ b/src/ports/SkThread_win.cpp
@@ -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, _InterlockedAdd, _InterlockedDecrement)
#pragma intrinsic(_InterlockedCompareExchange)
int32_t sk_atomic_inc(int32_t* addr) {
@@ -24,6 +24,12 @@ 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) {
+ // InterlockedAdd returns the new value, we want to return the old.
+ LONG value = reinterpret_cast<LONG>(inc);
+ return _InterlockedAdd(reinterpret_cast<LONG*>(addr), value) - value;
+}
+
int32_t sk_atomic_dec(int32_t* addr) {
return _InterlockedDecrement(reinterpret_cast<LONG*>(addr)) + 1;
}