rx: Add the add_and_read function to our atomics
authorSimon Wilkinson <sxw@your-file-system.com>
Mon, 9 Jul 2012 09:27:17 +0000 (10:27 +0100)
committerDerrick Brashear <shadow@dementix.org>
Tue, 14 Aug 2012 13:51:43 +0000 (06:51 -0700)
Add rx_atomic_add_and_read, which lets us get the new value out
of an atomic addition operation.

Change-Id: If74b1357390329b8c59042d3154491a103fe0d6c
Reviewed-on: http://gerrit.openafs.org/7974
Tested-by: BuildBot <buildbot@rampaginggeek.com>
Reviewed-by: Derrick Brashear <shadow@dementix.org>

src/rx/rx_atomic.h

index 028fbb2..51e2f76 100644 (file)
@@ -57,6 +57,11 @@ rx_atomic_add(rx_atomic_t *atomic, int change) {
     InterlockedExchangeAdd(&atomic->var, change);
 }
 
+static_inline int
+rx_atomic_add_and_read(rx_atomic_t *atomic, int change) {
+    return InterlockedExchangeAdd(&atomic->var, change) + change;
+}
+
 static_inline void
 rx_atomic_dec(rx_atomic_t *atomic) {
     InterlockedDecrement(&atomic->var);
@@ -110,6 +115,11 @@ rx_atomic_add(rx_atomic_t *atomic, int change) {
     OSAtomicAdd32(change, &atomic->var);
 }
 
+static_inline int
+rx_atomic_add_and_read(rx_atomic_t *atomic, int change) {
+    return OSAtomicAdd32(change, &atomic->var);
+}
+
 static_inline void
 rx_atomic_dec(rx_atomic_t *atomic) {
     OSAtomicDecrement32(&atomic->var);
@@ -134,6 +144,7 @@ typedef atomic_t rx_atomic_t;
 #define rx_atomic_inc(X)         atomic_inc(X)
 #define rx_atomic_inc_and_read(X) atomic_inc_return(X)
 #define rx_atomic_add(X, V)      atomic_add(V, X)
+#define rx_atomic_add_and_read(X, V) atomic_add_return(V, X);
 #define rx_atomic_dec(X)         atomic_dec(X)
 #define rx_atomic_dec_and_read(X) atomic_dec_return(X)
 #define rx_atomic_sub(X, V)      atomic_sub(V, X)
@@ -182,6 +193,11 @@ rx_atomic_add(rx_atomic_t *atomic, int change) {
     atomic_add_32(&atomic->var, change);
 }
 
+static_inline int
+rx_atomic_add_and_read(rx_atomic_t *atomic, int change) {
+    return atomic_add_32_nv(&atomic->var, change);
+}
+
 static_inline void
 rx_atomic_dec(rx_atomic_t *atomic) {
     atomic_dec_32(&atomic->var);
@@ -228,6 +244,11 @@ rx_atomic_add(rx_atomic_t *atomic, int change) {
     (void)__sync_fetch_and_add(&atomic->var, change);
 }
 
+static_inline int
+rx_atomic_add_and_read(rx_atomic_t *atomic, int change) {
+    return __sync_fetch_and_add(&atomic->var, change);
+}
+
 static_inline void
 rx_atomic_dec(rx_atomic_t *atomic) {
     (void)__sync_fetch_and_sub(&atomic->var, 1);
@@ -300,6 +321,18 @@ rx_atomic_add(rx_atomic_t *atomic, int change) {
     MUTEX_EXIT(&rx_atomic_mutex);
 }
 
+static_inline int
+rx_atomic_add_and_read(rx_atomic_t *atomic, int change) {
+    int retval;
+
+    MUTEX_ENTER(&rx_atomic_mutex);
+    atomic->var += change;
+    retval = atomic->var;
+    MUTEX_EXIT(&rx_atomic_mutex);
+
+    return retval;
+}
+
 static_inline void
 rx_atomic_dec(rx_atomic_t *atomic) {
     MUTEX_ENTER(&rx_atomic_mutex);