Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
284bf48bda09c2aac0b75b2e43f868ef83323847
[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_queue_t incomming_messages;       /* messages received from the wire and still to be used by master */
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_mutex_t init_mutex;       /* both this mutex and condition are used at initialization to make sure that */
26   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) */
27   xbt_thread_t listener;
28 } s_gras_msg_listener_t;
29
30 static void listener_function(void *p)
31 {
32   gras_msg_listener_t me = (gras_msg_listener_t) p;
33   gras_msg_t msg;
34   xbt_ex_t e;
35   gras_msgtype_t msg_wakeup_listener_t =
36       gras_msgtype_by_name("_wakeup_listener");
37   DEBUG0("I'm the listener");
38
39   /* get a free socket for the receiving part of the listener */
40   me->wakeup_sock_listener_side =
41       gras_socket_server_range(5000, 6000, -1, 0);
42
43   /* wake up the launcher */
44   xbt_mutex_acquire(me->init_mutex);
45   xbt_cond_signal(me->init_cond);
46   xbt_mutex_release(me->init_mutex);
47
48   /* Main loop */
49   while (1) {
50     msg = gras_msg_recv_any();
51     if (msg->type != msg_wakeup_listener_t) {
52                 /* Cannot display who sent this since in SG, gras_socket_peer_* wont work:
53                    I'm not the user process but I'm just the listener. Too bad */
54       VERB1("Got a '%s' message. Queue it for handling by main thread",
55             gras_msgtype_get_name(msg->type));
56       xbt_queue_push(me->incomming_messages, msg);
57     } else {
58       char got = *(char *) msg->payl;
59       if (got == '1') {
60         VERB0("Asked to get awake");
61         free(msg->payl);
62         free(msg);
63       } else {
64         VERB0("Asked to die");
65         //gras_socket_close(me->wakeup_sock_listener_side);
66         free(msg->payl);
67         free(msg);
68         return;
69       }
70     }
71     /* empty the list of sockets to trash */
72     TRY {
73       while (1) {
74         int sock;
75         xbt_queue_shift_timed(me->socks_to_close, &sock, 0);
76         if (tcp_close(sock) < 0) {
77 #ifdef _XBT_WIN32
78           WARN2("error while closing tcp socket %d: %d\n", sock,
79                 sock_errno);
80 #else
81           WARN3("error while closing tcp socket %d: %d (%s)\n",
82                 sock, sock_errno, sock_errstr(sock_errno));
83 #endif
84         }
85       }
86     }
87     CATCH(e) {
88       if (e.category != timeout_error)
89         RETHROW;
90       xbt_ex_free(e);
91     }
92   }
93 }
94
95 gras_msg_listener_t gras_msg_listener_launch(xbt_queue_t msg_received)
96 {
97   gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t, 1);
98
99   VERB0("Launch listener");
100   arg->incomming_messages = msg_received;
101   arg->socks_to_close = xbt_queue_new(0, sizeof(int));
102   arg->init_mutex = xbt_mutex_init();
103   arg->init_cond = xbt_cond_init();
104
105   /* declare the message used to awake the listener from the master */
106   gras_msgtype_declare("_wakeup_listener", gras_datadesc_by_name("char"));
107
108   /* actually start the thread, and */
109   /* wait for the listener to initialize before we connect to its socket */
110   xbt_mutex_acquire(arg->init_mutex);
111   arg->listener =
112       xbt_thread_create("listener", listener_function, arg,
113                         1 /*joinable */ );
114   xbt_cond_wait(arg->init_cond, arg->init_mutex);
115   xbt_mutex_release(arg->init_mutex);
116   xbt_cond_destroy(arg->init_cond);
117   xbt_mutex_destroy(arg->init_mutex);
118
119   /* Connect the other part of the socket */
120   arg->wakeup_sock_master_side =
121       gras_socket_client(gras_os_myname(),
122                          gras_socket_my_port
123                          (arg->wakeup_sock_listener_side));
124   return arg;
125 }
126
127 #include "gras/Virtu/virtu_private.h"   /* procdata_t content */
128 void gras_msg_listener_shutdown()
129 {
130   gras_procdata_t *pd = gras_procdata_get();
131   char kill = '0';
132   DEBUG0("Listener quit");
133
134   if (pd->listener)
135     gras_msg_send(pd->listener->wakeup_sock_master_side,
136                   "_wakeup_listener", &kill);
137
138   xbt_thread_join(pd->listener->listener);
139
140   //  gras_socket_close(pd->listener->wakeup_sock_master_side); FIXME: uncommenting this leads to deadlock at terminaison
141   xbt_queue_free(&pd->listener->incomming_messages);
142   xbt_queue_free(&pd->listener->socks_to_close);
143   xbt_free(pd->listener);
144 }
145
146 void gras_msg_listener_awake()
147 {
148   gras_procdata_t *pd;
149   char c = '1';
150
151   DEBUG0("Awaking the listener");
152   pd = gras_procdata_get();
153   if (pd->listener) {
154     gras_msg_send(pd->listener->wakeup_sock_master_side,
155                   "_wakeup_listener", &c);
156   }
157 }
158
159 void gras_msg_listener_close_socket(int sd)
160 {
161   gras_procdata_t *pd = gras_procdata_get();
162   if (pd->listener) {
163     xbt_queue_push(pd->listener->socks_to_close, &sd);
164     gras_msg_listener_awake();
165   } else {
166     /* do it myself */
167     tcp_close(sd);
168   }
169 }