Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
If the timeout is negative, the user wants to block
[simgrid.git] / src / gras / Transport / sg_transport.c
index 2b23182..b77eae4 100644 (file)
@@ -2,20 +2,21 @@
 
 /* sg_transport - SG specific functions for transport                       */
 
-/* Authors: Martin Quinson                                                  */
-/* Copyright (C) 2004 Martin Quinson.                                       */
+/* Copyright (c) 2004 Martin Quinson. 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. */
* under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "Transport/transport_private.h"
-#include <msg.h>
-#include "Virtu/virtu_sg.h"
+#include "xbt/ex.h"
+#include "gras/Transport/transport_private.h"
+#include "gras/Virtu/virtu_sg.h"
 
-GRAS_LOG_EXTERNAL_CATEGORY(transport);
-GRAS_LOG_DEFAULT_CATEGORY(transport);
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
 
-/**
+/* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */
+gras_socket_t _gras_lastly_selected_socket = NULL;
+
+/**    
  * gras_trp_select:
  *
  * Returns the next socket to service having a message awaiting.
@@ -26,131 +27,106 @@ GRAS_LOG_DEFAULT_CATEGORY(transport);
  *
  * if timeout>0 and no message there, wait at most that amount of time before giving up.
  */
-gras_error_t 
-gras_trp_select(double timeout, 
-               gras_socket_t **dst) {
-
-  gras_error_t errcode;
-  double startTime=gras_os_time();
-  gras_procdata_t *pd=gras_procdata_get();
+gras_socket_t gras_trp_select(double timeout) {
+  gras_socket_t res;
+  gras_trp_procdata_t pd = 
+    (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
   gras_trp_sg_sock_data_t *sockdata;
-  gras_trp_plugin_t *trp;
-
-  gras_socket_t *sock_iter; /* iterating over all sockets */
-  int cursor,cpt;
-
-  gras_sg_portrec_t pr;     /* iterating to find the chanel of expeditor */
-
-  int r_pid;
-  gras_hostdata_t *remote_hd;
+  gras_trp_plugin_t trp;
+  gras_socket_t active_socket = NULL;
+  gras_trp_sg_sock_data_t *active_socket_data;
+  gras_socket_t sock_iter; /* iterating over all sockets */
+  int cursor;
 
   DEBUG3("select on %s@%s with timeout=%f",
-        MSG_process_get_name(MSG_process_self()),
-        MSG_host_get_name(MSG_host_self()),
+        SIMIX_process_get_name(SIMIX_process_self()),
+        SIMIX_host_get_name(SIMIX_host_self()),
         timeout);
-  do {
-    r_pid = MSG_task_probe_from((m_channel_t) pd->chan);
-    if (r_pid >= 0) {
-      /* Try to reuse an already openned socket to that expeditor */
-      gras_dynar_foreach(pd->sockets,cursor,sock_iter) {
-       DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
-       sockdata = sock_iter->data;
-
-       if (sock_iter->raw || !sock_iter->outgoing)
-         continue;
-
-       if (sockdata->from_PID == r_pid) {
-         *dst=sock_iter;
-         return no_error;
-       }
-      }
-
-      /* Socket to expeditor not created yet */
-      DEBUG0("Create a socket to the expeditor");
-
-      TRY(gras_trp_plugin_get_by_name("sg",&trp));
-
-      TRY(gras_trp_socket_new(1,dst));
-      (*dst)->plugin   = trp;
-
-      (*dst)->incoming  = 1;
-      (*dst)->outgoing  = 1;
-      (*dst)->accepting = 0;
-      (*dst)->sd        = -1;
-
-      (*dst)->port      = -1;
-
-      if (!(sockdata = malloc(sizeof(gras_trp_sg_sock_data_t))))
-       RAISE_MALLOC;
-
-      sockdata->from_PID = MSG_process_self_PID();
-      sockdata->to_PID   = r_pid;
-      sockdata->to_host  = MSG_process_get_host(MSG_process_from_PID(r_pid));
-      (*dst)->data = sockdata;
-
-      (*dst)->peer_name = strdup(MSG_host_get_name(sockdata->to_host));
-
-      remote_hd=(gras_hostdata_t *)MSG_host_get_data(sockdata->to_host);
-      gras_assert0(remote_hd,"Run gras_process_init!!");
-
-      sockdata->to_chan = -1;
-      (*dst)->peer_port = -10;
-      for (cursor=0; cursor<GRAS_MAX_CHANNEL; cursor++) {
-       if (remote_hd->proc[cursor] == r_pid) {
-         sockdata->to_chan = cursor;
-         DEBUG2("Chan %d on %s is for my pal",
-                cursor,(*dst)->peer_name);
-
-         gras_dynar_foreach(remote_hd->ports, cpt, pr) {
-           if (sockdata->to_chan == pr.tochan) {
-             if (pr.raw) {
-               DEBUG0("Damn, it's raw");
-               continue;
-             }
-
-             (*dst)->peer_port = pr.port;
-             DEBUG1("Cool, it points to port %d", pr.port);
-             break;
-           } else {
-             DEBUG2("Wrong port (tochan=%d, looking for %d)\n",
-                    pr.tochan,sockdata->to_chan);
-           }
-         }
-         if ((*dst)->peer_port == -10) {
-           /* was raw */
-           sockdata->to_chan = -1;
-         } else {
-           /* found it, don't let it override by raw */
-           break;
-         }
-       }
-      }
-      gras_assert0(sockdata->to_chan != -1,
-                  "Got a message from a process without channel");
-
-      return no_error;
-    } else {
-      /*
-      DEBUG2("Select on %s@%s did not find anything yet",
-            MSG_process_get_name(MSG_process_self()),
-            MSG_host_get_name(MSG_host_self()));
-      */
-      // MSG_process_sleep(1);
-      MSG_process_sleep(0.01);
+   if (timeout>=0) {
+      xbt_queue_shift_timed(pd->msg_selectable_sockets,
+                           &active_socket, timeout);
+   } else {
+      xbt_queue_shift(pd->msg_selectable_sockets, &active_socket);
+   }
+   
+  if (active_socket == NULL) {
+    DEBUG0("TIMEOUT");
+    THROW0(timeout_error,0,"Timeout");
+  }
+  active_socket_data = (gras_trp_sg_sock_data_t*)active_socket->data;
+  
+  /* Ok, got something. Open a socket back to the expeditor */
+
+  /* Try to reuse an already openned socket to that expeditor */
+  DEBUG1("Open sockets size %lu",xbt_dynar_length(pd->sockets));
+  xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
+    gras_trp_sg_sock_data_t *sock_data;
+    DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
+    
+    if (sock_iter->meas || !sock_iter->outgoing)
+      continue;
+    sock_data = ((gras_trp_sg_sock_data_t*)sock_iter->data);
+
+    if ( (sock_data->to_socket == active_socket) && 
+        (sock_data->to_host == SIMIX_process_get_host(active_socket_data->from_process)) ) {
+                       xbt_dynar_cursor_unlock(pd->sockets);   
+      return sock_iter;
     }
-  } while (gras_os_time()-startTime < timeout
-          || MSG_task_Iprobe((m_channel_t) pd->chan));
+  }
+
+  /* Socket to expeditor not created yet */
+  DEBUG0("Create a socket to the expeditor");
 
-  return timeout_error;
+  trp = gras_trp_plugin_get_by_name("sg");
 
+  gras_trp_socket_new(1,&res);
+  res->plugin   = trp;
+
+  res->incoming  = 1;
+  res->outgoing  = 1;
+  res->accepting = 0;
+  res->sd        = -1;
+
+  res->port = -1;
+
+  /* initialize the ports */
+  //res->peer_port = active_socket->port;
+  res->port = active_socket->peer_port;
+  
+  /* create sockdata */
+  sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
+  sockdata->from_process = SIMIX_process_self();
+  sockdata->to_process   = active_socket_data->from_process;
+       
+  res->peer_port = 
+    ((gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sockdata->to_process))->myport;
+  sockdata->to_socket = active_socket;
+  /*update the peer to_socket  variable */
+  active_socket_data->to_socket = res;
+  sockdata->cond = SIMIX_cond_init();
+  sockdata->mutex = SIMIX_mutex_init();
+
+  sockdata->to_host = SIMIX_process_get_host(active_socket_data->from_process);
+
+  res->data = sockdata;
+  res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
+
+  gras_trp_buf_init_sock(res);
+
+  DEBUG4("Create socket to process:%s(Port %d) from process: %s(Port %d)",
+        SIMIX_process_get_name(sockdata->from_process),
+        res->peer_port,
+        SIMIX_process_get_name(sockdata->to_process), res->port);
+
+  return res;
 }
 
   
 /* dummy implementations of the functions used in RL mode */
 
-gras_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
-  return mismatch_error;
-}
-gras_error_t gras_trp_file_setup(gras_trp_plugin_t *plug) {
-  return mismatch_error;
-}
+void gras_trp_tcp_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
+void gras_trp_file_setup(gras_trp_plugin_t plug){  THROW0(mismatch_error,0,NULL); }
+void gras_trp_iov_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
+
+gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}
+