Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
589c3fe64a2ef3c5e5c862e14e6e80129d269733
[simgrid.git] / buildtools / Cmake / test_prog / prog_AC_CHECK_MCSC.c
1 /* Copyright (c) 2010. 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 #ifdef _WIN32
10 #include <win32_ucontext.h>
11 #else
12 #include <ucontext.h>
13 #endif
14
15 void child(void);
16
17 ucontext_t uc_child;
18 ucontext_t uc_main;
19
20
21
22 void child(void)
23 {
24     if (swapcontext(&uc_child, &uc_main) != 0)
25         exit(2);
26 }
27
28 int main(int argc, char *argv[])
29 {
30     FILE *fp;
31     void *stack;
32
33     /* the default is that it fails */
34     if ((fp = fopen("conftestval", "w")) == NULL)
35         exit(3);
36     fprintf(fp, "no\n");
37     fclose(fp);
38
39     /* configure a child user-space context */
40     if ((stack = malloc(64*1024)) == NULL)
41         exit(4);
42     if (getcontext(&uc_child) != 0)
43         exit(5);
44     uc_child.uc_link = NULL;
45     uc_child.uc_stack.ss_sp = (char *)stack+(32*1024);
46     uc_child.uc_stack.ss_size = 32*1024;
47     uc_child.uc_stack.ss_flags = 0;
48     makecontext(&uc_child, child, 0);
49
50     /* switch into the user context */
51     if (swapcontext(&uc_main, &uc_child) != 0)
52         exit(6);
53
54     /* Fine, child came home */
55     if ((fp = fopen("conftestval", "w")) == NULL)
56         exit(7);
57     fprintf(fp, "yes\n");
58     fclose(fp);
59
60     /* die successfully */
61     exit(0);
62         return 1;
63 }