Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a specific file to manage tracing in SimDag
[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_active())
52     MC_ignore_heap(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   return context;
68 }
69
70 void smx_ctx_base_free(smx_context_t context)
71 {
72   int i;
73   if (context) {
74
75     /* free argv */
76     if (context->argv) {
77       for (i = 0; i < context->argc; i++)
78         free(context->argv[i]);
79
80       free(context->argv);
81     }
82
83     /* free structure */
84     free(context);
85   }
86 }
87
88 void smx_ctx_base_stop(smx_context_t context)
89 {
90   if (context->cleanup_func)
91     context->cleanup_func(context->data);
92   context->iwannadie = 0;
93   simcall_process_cleanup(context->data);
94   context->iwannadie = 1;
95 }
96
97 smx_context_t smx_ctx_base_self(void)
98 {
99   return SIMIX_context_get_current();
100 }
101
102 void *smx_ctx_base_get_data(smx_context_t context)
103 {
104   return context->data;
105 }