Patchwork split stack patch committed: More split stack support for coroutines

login
register
mail settings
Submitter Ian Taylor
Date Nov. 28, 2011, 5:44 a.m.
Message ID <mcr39d86bst.fsf@dhcp-172-18-216-180.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/127930/
State New
Headers show

Comments

Ian Taylor - Nov. 28, 2011, 5:44 a.m.
This patch adds some more split stack runtime support for coroutines.
This is just a couple of small functions: one to reset an existing split
stack context to use the initial stack, and one to release an existing
split stack context.  Bootstrapped and tested on
x86_64-unknown-linux-gnu.  Committed to mainline.

Ian


2011-11-27  Ian Lance Taylor  <iant@google.com>

	* generic-morestack.c (__splitstack_find): Check for NULL old
	stack value.
	(__splitstack_resetcontext): New function.
	(__splitstack_releasecontext): New function.
	* libgcc-std.ver.in: Add new functions to GCC_4.7.0.

Patch

Index: generic-morestack.c
===================================================================
--- generic-morestack.c	(revision 181628)
+++ generic-morestack.c	(working copy)
@@ -115,6 +115,14 @@  extern void *
 __splitstack_makecontext (size_t, void *context[10], size_t *)
   __attribute__ ((visibility ("default")));
 
+extern void *
+__splitstack_resetcontext (void *context[10], size_t *)
+  __attribute__ ((visibility ("default")));
+
+extern void
+__splitstack_releasecontext (void *context[10])
+  __attribute__ ((visibility ("default")));
+
 extern void
 __splitstack_block_signals_context (void *context[10], int *, int *)
   __attribute__ ((visibility ("default")));
@@ -911,15 +919,23 @@  __splitstack_find (void *segment_arg, vo
 
   nsp = (char *) segment->old_stack;
 
+  if (nsp == NULL)
+    {
+      /* We've reached the top of the stack.  */
+      *next_segment = (void *) (uintptr_type) 2;
+    }
+  else
+    {
 #if defined (__x86_64__)
-  nsp -= 12 * sizeof (void *);
+      nsp -= 12 * sizeof (void *);
 #elif defined (__i386__)
-  nsp -= 6 * sizeof (void *);
+      nsp -= 6 * sizeof (void *);
 #else
 #error "unrecognized target"
 #endif
 
-  *next_sp = (void *) nsp;
+      *next_sp = (void *) nsp;
+    }
 
 #ifdef STACK_GROWS_DOWNWARD
   *len = (char *) (segment + 1) + segment->size - (char *) sp;
@@ -1037,6 +1053,60 @@  __splitstack_makecontext (size_t stack_s
   return (void *) (segment + 1);
 }
 
+/* Given an existing split stack context, reset it back to the start
+   of the stack.  Return the stack pointer and size, appropriate for
+   use with makecontext.  This may be used if a coroutine exits, in
+   order to reuse the stack segments for a new coroutine.  */
+
+void *
+__splitstack_resetcontext (void *context[10], size_t *size)
+{
+  struct stack_segment *segment;
+  void *initial_sp;
+  size_t initial_size;
+  void *ret;
+
+  /* Reset the context assuming that MORESTACK_SEGMENTS, INITIAL_SP
+     and INITIAL_SP_LEN are correct.  */
+
+  segment = context[MORESTACK_SEGMENTS];
+  context[CURRENT_SEGMENT] = segment;
+  context[CURRENT_STACK] = NULL;
+  if (segment == NULL)
+    {
+      initial_sp = context[INITIAL_SP];
+      initial_size = (uintptr_type) context[INITIAL_SP_LEN];
+      ret = initial_sp;
+#ifdef STACK_GROWS_DOWNWARD
+      ret = (void *) ((char *) ret - initial_size);
+#endif
+    }
+  else
+    {
+#ifdef STACK_GROWS_DOWNWARD
+      initial_sp = (void *) ((char *) (segment + 1) + segment->size);
+#else
+      initial_sp = (void *) (segment + 1);
+#endif
+      initial_size = segment->size;
+      ret = (void *) (segment + 1);
+    }
+  context[STACK_GUARD] = __morestack_make_guard (initial_sp, initial_size);
+  context[BLOCK_SIGNALS] = NULL;
+  *size = initial_size;
+  return ret;
+}
+
+/* Release all the memory associated with a splitstack context.  This
+   may be used if a coroutine exits and the associated stack should be
+   freed.  */
+
+void
+__splitstack_releasecontext (void *context[10])
+{
+  __morestack_release_segments (context[MORESTACK_SEGMENTS], 1);
+}
+
 /* Like __splitstack_block_signals, but operating on CONTEXT, rather
    than on the current state.  */
 
Index: libgcc-std.ver.in
===================================================================
--- libgcc-std.ver.in	(revision 181628)
+++ libgcc-std.ver.in	(working copy)
@@ -1932,4 +1932,6 @@  GCC_4.7.0 {
   __splitstack_makecontext
   __splitstack_block_signals_context
   __splitstack_find_context
+  __splitstack_resetcontext
+  __splitstack_releasecontext
 }