Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
model-checker : forget include ucontext.h for struct smx_ctx_sysv
[simgrid.git] / src / simix / smx_context_base.c
1 /* context_base - Code factorization across context switching implementations */
2
3 /* Copyright (c) 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9
10 #include "xbt/function_types.h"
11 #include "simgrid/simix.h"
12 #include "smx_private.h"
13 #include "mc/mc.h"
14
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(bindings);
16
17 void smx_ctx_base_factory_init(smx_context_factory_t *factory)
18 {
19   /* instantiate the context factory */
20   *factory = xbt_new0(s_smx_context_factory_t, 1);
21
22   (*factory)->create_context = NULL;
23   (*factory)->finalize = smx_ctx_base_factory_finalize;
24   (*factory)->free = smx_ctx_base_free;
25   (*factory)->stop = smx_ctx_base_stop;
26   (*factory)->suspend = NULL;
27   (*factory)->runall = NULL;
28   (*factory)->self = smx_ctx_base_self;
29   (*factory)->get_data = smx_ctx_base_get_data;
30
31   (*factory)->name = "base context factory";
32 }
33
34 int smx_ctx_base_factory_finalize(smx_context_factory_t * factory)
35 {
36   free(*factory);
37   *factory = NULL;
38   return 0;
39 }
40
41 smx_context_t
42 smx_ctx_base_factory_create_context_sized(size_t size,
43                                           xbt_main_func_t code, int argc,
44                                           char **argv,
45                                           void_pfn_smxprocess_t cleanup_func,
46                                           void *data)
47 {
48   smx_context_t context = xbt_malloc0(size);
49
50   /* Store the address of the stack in heap to compare it apart of heap comparison */
51   if(MC_IS_ENABLED)   
52     MC_ignore(context, size);
53
54   /* If the user provided a function for the process then use it.
55      Otherwise, it is the context for maestro and we should set it as the
56      current context */
57   if (code) {
58     context->cleanup_func = cleanup_func;
59     context->argc = argc;
60     context->argv = argv;
61     context->code = code;
62   } else {
63     SIMIX_context_set_current(context);
64   }
65   context->data = data;
66
67   if(MC_IS_ENABLED && code)
68     MC_new_stack_area(context, ((smx_process_t)context->data)->name);
69
70   return context;
71 }
72
73 void smx_ctx_base_free(smx_context_t context)
74 {
75   int i;
76   if (context) {
77
78     /* free argv */
79     if (context->argv) {
80       for (i = 0; i < context->argc; i++)
81         free(context->argv[i]);
82
83       free(context->argv);
84     }
85
86     /* free structure */
87     free(context);
88   }
89 }
90
91 void smx_ctx_base_stop(smx_context_t context)
92 {
93   if (context->cleanup_func)
94     context->cleanup_func(context->data);
95   context->iwannadie = 0;
96   simcall_process_cleanup(context->data);
97   context->iwannadie = 1;
98 }
99
100 smx_context_t smx_ctx_base_self(void)
101 {
102   return SIMIX_context_get_current();
103 }
104
105 void *smx_ctx_base_get_data(smx_context_t context)
106 {
107   return context->data;
108 }