Update.
[glibc.git] / stdlib / tst-setcontext.c
blob66ea0b7d3645cfae17a7401d957fb1f3794d09ec
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <ucontext.h>
24 static ucontext_t ctx[3];
26 static int was_in_f1;
27 static int was_in_f2;
29 static char st2[8192];
31 static void
32 f1 (long a0, long a1, long a2, long a3)
34 printf ("start f1(a0=%lx,a1=%lx,a2=%lx,a3=%lx)\n", a0, a1, a2, a3);
36 if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
38 puts ("arg mismatch");
39 exit (-1);
42 if (swapcontext (&ctx[1], &ctx[2]) != 0)
44 printf ("%s: swapcontext: %m\n", __FUNCTION__);
45 exit (1);
47 puts ("finish f1");
48 was_in_f1 = 1;
51 static void
52 f2 (void)
54 char on_stack[1];
56 puts ("start f2");
58 printf ("&on_stack=%p\n", on_stack);
59 if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
61 printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
62 exit (1);
65 if (swapcontext (&ctx[2], &ctx[1]) != 0)
67 printf ("%s: swapcontext: %m\n", __FUNCTION__);
68 exit (1);
70 puts ("finish f2");
71 was_in_f2 = 1;
74 volatile int global;
76 int
77 main (void)
79 char st1[8192];
81 puts ("making contexts");
82 if (getcontext (&ctx[1]) != 0)
84 if (errno == ENOSYS)
85 exit (0);
87 printf ("%s: getcontext: %m\n", __FUNCTION__);
88 exit (1);
91 /* Play some tricks with this context. */
92 if (++global == 1)
93 if (setcontext (&ctx[1]) != 0)
95 printf ("%s: setcontext: %m\n", __FUNCTION__);
96 exit (1);
98 if (global != 2)
100 printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
101 exit (1);
104 ctx[1].uc_stack.ss_sp = st1;
105 ctx[1].uc_stack.ss_size = sizeof st1;
106 ctx[1].uc_link = &ctx[0];
107 makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
109 if (getcontext (&ctx[2]) != 0)
111 printf ("%s: second getcontext: %m\n", __FUNCTION__);
112 exit (1);
114 ctx[2].uc_stack.ss_sp = st2;
115 ctx[2].uc_stack.ss_size = sizeof st2;
116 ctx[2].uc_link = &ctx[1];
117 makecontext (&ctx[2], f2, 0);
119 puts ("swapping contexts");
120 if (swapcontext (&ctx[0], &ctx[2]) != 0)
122 printf ("%s: swapcontext: %m\n", __FUNCTION__);
123 exit (1);
125 puts ("back at main program");
127 if (was_in_f1 == 0)
129 puts ("didn't reach f1");
130 exit (1);
132 if (was_in_f2 == 0)
134 puts ("didn't reach f2");
135 exit (1);
138 puts ("test succeeded");
139 return 0;