Merge up to gearmand.
[m6w6/libmemcached] / clients / ms_atomic.h
index 580afabfe93d2d9fa7785b93606ac0f893a2824e..0b05bcecd3e29ab8e5b65ed68c4c13a3a84416a2 100644 (file)
@@ -1,24 +1,69 @@
+/* LibMemcached
+ * Copyright (C) 2006-2009 Brian Aker
+ * All rights reserved.
+ *
+ * Use and distribution licensed under the BSD license.  See
+ * the COPYING file in the parent directory for full text.
+ *
+ * Summary:
+ *
+ */
+
 #ifndef CLIENTS_MS_ATOMIC_H
 #define CLIENTS_MS_ATOMIC_H
 
-#if defined(__SUNC)
-
-#define _KERNEL
-#include <atomic.h>
-#undef _KERNEL
-
-inline int32_t __sync_add_and_fetch(volatile int32_t* ptr, int32_t val)
-{
-  (val == 1) ? atomic_inc_32((volatile uint32_t*)ptr) : atomic_add_32((volatile uint32_t*)ptr, val);
-  return *ptr;
-}
-
-
-inline uint32_t __sync_sub_and_fetch(volatile uint32_t* ptr, uint32_t val)
-{
-  (val == 1) ? atomic_dec_32(ptr) : atomic_add_32(ptr, 0-(int32_t)val);
-  return *ptr;
-}
+#if defined(__SUNPRO_C)
+# define _KERNEL
+# include <atomic.h>
+# if SIZEOF_SIZE_T == 8
+#  define  atomic_add_size(X, Y) atomic_add_64((X), (Y))
+#  define  atomic_add_size_nv(X, Y) atomic_add_64((X), (Y))
+#  define  atomic_dec_size(X, Y) atomic_add_64((X), (Y))
+#  define  atomic_dec_size_nv(X, Y) atomic_add_64((X), (Y))
+# else
+#  define  atomic_add_size(X, Y) atomic_add_32((X), (Y))
+#  define  atomic_add_size_nv(X, Y) atomic_add_32((X), (Y))
+#  define  atomic_dec_size(X, Y) atomic_add_32((X), (Y))
+#  define  atomic_dec_size_nv(X, Y) atomic_add_32((X), (Y))
+# endif
+# undef _KERNEL
+#elif HAVE_GCC_ATOMIC_BUILTINS
+# define atomic_add_8(X, Y)  __sync_fetch_and_add((X), (Y))
+# define atomic_add_16(X, Y) __sync_fetch_and_add((X), (Y))
+# define atomic_add_32(X, Y) __sync_fetch_and_add((X), (Y))
+# define atomic_add_size(X, Y) __sync_fetch_and_add((X), (Y))
+# define atomic_dec_8(X)  __sync_fetch_and_sub((X), 1)
+# define atomic_dec_16(X) __sync_fetch_and_sub((X), 1)
+# define atomic_dec_32(X) __sync_fetch_and_sub((X), 1)
+# define atomic_dec_size(X) __sync_fetch_and_sub((X), 1)
+/* The same as above, but these return the new value instead of void */
+# define atomic_add_8_nv(X, Y)  __sync_fetch_and_add((X), (Y))
+# define atomic_add_16_nv(X, Y) __sync_fetch_and_add((X), (Y))
+# define atomic_add_32_nv(X, Y) __sync_fetch_and_add((X), (Y))
+# define atomic_add_size_nv(X, Y) __sync_fetch_and_add((X), (Y))
+# define atomic_dec_8_nv(X)  __sync_fetch_and_sub((X), 1)
+# define atomic_dec_16_nv(X) __sync_fetch_and_sub((X), 1)
+# define atomic_dec_32_nv(X) __sync_fetch_and_sub((X), 1)
+# define atomic_dec_size_nv(X) __sync_fetch_and_sub((X), 1)
+#else
+#warning "Atomic operators not found so memslap will not work correctly"
+# define atomic_add_8(X, Y)
+# define atomic_add_16(X, Y)
+# define atomic_add_32(X, Y)
+# define atomic_add_size(X, Y)
+# define atomic_dec_8(X)
+# define atomic_dec_16(X)
+# define atomic_dec_32(X)
+# define atomic_dec_size(X)
+/* The same as above, but these return the new value instead of void */
+# define atomic_add_8_nv(X, Y)
+# define atomic_add_16_nv(X, Y)
+# define atomic_add_32_nv(X, Y)
+# define atomic_add_size_nv(X, Y)
+# define atomic_dec_8_nv(X)
+# define atomic_dec_16_nv(X)
+# define atomic_dec_32_nv(X)
+# define atomic_dec_size_nv(X)
+#endif /* defined(__SUNPRO_C) */
 
-#endif /* defined(__SUNC) */
 #endif /* CLIENTS_MS_ATOMIC_H */