Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function gras_msgtype_get_name()
[simgrid.git] / src / gras / Msg / gras_msg_listener.c
index 846fbeb..9805950 100644 (file)
@@ -1,8 +1,7 @@
-/* $Id$ */
-
 /* Thread in charge of listening the network and queuing incoming messages  */
 
-/* Copyright (c) 2007 Martin Quinson. All rights reserved.                  */
+/* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
+ * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
@@ -19,7 +18,7 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_read, gras_msg,
 #include "gras/Transport/transport_interface.h" /* gras_select */
 
 typedef struct s_gras_msg_listener_ {
-  xbt_queue_t incomming_messages;
+  xbt_queue_t incomming_messages; /* messages received from the wire and still to be used by master */
   xbt_queue_t socks_to_close;   /* let the listener close the sockets, since it may be selecting on them. Darwin don't like this trick */
   gras_socket_t wakeup_sock_listener_side;
   gras_socket_t wakeup_sock_master_side;
@@ -29,27 +28,26 @@ typedef struct s_gras_msg_listener_ {
 static void listener_function(void *p)
 {
   gras_msg_listener_t me = (gras_msg_listener_t) p;
-  s_gras_msg_t msg;
+  gras_msg_t msg;
   xbt_ex_t e;
   gras_msgtype_t msg_wakeup_listener_t =
     gras_msgtype_by_name("_wakeup_listener");
   DEBUG0("I'm the listener");
   while (1) {
-    DEBUG0("Selecting");
-    msg.expe = gras_trp_select(-1);
-    DEBUG0("Select returned something");
-    gras_msg_recv(msg.expe, &msg);
-    if (msg.type != msg_wakeup_listener_t)
-      xbt_queue_push(me->incomming_messages, &msg);
+    msg = gras_msg_recv_any();
+    if (msg->type != msg_wakeup_listener_t)
+      xbt_queue_push(me->incomming_messages, msg);
     else {
-      char got = *(char *) msg.payl;
+      char got = *(char *) msg->payl;
       if (got == '1') {
         VERB0("Asked to get awake");
-        free(msg.payl);
+        free(msg->payl);
+        free(msg);
       } else {
         VERB0("Asked to die");
-        //        gras_socket_close(me->wakeup_sock_listener_side);
-        free(msg.payl);
+        //gras_socket_close(me->wakeup_sock_listener_side);
+        free(msg->payl);
+        free(msg);
         return;
       }
     }
@@ -59,8 +57,12 @@ static void listener_function(void *p)
         int sock;
         xbt_queue_shift_timed(me->socks_to_close, &sock, 0);
         if (tcp_close(sock) < 0) {
+#ifdef _XBT_WIN32
+          WARN2("error while closing tcp socket %d: %d (%s)\n", sock, sock_errno);
+#else
           WARN3("error while closing tcp socket %d: %d (%s)\n",
                 sock, sock_errno, sock_errstr(sock_errno));
+#endif
         }
       }
     }
@@ -97,34 +99,28 @@ gras_msg_listener_t gras_msg_listener_launch(xbt_queue_t msg_exchange)
   gras_msgtype_declare("_wakeup_listener", gras_datadesc_by_name("char"));
 
   /* actually start the thread */
-  arg->listener = xbt_thread_create("listener", listener_function, arg);
-  gras_os_sleep(0); /* TODO: useless? give the listener a chance to initialize even if the main is empty and we cancel it right afterward */
+  arg->listener = xbt_thread_create("listener", listener_function, arg,1/*joinable*/);
+  gras_os_sleep(0);             /* TODO: useless? give the listener a chance to initialize even if the main is empty and we cancel it right afterward */
   return arg;
 }
 
 #include "gras/Virtu/virtu_private.h"   /* procdata_t content */
-void gras_msg_listener_shutdown(gras_msg_listener_t l)
+void gras_msg_listener_shutdown()
 {
   gras_procdata_t *pd = gras_procdata_get();
   char kill = '0';
   DEBUG0("Listener quit");
 
-
   if (pd->listener)
     gras_msg_send(pd->listener->wakeup_sock_master_side, "_wakeup_listener",
-                  &kill);
+          &kill);
 
-  /* FIXME: thread_join is not implemented in SG (remove next conditional when fixed)
-   * But I guess it's not a big deal since we're terminating the thread mainly to
-   * make it free its OS locks on darwin.
-   * darwin is definitly different from the neat & nice SG world */
-  if (gras_if_RL())
-    xbt_thread_join(pd->listener->listener);
+  xbt_thread_join(pd->listener->listener);
 
   //  gras_socket_close(pd->listener->wakeup_sock_master_side); FIXME: uncommenting this leads to deadlock at terminaison
-  xbt_queue_free(&l->incomming_messages);
-  xbt_queue_free(&l->socks_to_close);
-  xbt_free(l);
+  xbt_queue_free(&pd->listener->incomming_messages);
+  xbt_queue_free(&pd->listener->socks_to_close);
+  xbt_free(pd->listener);
 }
 
 void gras_msg_listener_awake()