parlib: Add cpu_relax_any()
[akaros.git] / kern / include / common.h
1 #pragma once
2
3 #include <ros/common.h>
4 #include <compiler.h>
5
6 /* Force a rebuild of the whole kernel if 64BIT-ness changed */
7 #ifdef CONFIG_64BIT
8 #endif
9
10 #define SIZE_MAX        (~(size_t)0)
11
12 // Efficient min and max operations
13 #define MIN(_a, _b)                                             \
14 ({                                                              \
15         typeof(_a) __a = (_a);                                  \
16         typeof(_b) __b = (_b);                                  \
17         __a <= __b ? __a : __b;                                 \
18 })
19 #define MAX(_a, _b)                                             \
20 ({                                                              \
21         typeof(_a) __a = (_a);                                  \
22         typeof(_b) __b = (_b);                                  \
23         __a >= __b ? __a : __b;                                 \
24 })
25
26 /* Test for alignment, e.g. 2^6 */
27 #define ALIGNED(p, a)   (!(((uintptr_t)(p)) & ((a)-1)))
28 /* Aligns x up to the mask, e.g. (2^6 - 1) (round up if any mask bits are set)*/
29 #define __ALIGN_MASK(x, mask) (((uintptr_t)(x) + (mask)) & ~(mask))
30 /* Aligns x up to the alignment, e.g. 2^6. */
31 #define ALIGN(x, a) ((typeof(x)) __ALIGN_MASK(x, (a) - 1))
32 /* Will return false for 0.  Debatable, based on what you want. */
33 #define IS_PWR2(x) ((x) && !((x) & (x - 1)))
34
35 #define ARRAY_SIZE(x) COUNT_OF(x)
36
37 /* Makes sure func is run exactly once.  Can handle concurrent callers, and
38  * other callers spin til the func is complete. */
39 #define run_once(func)                                                         \
40 do {                                                                           \
41         static bool ran_once = FALSE;                                              \
42         static bool is_running = FALSE;                                            \
43         if (!ran_once) {                                                           \
44                 /* fetch and set TRUE, without a header or test_and_set weirdness */   \
45                 if (!__sync_fetch_and_or(&is_running, TRUE)) {                         \
46                         /* we won the race and get to run the func */                      \
47                         func;                                                              \
48                         wmb();  /* don't let the ran_once write pass previous writes */    \
49                         ran_once = TRUE;                                                   \
50                 } else {                                                               \
51                         /* someone else won, wait til they are done to break out */        \
52                         while (!ran_once)                                                  \
53                                 cpu_relax();                                                   \
54                 }                                                                      \
55         }                                                                          \
56 } while (0)
57
58 /* Unprotected, single-threaded version, makes sure func is run exactly once */
59 #define run_once_racy(func)                                                    \
60 do {                                                                           \
61         static bool ran_once = FALSE;                                              \
62         if (!ran_once) {                                                           \
63                 func;                                                                  \
64                 ran_once = TRUE;                                                       \
65         }                                                                          \
66 } while (0)