3 /* Thread in charge of listening the network and queuing incoming messages */
5 /* Copyright (c) 2007 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. */
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");
15 #include "xbt/queue.h"
16 #include "xbt/synchro.h"
18 #include "gras/Transport/transport_interface.h" /* gras_select */
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;
28 static void listener_function(void *p) {
29 gras_msg_listener_t me = (gras_msg_listener_t)p;
32 gras_msgtype_t msg_wakeup_listener_t = gras_msgtype_by_name("_wakeup_listener");
33 DEBUG0("I'm the listener");
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);
42 char got = *(char*)msg.payl;
44 VERB0("Asked to get awake");
47 VERB0("Asked to die");
48 // gras_socket_close(me->wakeup_sock_listener_side);
53 /* empty the list of sockets to trash */
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));
64 if (e.category != timeout_error)
72 gras_msg_listener_launch(xbt_queue_t msg_exchange){
73 gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t,1);
76 DEBUG0("Launch listener");
77 arg->incomming_messages = msg_exchange;
78 arg->socks_to_close = xbt_queue_new(0,sizeof(int));
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));
87 /* declare the message used to awake the listener from the master */
88 gras_msgtype_declare("_wakeup_listener",gras_datadesc_by_name("char"));
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 */
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();
100 DEBUG0("Listener quit");
104 gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&kill);
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 */
111 xbt_thread_join(pd->listener->listener);
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);
119 void gras_msg_listener_awake(){
123 DEBUG0("Awaking the listener");
124 pd = gras_procdata_get();
126 gras_msg_send(pd->listener->wakeup_sock_master_side,"_wakeup_listener",&c);
129 void gras_msg_listener_close_socket(int sd) {
130 gras_procdata_t *pd = gras_procdata_get();
132 xbt_queue_push(pd->listener->socks_to_close,&sd);
133 gras_msg_listener_awake();