Blame stdlib/tst-swapcontext1.c

Packit 6c4009
/* Check multiple makecontext calls.
Packit 6c4009
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <ucontext.h>
Packit 6c4009
Packit 6c4009
static ucontext_t uctx_main, uctx_func1, uctx_func2;
Packit 6c4009
const char *str1 = "\e[31mswapcontext(&uctx_func1, &uctx_main)\e[0m";
Packit 6c4009
const char *str2 = "\e[34mswapcontext(&uctx_func2, &uctx_main)\e[0m";
Packit 6c4009
const char *fmt1 = "\e[31m";
Packit 6c4009
const char *fmt2 = "\e[34m";
Packit 6c4009
Packit 6c4009
#define handle_error(msg) \
Packit 6c4009
  do { perror(msg); exit(EXIT_FAILURE); } while (0)
Packit 6c4009
Packit 6c4009
__attribute__((noinline, noclone))
Packit 6c4009
static void
Packit 6c4009
func4(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
Packit 6c4009
{
Packit 6c4009
  printf("      %sfunc4: %s\e[0m\n", fmt, str);
Packit 6c4009
  if (swapcontext(uocp, ucp) == -1)
Packit 6c4009
    handle_error("swapcontext");
Packit 6c4009
  printf("      %sfunc4: returning\e[0m\n", fmt);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
__attribute__((noinline, noclone))
Packit 6c4009
static void
Packit 6c4009
func3(ucontext_t *uocp, ucontext_t *ucp, const char *str, const char *fmt)
Packit 6c4009
{
Packit 6c4009
  printf("    %sfunc3: func4(uocp, ucp, str)\e[0m\n", fmt);
Packit 6c4009
  func4(uocp, ucp, str, fmt);
Packit 6c4009
  printf("    %sfunc3: returning\e[0m\n", fmt);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
__attribute__((noinline, noclone))
Packit 6c4009
static void
Packit 6c4009
func1(void)
Packit 6c4009
{
Packit 6c4009
  while ( 1 )
Packit 6c4009
    {
Packit 6c4009
      printf("  \e[31mfunc1: func3(&uctx_func1, &uctx_main, str1)\e[0m\n");
Packit 6c4009
      func3( &uctx_func1, &uctx_main, str1, fmt1);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
__attribute__((noinline, noclone))
Packit 6c4009
static void
Packit 6c4009
func2(void)
Packit 6c4009
{
Packit 6c4009
  while ( 1 )
Packit 6c4009
    {
Packit 6c4009
      printf("  \e[34mfunc2: func3(&uctx_func2, &uctx_main, str2)\e[0m\n");
Packit 6c4009
      func3(&uctx_func2, &uctx_main, str2, fmt2);
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  char func1_stack[16384];
Packit 6c4009
  char func2_stack[16384];
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  if (getcontext(&uctx_func1) == -1)
Packit 6c4009
    handle_error("getcontext");
Packit 6c4009
  uctx_func1.uc_stack.ss_sp = func1_stack;
Packit 6c4009
  uctx_func1.uc_stack.ss_size = sizeof(func1_stack);
Packit 6c4009
  uctx_func1.uc_link = &uctx_main;
Packit 6c4009
  makecontext(&uctx_func1, func1, 0);
Packit 6c4009
Packit 6c4009
  if (getcontext(&uctx_func2) == -1)
Packit 6c4009
    handle_error("getcontext");
Packit 6c4009
  uctx_func2.uc_stack.ss_sp = func2_stack;
Packit 6c4009
  uctx_func2.uc_stack.ss_size = sizeof(func2_stack);
Packit 6c4009
  uctx_func2.uc_link = &uctx_func1;
Packit 6c4009
  makecontext(&uctx_func2, func2, 0);
Packit 6c4009
Packit 6c4009
  for ( i = 0; i < 4; i++ )
Packit 6c4009
    {
Packit 6c4009
      if (swapcontext(&uctx_main, &uctx_func1) == -1)
Packit 6c4009
	handle_error("swapcontext");
Packit 6c4009
      printf("        \e[35mmain: swapcontext(&uctx_main, &uctx_func2)\n\e[0m");
Packit 6c4009
      if (swapcontext(&uctx_main, &uctx_func2) == -1)
Packit 6c4009
	handle_error("swapcontext");
Packit 6c4009
      printf("        \e[35mmain: swapcontext(&uctx_main, &uctx_func1)\n\e[0m");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  printf("main: exiting\n");
Packit 6c4009
  exit(EXIT_SUCCESS);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>