Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix copyright headers
[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   /* instantiate the context factory */
17   *factory = xbt_new0(s_smx_context_factory_t, 1);
18
19   (*factory)->create_context = NULL;
20   (*factory)->finalize = smx_ctx_base_factory_finalize;
21   (*factory)->free = smx_ctx_base_free;
22   (*factory)->stop = smx_ctx_base_stop;
23   (*factory)->suspend = NULL;
24   (*factory)->resume = NULL;
25
26   (*factory)->name = "base context factory";
27 }
28
29 int smx_ctx_base_factory_finalize(smx_context_factory_t * factory) {
30   free(*factory);
31   *factory = NULL;
32   return 0;
33 }
34
35 smx_context_t
36 smx_ctx_base_factory_create_context_sized(size_t size,
37     xbt_main_func_t code, int argc, char** argv,
38     void_f_pvoid_t cleanup_func, void* cleanup_arg)
39 {
40   smx_context_t context = xbt_malloc0(size);
41
42   /* If the user provided a function for the process then use it
43      otherwise is the context for maestro */
44   if(code){
45     context->cleanup_func = cleanup_func;
46     context->cleanup_arg = cleanup_arg;
47     context->argc = argc;
48     context->argv = argv;
49     context->code = code;
50   }
51     
52   return context;
53 }
54
55 void smx_ctx_base_free(smx_context_t context) {
56   int i;
57
58   if (context) {
59
60     /* free argv */
61     if (context->argv) {
62       for (i = 0; i < context->argc; i++)
63         if (context->argv[i])
64           free(context->argv[i]);
65
66       free(context->argv);
67     }
68
69     /* free structure */
70     free(context);
71   }
72
73
74 void smx_ctx_base_stop(smx_context_t context) {
75
76   if (context->cleanup_func)
77     (*(context->cleanup_func)) (context->cleanup_arg);
78
79 }