From 31891e01b907a4478bb427ca2830598a62da2210 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sun, 20 Oct 2013 16:54:12 +0200 Subject: [PATCH] frontend/sock: Added loop control to the socket handler. --- src/frontend/sock.c | 11 ++++++++--- src/include/frontend/sock.h | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/frontend/sock.c b/src/frontend/sock.c index 9b650dd..8e2bc59 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -276,7 +276,7 @@ sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address) } /* sdb_fe_sock_add_listener */ int -sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock) +sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop) { sdb_channel_t *chan; fd_set sockets; @@ -286,7 +286,7 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock) /* XXX: make the number of threads configurable */ pthread_t handler_threads[5]; - if ((! sock) || (! sock->listeners_num)) + if ((! sock) || (! sock->listeners_num) || (! loop)) return -1; FD_ZERO(&sockets); @@ -316,7 +316,7 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock) pthread_create(&handler_threads[i], /* attr = */ NULL, connection_handler, /* arg = */ chan); - while (42) { + while (loop->do_loop) { fd_set ready = sockets; int n; @@ -363,6 +363,11 @@ sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock) } } } + + for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) + pthread_cancel(handler_threads[i]); + for (i = 0; i < SDB_STATIC_ARRAY_LEN(handler_threads); ++i) + pthread_join(handler_threads[i], NULL); return 0; } /* sdb_fe_sock_listen_and_server */ diff --git a/src/include/frontend/sock.h b/src/include/frontend/sock.h index 1951614..ba33d13 100644 --- a/src/include/frontend/sock.h +++ b/src/include/frontend/sock.h @@ -32,6 +32,12 @@ extern "C" { #endif +/* manage a front-end listener loop */ +typedef struct { + _Bool do_loop; +} sdb_fe_loop_t; +#define SDB_FE_LOOP_INIT { 1 } + /* * sdb_fe_socket_t: * A front-end socket accepting connections from clients. @@ -73,14 +79,15 @@ sdb_fe_sock_add_listener(sdb_fe_socket_t *sock, const char *address); /* * sdb_fe_sock_listen_and_serve: - * Listen on the specified socket and serve client requests. + * Listen on the specified socket and serve client requests. The loop + * terminates on error or when the loop condition turns to false. * * Returns: * - 0 on success * - a negative value else */ int -sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock); +sdb_fe_sock_listen_and_serve(sdb_fe_socket_t *sock, sdb_fe_loop_t *loop); #ifdef __cplusplus } /* extern "C" */ -- 2.30.2