diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-05-06 00:40:49 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-05-06 00:40:49 +0200 |
commit | 5c5c6fc18c9445712a9bbd3612d1335f3a92d9ee (patch) | |
tree | 378356b69df6fea500e66dbf8abb62d71220d063 | |
parent | close files before running initrc (diff) | |
download | mds-5c5c6fc18c9445712a9bbd3612d1335f3a92d9ee.tar.gz mds-5c5c6fc18c9445712a9bbd3612d1335f3a92d9ee.tar.bz2 mds-5c5c6fc18c9445712a9bbd3612d1335f3a92d9ee.tar.xz |
kill all threads
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r-- | src/mds-server.c | 71 | ||||
-rw-r--r-- | src/mds-server.h | 6 |
2 files changed, 57 insertions, 20 deletions
diff --git a/src/mds-server.c b/src/mds-server.c index 1349ee3..55abea6 100644 --- a/src/mds-server.c +++ b/src/mds-server.c @@ -84,6 +84,11 @@ static pthread_mutex_t slave_mutex; static pthread_cond_t slave_cond; /** + * The thread that runs the master loop + */ +static pthread_t master_thread; + +/** * Map from client socket file descriptor to all information (client_t) */ static fd_table_t client_map; @@ -258,6 +263,10 @@ int main(int argc_, char** argv_) } + /* Store the current thread so it can be killed from elsewhere. */ + master_thread = pthread_self(); + + /* Make the server update without all slaves dying on SIGUSR1. */ { struct sigaction action; @@ -467,24 +476,6 @@ void* slave_loop(void* data) int r; - /* Make the server update without all slaves dying on SIGUSR1. */ - { - struct sigaction action; - sigset_t sigset; - - sigemptyset(&sigset); - action.sa_handler = sigusr1_trap; - action.sa_mask = sigset; - action.sa_flags = 0; - - if (sigaction(SIGUSR1, &action, NULL) < 0) - { - perror(*argv); - goto fail; - } - } - - if (information == NULL) { /* Create information table. */ @@ -526,6 +517,28 @@ void* slave_loop(void* data) } + /* Store the thread so that other threads can kill it. */ + information->thread = pthread_self(); + + + /* Make the server update without all slaves dying on SIGUSR1. */ + { + struct sigaction action; + sigset_t sigset; + + sigemptyset(&sigset); + action.sa_handler = sigusr1_trap; + action.sa_mask = sigset; + action.sa_flags = 0; + + if (sigaction(SIGUSR1, &action, NULL) < 0) + { + perror(*argv); + goto fail; + } + } + + /* Fetch messages from the slave. */ if (information->open) while (reexecing == 0) @@ -695,12 +708,30 @@ void run_initrc(char** args) * * @param signo The caught signal */ -void sigusr1_trap(int signo __attribute__((unused))) +void sigusr1_trap(int signo) { if (reexecing == 0) { + pthread_t current_thread; + ssize_t node; + reexecing = 1; - /* TODO send the signal to all threads. */ + current_thread = pthread_self(); + + if (pthread_equal(current_thread, master_thread) == 0) + pthread_kill(master_thread, signo); + + with_mutex(slave_mutex, + for (node = client_list.edge;;) + { + client_t* value; + if ((node = client_list.next[node]) == client_list.edge) + break; + + value = (client_t*)(void*)(client_list.values[node]); + if (pthread_equal(current_thread, value->thread) == 0) + pthread_kill(value->thread, signo); + }); } } diff --git a/src/mds-server.h b/src/mds-server.h index 851d391..67e97f6 100644 --- a/src/mds-server.h +++ b/src/mds-server.h @@ -22,6 +22,7 @@ #include <libmdsserver/mds-message.h> #include <stdlib.h> +#include <pthread.h> /** @@ -49,6 +50,11 @@ typedef struct client */ mds_message_t message; + /** + * The read thread for the client + */ + pthread_t thread; + } client_t; |