Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Integrate Bruno's work on SIMIX onto main stream. Tests are broken, but it looks...
[simgrid.git] / src / gras / Transport / sg_transport.c
1 /* $Id$ */
2
3 /* sg_transport - SG specific functions for transport                       */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
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. */
9
10 #include "xbt/ex.h"
11 #include "gras/Transport/transport_private.h"
12 #include "gras/Virtu/virtu_sg.h"
13
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(gras_trp);
15
16 /* check transport_private.h for an explanation of this variable; this just need to be defined to NULL in SG */
17 gras_socket_t _gras_lastly_selected_socket = NULL;
18
19 /**     
20  * gras_trp_select:
21  *
22  * Returns the next socket to service having a message awaiting.
23  *
24  * if timeout<0, we ought to implement the adaptative timeout (FIXME)
25  *
26  * if timeout=0, do not wait for new message, only handle the ones already there.
27  *
28  * if timeout>0 and no message there, wait at most that amount of time before giving up.
29  */
30 gras_socket_t gras_trp_select(double timeout) {
31         gras_socket_t res;
32         gras_trp_procdata_t pd = (gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id);
33         gras_trp_sg_sock_data_t *sockdata;
34         gras_trp_plugin_t trp;
35         gras_socket_t active_socket;
36         gras_socket_t sock_iter; /* iterating over all sockets */
37         int cursor;
38
39         DEBUG0("Trying to get the lock pd, trp_select");
40         SIMIX_mutex_lock(pd->mutex);
41         DEBUG3("select on %s@%s with timeout=%f",
42                         SIMIX_process_get_name(SIMIX_process_self()),
43                         SIMIX_host_get_name(SIMIX_host_self()),
44                         timeout);
45
46         if (xbt_fifo_size(pd->active_socket) == 0) {
47         /* message didn't arrive yet, wait */
48                 SIMIX_cond_wait_timeout(pd->cond,pd->mutex,timeout);
49         }
50
51         if (xbt_fifo_size(pd->active_socket) == 0) {
52                 DEBUG0("TIMEOUT");
53                 SIMIX_mutex_unlock(pd->mutex);
54                 THROW0(timeout_error,0,"Timeout");
55         }
56         active_socket = xbt_fifo_shift(pd->active_socket);
57         
58         /* Ok, got something. Open a socket back to the expeditor */
59
60         /* Try to reuse an already openned socket to that expeditor */
61         DEBUG1("Open sockets size %lu",xbt_dynar_length(pd->sockets));
62         xbt_dynar_foreach(pd->sockets,cursor,sock_iter) {
63                 DEBUG1("Consider %p as outgoing socket to expeditor",sock_iter);
64
65                 if (sock_iter->meas || !sock_iter->outgoing)
66                         continue;
67                         /*
68                 if ((sock_iter->peer_port == active_socket->port) && 
69                                 (((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))) {
70                                 */
71                 if ( (((gras_trp_sg_sock_data_t*)sock_iter->data)->to_socket == active_socket) && (((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)) ) {
72                         SIMIX_mutex_unlock(pd->mutex);
73                         return sock_iter;
74                 }
75         }
76
77         /* Socket to expeditor not created yet */
78         DEBUG0("Create a socket to the expeditor");
79
80         trp = gras_trp_plugin_get_by_name("sg");
81
82         gras_trp_socket_new(1,&res);
83         res->plugin   = trp;
84
85         res->incoming  = 1;
86         res->outgoing  = 1;
87         res->accepting = 0;
88         res->sd        = -1;
89
90         res->port = -1;
91
92         /* initialize the ports */
93         //res->peer_port = active_socket->port;
94         res->port = active_socket->peer_port;
95
96         /* create sockdata */
97         sockdata = xbt_new(gras_trp_sg_sock_data_t,1);
98         sockdata->from_process = SIMIX_process_self();
99         sockdata->to_process   = ((gras_trp_sg_sock_data_t*)(active_socket->data))->from_process;
100         
101         res->peer_port = ((gras_trp_procdata_t)gras_libdata_by_name_from_remote("gras_trp",sockdata->to_process))->myport;
102         sockdata->to_socket = active_socket;
103         /*update the peer to_socket  variable */
104         ((gras_trp_sg_sock_data_t*)active_socket->data)->to_socket = res;
105         sockdata->cond = SIMIX_cond_init();
106         sockdata->mutex = SIMIX_mutex_init();
107
108         sockdata->to_host  = SIMIX_process_get_host(((gras_trp_sg_sock_data_t*)(active_socket->data))->from_process);
109
110         res->data = sockdata;
111         res->peer_name = strdup(SIMIX_host_get_name(sockdata->to_host));
112
113         gras_trp_buf_init_sock(res);
114
115         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);
116
117         SIMIX_mutex_unlock(pd->mutex);
118         return res;
119 }
120
121   
122 /* dummy implementations of the functions used in RL mode */
123
124 void gras_trp_tcp_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
125 void gras_trp_file_setup(gras_trp_plugin_t plug){  THROW0(mismatch_error,0,NULL); }
126 void gras_trp_iov_setup(gras_trp_plugin_t plug) {  THROW0(mismatch_error,0,NULL); }
127
128 gras_socket_t gras_trp_buf_init_sock(gras_socket_t sock) { return sock;}
129