Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Give this module a minimal level of testing. I'm a jerk for commiting such shitty...
[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(gras_virtu,gras,"Virtualization code");
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_process,gras_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 *constructor;
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  *  This returns the module ID you can use for gras_libdata_by_id()
41  */
42 int gras_procdata_add(const char *name, pvoid_f_void_t constructor,void_f_pvoid_t destructor) {
43    
44    gras_procdata_fabric_t fab;
45    
46    if (!_gras_procdata_fabrics) {
47       /* create the dynar if needed */
48       _gras_procdata_fabrics = xbt_dynar_new(sizeof(s_gras_procdata_fabric_t),
49                                              gras_procdata_fabric_free);
50    }
51    
52    fab=xbt_dynar_push_ptr(_gras_procdata_fabrics);
53    
54    fab->name        = xbt_strdup(name);
55    fab->constructor = constructor;
56    fab->destructor  = destructor;
57    return xbt_dynar_length(_gras_procdata_fabrics)-1;
58 }
59
60 /* **************************************************************************
61  * Process data
62  * **************************************************************************/
63 void *gras_userdata_get(void) {
64   gras_procdata_t *pd=gras_procdata_get();
65   return pd->userdata;
66 }
67
68 void gras_userdata_set(void *ud) {
69   gras_procdata_t *pd=gras_procdata_get();
70
71   pd->userdata = ud;
72 }
73
74 void *gras_libdata_by_name(const char *name) {
75   gras_procdata_t *pd=gras_procdata_get();
76   void *res=NULL;
77   xbt_ex_t e;
78
79   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
80      /* Damn, some new modules were added since procdata_init(). Amok? */
81      /* Get 'em all */
82      gras_procdata_init();     
83   }
84    
85   TRY {
86     res = xbt_set_get_by_name(pd->libdata, name);
87   } CATCH(e) {
88     RETHROW1("Cannot retrieve the libdata associated to %s: %s",name);
89   }   
90   return res;
91 }
92
93 void *gras_libdata_by_id(int id) {
94   gras_procdata_t *pd=gras_procdata_get();
95   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
96      /* Damn, some new modules were added since procdata_init(). Amok? */
97      /* Get 'em all */
98      gras_procdata_init();     
99   }
100   return xbt_set_get_by_id(pd->libdata, id);
101 }
102
103 void
104 gras_procdata_init() {
105   gras_procdata_t *pd=gras_procdata_get();
106   s_gras_procdata_fabric_t fab;
107    
108   int cursor;
109    
110   xbt_ex_t e;
111   void *data;
112   xbt_set_elm_t elem;
113
114   if (!pd->libdata) {
115      pd->userdata  = NULL;
116      pd->libdata   = xbt_set_new();
117   }
118    
119   xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){ 
120     volatile int found = 0;
121      
122     if (cursor+1 <= xbt_set_length(pd->libdata)) {
123        DEBUG2("Skip fabric %d: there is already %ld libdata",
124              cursor, xbt_set_length(pd->libdata));
125        continue; /* allow to recall this function to get recently added fabrics */
126     }
127     DEBUG2("Go ahead for cursor %d, there is %ld libdata",
128           cursor,xbt_set_length(pd->libdata));
129      
130     xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
131     DEBUG1("Create the procdata for %s",fab.name);
132     /* Check for our own errors */
133     TRY {
134       data = xbt_set_get_by_name(pd->libdata, fab.name);
135       found = 1;
136     } CATCH(e) {
137       xbt_ex_free(e);
138       found = 0;
139     }
140     if (found)
141       THROW1(unknown_error,0,"MayDay: two modules use '%s' as libdata name", fab.name);
142     
143     /* Add the data in place, after some more sanity checking */
144     elem = (fab.constructor)();
145     if (elem->name_len && elem->name_len != strlen(elem->name)) {
146        elem->name_len = strlen(elem->name);
147        WARN1("Module '%s' constructor is borken: it does not set elem->name_len",
148              fab.name);
149     }
150      
151     xbt_set_add(pd->libdata, elem, fab.destructor);
152   }
153 }
154
155 void
156 gras_procdata_exit() {
157   int len;
158   gras_procdata_t *pd=gras_procdata_get();
159
160   xbt_set_free(&( pd->libdata ));
161   
162   /* Remove procdata in reverse order wrt creation */
163   while ((len=xbt_dynar_length(_gras_procdata_fabrics))) {
164     xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL);
165   }
166   xbt_dynar_free( & _gras_procdata_fabrics );
167 }