summaryrefslogtreecommitdiff
path: root/libcr/coroutine.c
diff options
context:
space:
mode:
Diffstat (limited to 'libcr/coroutine.c')
-rw-r--r--libcr/coroutine.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/libcr/coroutine.c b/libcr/coroutine.c
index df9cad3..176590a 100644
--- a/libcr/coroutine.c
+++ b/libcr/coroutine.c
@@ -32,6 +32,13 @@
#ifndef CONFIG_COROUTINE_DEBUG
# error config.h must define CONFIG_COROUTINE_DEBUG
#endif
+#ifndef CONFIG_COROUTINE_VALGRIND
+# error config.h must define CONFIG_COROUTINE_VALGRIND
+#endif
+
+#if CONFIG_COROUTINE_VALGRIND
+# include <valgrind/valgrind.h>
+#endif
/* Implementation *************************************************************/
@@ -271,6 +278,9 @@ struct coroutine {
#endif
size_t stack_size;
void *stack;
+#if CONFIG_COROUTINE_VALGRIND
+ unsigned stack_id;
+#endif
};
/* constants ******************************************************************/
@@ -433,6 +443,11 @@ cid_t coroutine_add_with_stack_size(size_t stack_size, cr_fn_t fn, void *args) {
((uint8_t *)coroutine_table[child-1].stack)[i] =
stack_pattern[i%sizeof(stack_pattern)];
#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);
+#endif
coroutine_running = child;
coroutine_table[child-1].state = CR_INITIALIZING;
@@ -488,6 +503,9 @@ void coroutine_main(void) {
/* This is where we jump to from cr_exit(), and from
* nowhere else. */
assert_cid_state(coroutine_running, state == CR_NONE);
+#if CONFIG_COROUTINE_VALGRIND
+ VALGRIND_STACK_DEREGISTER(coroutine_table[coroutine_running-1].stack_id);
+#endif
free(coroutine_table[coroutine_running-1].stack);
coroutine_table[coroutine_running-1] = (struct coroutine){0};
}