Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
write java class files into CMAKE_CURRENT_BINARY_DIR, not into source dir
[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)->resume = NULL;
26
27   (*factory)->name = "base context factory";
28 }
29
30 int smx_ctx_base_factory_finalize(smx_context_factory_t * factory)
31 {
32   free(*factory);
33   *factory = NULL;
34   return 0;
35 }
36
37 smx_context_t
38 smx_ctx_base_factory_create_context_sized(size_t size,
39                                           xbt_main_func_t code, int argc,
40                                           char **argv,
41                                           void_f_pvoid_t cleanup_func,
42                                           void *cleanup_arg)
43 {
44   smx_context_t context = xbt_malloc0(size);
45
46   /* If the user provided a function for the process then use it
47      otherwise is the context for maestro */
48   if (code) {
49     context->cleanup_func = cleanup_func;
50     context->cleanup_arg = cleanup_arg;
51     context->argc = argc;
52     context->argv = argv;
53     context->code = code;
54   }
55
56   return context;
57 }
58
59 void smx_ctx_base_free(smx_context_t context)
60 {
61   int i;
62
63   if (context) {
64
65     /* free argv */
66     if (context->argv) {
67       for (i = 0; i < context->argc; i++)
68         if (context->argv[i])
69           free(context->argv[i]);
70
71       free(context->argv);
72     }
73
74     /* free structure */
75     free(context);
76   }
77 }
78
79 void smx_ctx_base_stop(smx_context_t context)
80 {
81
82   if (context->cleanup_func)
83     (*(context->cleanup_func)) (context->cleanup_arg);
84
85 }