3 /* sg_transport - SG specific functions for transport */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
11 #include "gras_simix/Transport/gras_simix_transport_private.h"
12 //#include "msg/msg.h"
13 #include "gras_simix/Virtu/gras_simix_virtu_sg.h"
15 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
17 /* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */
18 gras_socket_t _gras_lastly_selected_socket = NULL;
23 * Returns the next socket to service having a message awaiting.
25 * if timeout<0, we ought to implement the adaptative timeout (FIXME)
27 * if timeout=0, do not wait for new message, only handle the ones already there.
29 * if timeout>0 and no message there, wait at most that amount of time before giving up.
31 gras_socket_t gras_trp_select(double timeout) {
33 gras_trp_procdata_t pd = (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
34 gras_trp_sg_sock_data_t *sockdata;
35 gras_trp_plugin_t trp;
36 gras_socket_t active_socket;
37 gras_socket_t sock_iter; /* iterating over all sockets */
41 gras_hostdata_t *remote_hd;
42 // gras_hostdata_t *local_hd;
44 DEBUG0("Trying to get the lock pd, trp_select");
45 SIMIX_mutex_lock(pd->mutex);
46 DEBUG3("select on %s@%s with timeout=%f",
47 SIMIX_process_get_name(SIMIX_process_self()),
48 SIMIX_host_get_name(SIMIX_host_self()),
51 if (xbt_fifo_size(pd->active_socket) == 0) {
52 /* message didn't arrive yet, wait */
53 SIMIX_cond_wait_timeout(pd->cond,pd->mutex,timeout);
56 if (xbt_fifo_size(pd->active_socket) == 0) {
58 SIMIX_mutex_unlock(pd->mutex);
59 THROW0(timeout_error,0,"Timeout");
61 active_socket = xbt_fifo_shift(pd->active_socket);
63 /* Ok, got something. Open a socket back to the expeditor */
65 /* Try to reuse an already openned socket to that expeditor */
66 xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
67 DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
69 if (sock_iter->meas || !sock_iter->outgoing)
71 //DEBUG4("sock_iter %p port %d active %p port %d",((gras_trp_sg_sock_data_t*)sock_iter->data)->to_process,sock_iter->peer_port,((gras_trp_sg_sock_data_t*)pd->active_socket->data)->from_process, pd->active_socket->port);
72 //DEBUG1("\nFrom process %p", ((gras_trp_sg_sock_data_t*)pd->active_socket->data)->from_process);
73 if ((sock_iter->peer_port == active_socket->port) &&
74 (((gras_trp_sg_sock_data_t*)sock_iter->data)->to_host == SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)active_socket->data)->from_process))) {
75 SIMIX_mutex_unlock(pd->mutex);
81 /* Socket to expeditor not created yet */
82 DEBUG0("Create a socket to the expeditor");
84 trp = gras_trp_plugin_get_by_name("sg");
86 gras_trp_socket_new(1,&res);
96 sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
97 sockdata->from_process = SIMIX_process_self();
98 sockdata->to_process = ((gras_trp_sg_sock_data_t*)(active_socket->data))->from_process;
100 DEBUG2("Create socket to process:%s from process: %s",SIMIX_process_get_name(sockdata->from_process),SIMIX_process_get_name(sockdata->to_process));
102 sockdata->to_host = SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)(active_socket->data))->from_process);
104 res->data = sockdata;
105 gras_trp_buf_init_sock(res);
107 res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
109 remote_hd=(gras_hostdata_t *)SIMIX_host_get_data(sockdata->to_host);
110 xbt_assert0(remote_hd,"Run gras_process_init!!");
112 res->peer_port = active_socket->port;
114 res->port = active_socket->peer_port;
115 /* search for a free port on the host */
117 local_hd = (gras_hostdata_t *)SIMIX_host_get_data(SIMIX_host_self());
118 for (i=1;i<65536;i++) {
119 if (local_hd->cond_port[i] == NULL)
123 SIMIX_mutex_unlock(pd->mutex);
124 THROW0(system_error,0,"No port free");
127 /*initialize the cond and mutex */
129 local_hd->cond_port[i] = SIMIX_cond_init();
130 local_hd->mutex_port[i] = SIMIX_mutex_init();
132 /* update remote peer_port to talk through the new port */
133 active_socket->peer_port = res->port;
137 DEBUG2("New socket: Peer port %d Local port %d", res->peer_port, res->port);
138 SIMIX_mutex_unlock(pd->mutex);
143 /* dummy implementations of the functions used in RL mode */
145 void gras_trp_tcp_setup(gras_trp_plugin_t plug) { THROW0(mismatch_error,0,NULL); }
146 void gras_trp_file_setup(gras_trp_plugin_t plug){ THROW0(mismatch_error,0,NULL); }
147 void gras_trp_iov_setup(gras_trp_plugin_t plug) { THROW0(mismatch_error,0,NULL); }
149 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}