Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Gcc is *very* permissive with pointers to functions. If we declare them as function...
[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 /* Functions to handle gras_procdata_t->libdata cells*/
24 typedef struct {
25    char *name;
26    pvoid_f_void_t constructor;
27    void_f_pvoid_t destructor;
28 } s_gras_procdata_fabric_t, *gras_procdata_fabric_t;
29
30 static xbt_dynar_t _gras_procdata_fabrics = NULL; /* content: s_gras_procdata_fabric_t */
31
32 static void gras_procdata_fabric_free(void *fab) {
33    free( ((gras_procdata_fabric_t)fab)->name );
34 }
35
36 /** @brief declare the functions in charge of creating/destructing the procdata of a module
37  *  
38  *  This is intended to be called from the gras_<module>_register function.
39  *  This returns the module ID you can use for gras_libdata_by_id()
40  */
41 int gras_procdata_add(const char *name, pvoid_f_void_t constructor,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->constructor = constructor;
55    fab->destructor  = destructor;
56    
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   return gras_libdata_by_name_from_procdata(name,pd);
77 }
78
79 /* this function is splitted from previous to allow SG to get libdata from remote hosts */
80 void *gras_libdata_by_name_from_procdata(const char*name, gras_procdata_t* pd) {
81   void *res=NULL;
82   xbt_ex_t e;
83   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
84      /* Damn, some new modules were added since procdata_init(). Amok? */
85      /* Get 'em all */
86      gras_procdata_init();     
87   }
88   TRY {
89     res = xbt_set_get_by_name(pd->libdata, name);
90   } CATCH(e) {
91     RETHROW1("Cannot retrieve the libdata associated to %s: %s",name);
92   }   
93   return res;
94 }
95
96 void *gras_libdata_by_id(int id) {
97   gras_procdata_t *pd=gras_procdata_get();
98   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
99      /* Damn, some new modules were added since procdata_init(). Amok? */
100      /* Get 'em all */
101      gras_procdata_init();     
102   }
103   return xbt_set_get_by_id(pd->libdata, id);
104 }
105
106
107 void
108 gras_procdata_init() {
109   gras_procdata_t *pd=gras_procdata_get();
110   s_gras_procdata_fabric_t fab;
111    
112   int cursor;
113    
114   xbt_ex_t e;
115   xbt_set_elm_t elem;
116
117   if (!pd->libdata) {
118      pd->userdata  = NULL;
119      pd->libdata   = xbt_set_new();
120   }
121   
122   xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){ 
123     volatile int found = 0;
124      
125     if (cursor+1 <= xbt_set_length(pd->libdata)) {
126        DEBUG2("Skip fabric %d: there is already %ld libdata",
127              cursor, xbt_set_length(pd->libdata));
128        continue; /* allow to recall this function to get recently added fabrics */
129     }
130     DEBUG2("Go ahead for cursor %d, there is %ld libdata",
131           cursor,xbt_set_length(pd->libdata));
132      
133     xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
134     DEBUG1("Create the procdata for %s",fab.name);
135     /* Check for our own errors */
136     TRY {
137       xbt_set_get_by_name(pd->libdata, fab.name);
138       found = 1;
139     } CATCH(e) {
140       xbt_ex_free(e);
141       found = 0;
142     }
143     if (found)
144       THROW1(unknown_error,0,"MayDay: two modules use '%s' as libdata name", fab.name);
145     
146     /* Add the data in place, after some more sanity checking */
147     elem = (*fab.constructor)();
148     if (elem->name_len && elem->name_len != strlen(elem->name)) {
149        elem->name_len = strlen(elem->name);
150        WARN1("Module '%s' constructor is borken: it does not set elem->name_len",
151              fab.name);
152     }
153     xbt_set_add(pd->libdata, elem, fab.destructor);
154   }
155 }
156
157 void
158 gras_procdata_exit() {
159   int len;
160   gras_procdata_t *pd=gras_procdata_get();
161
162   xbt_set_free(&( pd->libdata ));
163   
164   /* Remove procdata in reverse order wrt creation */
165   while ((len=xbt_dynar_length(_gras_procdata_fabrics))) {
166     xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL);
167   }
168   xbt_dynar_free( & _gras_procdata_fabrics );
169 }
170
171
172 const char *gras_os_hostport() {
173    static char *res=NULL;
174    if (res)
175      free(res); /* my port may have changed */
176    res = bprintf("%s:%d",gras_os_myname(),gras_os_myport());
177    return (const char*)res;
178 }