Added generic macro for running a function exactly once (XCC)
[akaros.git] / kern / include / ros / common.h
index 3e551cb..af2c3e7 100644 (file)
@@ -97,6 +97,14 @@ static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
        return 1 << LOG2_UP(value);
 }
 
+/* We wraparound if UINT_MAX < a * b, which is also UINT_MAX / a < b. */
+static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
+{
+       if (!a)
+               return FALSE;
+       return (uint64_t)(-1) / a < b;
+}
+
 // Return the offset of 'member' relative to the beginning of a struct type
 #ifndef offsetof
 #define offsetof(type, member)  ((size_t) (&((type*)0)->member))
@@ -111,4 +119,18 @@ static inline uintptr_t ROUNDUPPWR2(uintptr_t value)
 // a uint64_t programatically
 #define UINT64(upper, lower) ( (((uint64_t)(upper)) << 32) | (lower) )
 
+#define run_once(func) \
+{\
+       static atomic_t initializing = FALSE; \
+       static bool initialized = FALSE; \
+       if (!atomic_swap(&initializing, TRUE)) { \
+               func; \
+               initialized = TRUE; \
+       } \
+       else { \
+               while(!initialized) \
+                       cpu_relax(); \
+       } \
+}
+
 #endif /* ROS_COMMON_H */