aboutsummaryrefslogtreecommitdiff
path: root/stdlib
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib')
-rw-r--r--stdlib/Makefile2
-rw-r--r--stdlib/tst-setcontext.c113
2 files changed, 114 insertions, 1 deletions
diff --git a/stdlib/Makefile b/stdlib/Makefile
index 193c42b284..301e2d22f3 100644
--- a/stdlib/Makefile
+++ b/stdlib/Makefile
@@ -59,7 +59,7 @@ test-srcs := tst-fmtmsg
tests := tst-strtol tst-strtod testmb testrand testsort testdiv \
test-canon test-canon2 tst-strtoll tst-environ \
tst-xpg-basename tst-random tst-bsearch tst-limits \
- tst-rand48 bug-strtod
+ tst-rand48 bug-strtod tst-setcontext
# Several mpn functions from GNU MP are used by the strtod function.
diff --git a/stdlib/tst-setcontext.c b/stdlib/tst-setcontext.c
new file mode 100644
index 0000000000..6ee1b514a6
--- /dev/null
+++ b/stdlib/tst-setcontext.c
@@ -0,0 +1,113 @@
+/* Copyright (C) 2001 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <ucontext.h>
+
+static ucontext_t ctx[3];
+
+static int was_in_f1;
+static int was_in_f2;
+
+static void
+f1 (long a0, long a1, long a2, long a3)
+{
+ printf ("start f1(a0=%lx,a1=%lx,a2=%lx,a3=%lx)\n", a0, a1, a2, a3);
+
+ if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
+ {
+ puts ("arg mismatch");
+ exit (-1);
+ }
+
+ if (swapcontext (&ctx[1], &ctx[2]) != 0)
+ {
+ printf ("%s: swapcontext: %m\n", __FUNCTION__);
+ exit (1);
+ }
+ puts ("finish f1");
+ was_in_f1 = 1;
+}
+
+static void
+f2 (void)
+{
+ puts ("start f2");
+ if (swapcontext (&ctx[2], &ctx[1]) != 0)
+ {
+ printf ("%s: swapcontext: %m\n", __FUNCTION__);
+ exit (1);
+ }
+ puts ("finish f2");
+ was_in_f2 = 1;
+}
+
+int
+main (void)
+{
+ char st1[8192];
+ char st2[8192];
+
+ puts ("making contexts");
+ if (getcontext (&ctx[1]) != 0)
+ {
+ if (errno == ENOSYS)
+ exit (0);
+
+ printf ("%s: getcontext: %m\n", __FUNCTION__);
+ exit (1);
+ }
+ ctx[1].uc_stack.ss_sp = st1;
+ ctx[1].uc_stack.ss_size = sizeof st1;
+ ctx[1].uc_link = &ctx[0];
+ makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
+
+ if (getcontext (&ctx[2]) != 0)
+ {
+ printf ("%s: second getcontext: %m\n", __FUNCTION__);
+ exit (1);
+ }
+ ctx[2].uc_stack.ss_sp = st2;
+ ctx[2].uc_stack.ss_size = sizeof st2;
+ ctx[2].uc_link = &ctx[1];
+ makecontext (&ctx[2], f2, 0);
+
+ puts ("swapping contexts");
+ if (swapcontext (&ctx[0], &ctx[2]) != 0)
+ {
+ printf ("%s: swapcontext: %m\n", __FUNCTION__);
+ exit (1);
+ }
+ puts ("back at main program");
+
+ if (was_in_f1 == 0)
+ {
+ puts ("didn't reach f1");
+ exit (1);
+ }
+ if (was_in_f2 == 0)
+ {
+ puts ("didn't reach f2");
+ exit (1);
+ }
+
+ puts ("test succeeded");
+ return 0;
+}