Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
28ab76ca70c8260e0407459521506d22a3d1a987
[simgrid.git] / src / gras_simix / Virtu / gras_simix_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_simix/Virtu/gras_simix_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
84   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
85      /* Damn, some new modules were added since procdata_init(). Amok? */
86      /* Get 'em all */
87      gras_procdata_init();     
88   }
89    
90   TRY {
91     res = xbt_set_get_by_name(pd->libdata, name);
92   } CATCH(e) {
93     RETHROW1("Cannot retrieve the libdata associated to %s: %s",name);
94   }   
95   return res;
96 }
97
98 void *gras_libdata_by_id(int id) {
99   gras_procdata_t *pd=gras_procdata_get();
100   if (xbt_set_length(pd->libdata) < xbt_dynar_length(_gras_procdata_fabrics)) {
101      /* Damn, some new modules were added since procdata_init(). Amok? */
102      /* Get 'em all */
103      gras_procdata_init();     
104   }
105   return xbt_set_get_by_id(pd->libdata, id);
106 }
107
108
109 void
110 gras_procdata_init() {
111   gras_procdata_t *pd=gras_procdata_get();
112   s_gras_procdata_fabric_t fab;
113    
114   int cursor;
115    
116   xbt_ex_t e;
117   xbt_set_elm_t elem;
118
119   if (!pd->libdata) {
120      pd->userdata  = NULL;
121      pd->libdata   = xbt_set_new();
122   }
123   
124   xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){ 
125     volatile int found = 0;
126      
127     if (cursor+1 <= xbt_set_length(pd->libdata)) {
128        DEBUG2("Skip fabric %d: there is already %ld libdata",
129              cursor, xbt_set_length(pd->libdata));
130        continue; /* allow to recall this function to get recently added fabrics */
131     }
132     DEBUG2("Go ahead for cursor %d, there is %ld libdata",
133           cursor,xbt_set_length(pd->libdata));
134      
135     xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
136     DEBUG1("Create the procdata for %s",fab.name);
137     /* Check for our own errors */
138     TRY {
139       xbt_set_get_by_name(pd->libdata, fab.name);
140       found = 1;
141     } CATCH(e) {
142       xbt_ex_free(e);
143       found = 0;
144     }
145     if (found)
146       THROW1(unknown_error,0,"MayDay: two modules use '%s' as libdata name", fab.name);
147     
148     /* Add the data in place, after some more sanity checking */
149     elem = (fab.constructor)();
150     if (elem->name_len && elem->name_len != strlen(elem->name)) {
151        elem->name_len = strlen(elem->name);
152        WARN1("Module '%s' constructor is borken: it does not set elem->name_len",
153              fab.name);
154     }
155      
156     xbt_set_add(pd->libdata, elem, fab.destructor);
157   }
158 }
159
160 void
161 gras_procdata_exit() {
162   int len;
163   gras_procdata_t *pd=gras_procdata_get();
164
165   xbt_set_free(&( pd->libdata ));
166   
167   /* Remove procdata in reverse order wrt creation */
168   while ((len=xbt_dynar_length(_gras_procdata_fabrics))) {
169     xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL);
170   }
171   xbt_dynar_free( & _gras_procdata_fabrics );
172 }