Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
provide a backtrace implementation that uses boost.stacktrace
[simgrid.git] / tools / cmake / test_prog / prog_makecontext.c
1 /* Copyright (c) 2010-2018. 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 ucontext_t uc_child;
15 ucontext_t uc_main;
16
17 static void child(void)
18 {
19   /* switch back to the main context */
20   if (swapcontext(&uc_child, &uc_main) != 0)
21     exit(2);
22 }
23
24 int main(int argc, char *argv[])
25 {
26   void *stack = malloc(64 * 1024);
27
28   /* configure a child user-space context */
29   if (stack == NULL)
30     exit(3);
31   if (getcontext(&uc_child) != 0)
32     exit(4);
33   uc_child.uc_link = NULL;
34   uc_child.uc_stack.ss_sp = (char *) stack + (32 * 1024);
35   uc_child.uc_stack.ss_size = 32 * 1024;
36   uc_child.uc_stack.ss_flags = 0;
37   makecontext(&uc_child, child, 0);
38
39   /* switch into the user context */
40   if (swapcontext(&uc_main, &uc_child) != 0)
41     exit(5);
42
43   /* Fine, child came home */
44   printf("yes\n");
45
46   /* die successfully */
47   return 0;
48 }