tst-swapcontext1.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Check multiple makecontext calls.
  2. Copyright (C) 2018-2019 Free Software Foundation, Inc.
  3. 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.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <ucontext.h>
  18. static ucontext_t uctx_main, uctx_func1, uctx_func2;
  19. const char *str1 = "\e[31mswapcontext(&uctx_func1, &uctx_main)\e[0m";
  20. const char *str2 = "\e[34mswapcontext(&uctx_func2, &uctx_main)\e[0m";
  21. const char *fmt1 = "\e[31m";
  22. const char *fmt2 = "\e[34m";
  23. #define handle_error(msg) \
  24. do { perror(msg); exit(EXIT_FAILURE); } while (0)
  25. __attribute__((noinline, noclone))
  26. static void
  27. func4(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
  28. {
  29. printf(" %sfunc4: %s\e[0m\n", fmt, str);
  30. if (swapcontext(uocp, ucp) == -1)
  31. handle_error("swapcontext");
  32. printf(" %sfunc4: returning\e[0m\n", fmt);
  33. }
  34. __attribute__((noinline, noclone))
  35. static void
  36. func3(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
  37. {
  38. printf(" %sfunc3: func4(uocp, ucp, str)\e[0m\n", fmt);
  39. func4(uocp, ucp, str, fmt);
  40. printf(" %sfunc3: returning\e[0m\n", fmt);
  41. }
  42. __attribute__((noinline, noclone))
  43. static void
  44. func1(void)
  45. {
  46. while ( 1 )
  47. {
  48. printf(" \e[31mfunc1: func3(&uctx_func1, &uctx_main, str1)\e[0m\n");
  49. func3( &uctx_func1, &uctx_main, str1, fmt1);
  50. }
  51. }
  52. __attribute__((noinline, noclone))
  53. static void
  54. func2(void)
  55. {
  56. while ( 1 )
  57. {
  58. printf(" \e[34mfunc2: func3(&uctx_func2, &uctx_main, str2)\e[0m\n");
  59. func3(&uctx_func2, &uctx_main, str2, fmt2);
  60. }
  61. }
  62. static int
  63. do_test (void)
  64. {
  65. char func1_stack[16384];
  66. char func2_stack[16384];
  67. int i;
  68. if (getcontext(&uctx_func1) == -1)
  69. handle_error("getcontext");
  70. uctx_func1.uc_stack.ss_sp = func1_stack;
  71. uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
  72. uctx_func1.uc_link = &uctx_main;
  73. makecontext(&uctx_func1, func1, 0);
  74. if (getcontext(&uctx_func2) == -1)
  75. handle_error("getcontext");
  76. uctx_func2.uc_stack.ss_sp = func2_stack;
  77. uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
  78. uctx_func2.uc_link = &uctx_func1;
  79. makecontext(&uctx_func2, func2, 0);
  80. for ( i = 0; i < 4; i++ )
  81. {
  82. if (swapcontext(&uctx_main, &uctx_func1) == -1)
  83. handle_error("swapcontext");
  84. printf(" \e[35mmain: swapcontext(&uctx_main, &uctx_func2)\n\e[0m");
  85. if (swapcontext(&uctx_main, &uctx_func2) == -1)
  86. handle_error("swapcontext");
  87. printf(" \e[35mmain: swapcontext(&uctx_main, &uctx_func1)\n\e[0m");
  88. }
  89. printf("main: exiting\n");
  90. exit(EXIT_SUCCESS);
  91. }
  92. #include <support/test-driver.c>