From 4ee871c7812c84631e2e8127302f76df190ffe18 Mon Sep 17 00:00:00 2001 From: "Brian J. Cardiff" Date: Mon, 7 Jan 2019 17:41:35 -0300 Subject: [PATCH] Added functions to get and set the stackbottom of each thread --- include/gc.h | 10 ++++++++++ pthread_support.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/include/gc.h b/include/gc.h index d6fec2b5..1a80154d 100644 --- a/include/gc.h +++ b/include/gc.h @@ -362,6 +362,16 @@ GC_API GC_ATTR_DEPRECATED char *GC_stackbottom; /* GC_call_with_gc_active() and */ /* GC_register_my_thread() instead. */ +/* Sets the cool end of user stack in the specified thread. */ +/* MUST be called with the GC disabled. */ +GC_API void GC_CALL GC_set_stackbottom(void * thread, char * stackbottom); + +/* Returns the cool end of user stack of the current thread/coroutine. */ +/* When a thread starts it will be computed upon creation. */ +/* If GC_switch_to_coroutine() is called, the returned value will match */ +/* the specified value in that function. */ +GC_API char * GC_CALL GC_get_stackbottom(); + GC_API GC_ATTR_DEPRECATED int GC_dont_precollect; /* Do not collect as part of GC */ /* initialization. Should be set only */ diff --git a/pthread_support.c b/pthread_support.c index 78e5d35a..e99c4b81 100644 --- a/pthread_support.c +++ b/pthread_support.c @@ -1369,6 +1369,44 @@ GC_INNER void GC_do_blocking_inner(ptr_t data, void * context GC_ATTR_UNUSED) UNLOCK(); } +/* Sets the cool end of user stack in the specified thread. */ +/* MUST be called with the GC disabled. */ +GC_API void GC_CALL GC_set_stackbottom(void * thread, char * stackbottom) +{ + GC_thread me; + + me = GC_lookup_thread((pthread_t) thread); + + if ((me -> flags & MAIN_THREAD) == 0) { + me -> stack_end = stackbottom; + } else { + GC_stackbottom = stackbottom; + } +} + +/* Returns the cool end of user stack of the current thread/corutine. */ +/* When a thread starts it will be computed upon creation. */ +/* If GC_switch_to_coroutine() is called, the returned value will match */ +/* the specified value in that function. */ +GC_API char * GC_CALL GC_get_stackbottom() +{ + pthread_t self = pthread_self(); + GC_thread me; + char * ret; + + LOCK(); + me = GC_lookup_thread(self); + + if ((me -> flags & MAIN_THREAD) == 0) { + ret = me -> stack_end; + } else { + ret = GC_stackbottom; + } + UNLOCK(); + + return ret; +} + /* GC_call_with_gc_active() has the opposite to GC_do_blocking() */ /* functionality. It might be called from a user function invoked by */ /* GC_do_blocking() to temporarily back allow calling any GC function */