Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d38324d9b2b9601e7e40fb36fa5a6f1cc301a7d7
[simgrid.git] / src / gras / Virtu / process.c
1 /* $Id$ */
2
3 /* process - GRAS process handling (common code for RL and SG)              */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "xbt/ex.h"
11 #include "xbt/sysdep.h"
12 #include "xbt/log.h"
13 #include "gras/transport.h"
14 #include "gras/datadesc.h"
15 #include "gras/messages.h"
16 #include "gras_modinter.h"
17
18 #include "gras/Virtu/virtu_private.h"
19
20 XBT_LOG_NEW_SUBCATEGORY(virtu,gras,"Virtualization code");
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(process,virtu,"Process manipulation code");
22
23
24 /* Functions to handle gras_procdata_t->libdata cells*/
25 typedef struct {
26    char *name;
27    pvoid_f_void_t *creator;
28    void_f_pvoid_t *destructor;
29 } s_gras_procdata_fabric_t, *gras_procdata_fabric_t;
30
31 static xbt_dynar_t _gras_procdata_fabrics = NULL; /* content: s_gras_procdata_fabric_t */
32
33 static void gras_procdata_fabric_free(void *fab) {
34    free( ((gras_procdata_fabric_t)fab)->name );
35 }
36
37 /** @brief declare the functions in charge of creating/destructing the procdata of a module
38  *  
39  *  This is intended to be called from the gras_<module>_register function.
40  */
41 void gras_procdata_add(const char *name, pvoid_f_void_t creator,void_f_pvoid_t destructor) {
42    
43    gras_procdata_fabric_t fab;
44    
45    if (!_gras_procdata_fabrics) {
46       /* create the dynar if needed */
47       _gras_procdata_fabrics = xbt_dynar_new(sizeof(s_gras_procdata_fabric_t),
48                                              gras_procdata_fabric_free);
49    }
50    
51    fab=xbt_dynar_push_ptr(_gras_procdata_fabrics);
52    
53    fab->name       = xbt_strdup(name);
54    fab->creator    = creator;
55    fab->destructor = destructor;
56 }
57
58 /* **************************************************************************
59  * Process data
60  * **************************************************************************/
61 void *gras_userdata_get(void) {
62   gras_procdata_t *pd=gras_procdata_get();
63   return pd->userdata;
64 }
65
66 void gras_userdata_set(void *ud) {
67   gras_procdata_t *pd=gras_procdata_get();
68
69   pd->userdata = ud;
70 }
71
72 void *gras_libdata_get(const char *name) {
73   gras_procdata_t *pd=gras_procdata_get();
74   void *res;
75   xbt_ex_t e;
76    
77   TRY {
78     res = xbt_dict_get(pd->libdata, name);
79   } CATCH(e) {
80     RETHROW1("Cannot retrive the libdata associated to %s: %s",name);
81   }   
82   return res;
83 }
84
85 void
86 gras_procdata_init() {
87   gras_procdata_t *pd=gras_procdata_get();
88   s_gras_procdata_fabric_t fab;
89    
90   int cursor;
91    
92   xbt_ex_t e;
93   void *data;
94
95   pd->userdata  = NULL;
96   pd->libdata   = xbt_dict_new();
97    
98   xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){
99     int found = 0;
100      
101     xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
102     DEBUG1("Create the procdata for %s",fab.name);
103     /* Check for our own errors */
104     TRY {
105       data = xbt_dict_get(pd->libdata, fab.name);
106       found = 1;
107     } CATCH(e) {
108       xbt_ex_free(e);
109     }
110     xbt_assert1(!found,
111                 "MayDay: two modules use '%s' as libdata name", fab.name);
112     
113     /* Add the data in place */
114     xbt_dict_set(pd->libdata, fab.name, (fab.creator)(), fab.destructor);
115   }
116 }
117
118 void
119 gras_procdata_exit() {
120   int len;
121   gras_procdata_t *pd=gras_procdata_get();
122
123   xbt_dict_free(&( pd->libdata ));
124   
125   /* Remove procdata in reverse order wrt creation */
126   while ((len=xbt_dynar_length(_gras_procdata_fabrics))) {
127     xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL);
128   }
129   xbt_dynar_free( & _gras_procdata_fabrics );
130 }