Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
proper use of the HAVE_TRACING variable defined by Cmake through -Dtracing=on
[simgrid.git] / src / simix / smx_context_base.c
1 /* context_base - Code factorization accross context switching implementations */
2
3 /* Copyright (c) 2010 the SimGrid team. All right reserved */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8
9 #include "xbt/function_types.h"
10 #include "smx_context_private.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(bindings);
13
14 void smx_ctx_base_factory_init(smx_context_factory_t * factory) {
15   /* instantiate the context factory */
16   *factory = xbt_new0(s_smx_context_factory_t, 1);
17
18   (*factory)->create_context = NULL;
19   (*factory)->finalize = smx_ctx_base_factory_finalize;
20   (*factory)->free = smx_ctx_base_free;
21   (*factory)->stop = smx_ctx_base_stop;
22   (*factory)->suspend = NULL;
23   (*factory)->resume = NULL;
24
25   (*factory)->name = "base context factory";
26 }
27
28 int smx_ctx_base_factory_finalize(smx_context_factory_t * factory) {
29   free(*factory);
30   *factory = NULL;
31   return 0;
32 }
33
34 smx_context_t
35 smx_ctx_base_factory_create_context_sized(size_t size,
36     xbt_main_func_t code, int argc, char** argv,
37     void_f_pvoid_t cleanup_func, void* cleanup_arg)
38 {
39   smx_context_t context = xbt_malloc0(size);
40
41   /* If the user provided a function for the process then use it
42      otherwise is the context for maestro */
43   if(code){
44     context->cleanup_func = cleanup_func;
45     context->cleanup_arg = cleanup_arg;
46     context->argc = argc;
47     context->argv = argv;
48     context->code = code;
49   }
50     
51   return context;
52 }
53
54 void smx_ctx_base_free(smx_context_t context) {
55   int i;
56
57   if (context) {
58
59     /* free argv */
60     if (context->argv) {
61       for (i = 0; i < context->argc; i++)
62         if (context->argv[i])
63           free(context->argv[i]);
64
65       free(context->argv);
66     }
67
68     /* free structure */
69     free(context);
70   }
71
72
73 void smx_ctx_base_stop(smx_context_t context) {
74
75   if (context->cleanup_func)
76     (*(context->cleanup_func)) (context->cleanup_arg);
77
78 }