]> git.sesse.net Git - vlc/blobdiff - include/vlc_atomic.h
libvlc_MetaRequest: increment item i_preparse_depth
[vlc] / include / vlc_atomic.h
index 49cc1aeb5284fdda1803f5984ddc976553a73b93..af88eabb5e7af12d38e78ae972db728d496aa68f 100644 (file)
  * unsigned equivalents, i.e. 4-bytes and 8-bytes types, although GCC also
  * supports 1 and 2-bytes types. Some non-x86 architectures do not support
  * 8-byte atomic types (or not efficiently). */
+#  if defined (_MSC_VER)
+/* Some atomic operations of the Interlocked API are only
+   available for desktop apps. Thus we define the atomic types to
+   be at least 32 bits wide. */
+typedef      int_least32_t atomic_flag;
+typedef      int_least32_t atomic_bool;
+typedef      int_least32_t atomic_char;
+typedef      int_least32_t atomic_schar;
+typedef     uint_least32_t atomic_uchar;
+typedef      int_least32_t atomic_short;
+typedef     uint_least32_t atomic_ushort;
+#  else
 typedef          bool      atomic_flag;
 typedef          bool      atomic_bool;
 typedef          char      atomic_char;
@@ -64,6 +76,7 @@ typedef   signed char      atomic_schar;
 typedef unsigned char      atomic_uchar;
 typedef          short     atomic_short;
 typedef unsigned short     atomic_ushort;
+#  endif
 typedef          int       atomic_int;
 typedef unsigned int       atomic_uint;
 typedef          long      atomic_long;
@@ -304,25 +317,27 @@ typedef         uintmax_t atomic_uintmax_t;
 
 # elif defined (_MSC_VER)
 
+# include <windows.h>
+
 /*** Use the Interlocked API. ***/
 
 /* Define macros in order to dispatch to the correct function depending on the type.
    Several ranges are need because some operations are not implemented for all types. */
 #  define atomic_type_dispatch_32_64(operation, object, ...) \
-    (sizeof(*object) == 4 ? operation(object, __VA_ARGS__) : \
-     sizeof(*object) == 8 ? operation##64(object, __VA_ARGS__) : \
-     (abort(), 0))
+    (sizeof(*object) == 4 ? operation((LONG *)object, __VA_ARGS__) : \
+    sizeof(*object) == 8 ? operation##64((LONGLONG *)object, __VA_ARGS__) : \
+    (abort(), 0))
 
 #  define atomic_type_dispatch_16_64(operation, object, ...) \
-    (sizeof(*object) == 2 ? operation##16(object, __VA_ARGS__) : \
-     atomic_type_dispatch_32_64(operation, object, __VA_ARGS__))
+    (sizeof(*object) == 2 ? operation##16((short *)object, __VA_ARGS__) : \
+    atomic_type_dispatch_32_64(operation, object, __VA_ARGS__))
 
 #  define atomic_type_dispatch_8_64(operation, object, ...) \
-    (sizeof(*object) == 1 ? operation##8(object, __VA_ARGS__) : \
-     atomic_type_dispatch_16_64(operation, object, __VA_ARGS__))
+    (sizeof(*object) == 1 ? operation##8((char *)object, __VA_ARGS__) : \
+    atomic_type_dispatch_16_64(operation, object, __VA_ARGS__))
 
 #  define atomic_store(object,desired) \
-    atomic_type_dispatch_8_64(InterlockedExchange, object, desired)
+    atomic_type_dispatch_16_64(InterlockedExchange, object, desired)
 #  define atomic_store_explicit(object,desired,order) \
     atomic_store(object, desired)
 
@@ -332,12 +347,12 @@ typedef         uintmax_t atomic_uintmax_t;
     atomic_load(object)
 
 #  define atomic_exchange(object,desired) \
-    atomic_type_dispatch_8_64(InterlockedExchange, object, desired)
+    atomic_type_dispatch_16_64(InterlockedExchange, object, desired)
 #  define atomic_exchange_explicit(object,desired,order) \
     atomic_exchange(object, desired)
 
 #  define atomic_compare_exchange_strong(object,expected,desired) \
