summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test/nptl/tst-cancel1.c163
-rw-r--r--test/nptl/tst-raise1.c62
-rw-r--r--test/nptl/tst-signal3.c261
-rw-r--r--test/nptl/tst-signal6.c192
4 files changed, 678 insertions, 0 deletions
diff --git a/test/nptl/tst-cancel1.c b/test/nptl/tst-cancel1.c
new file mode 100644
index 000000000..690319d8c
--- /dev/null
+++ b/test/nptl/tst-cancel1.c
@@ -0,0 +1,163 @@
+/* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <pthread.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
+
+static int cntr;
+
+
+static void
+cleanup (void *arg)
+{
+ if (arg != (void *) 42l)
+ cntr = 42;
+ else
+ cntr = 1;
+}
+
+
+static void *
+tf (void *arg)
+{
+ /* Ignore all signals. This must not have any effect on delivering
+ the cancellation signal. */
+ sigset_t ss;
+
+ sigfillset (&ss);
+
+ if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
+ {
+ puts ("pthread_sigmask failed");
+ exit (1);
+ }
+
+ pthread_cleanup_push (cleanup, (void *) 42l);
+
+ int err = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
+ if (err != 0)
+ {
+ printf ("setcanceltype failed: %s\n", strerror (err));
+ exit (1);
+ }
+ /* The following code is not standard compliant: the mutex functions
+ must not be called with asynchronous cancellation enabled. */
+
+ err = pthread_mutex_unlock (&m2);
+ if (err != 0)
+ {
+ printf ("child: mutex_unlock failed: %s\n", strerror (err));
+ exit (1);
+ }
+
+ err = pthread_mutex_lock (&m1);
+ if (err != 0)
+ {
+ printf ("child: 1st mutex_lock failed: %s\n", strerror (err));
+ exit (1);
+ }
+
+ /* We should never come here. */
+
+ pthread_cleanup_pop (0);
+
+ return NULL;
+}
+
+
+static int
+do_test (void)
+{
+ int err;
+ pthread_t th;
+ int result = 0;
+ void *retval;
+
+ /* Get the mutexes. */
+ err = pthread_mutex_lock (&m1);
+ if (err != 0)
+ {
+ printf ("parent: 1st mutex_lock failed: %s\n", strerror (err));
+ return 1;
+ }
+ err = pthread_mutex_lock (&m2);
+ if (err != 0)
+ {
+ printf ("parent: 2nd mutex_lock failed: %s\n", strerror (err));
+ return 1;
+ }
+
+ err = pthread_create (&th, NULL, tf, NULL);
+ if (err != 0)
+ {
+ printf ("create failed: %s\n", strerror (err));
+ return 1;
+ }
+
+ err = pthread_mutex_lock (&m2);
+ if (err != 0)
+ {
+ printf ("parent: 3rd mutex_lock failed: %s\n", strerror (err));
+ return 1;
+ }
+
+ err = pthread_cancel (th);
+ if (err != 0)
+ {
+ printf ("cancel failed: %s\n", strerror (err));
+ return 1;
+ }
+
+ err = pthread_join (th, &retval);
+ if (err != 0)
+ {
+ printf ("join failed: %s\n", strerror (err));
+ return 1;
+ }
+
+ if (retval != PTHREAD_CANCELED)
+ {
+ printf ("wrong return value: %p\n", retval);
+ result = 1;
+ }
+
+ if (cntr == 42)
+ {
+ puts ("cleanup handler called with wrong argument");
+ result = 1;
+ }
+ else if (cntr != 1)
+ {
+ puts ("cleanup handling not called");
+ result = 1;
+ }
+
+ return result;
+}
+
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/test/nptl/tst-raise1.c b/test/nptl/tst-raise1.c
new file mode 100644
index 000000000..5ea9886a4
--- /dev/null
+++ b/test/nptl/tst-raise1.c
@@ -0,0 +1,62 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <errno.h>
+#include <error.h>
+#include <signal.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+volatile int count;
+
+void
+sh (int sig)
+{
+ ++count;
+}
+
+int
+main (void)
+{
+ struct sigaction sa;
+ sa.sa_handler = sh;
+ sigemptyset (&sa.sa_mask);
+ sa.sa_flags = 0;
+ if (sigaction (SIGUSR1, &sa, NULL) < 0)
+ {
+ printf ("sigaction failed: %m\n");
+ exit (1);
+ }
+ if (raise (SIGUSR1) < 0)
+ {
+ printf ("first raise failed: %m\n");
+ exit (1);
+ }
+ if (raise (SIGUSR1) < 0)
+ {
+ printf ("second raise failed: %m\n");
+ exit (1);
+ }
+ if (count != 2)
+ {
+ printf ("signal handler not called 2 times\n");
+ exit (1);
+ }
+ exit (0);
+}
diff --git a/test/nptl/tst-signal3.c b/test/nptl/tst-signal3.c
new file mode 100644
index 000000000..e4756c56a
--- /dev/null
+++ b/test/nptl/tst-signal3.c
@@ -0,0 +1,261 @@
+/* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <errno.h>
+#include <pthread.h>
+#include <semaphore.h>
+#include <signal.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+/* Number of different signalss to use. Also is the number of
+ threads. */
+#define N 10
+/* Maximum number of threads in flight at any one time. */
+#define INFLIGHT 5
+/* Number of signals sent in total. */
+#define ROUNDS 10000
+
+
+static int received[N][N];
+static int nsig[N];
+static pthread_t th[N];
+static sem_t sem;
+static pthread_mutex_t lock[N];
+static pthread_t th_main;
+static int sig0;
+
+static void
+handler (int sig)
+{
+ int i;
+ for (i = 0; i < N; ++i)
+ if (pthread_equal (pthread_self (), th[i]))
+ break;
+
+ if (i == N)
+ {
+ if (pthread_equal (pthread_self (), th_main))
+ puts ("signal received by main thread");
+ else
+ printf ("signal received by unknown thread (%lx)\n",
+ (unsigned long int) pthread_self ());
+ exit (1);
+ }
+
+ ++received[i][sig - sig0];
+
+ sem_post (&sem);
+}
+
+
+static void *
+tf (void *arg)
+{
+ int idx = (long int) arg;
+
+ sigset_t ss;
+ sigemptyset (&ss);
+
+ int i;
+ for (i = 0; i <= idx; ++i)
+ sigaddset (&ss, sig0 + i);
+
+ if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
+ {
+ printf ("thread %d: pthread_sigmask failed\n", i);
+ exit (1);
+ }
+
+ pthread_mutex_lock (&lock[idx]);
+
+ return NULL;
+}
+
+
+static int
+do_test (void)
+{
+ /* Block all signals. */
+ sigset_t ss;
+ sigfillset (&ss);
+
+ th_main = pthread_self ();
+
+ sig0 = SIGRTMIN;
+
+ if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
+ {
+ puts ("1st pthread_sigmask failed");
+ exit (1);
+ }
+
+ /* Install the handler. */
+ int i;
+ for (i = 0; i < N; ++i)
+ {
+ struct sigaction sa =
+ {
+ .sa_handler = handler,
+ .sa_flags = 0
+ };
+ sigfillset (&sa.sa_mask);
+
+ if (sigaction (sig0 + i, &sa, NULL) != 0)
+ {
+ printf ("sigaction for signal %d failed\n", i);
+ exit (1);
+ }
+ }
+
+ if (sem_init (&sem, 0, INFLIGHT) != 0)
+ {
+ puts ("sem_init failed");
+ exit (1);
+ }
+
+ pthread_attr_t a;
+
+ if (pthread_attr_init (&a) != 0)
+ {
+ puts ("attr_init failed");
+ exit (1);
+ }
+
+ if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
+ {
+ puts ("attr_setstacksize failed");
+ return 1;
+ }
+
+ for (i = 0; i < N; ++i)
+ {
+ if (pthread_mutex_init (&lock[i], NULL) != 0)
+ {
+ printf ("mutex_init[%d] failed\n", i);
+ }
+
+ if (pthread_mutex_lock (&lock[i]) != 0)
+ {
+ printf ("mutex_lock[%d] failed\n", i);
+ }
+
+ if (pthread_create (&th[i], &a, tf, (void *) (long int) i) != 0)
+ {
+ printf ("create of thread %d failed\n", i);
+ exit (1);
+ }
+ }
+
+ if (pthread_attr_destroy (&a) != 0)
+ {
+ puts ("attr_destroy failed");
+ exit (1);
+ }
+
+ int result = 0;
+ unsigned int r = 42;
+ pid_t pid = getpid ();
+
+ for (i = 0; i < ROUNDS; ++i)
+ {
+ if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
+ {
+ printf ("sem_wait round %d failed: %m\n", i);
+ exit (1);
+ }
+
+ int s = rand_r (&r) % N;
+
+ kill (pid, sig0 + s);
+ }
+
+ void *status;
+ for (i = 0; i < N; ++i)
+ {
+ if (pthread_mutex_unlock (&lock[i]) != 0)
+ {
+ printf ("unlock %d failed\n", i);
+ exit (1);
+ }
+
+ if (pthread_join (th[i], &status) != 0)
+ {
+ printf ("join %d failed\n", i);
+ result = 1;
+ }
+ else if (status != NULL)
+ {
+ printf ("%d: result != NULL\n", i);
+ result = 1;
+ }
+ }
+
+ int total = 0;
+ for (i = 0; i < N; ++i)
+ {
+ int j;
+
+ for (j = 0; j <= i; ++j)
+ total += received[i][j];
+
+ for (j = i + 1; j < N; ++j)
+ if (received[i][j] != 0)
+ {
+ printf ("thread %d received signal SIGRTMIN+%d\n", i, j);
+ result = 1;
+ }
+ }
+
+ if (total != ROUNDS)
+ {
+ printf ("total number of handled signals is %d, expected %d\n",
+ total, ROUNDS);
+ result = 1;
+ }
+
+ printf ("A total of %d signals sent and received\n", total);
+ for (i = 0; i < N; ++i)
+ {
+ printf ("thread %2d:", i);
+
+ int j;
+ for (j = 0; j <= i; ++j)
+ {
+ printf (" %5d", received[i][j]);
+ nsig[j] += received[i][j];
+ }
+
+ putchar ('\n');
+
+ }
+
+ printf ("\nTotal :");
+ for (i = 0; i < N; ++i)
+ printf (" %5d", nsig[i]);
+ putchar ('\n');
+
+ return result;
+}
+
+#define TIMEOUT 10
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/test/nptl/tst-signal6.c b/test/nptl/tst-signal6.c
new file mode 100644
index 000000000..85a864002
--- /dev/null
+++ b/test/nptl/tst-signal6.c
@@ -0,0 +1,192 @@
+/* Copyright (C) 2003 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, write to the Free
+ Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ 02111-1307 USA. */
+
+#include <pthread.h>
+#include <signal.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+#define N 2
+static pthread_barrier_t bar;
+static struct
+{
+ void *p;
+ pthread_t s;
+} ti[N];
+static int sig1;
+
+
+static void
+handler (int sig)
+{
+ pthread_t self = pthread_self ();
+ size_t i;
+
+ for (i = 0; i < N; ++i)
+ if (ti[i].s == self)
+ {
+ if ((uintptr_t) ti[i].p <= (uintptr_t) &self
+ && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
+ {
+ puts ("alt stack not used");
+ exit (1);
+ }
+
+ printf ("thread %zu used alt stack for signal %d\n", i, sig);
+
+ return;
+ }
+
+ puts ("handler: thread not found");
+ exit (1);
+}
+
+
+static void *
+tf (void *arg)
+{
+ size_t nr = (uintptr_t) arg;
+ if (nr >= N)
+ {
+ puts ("wrong nr parameter");
+ exit (1);
+ }
+
+ sigset_t ss;
+ sigemptyset (&ss);
+ size_t i;
+ for (i = 0; i < N; ++i)
+ if (i != nr)
+ if (sigaddset (&ss, sig1 + i) != 0)
+ {
+ puts ("tf: sigaddset failed");
+ exit (1);
+ }
+ if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
+ {
+ puts ("tf: sigmask failed");
+ exit (1);
+ }
+
+ void *p = malloc (2 * MINSIGSTKSZ);
+ if (p == NULL)
+ {
+ puts ("tf: malloc failed");
+ exit (1);
+ }
+
+ stack_t s;
+ s.ss_sp = p;
+ s.ss_size = 2 * MINSIGSTKSZ;
+ s.ss_flags = 0;
+ if (sigaltstack (&s, NULL) != 0)
+ {
+ puts ("tf: sigaltstack failed");
+ exit (1);
+ }
+
+ ti[nr].p = p;
+ ti[nr].s = pthread_self ();
+
+ pthread_barrier_wait (&bar);
+
+ pthread_barrier_wait (&bar);
+
+ return NULL;
+}
+
+
+static int
+do_test (void)
+{
+ sig1 = SIGRTMIN;
+ if (sig1 + N > SIGRTMAX)
+ {
+ puts ("too few RT signals");
+ return 0;
+ }
+
+ struct sigaction sa;
+ sa.sa_handler = handler;
+ sa.sa_flags = 0;
+ sigemptyset (&sa.sa_mask);
+
+ if (sigaction (sig1, &sa, NULL) != 0
+ || sigaction (sig1 + 1, &sa, NULL) != 0
+ || sigaction (sig1 + 2, &sa, NULL) != 0)
+ {
+ puts ("sigaction failed");
+ return 1;
+ }
+
+ if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
+ {
+ puts ("barrier_init failed");
+ return 1;
+ }
+
+ pthread_t th[N];
+ size_t i;
+ for (i = 0; i < N; ++i)
+ if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0)
+ {
+ puts ("create failed");
+ return 1;
+ }
+
+ /* Block the three signals. */
+ sigset_t ss;
+ sigemptyset (&ss);
+ for (i = 0; i <= N; ++i)
+ sigaddset (&ss, sig1 + i);
+ if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
+ {
+ puts ("main: sigmask failed");
+ return 1;
+ }
+
+ pthread_barrier_wait (&bar);
+
+ /* Send some signals. */
+ pid_t me = getpid ();
+ kill (me, sig1 + N);
+ for (i = 0; i < N; ++i)
+ kill (me, sig1 + i);
+ kill (me, sig1 + N);
+
+ /* Give the signals a chance to be worked on. */
+ sleep (1);
+
+ pthread_barrier_wait (&bar);
+
+ for (i = 0; i < N; ++i)
+ if (pthread_join (th[i], NULL) != 0)
+ {
+ puts ("join failed");
+ return 1;
+ }
+
+ return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"