Add READ_ONCE and WRITE_ONCE (XCC)
[akaros.git] / kern / include / ros / common.h
index 2c451b2..bb23ca2 100644 (file)
@@ -1,15 +1,12 @@
-#ifndef ROS_COMMON_H
-#define ROS_COMMON_H
+#pragma once
 
 #ifndef __ASSEMBLER__
-#ifndef __IVY__
-#include <ros/noivy.h>
-#endif
 
 #include <stddef.h>
 #include <stdint.h>
 #include <sys/types.h>
 #include <stdbool.h>
+#include <string.h>
 
 typedef uintptr_t physaddr_t;
 typedef long intreg_t;
@@ -27,13 +24,6 @@ typedef unsigned long uintreg_t;
 #define FALSE  0
 #endif
 
-#define KiB    1024u
-#define MiB    1048576u
-#define GiB    1073741824u
-#define TiB    1099511627776ull
-#define PiB    1125899906842624ull
-#define EiB    1152921504606846976ull
-
 #define FOR_CIRC_BUFFER(next, size, var) \
        for (int _var = 0, var = (next); _var < (size); _var++, var = (var + 1) % (size))
 
@@ -48,35 +38,9 @@ typedef unsigned long uintreg_t;
 #define is_defined__(comma, value) is_defined___(comma 1, 0)
 #define is_defined___(_, v, ...) v
 
-// Efficient min and max operations
-#ifdef ROS_KERNEL /* Glibc or other user libs have their own */
-#define MIN(_a, _b)                                            \
-({                                                             \
-       typeof(_a) __a = (_a);                                  \
-       typeof(_b) __b = (_b);                                  \
-       __a <= __b ? __a : __b;                                 \
-})
-#define MAX(_a, _b)                                            \
-({                                                             \
-       typeof(_a) __a = (_a);                                  \
-       typeof(_b) __b = (_b);                                  \
-       __a >= __b ? __a : __b;                                 \
-})
-
-/* Other kernel-only includes */
-
-/* Test for alignment, e.g. 2^6 */
-#define ALIGNED(p, a)  (!(((uintptr_t)(p)) & ((a)-1)))
-/* Aligns x up to the mask, e.g. (2^6 - 1) (round up if any mask bits are set)*/
-#define __ALIGN_MASK(x, mask) (((uintptr_t)(x) + (mask)) & ~(mask))
-/* Aligns x up to the alignment, e.g. 2^6. */
-#define ALIGN(x, a) ((typeof(x)) __ALIGN_MASK(x, (a) - 1))
-/* Will return false for 0.  Debatable, based on what you want. */
-#define IS_PWR2(x) ((x) && !((x) & (x - 1)))
-
-#define ARRAY_SIZE(x) (sizeof((x))/sizeof((x)[0]))
+#define COUNT_OF(x) (sizeof((x))/sizeof((x)[0]))
 
-#endif
+#define ZERO_DATA(s) memset(&(s), 0, sizeof(s))
 
 /* Rounding operations (efficient when n is a power of 2)
  * Round down to the nearest multiple of n.
@@ -156,18 +120,21 @@ static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
        (type*)((char*)ptr - offsetof(type, member));                             \
 })
 
-/* Force the reading exactly once of x.  You may still need mbs().  See
+/* Force the reading/writing exactly once of x.  You may still need mbs().  See
  * http://lwn.net/Articles/508991/ for more info. */
 #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
+#define READ_ONCE(x) ACCESS_ONCE(x)
+#define WRITE_ONCE(x, val) ((*(volatile typeof(x) *)&(x)) = val)
 
 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
  * other callers spin til the func is complete. */
 #define run_once(func)                                                         \
-{                                                                              \
+do {                                                                           \
        static bool ran_once = FALSE;                                              \
-       static atomic_t is_running = FALSE;                                        \
+       static bool is_running = FALSE;                                            \
        if (!ran_once) {                                                           \
-               if (!atomic_swap(&is_running, TRUE)) {                                 \
+               /* fetch and set TRUE, without a header or test_and_set weirdness */   \
+               if (!__sync_fetch_and_or(&is_running, TRUE)) {                         \
                        /* we won the race and get to run the func */                      \
                        func;                                                              \
                        wmb();  /* don't let the ran_once write pass previous writes */    \
@@ -176,33 +143,18 @@ static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
                        /* someone else won, wait til they are done to break out */        \
                        while (!ran_once)                                                  \
                                cpu_relax();                                                   \
-                                                                               \
                }                                                                      \
        }                                                                          \
-}
+} while (0)
 
 /* Unprotected, single-threaded version, makes sure func is run exactly once */
 #define run_once_racy(func)                                                    \
-{                                                                              \
+do {                                                                           \
        static bool ran_once = FALSE;                                              \
        if (!ran_once) {                                                           \
                func;                                                                  \
                ran_once = TRUE;                                                       \
        }                                                                          \
-}
-
-/* Aborts with 'retcmd' if this function has already been called.  Compared to
- * run_once, this is put at the top of a function that can be called from
- * multiple sources but should only execute once. */
-#define init_once_racy(retcmd)                                                 \
-{                                                                              \
-       static bool initialized = FALSE;                                           \
-       if (initialized) {                                                         \
-               retcmd;                                                                \
-       }                                                                          \
-       initialized = TRUE;                                                        \
-}
+} while (0)
 
 #endif /* __ASSEMBLER__ */
-
-#endif /* ROS_COMMON_H */