Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Tsss... :)
[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/sysdep.h"
11 #include "xbt/log.h"
12 #include "xbt/error.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_DEFAULT_SUBCATEGORY(process,gras,"Process manipulation code");
21
22
23 /* Functions to handle gras_procdata_t->libdata cells*/
24 typedef struct {
25    char *name;
26    pvoid_f_void_t *creator;
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  */
40 void gras_procdata_add(const char *name, pvoid_f_void_t creator,void_f_pvoid_t destructor) {
41    
42    gras_procdata_fabric_t fab;
43    
44    if (!_gras_procdata_fabrics) {
45       /* create the dynar if needed */
46       _gras_procdata_fabrics = xbt_dynar_new(sizeof(s_gras_procdata_fabric_t),
47                                              gras_procdata_fabric_free);
48    }
49    
50    fab=xbt_dynar_push_ptr(_gras_procdata_fabrics);
51    
52    fab->name       = xbt_strdup(name);
53    fab->creator    = creator;
54    fab->destructor = destructor;
55 }
56
57 /* **************************************************************************
58  * Process data
59  * **************************************************************************/
60 void *gras_userdata_get(void) {
61   gras_procdata_t *pd=gras_procdata_get();
62   return pd->userdata;
63 }
64
65 void gras_userdata_set(void *ud) {
66   gras_procdata_t *pd=gras_procdata_get();
67
68   pd->userdata = ud;
69 }
70
71 void *gras_libdata_get(const char *name) {
72   gras_procdata_t *pd=gras_procdata_get();
73   void *res;
74   xbt_error_t errcode;
75    
76   errcode = xbt_dict_get(pd->libdata, name, &res);
77   xbt_assert2(errcode == no_error, 
78               "Cannot retrive the libdata associated to %s: %s",
79               name, xbt_error_name(errcode));
80    
81   return res;
82 }
83
84 void
85 gras_procdata_init() {
86   gras_procdata_t *pd=gras_procdata_get();
87   s_gras_procdata_fabric_t fab;
88    
89   int cursor;
90    
91   xbt_error_t errcode;
92   void *data;
93
94   pd->userdata  = NULL;
95   pd->libdata   = xbt_dict_new();
96    
97   xbt_dynar_foreach(_gras_procdata_fabrics,cursor,fab){
98      
99      xbt_assert1(fab.name,"Name of fabric #%d is NULL!",cursor);
100      DEBUG1("Create the procdata for %s",fab.name);
101      /* Check for our own errors */
102      errcode = xbt_dict_get(pd->libdata, fab.name, &data);
103      xbt_assert1(errcode == mismatch_error,
104                  "MayDay: two modules use '%s' as libdata name", fab.name);
105      
106      /* Add the data in place */
107      xbt_dict_set(pd->libdata, fab.name, (fab.creator)(), fab.destructor);
108
109   }
110 }
111
112 void
113 gras_procdata_exit() {
114   gras_procdata_t *pd=gras_procdata_get();
115
116   xbt_dict_free(&( pd->libdata ));
117 }