diff --git a/man/coap_threads.txt.in b/man/coap_threads.txt.in index d5fb0658..950f1cfb 100644 --- a/man/coap_threads.txt.in +++ b/man/coap_threads.txt.in @@ -128,17 +128,28 @@ thread that called *coap_io_process_loop*() to return. *Function: coap_io_process_configure_threads()* The *coap_io_process_configure_threads*() function is used to set up -an additional _thread_count_ threads for _context_. Usually -*coap_io_process_loop*() would be called, but can be used to wrap with -*coap_io_process_remove_threads*() a complex version of _main_loop_code_. +an additional _thread_count_ threads for _context_ which loop doing a +*coap_io_process*(3) until told to exit. Usually *coap_io_process_loop*() +would be called, but *coap_io_process_configure_threads*() can be used to wrap +with *coap_io_process_remove_threads*() a complex version +of _main_loop_code_. + +*NOTE:* *coap_io_process_configure_threads*() sets up its own *signal*(3) / +*sigaction*(3) handler to handle the additional threads shutdown using SIGINT. +This handler will also call any handler set up by *signal*(3) / *sigaction*(3) +for SIGINT prior to the call to *coap_io_process_configure_threads*(). +Ideally, *signal*(3) / *sigaction*(3) for SIGINT should not be called after +calling *coap_io_process_configure_threads*(). *coap_io_process_loop*() uses *coap_io_process_configure_threads*() and -*coap_io_process_remove_threads*() to wrap the call to _main_loop_code_. +*coap_io_process_remove_threads*() to wrap the call to _main_loop_code_ until +told to exit.. *Function: coap_io_process_remove_threads()* The *coap_io_process_remove_threads*() function is used stop and remove threads created by *coap_io_process_configure_threads*() for _context_. +For most environments, the additional threads are killed off with SIGINT. RETURN VALUES ------------- diff --git a/src/coap_threadsafe.c b/src/coap_threadsafe.c index 6763d872..0acd7a83 100644 --- a/src/coap_threadsafe.c +++ b/src/coap_threadsafe.c @@ -145,11 +145,68 @@ coap_io_process_worker_thread(void *arg) { return 0; } +#if defined(HAVE_SIGNAL_H) || defined(__ZEPHYR__) +#include +#endif /* HAVE_SIGNAL_H || __ZEPHYR__ */ + +/* sighandler_t is not defined is all OS */ +typedef void (*coap_sig_handler_t)(int); + +static coap_sig_handler_t old_sigint_handler = SIG_IGN; + +static void +coap_thread_sigint_handler(int signum) { + coap_thread_quit = 1; + if (old_sigint_handler != SIG_IGN && old_sigint_handler != SIG_DFL) { + old_sigint_handler(signum); + } +} + +#ifndef _WIN32 +typedef void (*coap_sig_action_t)(int, siginfo_t *, void *); +static coap_sig_action_t old_sigint_action = NULL; + +static void +coap_thread_sigint_action(int signum, siginfo_t *siginfo, void *ptr) { + coap_thread_quit = 1; + if (old_sigint_action != NULL) { + old_sigint_action(signum, siginfo, ptr); + } +} +#endif /*! _WIN32 */ + int coap_io_process_configure_threads(coap_context_t *context, uint32_t thread_count) { uint32_t i; coap_mutex_lock(&m_io_threads); +#ifdef _WIN32 + old_sigint_handler = signal(SIGINT, coap_thread_sigint_handler); +#else /* ! _WIN32 */ + struct sigaction oldact; + struct sigaction newact; + + /* Get the old handler / action */ + if (sigaction(SIGINT, NULL, &oldact) != -1) { + if (oldact.sa_flags & SA_SIGINFO) { + memset(&newact, 0, sizeof(newact)); + sigemptyset(&newact.sa_mask); + newact.sa_sigaction = coap_thread_sigint_action; + newact.sa_flags = SA_SIGINFO; + if (sigaction(SIGINT, &newact, &oldact) != -1) { + old_sigint_action = oldact.sa_sigaction; + } + } else { + memset(&newact, 0, sizeof(newact)); + sigemptyset(&newact.sa_mask); + newact.sa_handler = coap_thread_sigint_handler; + newact.sa_flags = 0; + if (sigaction(SIGINT, &newact, &oldact) != -1) { + old_sigint_handler = oldact.sa_handler; + } + } + } +#endif /* ! _WIN32 */ thread_no = 1; max_thread_no = 1 + thread_count; @@ -186,9 +243,6 @@ coap_io_process_remove_threads(coap_context_t *context) { coap_lock_unlock(); } -#ifdef HAVE_SIGNAL_H -#include -#endif /* HAVE_SIGNAL_H */ void coap_io_process_remove_threads_lkd(coap_context_t *context) { uint32_t i;