Added generic macro for running a function exactly once (XCC)
authorKevin Klues <klueska@cs.berkeley.edu>
Tue, 18 Dec 2012 20:12:45 +0000 (12:12 -0800)
committerKevin Klues <klueska@cs.berkeley.edu>
Tue, 18 Dec 2012 20:12:45 +0000 (12:12 -0800)
kern/include/ros/common.h

index b286202..af2c3e7 100644 (file)
@@ -119,4 +119,18 @@ static inline bool mult_will_overflow_u64(uint64_t a, uint64_t b)
 // 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 */