-    atomic_type_dispatch_16_64(InterlockedCompareExchange, object, expected, desired) == expected
+    atomic_type_dispatch_16_64(InterlockedCompareExchange, object, *expected, desired) == *expected
 #  define atomic_compare_exchange_strong_explicit(object,expected,desired,order) \
     atomic_compare_exchange_strong(object, expected, desired)
 #  define atomic_compare_exchange_weak(object,expected,desired) \
@@ -351,7 +366,7 @@ typedef         uintmax_t atomic_uintmax_t;
     atomic_fetch_add(object, operand)
 
 #  define atomic_fetch_sub(object,operand) \
-    atomic_type_dispatch_32_64(InterlockedExchangeAdd, object, -operand)
+    atomic_type_dispatch_32_64(InterlockedExchangeAdd, object, -(LONGLONG)operand)
 #  define atomic_fetch_sub_explicit(object,operand,order) \
     atomic_fetch_sub(object, operand)
 
@@ -387,65 +402,17 @@ typedef         uintmax_t atomic_uintmax_t;
 # endif
 # endif
 
-/**
- * Memory storage space for an atom. Never access it directly.
- */
-typedef union
-{
-    atomic_uintptr_t u;
-} vlc_atomic_t;
-
-/** Static initializer for \ref vlc_atomic_t */
-# define VLC_ATOMIC_INIT(val) { (val) }
-
-/* All functions return the atom value _after_ the operation. */
-static inline uintptr_t vlc_atomic_get(vlc_atomic_t *atom)
-{
-    return atomic_load(&atom->u);
-}
-
-static inline uintptr_t vlc_atomic_set(vlc_atomic_t *atom, uintptr_t v)
-{
-    atomic_store(&atom->u, v);
-    return v;
-}
-
-static inline uintptr_t vlc_atomic_add(vlc_atomic_t *atom, uintptr_t v)
-{
-    return atomic_fetch_add(&atom->u, v) + v;
-}
-
-static inline uintptr_t vlc_atomic_sub (vlc_atomic_t *atom, uintptr_t v)
-{
-    return atomic_fetch_sub (&atom->u, v) - v;
-}
-
-static inline uintptr_t vlc_atomic_inc (vlc_atomic_t *atom)
-{
-    return vlc_atomic_add (atom, 1);
-}
-
-static inline uintptr_t vlc_atomic_dec (vlc_atomic_t *atom)
-{
-    return vlc_atomic_sub (atom, 1);
-}
-
-static inline uintptr_t vlc_atomic_swap(vlc_atomic_t *atom, uintptr_t v)
-{
-    return atomic_exchange(&atom->u, v);
-}
+typedef atomic_uint_least32_t vlc_atomic_float;
 
-static inline uintptr_t vlc_atomic_compare_swap(vlc_atomic_t *atom,
-                                                uintptr_t u, uintptr_t v)
+static inline void vlc_atomic_init_float(vlc_atomic_float *var, float f)
 {
-    atomic_compare_exchange_strong(&atom->u, &u, v);
-    return u;
+    union { float f; uint32_t i; } u;
+    u.f = f;
+    atomic_init(var, u.i);
 }
 
-typedef atomic_uint_least32_t vlc_atomic_float;
-
 /** Helper to retrieve a single precision from an atom. */
-static inline float vlc_atomic_loadf(vlc_atomic_float *atom)
+static inline float vlc_atomic_load_float(vlc_atomic_float *atom)
 {
     union { float f; uint32_t i; } u;
     u.i = atomic_load(atom);
@@ -453,7 +420,7 @@ static inline float vlc_atomic_loadf(vlc_atomic_float *atom)
 }
 
 /** Helper to store a single precision into an atom. */
-static inline void vlc_atomic_storef(vlc_atomic_float *atom, float f)
+static inline void vlc_atomic_store_float(vlc_atomic_float *atom, float f)
 {
     union { float f; uint32_t i; } u;
     u.f = f;