]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
atomic: convert to uatomic
authorDave Chinner <dchinner@redhat.com>
Wed, 29 Sep 2021 20:57:02 +0000 (16:57 -0400)
committerEric Sandeen <sandeen@sandeen.net>
Wed, 29 Sep 2021 20:57:02 +0000 (16:57 -0400)
Now we have liburcu, we can make use of it's atomic variable
implementation. It is almost identical to the kernel API - it's just
got a "uatomic" prefix. liburcu also provides all the same aomtic
variable memory barriers as the kernel, so if we pull memory barrier
dependent kernel code across, it will just work with the right
barrier wrappers.

This is preparation the addition of more extensive atomic operations
the that kernel buffer cache requires to function correctly.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
[chandan.babu@oracle.com: Swap order of arguments provided to atomic[64]_[add|sub]()]
Signed-off-by: Chandan Babu R <chandan.babu@oracle.com>
[sandeen: fix whitespace]
[sandeen: make #defines a little more consistent about (a,v)]
Reviewed-by: Eric Sandeen <sandeen@redhat.com>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
include/atomic.h

index e0e1ba84bc82a4e414d0d5445e7d67762d9a065f..2804815eea5f3eb2a08bfb29753dd447ab1d3b09 100644 (file)
@@ -7,21 +7,64 @@
 #define __ATOMIC_H__
 
 /*
- * Warning: These are not really atomic at all. They are wrappers around the
- * kernel atomic variable interface. If we do need these variables to be atomic
- * (due to multithreading of the code that uses them) we need to add some
- * pthreads magic here.
+ * Atomics are provided by liburcu.
+ *
+ * API and guidelines for which operations provide memory barriers is here:
+ *
+ * https://github.com/urcu/userspace-rcu/blob/master/doc/uatomic-api.md
+ *
+ * Unlike the kernel, the same interface supports 32 and 64 bit atomic integers.
  */
+#include <urcu/uatomic.h>
+#include "spinlock.h"
+
 typedef        int32_t atomic_t;
 typedef        int64_t atomic64_t;
 
-#define atomic_inc_return(x)   (++(*(x)))
-#define atomic_dec_return(x)   (--(*(x)))
+#define atomic_read(a)         uatomic_read(a)
+#define atomic_set(a, v)       uatomic_set(a, v)
+#define atomic_add(v, a)       uatomic_add(a, v)
+#define atomic_sub(v, a)       uatomic_sub(a, v)
+#define atomic_inc(a)          uatomic_inc(a)
+#define atomic_dec(a)          uatomic_dec(a)
+#define atomic_inc_return(a)   uatomic_add_return(a, 1)
+#define atomic_dec_return(a)   uatomic_sub_return(a, 1)
+#define atomic_dec_and_test(a) (atomic_dec_return(a) == 0)
+#define cmpxchg(a, o, n)       uatomic_cmpxchg(a, o, n);
+
+static inline bool atomic_add_unless(atomic_t *a, int v, int u)
+{
+       int r = atomic_read(a);
+       int n, o;
+
+       do {
+               o = r;
+               if (o == u)
+                       break;
+               n = o + v;
+               r = uatomic_cmpxchg(a, o, n);
+       } while (r != o);
+
+       return o != u;
+}
+
+static inline bool atomic_dec_and_lock(atomic_t *a, spinlock_t *lock)
+{
+       if (atomic_add_unless(a, -1, 1))
+               return 0;
+
+       spin_lock(lock);
+       if (atomic_dec_and_test(a))
+               return 1;
+       spin_unlock(lock);
+       return 0;
+}
 
-#define atomic64_read(x)       *(x)
-#define atomic64_set(x, v)     (*(x) = v)
-#define atomic64_add(v, x)     (*(x) += v)
-#define atomic64_inc(x)                ((*(x))++)
-#define atomic64_dec(x)                ((*(x))--)
+#define atomic64_read(a)       uatomic_read(a)
+#define atomic64_set(a, v)     uatomic_set(a, v)
+#define atomic64_add(v, a)     uatomic_add(a, v)
+#define atomic64_sub(v, a)     uatomic_sub(a, v)
+#define atomic64_inc(a)                uatomic_inc(a)
+#define atomic64_dec(a)                uatomic_dec(a)
 
 #endif /* __ATOMIC_H__ */