Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill old $Id$ command dating from CVS
[simgrid.git] / src / gras / Virtu / process.c
1 /* process - GRAS process handling (common code for RL and SG)              */
2
3 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "xbt/ex.h"
9 #include "xbt/sysdep.h"
10 #include "xbt/log.h"
11 #include "gras/transport.h"
12 #include "gras/datadesc.h"
13 #include "gras/messages.h"
14 #include "gras_modinter.h"
15
16 #include "gras/Virtu/virtu_private.h"
17
18 XBT_LOG_NEW_SUBCATEGORY(gras_virtu, gras, "Virtualization code");
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_virtu_process, gras_virtu,
20                                 "Process manipulation code");
21
22 /* Functions to handle gras_procdata_t->libdata cells*/
23 typedef struct {
24   char *name;
25   pvoid_f_void_t constructor;
26   void_f_pvoid_t destructor;
27 } s_gras_procdata_fabric_t, *gras_procdata_fabric_t;
28
29 static xbt_dynar_t _gras_procdata_fabrics = NULL;       /* content: s_gras_procdata_fabric_t */
30
31 static void gras_procdata_fabric_free(void *fab)
32 {
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,
42                       void_f_pvoid_t destructor)
43 {
44
45   gras_procdata_fabric_t fab;
46
47   if (!_gras_procdata_fabrics) {
48     /* create the dynar if needed */
49     _gras_procdata_fabrics = xbt_dynar_new(sizeof(s_gras_procdata_fabric_t),
50                                            gras_procdata_fabric_free);
51   }
52
53   fab = xbt_dynar_push_ptr(_gras_procdata_fabrics);
54
55   fab->name = xbt_strdup(name);
56   fab->constructor = constructor;
57   fab->destructor = destructor;
58
59   return xbt_dynar_length(_gras_procdata_fabrics) - 1;
60 }
61
62 /* **************************************************************************
63  * Process data
64  * **************************************************************************/
65 void *gras_userdata_get(void)
66 {
67   gras_procdata_t *pd = gras_procdata_get();
68   return pd->userdata;
69 }
70
71 void *gras_userdata_set(void *ud)
72 {
73   gras_procdata_t *pd = gras_procdata_get();
74
75   pd->userdata = ud;
76   return ud;
77 }
78
79 void *gras_libdata_by_name(const char *name)
80 {
81   gras_procdata_t *pd = gras_procdata_get();
82   return gras_libdata_by_name_from_procdata(name, pd);
83 }
84
85 /* this function is splitted from previous to allow SG to get libdata from remote hosts */
86 void *gras_libdata_by_name_from_procdata(const char *name,
87                                          gras_procdata_t * pd)
88 {
89   void *res = NULL;
90   xbt_ex_t e;
91   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
92     /* Damn, some new modules were added since procdata_init(). Amok? */
93     /* Get 'em all */
94     gras_procdata_init();
95   }
96   TRY {
97     res = xbt_set_get_by_name(pd->libdata, name);
98   }
99   CATCH(e) {
100     RETHROW1("Cannot retrieve the libdata associated to %s: %s", name);
101   }
102   return res;
103 }
104
105 void *gras_libdata_by_id(int id)
106 {
107   gras_procdata_t *pd = gras_procdata_get();
108   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
109     /* Damn, some new modules were added since procdata_init(). Amok? */
110     /* Get 'em all */
111     gras_procdata_init();
112   }
113   return xbt_set_get_by_id(pd->libdata, id);
114 }
115
116
117 void gras_procdata_init()
118 {
119   gras_procdata_t *pd = gras_procdata_get();
120   s_gras_procdata_fabric_t fab;
121
122   unsigned int cursor;
123
124   xbt_ex_t e;
125   xbt_set_elm_t elem;
126
127   if (!pd->libdata) {
128     pd->userdata = NULL;
129     pd->libdata = xbt_set_new();
130   }
131
132   xbt_dynar_foreach(_gras_procdata_fabrics, cursor, fab) {
133     volatile int found = 0;
134
135     if (cursor + 1 <= xbt_set_length(pd->libdata)) {
136       DEBUG2("Skip fabric %d: there is already %ld libdata",
137              cursor, xbt_set_length(pd->libdata));
138       continue;                 /* allow to recall this function to get recently added fabrics */
139     }
140     DEBUG2("Go ahead for cursor %d, there is %ld libdata",
141            cursor, xbt_set_length(pd->libdata));
142
143     xbt_assert1(fab.name, "Name of fabric #%d is NULL!", cursor);
144     DEBUG1("Create the procdata for %s", fab.name);
145     /* Check for our own errors */
146     TRY {
147       xbt_set_get_by_name(pd->libdata, fab.name);
148       found = 1;
149     }
150     CATCH(e) {
151       xbt_ex_free(e);
152       found = 0;
153     }
154     if (found)
155       THROW1(unknown_error, 0, "MayDay: two modules use '%s' as libdata name",
156              fab.name);
157
158     /* Add the data in place, after some more sanity checking */
159     elem = (*(fab.constructor)) ();
160     if (elem->name_len && elem->name_len != strlen(elem->name)) {
161       elem->name_len = strlen(elem->name);
162       WARN1
163         ("Module '%s' constructor is borken: it does not set elem->name_len",
164          fab.name);
165     }
166     xbt_set_add(pd->libdata, elem, fab.destructor);
167   }
168 }
169
170 void gras_procdata_exit()
171 {
172   int len;
173   gras_procdata_t *pd = gras_procdata_get();
174
175   xbt_set_free(&(pd->libdata));
176
177   /* Remove procdata in reverse order wrt creation */
178   while ((len = xbt_dynar_length(_gras_procdata_fabrics))) {
179     xbt_dynar_remove_at(_gras_procdata_fabrics, len - 1, NULL);
180   }
181   xbt_dynar_free(&_gras_procdata_fabrics);
182 }
183
184
185 const char *gras_os_hostport()
186 {
187   static char *res = NULL;
188   if (res)
189     free(res);                  /* my port may have changed */
190   res = bprintf("%s:%d", gras_os_myname(), gras_os_myport());
191   return (const char *) res;
192 }