Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
dbe87a09d3a5247b41273a4929bf4ef149693ea1
[simgrid.git] / src / gras / Msg / gras_msg_listener.c
1 /* Thread in charge of listening the network and queuing incoming messages  */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "gras/Msg/msg_private.h"
10 #include "gras/Transport/transport_private.h"
11 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_read, gras_msg,
12                                 "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_thread_t listener; /* keep this first, gras_socket_im_the_server() does funky transtyping in sg_msg.c */
22   xbt_queue_t incomming_messages;       /* messages received from the wire and still to be used by master */
23   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 */
24   gras_socket_t wakeup_sock_listener_side;
25   gras_socket_t wakeup_sock_master_side;
26   int port; /* The port on which the listener opened the command socket */
27   xbt_mutex_t init_mutex;       /* both this mutex and condition are used at initialization to make sure that */
28   xbt_cond_t init_cond;         /* the main thread speaks to the listener only once it is started (FIXME: It would be easier using a semaphore, if only semaphores were in xbt_synchro) */
29 } s_gras_msg_listener_t;
30
31 #include "gras/Virtu/virtu_private.h" /* gras_procdata_t */
32 static void listener_function(void *p)
33 {
34   gras_msg_listener_t me = (gras_msg_listener_t) p;
35   gras_msg_t msg;
36   xbt_ex_t e;
37   gras_msgtype_t msg_wakeup_listener_t =
38       gras_msgtype_by_name("_wakeup_listener");
39   XBT_DEBUG("I'm the listener");
40
41   /* get a free socket for the receiving part of the listener */
42   me->wakeup_sock_listener_side =NULL;
43   for (me->port = 5000; me->port < 6000; me->port++) {
44     TRY {
45       me->wakeup_sock_listener_side = gras_socket_server_ext(me->port, -1, 0);
46     }
47     CATCH(e) {
48       if (me->port == 6000)
49         RETHROW;
50       xbt_ex_free(e);
51     }
52     if (me->wakeup_sock_listener_side)
53       break;
54   }
55
56   /* wake up the launcher */
57   xbt_mutex_acquire(me->init_mutex);
58   xbt_cond_signal(me->init_cond);
59   xbt_mutex_release(me->init_mutex);
60
61   /* Main loop */
62   while (1) {
63     msg = gras_msg_recv_any();
64     if (msg->type != msg_wakeup_listener_t) {
65                 /* Cannot display who sent this since in SG, gras_socket_peer_* wont work:
66                    I'm not the user process but I'm just the listener. Too bad */
67       XBT_VERB("Got a '%s' message (%s) from sock %p. Queue it for handling by main thread",
68             gras_msgtype_get_name(msg->type),e_gras_msg_kind_names[msg->kind],msg->expe);
69       xbt_queue_push(me->incomming_messages, msg);
70     } else {
71       char got = *(char *) msg->payl;
72       if (got == '1') {
73         XBT_VERB("Asked to get awake");
74         free(msg->payl);
75         free(msg);
76       } else {
77         XBT_VERB("Asked to die");
78         //gras_socket_close(me->wakeup_sock_listener_side);
79         free(msg->payl);
80         free(msg);
81         return;
82       }
83     }
84     /* empty the list of sockets to trash */
85     TRY {
86       while (1) {
87         int sock;
88         xbt_queue_shift_timed(me->socks_to_close, &sock, 0);
89         if (tcp_close(sock) < 0) {
90 #ifdef _XBT_WIN32
91           XBT_WARN("error while closing tcp socket %d: %d\n", sock,
92                 sock_errno);
93 #else
94           XBT_WARN("error while closing tcp socket %d: %d (%s)\n",
95                 sock, sock_errno, sock_errstr(sock_errno));
96 #endif
97         }
98       }
99     }
100     CATCH(e) {
101       if (e.category != timeout_error)
102         RETHROW;
103       xbt_ex_free(e);
104     }
105   }
106 }
107
108 gras_msg_listener_t gras_msg_listener_launch(xbt_queue_t msg_received)
109 {
110   gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t, 1);
111
112   XBT_VERB("Launch listener");
113   arg->incomming_messages = msg_received;
114   arg->socks_to_close = xbt_queue_new(0, sizeof(int));
115   arg->init_mutex = xbt_mutex_init();
116   arg->init_cond = xbt_cond_init();
117
118   /* declare the message used to awake the listener from the master */
119   gras_msgtype_declare("_wakeup_listener", gras_datadesc_by_name("char"));
120
121   /* actually start the thread, and */
122   /* wait for the listener to initialize before we connect to its socket */
123   xbt_mutex_acquire(arg->init_mutex);
124   arg->listener =
125       xbt_thread_create("listener", listener_function, arg,
126                         1 /*joinable */ );
127   xbt_cond_wait(arg->init_cond, arg->init_mutex);
128   xbt_mutex_release(arg->init_mutex);
129   xbt_cond_destroy(arg->init_cond);
130   xbt_mutex_destroy(arg->init_mutex);
131
132   /* Connect the other part of the socket */
133   arg->wakeup_sock_master_side =
134       gras_socket_client(gras_os_myname(),
135                          arg->port);
136   return arg;
137 }
138
139 #include "gras/Virtu/virtu_private.h"   /* procdata_t content */
140 void gras_msg_listener_shutdown()
141 {
142   gras_procdata_t *pd = gras_procdata_get();
143   char kill = '0';
144   XBT_DEBUG("Listener quit");
145
146   if (pd->listener)
147     gras_msg_send(pd->listener->wakeup_sock_master_side,
148                   "_wakeup_listener", &kill);
149
150   xbt_thread_join(pd->listener->listener);
151
152   //  gras_socket_close(pd->listener->wakeup_sock_master_side); FIXME: uncommenting this leads to deadlock at terminaison
153   xbt_queue_free(&pd->listener->incomming_messages);
154   xbt_queue_free(&pd->listener->socks_to_close);
155   xbt_free(pd->listener);
156 }
157
158 void gras_msg_listener_awake()
159 {
160   gras_procdata_t *pd;
161   char c = '1';
162
163   XBT_DEBUG("Awaking the listener");
164   pd = gras_procdata_get();
165   if (pd->listener) {
166     gras_msg_send(pd->listener->wakeup_sock_master_side,
167                   "_wakeup_listener", &c);
168   }
169 }
170
171 void gras_msg_listener_close_socket(int sd)
172 {
173   gras_procdata_t *pd = gras_procdata_get();
174   if (pd->listener) {
175     xbt_queue_push(pd->listener->socks_to_close, &sd);
176     gras_msg_listener_awake();
177   } else {
178     /* do it myself */
179     tcp_close(sd);
180   }
181 }