dpif-netdev: optmizing emc_processing()
[cascardo/ovs.git] / lib / ovs-atomic.h
index b1c7b22..b38c9ef 100644 (file)
  *        operation (atomic_read_explicit, or a load operation preceding a
  *        atomic_thread_fence) will not be moved prior to the consume
  *        barrier.  Non-data-dependent loads and stores can be reordered to
- *        happen before the the consume barrier.
+ *        happen before the consume barrier.
  *
  *        RCU is the prime example of the use of the consume barrier: The
  *        consume barrier guarantees that reads from a RCU protected object
     #if __CHECKER__
         /* sparse doesn't understand some GCC extensions we use. */
         #include "ovs-atomic-pthreads.h"
-    #elif HAVE_STDATOMIC_H
-        #include "ovs-atomic-c11.h"
     #elif __has_extension(c_atomic)
         #include "ovs-atomic-clang.h"
+    #elif HAVE_STDATOMIC_H
+        #include "ovs-atomic-c11.h"
     #elif __GNUC__ >= 4 && __GNUC_MINOR__ >= 7
         #include "ovs-atomic-gcc4.7+.h"
     #elif __GNUC__ && defined(__x86_64__)
         #include "ovs-atomic-i586.h"
     #elif HAVE_GCC4_ATOMICS
         #include "ovs-atomic-gcc4+.h"
+    #elif _MSC_VER && _M_IX86 >= 500
+        #include "ovs-atomic-msvc.h"
     #else
         /* ovs-atomic-pthreads implementation is provided for portability.
          * It might be too slow for real use because Open vSwitch is
@@ -412,6 +414,63 @@ typedef ATOMIC(int32_t)   atomic_int32_t;
 #define atomic_flag_clear_relaxed(FLAG)                         \
     atomic_flag_clear_explicit(FLAG, memory_order_relaxed)
 
+/* A simplified atomic count.  Does not provide any synchronization with any
+ * other variables.
+ *
+ * Typically a counter is not used to synchronize the state of any other
+ * variables (with the notable exception of reference count, below).
+ * This abstraction releaves the user from the memory order considerations,
+ * and may make the code easier to read.
+ *
+ * We only support the unsigned int counters, as those are the most common. */
+typedef struct atomic_count {
+    atomic_uint count;
+} atomic_count;
+
+#define ATOMIC_COUNT_INIT(VALUE) { VALUE }
+
+static inline void
+atomic_count_init(atomic_count *count, unsigned int value)
+{
+    atomic_init(&count->count, value);
+}
+
+static inline unsigned int
+atomic_count_inc(atomic_count *count)
+{
+    unsigned int old;
+
+    atomic_add_relaxed(&count->count, 1, &old);
+
+    return old;
+}
+
+static inline unsigned int
+atomic_count_dec(atomic_count *count)
+{
+    unsigned int old;
+
+    atomic_sub_relaxed(&count->count, 1, &old);
+
+    return old;
+}
+
+static inline unsigned int
+atomic_count_get(atomic_count *count)
+{
+    unsigned int value;
+
+    atomic_read_relaxed(&count->count, &value);
+
+    return value;
+}
+
+static inline void
+atomic_count_set(atomic_count *count, unsigned int value)
+{
+    atomic_store_relaxed(&count->count, value);
+}
+
 /* Reference count. */
 struct ovs_refcount {
     atomic_uint count;