Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[simgrid.git] / tools / cmake / test_prog / prog_makecontext.c
1 /* Copyright (c) 2010-2022. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifdef __APPLE__
7 #define _XOPEN_SOURCE 700
8 #endif
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <ucontext.h>
13
14 unsigned char *stack[64 * 1024];
15 ucontext_t uc_child;
16 ucontext_t uc_main;
17
18 static void child(void)
19 {
20   /* switch back to the main context */
21   if (swapcontext(&uc_child, &uc_main) != 0)
22     exit(2);
23 }
24
25 int main(int argc, char *argv[])
26 {
27   /* configure a child user-space context */
28   if (getcontext(&uc_child) != 0)
29     exit(4);
30   uc_child.uc_link = NULL;
31   uc_child.uc_stack.ss_sp    = stack + (32 * 1024);
32   uc_child.uc_stack.ss_size = 32 * 1024;
33   uc_child.uc_stack.ss_flags = 0;
34   makecontext(&uc_child, child, 0);
35
36   /* switch into the user context */
37   if (swapcontext(&uc_main, &uc_child) != 0)
38     exit(5);
39
40   /* Fine, child came home */
41   printf("yes\n");
42
43   /* die successfully */
44   return 0;
45 }