swapcontext.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Complete Context Control
  2. Copyright (C) 1991-2019 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <signal.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ucontext.h>
  18. #include <sys/time.h>
  19. /* Set by the signal handler. */
  20. static volatile int expired;
  21. /* The contexts. */
  22. static ucontext_t uc[3];
  23. /* We do only a certain number of switches. */
  24. static int switches;
  25. /* This is the function doing the work. It is just a
  26. skeleton, real code has to be filled in. */
  27. static void
  28. f (int n)
  29. {
  30. int m = 0;
  31. while (1)
  32. {
  33. /* This is where the work would be done. */
  34. if (++m % 100 == 0)
  35. {
  36. putchar ('.');
  37. fflush (stdout);
  38. }
  39. /* Regularly the @var{expire} variable must be checked. */
  40. if (expired)
  41. {
  42. /* We do not want the program to run forever. */
  43. if (++switches == 20)
  44. return;
  45. printf ("\nswitching from %d to %d\n", n, 3 - n);
  46. expired = 0;
  47. /* Switch to the other context, saving the current one. */
  48. swapcontext (&uc[n], &uc[3 - n]);
  49. }
  50. }
  51. }
  52. /* This is the signal handler which simply set the variable. */
  53. void
  54. handler (int signal)
  55. {
  56. expired = 1;
  57. }
  58. int
  59. main (void)
  60. {
  61. struct sigaction sa;
  62. struct itimerval it;
  63. char st1[8192];
  64. char st2[8192];
  65. /* Initialize the data structures for the interval timer. */
  66. sa.sa_flags = SA_RESTART;
  67. sigfillset (&sa.sa_mask);
  68. sa.sa_handler = handler;
  69. it.it_interval.tv_sec = 0;
  70. it.it_interval.tv_usec = 1;
  71. it.it_value = it.it_interval;
  72. /* Install the timer and get the context we can manipulate. */
  73. if (sigaction (SIGPROF, &sa, NULL) < 0
  74. || setitimer (ITIMER_PROF, &it, NULL) < 0
  75. || getcontext (&uc[1]) == -1
  76. || getcontext (&uc[2]) == -1)
  77. abort ();
  78. /* Create a context with a separate stack which causes the
  79. function @code{f} to be call with the parameter @code{1}.
  80. Note that the @code{uc_link} points to the main context
  81. which will cause the program to terminate once the function
  82. return. */
  83. uc[1].uc_link = &uc[0];
  84. uc[1].uc_stack.ss_sp = st1;
  85. uc[1].uc_stack.ss_size = sizeof st1;
  86. makecontext (&uc[1], (void (*) (void)) f, 1, 1);
  87. /* Similarly, but @code{2} is passed as the parameter to @code{f}. */
  88. uc[2].uc_link = &uc[0];
  89. uc[2].uc_stack.ss_sp = st2;
  90. uc[2].uc_stack.ss_size = sizeof st2;
  91. makecontext (&uc[2], (void (*) (void)) f, 1, 2);
  92. /* Start running. */
  93. swapcontext (&uc[0], &uc[1]);
  94. putchar ('\n');
  95. return 0;
  96. }