1 /* Complete Context Control
2 Copyright (C) 1991-2017 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program 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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, if not, see <http://www.gnu.org/licenses/>.
24 /* Set by the signal handler. */
25 static volatile int expired
;
28 static ucontext_t uc
[3];
30 /* We do only a certain number of switches. */
34 /* This is the function doing the work. It is just a
35 skeleton, real code has to be filled in. */
42 /* This is where the work would be done. */
49 /* Regularly the @var{expire} variable must be checked. */
52 /* We do not want the program to run forever. */
56 printf ("\nswitching from %d to %d\n", n
, 3 - n
);
58 /* Switch to the other context, saving the current one. */
59 swapcontext (&uc
[n
], &uc
[3 - n
]);
64 /* This is the signal handler which simply set the variable. */
80 /* Initialize the data structures for the interval timer. */
81 sa
.sa_flags
= SA_RESTART
;
82 sigfillset (&sa
.sa_mask
);
83 sa
.sa_handler
= handler
;
84 it
.it_interval
.tv_sec
= 0;
85 it
.it_interval
.tv_usec
= 1;
86 it
.it_value
= it
.it_interval
;
88 /* Install the timer and get the context we can manipulate. */
89 if (sigaction (SIGPROF
, &sa
, NULL
) < 0
90 || setitimer (ITIMER_PROF
, &it
, NULL
) < 0
91 || getcontext (&uc
[1]) == -1
92 || getcontext (&uc
[2]) == -1)
95 /* Create a context with a separate stack which causes the
96 function @code{f} to be call with the parameter @code{1}.
97 Note that the @code{uc_link} points to the main context
98 which will cause the program to terminate once the function
100 uc
[1].uc_link
= &uc
[0];
101 uc
[1].uc_stack
.ss_sp
= st1
;
102 uc
[1].uc_stack
.ss_size
= sizeof st1
;
103 makecontext (&uc
[1], (void (*) (void)) f
, 1, 1);
105 /* Similarly, but @code{2} is passed as the parameter to @code{f}. */
106 uc
[2].uc_link
= &uc
[0];
107 uc
[2].uc_stack
.ss_sp
= st2
;
108 uc
[2].uc_stack
.ss_size
= sizeof st2
;
109 makecontext (&uc
[2], (void (*) (void)) f
, 1, 2);
112 swapcontext (&uc
[0], &uc
[1]);