Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9fb983d1dd00287a9abea39fa754945bb487c7a0
[simgrid.git] / src / gras / Virtu / sg_process.c
1 /* $Id$ */
2
3 /* process_sg - GRAS process handling on simulator                          */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003,2004 da GRAS posse.                                   */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include "gras/Virtu/virtu_sg.h"
12
13 GRAS_LOG_EXTERNAL_CATEGORY(process);
14 GRAS_LOG_DEFAULT_CATEGORY(process);
15
16 gras_error_t
17 gras_process_init() {
18   gras_error_t errcode;
19   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
20   gras_procdata_t *pd;
21   gras_sg_portrec_t prraw,pr;
22   int i;
23   
24   if (!(pd=gras_new(gras_procdata_t,1))) 
25     RAISE_MALLOC;
26
27   if (MSG_process_set_data(MSG_process_self(),(void*)pd) != MSG_OK) {
28     return unknown_error;
29   }
30   TRY(gras_procdata_init());
31
32   if (!hd) {
33     if (!(hd=gras_new(gras_hostdata_t,1)))
34       RAISE_MALLOC;
35
36     TRY(gras_dynar_new(&(hd->ports),sizeof(gras_sg_portrec_t),NULL));
37
38     memset(hd->proc, 0, sizeof(hd->proc[0]) * GRAS_MAX_CHANNEL); 
39
40     if (MSG_host_set_data(MSG_host_self(),(void*)hd) != MSG_OK) {
41       return unknown_error;
42     }
43   }
44   
45   /* take a free channel for this process */
46   for (i=0; i<GRAS_MAX_CHANNEL && hd->proc[i]; i++);
47   if (i == GRAS_MAX_CHANNEL) 
48     RAISE2(system_error,
49            "GRAS: Can't add a new process on %s, because all channel are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS\n.",
50             MSG_host_get_name(MSG_host_self()),GRAS_MAX_CHANNEL);
51
52   pd->chan = i;
53   hd->proc[ i ] = MSG_process_self_PID();
54
55   /* regiter it to the ports structure */
56   pr.port = -1;
57   pr.tochan = i;
58   pr.raw = 0;
59   TRY(gras_dynar_push(hd->ports,&pr));
60
61   /* take a free RAW channel for this process */
62   for (i=0; i<GRAS_MAX_CHANNEL && hd->proc[i]; i++);
63   if (i == GRAS_MAX_CHANNEL) {
64     RAISE2(system_error,
65            "GRAS: Can't add a new process on %s, because all channel are already in use. Please increase MAX CHANNEL (which is %d for now) and recompile GRAS\n.",
66             MSG_host_get_name(MSG_host_self()),GRAS_MAX_CHANNEL);
67   }
68   pd->rawChan = i;
69
70   hd->proc[ i ] = MSG_process_self_PID();
71
72   /* regiter it to the ports structure */
73   prraw.port = -1;
74   prraw.tochan = i;
75   prraw.raw = 1;
76   TRY(gras_dynar_push(hd->ports,&prraw));
77
78   VERB2("Creating process '%s' (%d)",
79            MSG_process_get_name(MSG_process_self()),
80            MSG_process_self_PID());
81   return no_error;
82 }
83
84 gras_error_t
85 gras_process_exit() {
86   gras_hostdata_t *hd=(gras_hostdata_t *)MSG_host_get_data(MSG_host_self());
87   gras_procdata_t *pd=gras_procdata_get();
88   int myPID=MSG_process_self_PID();
89   int cpt;
90   gras_sg_portrec_t pr;
91
92   gras_assert0(hd && pd,"Run gras_process_init!!");
93
94   INFO2("GRAS: Finalizing process '%s' (%d)",
95         MSG_process_get_name(MSG_process_self()),MSG_process_self_PID());
96
97   if (gras_dynar_length(pd->msg_queue))
98     WARN1("process %d terminated, but some messages are still queued",
99           MSG_process_self_PID());
100
101   for (cpt=0; cpt< GRAS_MAX_CHANNEL; cpt++)
102     if (myPID == hd->proc[cpt])
103       hd->proc[cpt] = 0;
104
105   gras_dynar_foreach(hd->ports, cpt, pr) {
106     if (pr.port == pd->chan || pr.port == pd->rawChan) {
107       gras_dynar_cursor_rm(hd->ports, &cpt);
108     }
109   }
110
111   return no_error;
112 }
113
114 /* **************************************************************************
115  * Process data
116  * **************************************************************************/
117
118 gras_procdata_t *gras_procdata_get(void) {
119   gras_procdata_t *pd=
120     (gras_procdata_t *)MSG_process_get_data(MSG_process_self());
121
122   gras_assert0(pd,"Run gras_process_init!");
123
124   return pd;
125 }
126