|
Packit |
b5b901 |
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
|
Packit |
b5b901 |
*
|
|
Packit |
b5b901 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
Packit |
b5b901 |
* of this software and associated documentation files (the "Software"), to
|
|
Packit |
b5b901 |
* deal in the Software without restriction, including without limitation the
|
|
Packit |
b5b901 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
Packit |
b5b901 |
* sell copies of the Software, and to permit persons to whom the Software is
|
|
Packit |
b5b901 |
* furnished to do so, subject to the following conditions:
|
|
Packit |
b5b901 |
*
|
|
Packit |
b5b901 |
* The above copyright notice and this permission notice shall be included in
|
|
Packit |
b5b901 |
* all copies or substantial portions of the Software.
|
|
Packit |
b5b901 |
*
|
|
Packit |
b5b901 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
b5b901 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
b5b901 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit |
b5b901 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit |
b5b901 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
Packit |
b5b901 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
Packit |
b5b901 |
* IN THE SOFTWARE.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* This test does not pretend to be cross-platform. */
|
|
Packit |
b5b901 |
#ifndef _WIN32
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
#include "uv.h"
|
|
Packit |
b5b901 |
#include "task.h"
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
#include <errno.h>
|
|
Packit |
b5b901 |
#include <signal.h>
|
|
Packit |
b5b901 |
#include <stdarg.h>
|
|
Packit |
b5b901 |
#include <stdio.h>
|
|
Packit |
b5b901 |
#include <stdlib.h>
|
|
Packit |
b5b901 |
#include <string.h>
|
|
Packit |
b5b901 |
#include <unistd.h>
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* The value of NUM_SIGNAL_HANDLING_THREADS is not arbitrary; it needs to be a
|
|
Packit |
b5b901 |
* multiple of three for reasons that will become clear when you scroll down.
|
|
Packit |
b5b901 |
* We're basically creating three different thread groups. The total needs
|
|
Packit |
b5b901 |
* to be divisible by three in order for the numbers in the final check to
|
|
Packit |
b5b901 |
* match up.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
#define NUM_SIGNAL_HANDLING_THREADS 24
|
|
Packit |
b5b901 |
#define NUM_LOOP_CREATING_THREADS 10
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
enum signal_action {
|
|
Packit |
b5b901 |
ONLY_SIGUSR1,
|
|
Packit |
b5b901 |
ONLY_SIGUSR2,
|
|
Packit |
b5b901 |
SIGUSR1_AND_SIGUSR2
|
|
Packit |
b5b901 |
};
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static uv_sem_t sem;
|
|
Packit |
b5b901 |
static uv_mutex_t counter_lock;
|
|
Packit |
b5b901 |
static volatile int stop = 0;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static volatile int signal1_cb_counter = 0;
|
|
Packit |
b5b901 |
static volatile int signal2_cb_counter = 0;
|
|
Packit |
b5b901 |
static volatile int loop_creation_counter = 0;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void increment_counter(volatile int* counter) {
|
|
Packit |
b5b901 |
uv_mutex_lock(&counter_lock);
|
|
Packit |
b5b901 |
++(*counter);
|
|
Packit |
b5b901 |
uv_mutex_unlock(&counter_lock);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void signal1_cb(uv_signal_t* handle, int signum) {
|
|
Packit |
b5b901 |
ASSERT(signum == SIGUSR1);
|
|
Packit |
b5b901 |
increment_counter(&signal1_cb_counter);
|
|
Packit |
b5b901 |
uv_signal_stop(handle);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void signal2_cb(uv_signal_t* handle, int signum) {
|
|
Packit |
b5b901 |
ASSERT(signum == SIGUSR2);
|
|
Packit |
b5b901 |
increment_counter(&signal2_cb_counter);
|
|
Packit |
b5b901 |
uv_signal_stop(handle);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void signal_handling_worker(void* context) {
|
|
Packit |
b5b901 |
enum signal_action action;
|
|
Packit |
b5b901 |
uv_signal_t signal1a;
|
|
Packit |
b5b901 |
uv_signal_t signal1b;
|
|
Packit |
b5b901 |
uv_signal_t signal2;
|
|
Packit |
b5b901 |
uv_loop_t loop;
|
|
Packit |
b5b901 |
int r;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
action = (enum signal_action) (uintptr_t) context;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
ASSERT(0 == uv_loop_init(&loop);;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Setup the signal watchers and start them. */
|
|
Packit |
b5b901 |
if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
|
|
Packit |
b5b901 |
r = uv_signal_init(&loop, &signal1a);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_signal_init(&loop, &signal1b);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
|
|
Packit |
b5b901 |
r = uv_signal_init(&loop, &signal2);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Signal watchers are now set up. */
|
|
Packit |
b5b901 |
uv_sem_post(&sem);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait for all signals. The signal callbacks stop the watcher, so uv_run
|
|
Packit |
b5b901 |
* will return when all signal watchers caught a signal.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
r = uv_run(&loop, UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Restart the signal watchers. */
|
|
Packit |
b5b901 |
if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal1a, signal1_cb, SIGUSR1);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal1b, signal1_cb, SIGUSR1);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal2, signal2_cb, SIGUSR2);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait for signals once more. */
|
|
Packit |
b5b901 |
uv_sem_post(&sem);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_run(&loop, UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Close the watchers. */
|
|
Packit |
b5b901 |
if (action == ONLY_SIGUSR1 || action == SIGUSR1_AND_SIGUSR2) {
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &signal1a, NULL);
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &signal1b, NULL);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
if (action == ONLY_SIGUSR2 || action == SIGUSR1_AND_SIGUSR2) {
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &signal2, NULL);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait for the signal watchers to close. */
|
|
Packit |
b5b901 |
r = uv_run(&loop, UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_loop_close(&loop);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void signal_unexpected_cb(uv_signal_t* handle, int signum) {
|
|
Packit |
b5b901 |
ASSERT(0 && "signal_unexpected_cb should never be called");
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
static void loop_creating_worker(void* context) {
|
|
Packit |
b5b901 |
(void) context;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
do {
|
|
Packit |
b5b901 |
uv_loop_t *loop;
|
|
Packit |
b5b901 |
uv_signal_t signal;
|
|
Packit |
b5b901 |
int r;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
loop = malloc(sizeof(*loop));
|
|
Packit |
b5b901 |
ASSERT(loop != NULL);
|
|
Packit |
b5b901 |
ASSERT(0 == uv_loop_init(loop));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_signal_init(loop, &signal);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_signal_start(&signal, signal_unexpected_cb, SIGTERM);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_close((uv_handle_t*) &signal, NULL);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_run(loop, UV_RUN_DEFAULT);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_loop_close(loop);
|
|
Packit |
b5b901 |
free(loop);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
increment_counter(&loop_creation_counter);
|
|
Packit |
b5b901 |
} while (!stop);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
TEST_IMPL(signal_multiple_loops) {
|
|
Packit |
b5b901 |
#if defined(__CYGWIN__) || defined(__MSYS__)
|
|
Packit |
b5b901 |
/* FIXME: This test needs more investigation. Somehow the `read` in
|
|
Packit |
b5b901 |
uv__signal_lock fails spuriously with EACCES or even EAGAIN even
|
|
Packit |
b5b901 |
though it is supposed to be blocking. Also the test hangs during
|
|
Packit |
b5b901 |
thread setup occasionally. */
|
|
Packit |
b5b901 |
RETURN_SKIP("FIXME: This test needs more investigation on Cygwin");
|
|
Packit |
b5b901 |
#endif
|
|
Packit |
b5b901 |
uv_thread_t loop_creating_threads[NUM_LOOP_CREATING_THREADS];
|
|
Packit |
b5b901 |
uv_thread_t signal_handling_threads[NUM_SIGNAL_HANDLING_THREADS];
|
|
Packit |
b5b901 |
enum signal_action action;
|
|
Packit |
b5b901 |
sigset_t sigset;
|
|
Packit |
b5b901 |
int i;
|
|
Packit |
b5b901 |
int r;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_sem_init(&sem, 0);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_mutex_init(&counter_lock);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Create a couple of threads that create a destroy loops continuously. */
|
|
Packit |
b5b901 |
for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
|
|
Packit |
b5b901 |
r = uv_thread_create(&loop_creating_threads[i],
|
|
Packit |
b5b901 |
loop_creating_worker,
|
|
Packit |
b5b901 |
NULL);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Create a couple of threads that actually handle signals. */
|
|
Packit |
b5b901 |
for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
|
|
Packit |
b5b901 |
switch (i % 3) {
|
|
Packit |
b5b901 |
case 0: action = ONLY_SIGUSR1; break;
|
|
Packit |
b5b901 |
case 1: action = ONLY_SIGUSR2; break;
|
|
Packit |
b5b901 |
case 2: action = SIGUSR1_AND_SIGUSR2; break;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = uv_thread_create(&signal_handling_threads[i],
|
|
Packit |
b5b901 |
signal_handling_worker,
|
|
Packit |
b5b901 |
(void*) (uintptr_t) action);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait until all threads have started and set up their signal watchers. */
|
|
Packit |
b5b901 |
for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
|
|
Packit |
b5b901 |
uv_sem_wait(&sem);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = kill(getpid(), SIGUSR1);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = kill(getpid(), SIGUSR2);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait for all threads to handle these signals. */
|
|
Packit |
b5b901 |
for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++)
|
|
Packit |
b5b901 |
uv_sem_wait(&sem);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Block all signals to this thread, so we are sure that from here the signal
|
|
Packit |
b5b901 |
* handler runs in another thread. This is more likely to catch thread and
|
|
Packit |
b5b901 |
* signal safety issues if there are any.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
sigfillset(&sigset);
|
|
Packit |
b5b901 |
pthread_sigmask(SIG_SETMASK, &sigset, NULL);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
r = kill(getpid(), SIGUSR1);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
r = kill(getpid(), SIGUSR2);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait for all signal handling threads to exit. */
|
|
Packit |
b5b901 |
for (i = 0; i < NUM_SIGNAL_HANDLING_THREADS; i++) {
|
|
Packit |
b5b901 |
r = uv_thread_join(&signal_handling_threads[i]);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Tell all loop creating threads to stop. */
|
|
Packit |
b5b901 |
stop = 1;
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* Wait for all loop creating threads to exit. */
|
|
Packit |
b5b901 |
for (i = 0; i < NUM_LOOP_CREATING_THREADS; i++) {
|
|
Packit |
b5b901 |
r = uv_thread_join(&loop_creating_threads[i]);
|
|
Packit |
b5b901 |
ASSERT(r == 0);
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
uv_sem_destroy(&sem);
|
|
Packit |
b5b901 |
printf("signal1_cb calls: %d\n", signal1_cb_counter);
|
|
Packit |
b5b901 |
printf("signal2_cb calls: %d\n", signal2_cb_counter);
|
|
Packit |
b5b901 |
printf("loops created and destroyed: %d\n", loop_creation_counter);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* The division by three reflects the fact that we spawn three different
|
|
Packit |
b5b901 |
* thread groups of (NUM_SIGNAL_HANDLING_THREADS / 3) threads each.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
ASSERT(signal1_cb_counter == 8 * (NUM_SIGNAL_HANDLING_THREADS / 3));
|
|
Packit |
b5b901 |
ASSERT(signal2_cb_counter == 4 * (NUM_SIGNAL_HANDLING_THREADS / 3));
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
/* We don't know exactly how much loops will be created and destroyed, but at
|
|
Packit |
b5b901 |
* least there should be 1 for every loop creating thread.
|
|
Packit |
b5b901 |
*/
|
|
Packit |
b5b901 |
ASSERT(loop_creation_counter >= NUM_LOOP_CREATING_THREADS);
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
MAKE_VALGRIND_HAPPY();
|
|
Packit |
b5b901 |
return 0;
|
|
Packit |
b5b901 |
}
|
|
Packit |
b5b901 |
|
|
Packit |
b5b901 |
#endif /* !_WIN32 */
|