Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Mark as volatile a variable used in a CATCH block
[simgrid.git] / src / gras / Msg / msg.c
index e8f1773..65ffedd 100644 (file)
@@ -272,10 +272,9 @@ gras_msg_wait_ext(double           timeout,
 
   xbt_assert0(msg_got,"msg_got is an output parameter");
 
+  start = gras_os_time();
   VERB1("Waiting for message '%s'",msgt_want?msgt_want->name:"(any)");
 
-  start = now = gras_os_time();
-
   xbt_dynar_foreach(pd->msg_queue,cpt,msg){
     if ( (   !msgt_want || (msg.type->code == msgt_want->code)) 
         && (!expe_want || (!strcmp( gras_socket_peer_name(msg.expe),
@@ -290,10 +289,30 @@ gras_msg_wait_ext(double           timeout,
   }
 
   while (1) {
+    int need_restart;
+    xbt_ex_t e;
+
+  restart_receive: /* Goto here when the receive of a message failed */
+    need_restart=0;
+    now=gras_os_time();
     memset(&msg,sizeof(msg),0);
 
-    msg.expe = gras_trp_select(timeout ? timeout - now + start : 0);
-    gras_msg_recv(msg.expe, &msg);
+    TRY {
+      msg.expe = gras_trp_select(timeout ? timeout - now + start : 0);
+      gras_msg_recv(msg.expe, &msg);
+    } CATCH(e) {
+      if (e.category == system_error &&
+         !strncmp("Socket closed by remote side",e.msg,
+                 strlen("Socket closed by remote side"))) {
+       xbt_ex_free(e);
+       need_restart=1;
+      }        else {
+       RETHROW;
+      }
+    }
+    if (need_restart)
+      goto restart_receive;
+
     DEBUG0("Got a message from the socket");
 
     if ( (   !msgt_want || (msg.type->code == msgt_want->code)) 
@@ -365,7 +384,7 @@ static int gras_msg_wait_or_filter(gras_msg_t msg, void *ctx) {
   xbt_dynar_t dyn=(xbt_dynar_t)ctx;
   int res =  xbt_dynar_member(dyn,msg->type);
   if (res)
-    VERB0("Got matching message");
+    VERB1("Got matching message (type=%s)",msg->type->name);
   else
     VERB0("Got message not matching our expectations");
   return res;
@@ -392,7 +411,7 @@ void gras_msg_wait_or(double         timeout,
                       void          *payload) {
   s_gras_msg_t msg;
 
-  INFO1("Wait %f seconds for several message types",timeout);
+  VERB1("Wait %f seconds for several message types",timeout);
   gras_msg_wait_ext(timeout,
                    NULL, NULL,      
                    &gras_msg_wait_or_filter, (void*)msgt_want,
@@ -443,8 +462,7 @@ gras_msg_send(gras_socket_t   sock,
 
 /** @brief Handle all messages arriving within the given period
  *
- * @param timeOut: How long to wait for incoming messages (in seconds)
- * @return the error code (or no_error).
+ * @param period: How long to wait for incoming messages (in seconds)
  *
  * Messages are dealed with just like gras_msg_handle() would do. The
  * difference is that gras_msg_handle() handles at most one message (or wait up
@@ -460,7 +478,8 @@ gras_msg_handleall(double period) {
   do {
     now=gras_os_time();
     TRY{
-      gras_msg_handle(period - now + begin);
+      if (period - now + begin > 0)
+       gras_msg_handle(period - now + begin);
     } CATCH(e) {
       if (e.category != timeout_error) 
        RETHROW0("Error while waiting for messages: %s");
@@ -468,6 +487,7 @@ gras_msg_handleall(double period) {
     }
   } while (now - begin < period);
 }
+
 /** @brief Handle an incomming message or timer (or wait up to \a timeOut seconds)
  *
  * @param timeOut: How long to wait for incoming messages (in seconds)
@@ -480,7 +500,8 @@ gras_msg_handle(double timeOut) {
   
   double          untiltimer;
    
-  int             cpt, ran_ok;
+  int             cpt;
+  int volatile ran_ok;
 
   s_gras_msg_t    msg;
 
@@ -590,8 +611,8 @@ gras_msg_handle(double timeOut) {
     TRY {
       xbt_dynar_foreach(list->cbs,cpt,cb) { 
        if (!ran_ok) {
-         VERB3("Use the callback #%d (@%p) for incomming msg %s",
-               cpt+1,cb,msg.type->name);
+         DEBUG4("Use the callback #%d (@%p) for incomming msg %s (payload_size=%d)",
+               cpt+1,cb,msg.type->name,msg.payl_size);
          if ((*cb)(&ctx,msg.payl)) {
            /* cb handled the message */
            free(msg.payl);