Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement a dynamic parallel execution mode.
[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.
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 "simix/context.h"
12
13 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(bindings);
14
15 void smx_ctx_base_factory_init(smx_context_factory_t *factory)
16 {
17   /* instantiate the context factory */
18   *factory = xbt_new0(s_smx_context_factory_t, 1);
19
20   (*factory)->create_context = NULL;
21   (*factory)->finalize = smx_ctx_base_factory_finalize;
22   (*factory)->free = smx_ctx_base_free;
23   (*factory)->stop = smx_ctx_base_stop;
24   (*factory)->suspend = NULL;
25   (*factory)->runall = NULL;
26   (*factory)->self = smx_ctx_base_self;
27   (*factory)->get_data = smx_ctx_base_get_data;
28   (*factory)->get_thread_id = smx_ctx_base_get_thread_id;
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,
42                                           xbt_main_func_t code, int argc,
43                                           char **argv,
44                                           void_pfn_smxprocess_t cleanup_func,
45                                           void *data)
46 {
47   smx_context_t context = xbt_malloc0(size);
48
49   /* If the user provided a function for the process then use it
50      otherwise is the context for maestro and we should set it as the
51      current context */
52   if (code) {
53     context->cleanup_func = cleanup_func;
54     context->argc = argc;
55     context->argv = argv;
56     context->code = code;
57   }else{
58     smx_current_context = context;
59   }
60   context->data = data;
61
62   return context;
63 }
64
65 void smx_ctx_base_free(smx_context_t context)
66 {
67   int i;
68
69   if (context) {
70
71     /* free argv */
72     if (context->argv) {
73       for (i = 0; i < context->argc; i++)
74         if (context->argv[i])
75           free(context->argv[i]);
76
77       free(context->argv);
78     }
79
80     /* free structure */
81     free(context);
82   }
83 }
84
85 void smx_ctx_base_stop(smx_context_t context)
86 {
87   if (context->cleanup_func)
88     (*(context->cleanup_func)) (context->data);
89 }
90
91 smx_context_t smx_ctx_base_self(void)
92 {
93   return smx_current_context;
94 }
95
96 void *smx_ctx_base_get_data(smx_context_t context)
97 {
98   return context->data;
99 }
100
101 int smx_ctx_base_get_thread_id()
102 {
103   return 0;
104 }