1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;
}
|