summaryrefslogtreecommitdiff
path: root/libcr/coroutine.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcr/coroutine.c')
-rw-r--r--libcr/coroutine.c65
1 files changed, 27 insertions, 38 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c
index aa23d58..7278225 100644
--- a/libcr/coroutine.c
+++ b/libcr/coroutine.c
@@ -9,6 +9,7 @@
#include <string.h> /* for strncpy(), memset() */
#include <libmisc/assert.h>
+#include <libmisc/macro.h>
#define LOG_NAME COROUTINE
#include <libmisc/log.h>
@@ -128,18 +129,6 @@
* no longer exists.
*/
-/* preprocessor macros ********************************************************/
-
-/** Return `n` rounded up to the nearest multiple of `d` */
-#define ROUND_UP(n, d) ( ( ((n)+(d)-1) / (d) ) * (d) )
-#define ARRAY_LEN(arr) (sizeof(arr)/sizeof((arr)[0]))
-#define NEXT_POWER_OF_2(x) ((1ULL)<<((sizeof(unsigned long long)*8)-__builtin_clzll(x)))
-
-#define UNUSED(name)
-
-#define ALWAYS_INLINE [[gnu::always_inline]] inline
-#define NEVER_INLINE [[gnu::noinline]]
-
/* platform support ***********************************************************/
/* As part of sbc-harness, this only really needs to support ARM-32, but being
@@ -211,7 +200,7 @@
sigprocmask(SIG_SETMASK, &zero, NULL);
}
#if CONFIG_COROUTINE_GDB
- static void _cr_gdb_intrhandler(int UNUSED(sig)) {}
+ static void _cr_gdb_intrhandler(int LM_UNUSED(sig)) {}
#endif
static void cr_plat_init(void) {
#if CONFIG_COROUTINE_GDB
@@ -231,7 +220,7 @@
);
return isr_number != 0;
}
- ALWAYS_INLINE static bool _cr_plat_are_interrupts_enabled(void) {
+ LM_ALWAYS_INLINE static bool _cr_plat_are_interrupts_enabled(void) {
assert(!cr_plat_is_in_intrhandler());
uint32_t primask;
asm volatile ("mrs %0, PRIMASK"
@@ -240,7 +229,7 @@
return primask == 0;
}
- ALWAYS_INLINE static void cr_plat_wait_for_interrupt(void) {
+ LM_ALWAYS_INLINE static void cr_plat_wait_for_interrupt(void) {
assert(!cr_plat_is_in_intrhandler());
assert(!_cr_plat_are_interrupts_enabled());
asm volatile ("wfi\n"
@@ -271,7 +260,7 @@
#define CR_PLAT_STACK_GROWS_DOWNWARD 1
#if CONFIG_COROUTINE_MEASURE_STACK
- ALWAYS_INLINE static uintptr_t cr_plat_get_sp(void) {
+ LM_ALWAYS_INLINE static uintptr_t cr_plat_get_sp(void) {
uintptr_t sp;
asm volatile ("mov %0, sp":"=r"(sp));
return sp;
@@ -303,7 +292,7 @@
#define CR_PLAT_STACK_GROWS_DOWNWARD 1
#if CONFIG_COROUTINE_MEASURE_STACK
- ALWAYS_INLINE static uintptr_t cr_plat_get_sp(void) {
+ LM_ALWAYS_INLINE static uintptr_t cr_plat_get_sp(void) {
uintptr_t sp;
asm volatile ("movq %%rsp, %0":"=r"(sp));
return sp;
@@ -419,10 +408,10 @@ static const uint8_t stack_pattern[] = {
};
#endif
#if CONFIG_COROUTINE_PROTECT_STACK
- #define STACK_GUARD_SIZE \
- ROUND_UP(sizeof(stack_pattern), CR_PLAT_STACK_ALIGNMENT)
+ #define CR_STACK_GUARD_SIZE \
+ LM_ROUND_UP(sizeof(stack_pattern), CR_PLAT_STACK_ALIGNMENT)
#else
- #define STACK_GUARD_SIZE 0
+ #define CR_STACK_GUARD_SIZE 0
#endif
/* global variables ***********************************************************/
@@ -463,7 +452,7 @@ static struct {
* compiler will optimize `%array_len` to &(array_len-1)`, (b)
* we don't have to worry about funny wrap-around behavior
* when head or tail overflow. */
- cid_t buf[NEXT_POWER_OF_2(CONFIG_COROUTINE_NUM)];
+ cid_t buf[LM_NEXT_POWER_OF_2(CONFIG_COROUTINE_NUM)];
} coroutine_ringbuf = {0};
static cid_t coroutine_running = 0;
static size_t coroutine_cnt = 0;
@@ -471,28 +460,28 @@ static size_t coroutine_cnt = 0;
/* utility functions **********************************************************/
static inline const char* coroutine_state_str(enum coroutine_state state) {
- assert(state < ARRAY_LEN(coroutine_state_strs));
+ assert(state < LM_ARRAY_LEN(coroutine_state_strs));
return coroutine_state_strs[state];
}
static inline void coroutine_ringbuf_push(cid_t val) {
- coroutine_ringbuf.buf[coroutine_ringbuf.head++ % ARRAY_LEN(coroutine_ringbuf.buf)] = val;
- assert((coroutine_ringbuf.head % ARRAY_LEN(coroutine_ringbuf.buf)) !=
- (coroutine_ringbuf.tail % ARRAY_LEN(coroutine_ringbuf.buf)));
+ coroutine_ringbuf.buf[coroutine_ringbuf.head++ % LM_ARRAY_LEN(coroutine_ringbuf.buf)] = val;
+ assert((coroutine_ringbuf.head % LM_ARRAY_LEN(coroutine_ringbuf.buf)) !=
+ (coroutine_ringbuf.tail % LM_ARRAY_LEN(coroutine_ringbuf.buf)));
}
static inline cid_t coroutine_ringbuf_pop(void) {
if (coroutine_ringbuf.tail == coroutine_ringbuf.head)
return 0;
- return coroutine_ringbuf.buf[coroutine_ringbuf.tail++ % ARRAY_LEN(coroutine_ringbuf.buf)];
+ return coroutine_ringbuf.buf[coroutine_ringbuf.tail++ % LM_ARRAY_LEN(coroutine_ringbuf.buf)];
}
#if CONFIG_COROUTINE_GDB
-NEVER_INLINE void cr_gdb_breakpoint(void) {
+LM_NEVER_INLINE void cr_gdb_breakpoint(void) {
/* Prevent the call from being optimized away. */
asm ("");
}
-NEVER_INLINE void cr_gdb_readjmp(cr_plat_jmp_buf *env) {
+LM_NEVER_INLINE void cr_gdb_readjmp(cr_plat_jmp_buf *env) {
if (!cr_plat_setjmp(&coroutine_gdb_env))
cr_plat_longjmp(env, 2);
}
@@ -516,7 +505,7 @@ static inline void assert_cid(cid_t cid) {
assert(coroutine_table[cid-1].stack_size);
uint8_t *stack = coroutine_table[cid-1].stack;
assert(stack);
- for (size_t i = 0; i < STACK_GUARD_SIZE; i++) {
+ for (size_t i = 0; i < CR_STACK_GUARD_SIZE; i++) {
size_t j = coroutine_table[cid-1].stack_size - (i+1);
assert(stack[i] == stack_pattern[i%sizeof(stack_pattern)]);
assert(stack[j] == stack_pattern[j%sizeof(stack_pattern)]);
@@ -583,8 +572,8 @@ cid_t coroutine_add_with_stack_size(size_t stack_size,
#endif
#if CONFIG_COROUTINE_VALGRIND
coroutine_table[child-1].stack_id = VALGRIND_STACK_REGISTER(
- coroutine_table[child-1].stack + STACK_GUARD_SIZE,
- coroutine_table[child-1].stack + stack_size - STACK_GUARD_SIZE);
+ coroutine_table[child-1].stack + CR_STACK_GUARD_SIZE,
+ coroutine_table[child-1].stack + stack_size - CR_STACK_GUARD_SIZE);
#endif
coroutine_running = child;
@@ -594,9 +583,9 @@ cid_t coroutine_add_with_stack_size(size_t stack_size,
void *stack_base = coroutine_table[child-1].stack
#if CR_PLAT_STACK_GROWS_DOWNWARD
+ stack_size
- - STACK_GUARD_SIZE
+ - CR_STACK_GUARD_SIZE
#else
- + STACK_GUARD_SIZE
+ + CR_STACK_GUARD_SIZE
#endif
;
debugf("...stack =%p", coroutine_table[child-1].stack);
@@ -782,7 +771,7 @@ void cr_cid_info(cid_t cid, struct cr_cid_info *ret) {
assert(ret);
/* stack_cap */
- ret->stack_cap = coroutine_table[cid-1].stack_size - 2*STACK_GUARD_SIZE;
+ ret->stack_cap = coroutine_table[cid-1].stack_size - 2*CR_STACK_GUARD_SIZE;
/* stack_max */
ret->stack_max = ret->stack_cap;
@@ -790,9 +779,9 @@ void cr_cid_info(cid_t cid, struct cr_cid_info *ret) {
for (;;) {
size_t i =
#if CR_PLAT_STACK_GROWS_DOWNWARD
- STACK_GUARD_SIZE + ret->stack_cap - ret->stack_max
+ CR_STACK_GUARD_SIZE + ret->stack_cap - ret->stack_max
#else
- ret->stack_max - 1 - STACK_GUARD_SIZE
+ ret->stack_max - 1 - CR_STACK_GUARD_SIZE
#endif
;
if (ret->stack_max == 0 ||
@@ -812,9 +801,9 @@ void cr_cid_info(cid_t cid, struct cr_cid_info *ret) {
assert(sp);
uintptr_t sb = (uintptr_t)coroutine_table[cid-1].stack;
#if CR_PLAT_STACK_GROWS_DOWNWARD
- ret->stack_cur = (sb - STACK_GUARD_SIZE) - sp;
+ ret->stack_cur = (sb - CR_STACK_GUARD_SIZE) - sp;
#else
- ret->stack_cur = sp - (sb + STACK_GUARD_SIZE);
+ ret->stack_cur = sp - (sb + CR_STACK_GUARD_SIZE);
#endif
}