|
Packit Service |
a2ae7a |
/* Test of pthread_sigmask in a multi-threaded program.
|
|
Packit Service |
a2ae7a |
Copyright (C) 2011-2019 Free Software Foundation, Inc.
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
This program is free software: you can redistribute it and/or modify
|
|
Packit Service |
a2ae7a |
it under the terms of the GNU General Public License as published by
|
|
Packit Service |
a2ae7a |
the Free Software Foundation; either version 3 of the License, or
|
|
Packit Service |
a2ae7a |
(at your option) any later version.
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
This program is distributed in the hope that it will be useful,
|
|
Packit Service |
a2ae7a |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
a2ae7a |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
a2ae7a |
GNU General Public License for more details.
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
a2ae7a |
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Written by Bruno Haible <bruno@clisp.org>, 2011. */
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <config.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <signal.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include <errno.h>
|
|
Packit Service |
a2ae7a |
#include <stdio.h>
|
|
Packit Service |
a2ae7a |
#include <unistd.h>
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include "glthread/thread.h"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#include "macros.h"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#if USE_POSIX_THREADS
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
static gl_thread_t main_thread;
|
|
Packit Service |
a2ae7a |
static gl_thread_t killer_thread;
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
static void *
|
|
Packit Service |
a2ae7a |
killer_thread_func (void *arg)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
sleep (1);
|
|
Packit Service |
a2ae7a |
pthread_kill (main_thread, SIGINT);
|
|
Packit Service |
a2ae7a |
return NULL;
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
static volatile int sigint_occurred;
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
static void
|
|
Packit Service |
a2ae7a |
sigint_handler (int sig)
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
sigint_occurred++;
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
int
|
|
Packit Service |
a2ae7a |
main (int argc, char *argv[])
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
sigset_t set;
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
signal (SIGINT, sigint_handler);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
sigemptyset (&set);
|
|
Packit Service |
a2ae7a |
sigaddset (&set, SIGINT);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Check error handling. */
|
|
Packit Service |
a2ae7a |
ASSERT (pthread_sigmask (1729, &set, NULL) == EINVAL);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Block SIGINT. */
|
|
Packit Service |
a2ae7a |
ASSERT (pthread_sigmask (SIG_BLOCK, &set, NULL) == 0);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Request a SIGINT signal from another thread. */
|
|
Packit Service |
a2ae7a |
main_thread = gl_thread_self ();
|
|
Packit Service |
a2ae7a |
ASSERT (glthread_create (&killer_thread, killer_thread_func, NULL) == 0);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Wait. */
|
|
Packit Service |
a2ae7a |
sleep (2);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* The signal should not have arrived yet, because it is blocked. */
|
|
Packit Service |
a2ae7a |
ASSERT (sigint_occurred == 0);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Unblock SIGINT. */
|
|
Packit Service |
a2ae7a |
ASSERT (pthread_sigmask (SIG_UNBLOCK, &set, NULL) == 0);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* The signal should have arrived now, because POSIX says
|
|
Packit Service |
a2ae7a |
"If there are any pending unblocked signals after the call to
|
|
Packit Service |
a2ae7a |
pthread_sigmask(), at least one of those signals shall be delivered
|
|
Packit Service |
a2ae7a |
before the call to pthread_sigmask() returns." */
|
|
Packit Service |
a2ae7a |
ASSERT (sigint_occurred == 1);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
/* Clean up the thread. This avoid a "ThreadSanitizer: thread leak" warning
|
|
Packit Service |
a2ae7a |
from "gcc -fsanitize=thread". */
|
|
Packit Service |
a2ae7a |
gl_thread_join (killer_thread, NULL);
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
return 0;
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#else
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
int
|
|
Packit Service |
a2ae7a |
main ()
|
|
Packit Service |
a2ae7a |
{
|
|
Packit Service |
a2ae7a |
fputs ("Skipping test: POSIX threads not enabled\n", stderr);
|
|
Packit Service |
a2ae7a |
return 77;
|
|
Packit Service |
a2ae7a |
}
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
#endif
|