Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
new function: gras_socket_meas_accept (mandatory on meas sockets, forbiden on others...
[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/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/error.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_error_t errcode;
76    
77   errcode = xbt_dict_get(pd->libdata, name, &res);
78   xbt_assert2(errcode == no_error, 
79               "Cannot retrive the libdata associated to %s: %s",
80               name, xbt_error_name(errcode));
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_error_t errcode;
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      
100      xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
101      DEBUG1("Create the procdata for %s",fab.name);
102      /* Check for our own errors */
103      errcode = xbt_dict_get(pd->libdata, fab.name, &data);
104      xbt_assert1(errcode == mismatch_error,
105                  "MayDay: two modules use '%s' as libdata name", fab.name);
106      
107      /* Add the data in place */
108      xbt_dict_set(pd->libdata, fab.name, (fab.creator)(), fab.destructor);
109
110   }
111 }
112
113 void
114 gras_procdata_exit() {
115   int len;
116   gras_procdata_t *pd=gras_procdata_get();
117
118   xbt_dict_free(&( pd->libdata ));
119   
120   /* Remove procdata in reverse order wrt creation */
121   while ((len=xbt_dynar_length(_gras_procdata_fabrics))) {
122     xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL);
123   }
124   xbt_dynar_free( & _gras_procdata_fabrics );
125 }