Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
1e69da53967b4bb4abbd85bd7cbe736342446bc9
[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 *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  *  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 creator,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->creator    = creator;
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   TRY {
80     res = xbt_set_get_by_name(pd->libdata, name);
81   } CATCH(e) {
82     RETHROW1("Cannot retrive the libdata associated to %s: %s",name);
83   }   
84   return res;
85 }
86
87 void *gras_libdata_by_id(int id) {
88   gras_procdata_t *pd=gras_procdata_get();
89   return xbt_set_get_by_id(pd->libdata, id);
90 }
91
92 void
93 gras_procdata_init() {
94   gras_procdata_t *pd=gras_procdata_get();
95   s_gras_procdata_fabric_t fab;
96    
97   int cursor;
98    
99   xbt_ex_t e;
100   void *data;
101
102   pd->userdata  = NULL;
103   pd->libdata   = xbt_set_new();
104    
105   xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){
106     volatile int found = 0;
107      
108     xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
109     DEBUG1("Create the procdata for %s",fab.name);
110     /* Check for our own errors */
111     TRY {
112       data = xbt_set_get_by_name(pd->libdata, fab.name);
113       found = 1;
114     } CATCH(e) {
115       xbt_ex_free(e);
116       found = 0;
117     }
118     if (found)
119       THROW1(unknown_error,0,"MayDay: two modules use '%s' as libdata name", fab.name);
120     
121     /* Add the data in place */
122     xbt_set_add(pd->libdata, (fab.creator)(), fab.destructor);
123   }
124 }
125
126 void
127 gras_procdata_exit() {
128   int len;
129   gras_procdata_t *pd=gras_procdata_get();
130
131   xbt_set_free(&( pd->libdata ));
132   
133   /* Remove procdata in reverse order wrt creation */
134   while ((len=xbt_dynar_length(_gras_procdata_fabrics))) {
135     xbt_dynar_remove_at(_gras_procdata_fabrics,len-1,NULL);
136   }
137   xbt_dynar_free( & _gras_procdata_fabrics );
138 }