Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a indent rule to format the code in an uniform way, avoiding breaking the branche...
[simgrid.git] / src / gras / Msg / gras_msg_listener.c
1 /* $Id$ */
2
3 /* Thread in charge of listening the network and queuing incoming messages  */
4
5 /* Copyright (c) 2007 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 "gras/Msg/msg_private.h"
11 #include "gras/Transport/transport_private.h"
12 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_read,gras_msg,"Message reader thread");
13
14 #include "xbt/ex.h"
15 #include "xbt/queue.h"
16 #include "xbt/synchro.h"
17
18 #include "gras/Transport/transport_interface.h" /* gras_select */
19
20 typedef struct s_gras_msg_listener_ {
21         xbt_queue_t incomming_messages;
22         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 */
23         gras_socket_t wakeup_sock_listener_side;
24         gras_socket_t wakeup_sock_master_side;
25         xbt_thread_t listener;
26 } s_gras_msg_listener_t;
27
28 static void listener_function(void *p) {
29         gras_msg_listener_t me = (gras_msg_listener_t)p;
30         s_gras_msg_t msg;
31         xbt_ex_t e;
32         gras_msgtype_t msg_wakeup_listener_t = gras_msgtype_by_name("_wakeup_listener");
33         DEBUG0("I'm the listener");
34         while (1) {
35                 DEBUG0("Selecting");
36                 msg.expe = gras_trp_select(-1);
37                 DEBUG0("Select returned something");
38                 gras_msg_recv(msg.expe, &msg);
39                 if (msg.type!=msg_wakeup_listener_t)
40                         xbt_queue_push(me->incomming_messages, &msg);
41                 else  {
42                         char got = *(char*)msg.payl;
43                         if (got == '1') {
44                                 VERB0("Asked to get awake");
45                                 free(msg.payl);
46                         } else {
47                                 VERB0("Asked to die");
48                                 //        gras_socket_close(me->wakeup_sock_listener_side);
49                                 free(msg.payl);
50                                 return ;
51                         }
52                 }
53                 /* empty the list of sockets to trash */
54                 TRY {
55                         while (1) {
56                                 int sock;
57                                 xbt_queue_shift_timed(me->socks_to_close,&sock,0);
58                                 if(tcp_close(sock) < 0) {
59                                         WARN3("error while closing tcp socket %d: %d (%s)\n",
60                                                         sock, sock_errno, sock_errstr(sock_errno));
61                                 }
62                         }
63                 } CATCH(e) {
64                         if (e.category != timeout_error)
65                                 RETHROW;
66                         xbt_ex_free(e);
67                 }
68         }
69 }
70
71 gras_msg_listener_t
72 gras_msg_listener_launch(xbt_queue_t msg_exchange){
73         gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t,1);
74         int my_port;
75
76         DEBUG0("Launch listener");
77         arg->incomming_messages = msg_exchange;
78         arg->socks_to_close = xbt_queue_new(0,sizeof(int));
79
80         /* get a free socket for the receiving part of the listener, taking care that it does not get saved as "myport" number */
81         my_port = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport;
82         arg->wakeup_sock_listener_side=gras_socket_server_range(5000,6000,-1,0);
83         ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport = my_port;
84         /* Connect the other part of the socket */
85         arg->wakeup_sock_master_side=gras_socket_client(gras_os_myname(),gras_socket_my_port(arg->wakeup_sock_listener_side));
86
87         /* declare the message used to awake the listener from the master */
88         gras_msgtype_declare("_wakeup_listener",gras_datadesc_by_name("char"));
89
90         /* actually start the thread */
91         arg->listener =  xbt_thread_create("listener",listener_function,arg);
92         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 */
93         return arg;
94 }
95
96 #include "gras/Virtu/virtu_private.h" /* procdata_t content */
97 void gras_msg_listener_shutdown(gras_msg_listener_t l) {
98         gras_procdata_t *pd = gras_procdata_get();
99         char kill = '0';
100         DEBUG0("Listener quit");
101
102
103         if (pd->listener)
104                 gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&kill);
105
106         /* FIXME: thread_join is not implemented in SG (remove next conditional when fixed)
107          * But I guess it's not a big deal since we're terminating the thread mainly to
108          * make it free its OS locks on darwin.
109          * darwin is definitly different from the neat & nice SG world */
110         if (gras_if_RL())
111                 xbt_thread_join(pd->listener->listener);
112
113         //  gras_socket_close(pd->listener->wakeup_sock_master_side); FIXME: uncommenting this leads to deadlock at terminaison
114         xbt_queue_free(&l->incomming_messages);
115         xbt_queue_free(&l->socks_to_close);
116         xbt_free(l);
117 }
118
119 void gras_msg_listener_awake(){
120         gras_procdata_t *pd;
121         char c = '1';
122
123         DEBUG0("Awaking the listener");
124         pd = gras_procdata_get();
125         if (pd->listener) {
126                 gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&c);
127         }
128 }
129 void  gras_msg_listener_close_socket(int sd) {
130         gras_procdata_t *pd = gras_procdata_get();
131         if (pd->listener) {
132                 xbt_queue_push(pd->listener->socks_to_close,&sd);
133                 gras_msg_listener_awake();
134         } else {
135                 /* do it myself */
136                 tcp_close(sd);
137         }
138 }