Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[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 "smx_context_private.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
28   (*factory)->name = "base context factory";
29 }
30
31 int smx_ctx_base_factory_finalize(smx_context_factory_t * factory)
32 {
33   free(*factory);
34   *factory = NULL;
35   return 0;
36 }
37
38 smx_context_t
39 smx_ctx_base_factory_create_context_sized(size_t size,
40                                           xbt_main_func_t code, int argc,
41                                           char **argv,
42                                           void_pfn_smxprocess_t cleanup_func,
43                                           smx_process_t process)
44 {
45   smx_context_t context = xbt_malloc0(size);
46
47   /* If the user provided a function for the process then use it
48      otherwise is the context for maestro */
49   if (code) {
50     context->cleanup_func = cleanup_func;
51     context->process = process;
52     context->argc = argc;
53     context->argv = argv;
54     context->code = code;
55   }
56
57   return context;
58 }
59
60 void smx_ctx_base_free(smx_context_t context)
61 {
62   int i;
63
64   if (context) {
65
66     /* free argv */
67     if (context->argv) {
68       for (i = 0; i < context->argc; i++)
69         if (context->argv[i])
70           free(context->argv[i]);
71
72       free(context->argv);
73     }
74
75     /* free structure */
76     free(context);
77   }
78 }
79
80 void smx_ctx_base_stop(smx_context_t context)
81 {
82
83   if (context->cleanup_func)
84     (*(context->cleanup_func)) (context->process);
85
86 }
87
88 smx_process_t smx_ctx_base_self(void)
89 {
90   return simix_global->current_process;
91 }