]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
atomic_t: add atomic64_t
authorYehuda Sadeh <yehuda@inktank.com>
Mon, 31 Mar 2014 21:49:50 +0000 (14:49 -0700)
committerSage Weil <sage@redhat.com>
Sat, 2 Aug 2014 00:50:20 +0000 (17:50 -0700)
Signed-off-by: Yehuda Sadeh <yehuda@inktank.com>
(cherry picked from commit bf3ba6001c7b4cf37edfe6551d3ef298ebcbf421)

configure.ac
src/include/atomic.h

index fb54df1fed1d971b0122cb73a9ab92ab325af776..8627aa815db1821301d6156a67c4d00a62b52849 100644 (file)
@@ -472,9 +472,14 @@ AS_IF([test "x$with_libatomic_ops" != xno],
                    [no libatomic-ops found (use --without-libatomic-ops to disable)])
               ])])
 AS_IF([test "$HAVE_ATOMIC_OPS" = "1"],
-       [],
+       [
+         AC_CHECK_SIZEOF(AO_t, [], [
+                                #include <atomic_ops.h>
+                                ])
+         ],
        [AC_DEFINE([NO_ATOMIC_OPS], [1], [Defined if you do not have atomic_ops])])
 
+
 AM_CONDITIONAL(WITH_LIBATOMIC, [test "$HAVE_ATOMIC_OPS" = "1"])
 
 # newsyn?  requires mpi.
index 537fa989cd3f6f9357710f23d23a2eb3ba23ff07..b03e0293e6424982b5f0ab2ebfc21758a2a20ddd 100644 (file)
 #endif
 
 #include <stdlib.h>
+#include "include/Spinlock.h"
+
+namespace ceph {
+  template <class T>
+  class atomic_spinlock_t {
+    mutable ceph_spinlock_t lock;
+    T val;
+  public:
+    atomic_spinlock_t(T i=0)
+      : val(i) {
+      ceph_spin_init(&lock);
+    }
+    ~atomic_spinlock_t() {
+      ceph_spin_destroy(&lock);
+    }
+    void set(size_t v) {
+      ceph_spin_lock(&lock);
+      val = v;
+      ceph_spin_unlock(&lock);
+    }
+    T inc() {
+      ceph_spin_lock(&lock);
+      T r = ++val;
+      ceph_spin_unlock(&lock);
+      return r;
+    }
+    T dec() {
+      ceph_spin_lock(&lock);
+      T r = --val;
+      ceph_spin_unlock(&lock);
+      return r;
+    }
+    void add(T d) {
+      ceph_spin_lock(&lock);
+      val += d;
+      ceph_spin_unlock(&lock);
+    }
+    void sub(T d) {
+      ceph_spin_lock(&lock);
+      val -= d;
+      ceph_spin_unlock(&lock);
+    }
+    T read() const {
+      signed long ret;
+      ceph_spin_lock(&lock);
+      ret = val;
+      ceph_spin_unlock(&lock);
+      return ret;
+    }
+  private:
+    // forbid copying
+    atomic_spinlock_t(const atomic_spinlock_t<T> &other);
+    atomic_spinlock_t &operator=(const atomic_spinlock_t<T> &rhs);
+  };
+}
 
 #ifndef NO_ATOMIC_OPS
 
@@ -62,7 +117,15 @@ namespace ceph {
     atomic_t(const atomic_t &other);
     atomic_t &operator=(const atomic_t &rhs);
   };
+
+#if SIZEOF_AO_T == 8
+  typedef atomic_t atomic64_t;
+#else
+  typedef atomic_spinlock_t<long long> atomic64_t;
+#endif
+
 }
+
 #else
 /*
  * crappy slow implementation that uses a pthreads spinlock.
@@ -70,56 +133,9 @@ namespace ceph {
 #include "include/Spinlock.h"
 
 namespace ceph {
-  class atomic_t {
-    mutable ceph_spinlock_t lock;
-    signed long val;
-  public:
-    atomic_t(int i=0)
-      : val(i) {
-      ceph_spin_init(&lock);
-    }
-    ~atomic_t() {
-      ceph_spin_destroy(&lock);
-    }
-    void set(size_t v) {
-      ceph_spin_lock(&lock);
-      val = v;
-      ceph_spin_unlock(&lock);
-    }
-    int inc() {
-      ceph_spin_lock(&lock);
-      int r = ++val;
-      ceph_spin_unlock(&lock);
-      return r;
-    }
-    int dec() {
-      ceph_spin_lock(&lock);
-      int r = --val;
-      ceph_spin_unlock(&lock);
-      return r;
-    }
-    void add(int d) {
-      ceph_spin_lock(&lock);
-      val += d;
-      ceph_spin_unlock(&lock);
-    }
-    void sub(int d) {
-      ceph_spin_lock(&lock);
-      val -= d;
-      ceph_spin_unlock(&lock);
-    }
-    int read() const {
-      signed long ret;
-      ceph_spin_lock(&lock);
-      ret = val;
-      ceph_spin_unlock(&lock);
-      return ret;
-    }
-  private:
-    // forbid copying
-    atomic_t(const atomic_t &other);
-    atomic_t &operator=(const atomic_t &rhs);
-  };
+  typedef atomic_spinlock_t<int> atomic_t;
+  typedef atomic_spinlock_t<long long> atomic64_t;
 }
+
 #endif
 #endif