Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Replace the override word with MC_OVERRIDE
[simgrid.git] / src / simix / smx_context_base.c
1 /* context_base - Code factorization across context switching implementations */
2
3 /* Copyright (c) 2010-2014. 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 #include "xbt/function_types.h"
10 #include "simgrid/simix.h"
11 #include "smx_private.h"
12 #include "mc/mc.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(simix_context);
15
16 void smx_ctx_base_factory_init(smx_context_factory_t *factory)
17 {
18   /* instantiate the context factory */
19   *factory = xbt_new0(s_smx_context_factory_t, 1);
20
21   (*factory)->create_context = NULL;
22   (*factory)->finalize = smx_ctx_base_factory_finalize;
23   (*factory)->free = smx_ctx_base_free;
24   (*factory)->stop = smx_ctx_base_stop;
25   (*factory)->suspend = NULL;
26   (*factory)->runall = NULL;
27   (*factory)->self = smx_ctx_base_self;
28   (*factory)->get_process = smx_ctx_base_get_process;
29
30   (*factory)->name = "base context factory";
31 }
32
33 int smx_ctx_base_factory_finalize(smx_context_factory_t * factory)
34 {
35   free(*factory);
36   *factory = NULL;
37   return 0;
38 }
39
40 smx_context_t
41 smx_ctx_base_factory_create_context_sized(size_t size, xbt_main_func_t code,
42                                           int argc, char **argv,
43                                           void_pfn_smxprocess_t cleanup_func,
44                                           smx_process_t process)
45 {
46   smx_context_t context = xbt_malloc0(size);
47
48   /* Store the address of the stack in heap to compare it apart of heap comparison */
49   if(MC_is_active())
50     MC_ignore_heap(context, size);
51
52   /* If the user provided a function for the process then use it.
53      Otherwise, it is the context for maestro and we should set it as the
54      current context */
55   if (code) {
56     context->cleanup_func = cleanup_func;
57     context->argc = argc;
58     context->argv = argv;
59     context->code = code;
60   } else {
61     SIMIX_context_set_current(context);
62   }
63   context->process = process;
64
65   return context;
66 }
67
68 void smx_ctx_base_free(smx_context_t context)
69 {
70   int i;
71   if (context) {
72
73     /* free argv */
74     if (context->argv) {
75       for (i = 0; i < context->argc; i++)
76         free(context->argv[i]);
77
78       free(context->argv);
79     }
80
81     /* free structure */
82     free(context);
83   }
84 }
85
86 void smx_ctx_base_stop(smx_context_t context)
87 {
88   if (context->cleanup_func)
89     context->cleanup_func(context->process);
90   context->process->suspended = 0;
91   context->iwannadie = 0;
92   simcall_process_cleanup(context->process);
93   context->iwannadie = 1;
94 }
95
96 smx_context_t smx_ctx_base_self(void)
97 {
98   return SIMIX_context_get_current();
99 }
100
101 smx_process_t smx_ctx_base_get_process(smx_context_t context)
102 {
103   return context->process;
104 }