Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ef9e4267488642fc960b6c1b9c443bbd9fd32ca8
[simgrid.git] / tools / cmake / test_prog / prog_AC_CHECK_MCSC.c
1 /* Copyright (c) 2010, 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <ucontext.h>
10
11 ucontext_t uc_child;
12 ucontext_t uc_main;
13
14 static void child(void)
15 {
16   if (swapcontext(&uc_child, &uc_main) != 0)
17     exit(2);
18 }
19
20 int main(int argc, char *argv[])
21 {
22   FILE *fp;
23   void *stack;
24
25   /* the default is that it fails */
26   if ((fp = fopen("conftestval", "w")) == NULL)
27     exit(3);
28   fprintf(fp, "no\n");
29   fclose(fp);
30
31   /* configure a child user-space context */
32   if ((stack = malloc(64 * 1024)) == NULL)
33     exit(4);
34   if (getcontext(&uc_child) != 0)
35     exit(5);
36   uc_child.uc_link = NULL;
37   uc_child.uc_stack.ss_sp = (char *) stack + (32 * 1024);
38   uc_child.uc_stack.ss_size = 32 * 1024;
39   uc_child.uc_stack.ss_flags = 0;
40   makecontext(&uc_child, child, 0);
41
42   /* switch into the user context */
43   if (swapcontext(&uc_main, &uc_child) != 0)
44     exit(6);
45
46   /* Fine, child came home */
47   if ((fp = fopen("conftestval", "w")) == NULL)
48     exit(7);
49   fprintf(fp, "yes\n");
50   fclose(fp);
51
52   /* die successfully */
53   exit(0);
54   return 1;
55 }