diff options
author | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2020-02-09 17:15:47 +0000 |
---|---|---|
committer | Samuel Thibault <samuel.thibault@ens-lyon.org> | 2020-02-09 17:17:06 +0000 |
commit | 74159dc58af2ce8a32b62daecd587f29ff7931f2 (patch) | |
tree | 089ea56ba675c3aed5c9a8f66ec8cc2e3469338f /sysdeps/pthread | |
parent | 71d52ac4d65435791d8fa9f52abab7107ef7f7e8 (diff) | |
download | glibc-74159dc58af2ce8a32b62daecd587f29ff7931f2.tar.gz glibc-74159dc58af2ce8a32b62daecd587f29ff7931f2.tar.xz glibc-74159dc58af2ce8a32b62daecd587f29ff7931f2.zip |
pthread: Move most mutex tests from nptl to sysdeps/pthread
So they can be checked with htl too. XFAIL tst-mutex4, for which support is still missing in htl.
Diffstat (limited to 'sysdeps/pthread')
-rw-r--r-- | sysdeps/pthread/Makefile | 3 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex1.c | 83 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex10.c | 109 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex2.c | 241 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex3.c | 241 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex4.c | 276 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex6.c | 76 | ||||
-rw-r--r-- | sysdeps/pthread/tst-mutex8.c | 435 |
8 files changed, 1463 insertions, 1 deletions
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 6b66031706..fb467d8e79 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -44,6 +44,7 @@ tests += tst-cnd-basic tst-mtx-trylock tst-cnd-broadcast \ tst-mtx-recursive tst-tss-basic tst-call-once tst-mtx-timedlock \ tst-basic1 tst-basic2 tst-basic3 tst-basic4 tst-basic5 tst-basic6 \ tst-basic7 \ - tst-spin1 tst-spin2 tst-spin3 tst-spin4 \ + tst-mutex1 tst-mutex2 tst-mutex3 tst-mutex4 tst-mutex6 tst-mutex10 \ + tst-spin1 tst-spin2 tst-spin3 tst-spin4 endif diff --git a/sysdeps/pthread/tst-mutex1.c b/sysdeps/pthread/tst-mutex1.c new file mode 100644 index 0000000000..dba1f5fad6 --- /dev/null +++ b/sysdeps/pthread/tst-mutex1.c @@ -0,0 +1,83 @@ +/* Copyright (C) 2002-2020 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, see + <https://www.gnu.org/licenses/>. */ + +#include <pthread.h> +#include <stdio.h> +#include <errno.h> +#include <stdbool.h> +#include <libc-diag.h> + +#ifndef ATTR +# define ATTR NULL +# define ATTR_NULL true +#endif + + +static int +do_test (void) +{ + pthread_mutex_t m; + + int e = pthread_mutex_init (&m, ATTR); + if (!ATTR_NULL && e == ENOTSUP) + { + puts ("cannot support selected type of mutexes"); + return 0; + } + else if (e != 0) + { + puts ("mutex_init failed"); + return 1; + } + + /* This deliberately tests supplying a null pointer to a function whose + argument is marked __attribute__ ((nonnull)). */ + DIAG_PUSH_NEEDS_COMMENT; + DIAG_IGNORE_NEEDS_COMMENT (5, "-Wnonnull"); + if (!ATTR_NULL && pthread_mutexattr_destroy (ATTR) != 0) + { + puts ("mutexattr_destroy failed"); + return 1; + } + DIAG_POP_NEEDS_COMMENT; + + if (pthread_mutex_lock (&m) != 0) + { + puts ("mutex_lock failed"); + return 1; + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("mutex_unlock failed"); + return 1; + } + + if (pthread_mutex_destroy (&m) != 0) + { + puts ("mutex_destroy failed"); + return 1; + } + + return 0; +} + +#ifndef TEST_FUNCTION +# define TEST_FUNCTION do_test () +#endif +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-mutex10.c b/sysdeps/pthread/tst-mutex10.c new file mode 100644 index 0000000000..646ef13bb3 --- /dev/null +++ b/sysdeps/pthread/tst-mutex10.c @@ -0,0 +1,109 @@ +/* Testing race while enabling lock elision. + Copyright (C) 2018-2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + 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, see + <https://www.gnu.org/licenses/>. */ +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <pthread.h> +#include <unistd.h> +#include <getopt.h> +#include <support/support.h> +#include <support/xthread.h> + +static pthread_barrier_t barrier; +static pthread_mutex_t mutex; +static long long int iteration_count = 1000000; +static unsigned int thread_count = 3; + +static void * +thr_func (void *arg) +{ + long long int i; + for (i = 0; i < iteration_count; i++) + { + if ((uintptr_t) arg == 0) + { + xpthread_mutex_destroy (&mutex); + xpthread_mutex_init (&mutex, NULL); + } + + xpthread_barrier_wait (&barrier); + + /* Test if enabling lock elision works if it is enabled concurrently. + There was a race in FORCE_ELISION macro which leads to either + pthread_mutex_destroy returning EBUSY as the owner was recorded + by pthread_mutex_lock - in "normal mutex" code path - but was not + resetted in pthread_mutex_unlock - in "elision" code path. + Or it leads to the assertion in nptl/pthread_mutex_lock.c: + assert (mutex->__data.__owner == 0); + Please ensure that the test is run with lock elision: + export GLIBC_TUNABLES=glibc.elision.enable=1 */ + xpthread_mutex_lock (&mutex); + xpthread_mutex_unlock (&mutex); + + xpthread_barrier_wait (&barrier); + } + return NULL; +} + +static int +do_test (void) +{ + unsigned int i; + printf ("Starting %d threads to run %lld iterations.\n", + thread_count, iteration_count); + + pthread_t *threads = xmalloc (thread_count * sizeof (pthread_t)); + xpthread_barrier_init (&barrier, NULL, thread_count); + xpthread_mutex_init (&mutex, NULL); + + for (i = 0; i < thread_count; i++) + threads[i] = xpthread_create (NULL, thr_func, (void *) (uintptr_t) i); + + for (i = 0; i < thread_count; i++) + xpthread_join (threads[i]); + + xpthread_barrier_destroy (&barrier); + free (threads); + + return EXIT_SUCCESS; +} + +#define OPT_ITERATIONS 10000 +#define OPT_THREADS 10001 +#define CMDLINE_OPTIONS \ + { "iterations", required_argument, NULL, OPT_ITERATIONS }, \ + { "threads", required_argument, NULL, OPT_THREADS }, +static void +cmdline_process (int c) +{ + long long int arg = strtoll (optarg, NULL, 0); + switch (c) + { + case OPT_ITERATIONS: + if (arg > 0) + iteration_count = arg; + break; + case OPT_THREADS: + if (arg > 0 && arg < 100) + thread_count = arg; + break; + } +} +#define CMDLINE_PROCESS cmdline_process +#define TIMEOUT 50 +#include <support/test-driver.c> diff --git a/sysdeps/pthread/tst-mutex2.c b/sysdeps/pthread/tst-mutex2.c new file mode 100644 index 0000000000..10a586f8ee --- /dev/null +++ b/sysdeps/pthread/tst-mutex2.c @@ -0,0 +1,241 @@ +/* Copyright (C) 2002-2020 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, see + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> + + +static pthread_mutex_t m; +static pthread_barrier_t b; + + +static void * +tf (void *arg) +{ + int e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("child: 1st mutex_unlock succeeded"); + exit (1); + } + else if (e != EPERM) + { + puts ("child: 1st mutex_unlock error != EPERM"); + exit (1); + } + + e = pthread_mutex_trylock (&m); + if (e == 0) + { + puts ("child: 1st trylock suceeded"); + exit (1); + } + if (e != EBUSY) + { + puts ("child: 1st trylock didn't return EBUSY"); + exit (1); + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("child: 1st barrier_wait failed"); + exit (1); + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("child: 2nd barrier_wait failed"); + exit (1); + } + + e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("child: 2nd mutex_unlock succeeded"); + exit (1); + } + else if (e != EPERM) + { + puts ("child: 2nd mutex_unlock error != EPERM"); + exit (1); + } + + if (pthread_mutex_trylock (&m) != 0) + { + puts ("child: 2nd trylock failed"); + exit (1); + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("child: 3rd mutex_unlock failed"); + exit (1); + } + + return NULL; +} + + +static int +do_test (void) +{ + pthread_mutexattr_t a; + int e; + + if (pthread_mutexattr_init (&a) != 0) + { + puts ("mutexattr_init failed"); + return 1; + } + + if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_ERRORCHECK) != 0) + { + puts ("mutexattr_settype failed"); + return 1; + } + +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) + { + puts ("pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + + e = pthread_mutex_init (&m, &a); + if (e != 0) + { +#ifdef ENABLE_PI + if (e == ENOTSUP) + { + puts ("PI mutexes unsupported"); + return 0; + } +#endif + puts ("mutex_init failed"); + return 1; + } + + if (pthread_barrier_init (&b, NULL, 2) != 0) + { + puts ("barrier_init failed"); + return 1; + } + + e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("1st mutex_unlock succeeded"); + return 1; + } + else if (e != EPERM) + { + puts ("1st mutex_unlock error != EPERM"); + return 1; + } + + if (pthread_mutex_lock (&m) != 0) + { + puts ("mutex_lock failed"); + return 1; + } + + e = pthread_mutex_lock (&m); + if (e == 0) + { + puts ("2nd mutex_lock succeeded"); + return 1; + } + else if (e != EDEADLK) + { + puts ("2nd mutex_lock error != EDEADLK"); + return 1; + } + + pthread_t th; + if (pthread_create (&th, NULL, tf, NULL) != 0) + { + puts ("create failed"); + return 1; + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("1st barrier_wait failed"); + return 1; + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("2nd mutex_unlock failed"); + return 1; + } + + e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("3rd mutex_unlock succeeded"); + return 1; + } + else if (e != EPERM) + { + puts ("3rd mutex_unlock error != EPERM"); + return 1; + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("2nd barrier_wait failed"); + return 1; + } + + if (pthread_join (th, NULL) != 0) + { + puts ("join failed"); + return 1; + } + + if (pthread_mutex_destroy (&m) != 0) + { + puts ("mutex_destroy failed"); + return 1; + } + + if (pthread_barrier_destroy (&b) != 0) + { + puts ("barrier_destroy failed"); + return 1; + } + + if (pthread_mutexattr_destroy (&a) != 0) + { + puts ("mutexattr_destroy failed"); + return 1; + } + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-mutex3.c b/sysdeps/pthread/tst-mutex3.c new file mode 100644 index 0000000000..d18ebb2f10 --- /dev/null +++ b/sysdeps/pthread/tst-mutex3.c @@ -0,0 +1,241 @@ +/* Copyright (C) 2002-2020 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, see + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdio.h> +#include <stdlib.h> + + +static pthread_mutex_t m; +static pthread_barrier_t b; + + +static void * +tf (void *arg) +{ + int e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("1st mutex_unlock in child succeeded"); + exit (1); + } + if (e != EPERM) + { + puts ("1st mutex_unlock in child didn't return EPERM"); + exit (1); + } + + e = pthread_mutex_trylock (&m); + if (e == 0) + { + puts ("mutex_trylock in second thread succeeded"); + exit (1); + } + if (e != EBUSY) + { + puts ("mutex_trylock returned wrong value"); + exit (1); + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("barrier_wait failed"); + exit (1); + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("barrier_wait failed"); + exit (1); + } + + e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("2nd mutex_unlock in child succeeded"); + exit (1); + } + if (e != EPERM) + { + puts ("2nd mutex_unlock in child didn't return EPERM"); + exit (1); + } + + if (pthread_mutex_trylock (&m) != 0) + { + puts ("2nd mutex_trylock in second thread failed"); + exit (1); + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("3rd mutex_unlock in second thread failed"); + exit (1); + } + + return NULL; +} + + +static int +do_test (void) +{ + pthread_mutexattr_t a; + + if (pthread_mutexattr_init (&a) != 0) + { + puts ("mutexattr_init failed"); + return 1; + } + + if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE) != 0) + { + puts ("mutexattr_settype failed"); + return 1; + } + +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) + { + puts ("pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + + int e; + e = pthread_mutex_init (&m, &a); + if (e != 0) + { +#ifdef ENABLE_PI + if (e == ENOTSUP) + { + puts ("PI mutexes unsupported"); + return 0; + } +#endif + puts ("mutex_init failed"); + return 1; + } + + if (pthread_barrier_init (&b, NULL, 2) != 0) + { + puts ("barrier_init failed"); + return 1; + } + + if (pthread_mutex_lock (&m) != 0) + { + puts ("mutex_lock failed"); + return 1; + } + + if (pthread_mutex_lock (&m) != 0) + { + puts ("2nd mutex_lock failed"); + return 1; + } + + if (pthread_mutex_trylock (&m) != 0) + { + puts ("1st trylock failed"); + return 1; + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("mutex_unlock failed"); + return 1; + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("2nd mutex_unlock failed"); + return 1; + } + + pthread_t th; + if (pthread_create (&th, NULL, tf, NULL) != 0) + { + puts ("create failed"); + return 1; + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("barrier_wait failed"); + return 1; + } + + if (pthread_mutex_unlock (&m) != 0) + { + puts ("3rd mutex_unlock failed"); + return 1; + } + + e = pthread_mutex_unlock (&m); + if (e == 0) + { + puts ("4th mutex_unlock succeeded"); + return 1; + } + if (e != EPERM) + { + puts ("4th mutex_unlock didn't return EPERM"); + return 1; + } + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("barrier_wait failed"); + return 1; + } + + if (pthread_join (th, NULL) != 0) + { + puts ("join failed"); + return 1; + } + + if (pthread_barrier_destroy (&b) != 0) + { + puts ("barrier_destroy failed"); + return 1; + } + + if (pthread_mutex_destroy (&m) != 0) + { + puts ("mutex_destroy failed"); + return 1; + } + + if (pthread_mutexattr_destroy (&a) != 0) + { + puts ("mutexattr_destroy failed"); + return 1; + } + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-mutex4.c b/sysdeps/pthread/tst-mutex4.c new file mode 100644 index 0000000000..8ac6666fa0 --- /dev/null +++ b/sysdeps/pthread/tst-mutex4.c @@ -0,0 +1,276 @@ +/* Copyright (C) 2002-2020 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, see + <https://www.gnu.org/licenses/>. */ + +#include <errno.h> +#include <pthread.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/wait.h> + + +static int +do_test (void) +{ + size_t ps = sysconf (_SC_PAGESIZE); + char tmpfname[] = "/tmp/tst-mutex4.XXXXXX"; + char data[ps]; + void *mem; + int fd; + pthread_mutex_t *m; + pthread_mutexattr_t a; + pid_t pid; + char *p; + int err; + int s; + pthread_barrier_t *b; + pthread_barrierattr_t ba; + + fd = mkstemp (tmpfname); + if (fd == -1) + { + printf ("cannot open temporary file: %m\n"); + return 1; + } + + /* Make sure it is always removed. */ + unlink (tmpfname); + + /* Create one page of data. */ + memset (data, '\0', ps); + + /* Write the data to the file. */ + if (write (fd, data, ps) != (ssize_t) ps) + { + puts ("short write"); + return 1; + } + + mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (mem == MAP_FAILED) + { + printf ("mmap failed: %m\n"); + return 1; + } + + m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t) - 1) + & ~(__alignof (pthread_mutex_t) - 1)); + b = (pthread_barrier_t *) (((uintptr_t) (m + 1) + + __alignof (pthread_barrier_t) - 1) + & ~(__alignof (pthread_barrier_t) - 1)); + p = (char *) (b + 1); + + if (pthread_mutexattr_init (&a) != 0) + { + puts ("mutexattr_init failed"); + return 1; + } + + if (pthread_mutexattr_getpshared (&a, &s) != 0) + { + puts ("1st mutexattr_getpshared failed"); + return 1; + } + + if (s != PTHREAD_PROCESS_PRIVATE) + { + puts ("default pshared value wrong"); + return 1; + } + + if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0) + { + puts ("mutexattr_setpshared failed"); + return 1; + } + + if (pthread_mutexattr_getpshared (&a, &s) != 0) + { + puts ("2nd mutexattr_getpshared failed"); + return 1; + } + + if (s != PTHREAD_PROCESS_SHARED) + { + puts ("pshared value after setpshared call wrong"); + return 1; + } + +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0) + { + puts ("pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + + if ((err = pthread_mutex_init (m, &a)) != 0) + { +#ifdef ENABLE_PI + if (err == ENOTSUP) + { + puts ("PI mutexes unsupported"); + return 0; + } +#endif + puts ("mutex_init failed"); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + puts ("mutex_lock failed"); + return 1; + } + + if (pthread_mutexattr_destroy (&a) != 0) + { + puts ("mutexattr_destroy failed"); + return 1; + } + + if (pthread_barrierattr_init (&ba) != 0) + { + puts ("barrierattr_init failed"); + return 1; + } + + if (pthread_barrierattr_setpshared (&ba, PTHREAD_PROCESS_SHARED) != 0) + { + puts ("barrierattr_setpshared failed"); + return 1; + } + + if (pthread_barrier_init (b, &ba, 2) != 0) + { + puts ("barrier_init failed"); + return 1; + } + + if (pthread_barrierattr_destroy (&ba) != 0) + { + puts ("barrierattr_destroy failed"); + return 1; + } + + err = pthread_mutex_trylock (m); + if (err == 0) + { + puts ("mutex_trylock succeeded"); + return 1; + } + else if (err != EBUSY) + { + puts ("mutex_trylock didn't return EBUSY"); + return 1; + } + + *p = 0; + + if (pthread_mutex_unlock (m) != 0) + { + puts ("parent: 1st mutex_unlock failed"); + return 1; + } + + puts ("going to fork now"); + pid = fork (); + if (pid == -1) + { + puts ("fork failed"); + return 1; + } + else if (pid == 0) + { + if (pthread_mutex_lock (m) != 0) + { + puts ("child: mutex_lock failed"); + return 1; + } + + int e = pthread_barrier_wait (b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("child: barrier_wait failed"); + return 1; + } + + if ((*p)++ != 0) + { + puts ("child: *p != 0"); + return 1; + } + + if (pthread_mutex_unlock (m) != 0) + { + puts ("child: mutex_unlock failed"); + return 1; + } + + puts ("child done"); + } + else + { + int e = pthread_barrier_wait (b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("parent: barrier_wait failed"); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + puts ("parent: 2nd mutex_lock failed"); + return 1; + } + + if (*p != 1) + { + puts ("*p != 1"); + return 1; + } + + if (pthread_mutex_unlock (m) != 0) + { + puts ("parent: 2nd mutex_unlock failed"); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + puts ("mutex_destroy failed"); + return 1; + } + + if (pthread_barrier_destroy (b) != 0) + { + puts ("barrier_destroy failed"); + return 1; + } + + puts ("parent done"); + } + + return 0; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" diff --git a/sysdeps/pthread/tst-mutex6.c b/sysdeps/pthread/tst-mutex6.c new file mode 100644 index 0000000000..2b06d10586 --- /dev/null +++ b/sysdeps/pthread/tst-mutex6.c @@ -0,0 +1,76 @@ +/* Copyright (C) 2002-2020 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, see + <https://www.gnu.org/licenses/>. */ + +#include <pthread.h> +#include <signal.h> +#include <stdio.h> +#include <unistd.h> +#include <errno.h> +#include <stdbool.h> + +#ifndef TEST_FUNCTION +static int do_test (void); +# define TEST_FUNCTION do_test () +#endif +#include "../test-skeleton.c" + +#ifndef ATTR +pthread_mutexattr_t *attr; +# define ATTR attr +#endif + +#ifndef ATTR_NULL +# define ATTR_NULL (ATTR == NULL) +#endif + +static int +do_test (void) +{ + pthread_mutex_t m; + + int e = pthread_mutex_init (&m, ATTR); + if (!ATTR_NULL && e == ENOTSUP) + { + puts ("cannot support selected type of mutexes"); + e = pthread_mutex_init (&m, NULL); + } + if (e != 0) + { + puts ("mutex_init failed"); + return 1; + } + + if (!ATTR_NULL && pthread_mutexattr_destroy (ATTR) != 0) + { + puts ("mutexattr_destroy failed"); + return 1; + } + + if (pthread_mutex_lock (&m) != 0) + { + puts ("1st mutex_lock failed"); + return 1; + } + + delayed_exit (1); + /* This call should never return. */ + xpthread_mutex_lock (&m); + + puts ("2nd mutex_lock returned"); + return 1; +} diff --git a/sysdeps/pthread/tst-mutex8.c b/sysdeps/pthread/tst-mutex8.c new file mode 100644 index 0000000000..8e56ea5a3a --- /dev/null +++ b/sysdeps/pthread/tst-mutex8.c @@ -0,0 +1,435 @@ +/* Copyright (C) 2003-2020 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, see + <https://www.gnu.org/licenses/>. */ + +/* This test checks behavior not required by POSIX. */ +#include <errno.h> +#include <pthread.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <elf/dl-tunables.h> + +static pthread_mutex_t *m; +static pthread_barrier_t b; +static pthread_cond_t c; +static bool done; + + +static void +cl (void *arg) +{ + if (pthread_mutex_unlock (m) != 0) + { + puts ("cl: mutex_unlocked failed"); + exit (1); + } +} + + +static void * +tf (void *arg) +{ + if (pthread_mutex_lock (m) != 0) + { + puts ("tf: mutex_lock failed"); + return (void *) 1l; + } + + int e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("barrier_wait failed"); + return (void *) 1l; + } + + if (arg == NULL) + do + if (pthread_cond_wait (&c, m) != 0) + { + puts ("tf: cond_wait failed"); + return (void *) 1l; + } + while (! done); + else + do + { + pthread_cleanup_push (cl, NULL); + + if (pthread_cond_wait (&c, m) != 0) + { + puts ("tf: cond_wait failed"); + return (void *) 1l; + } + + pthread_cleanup_pop (0); + } + while (! done); + + if (pthread_mutex_unlock (m) != 0) + { + puts ("tf: mutex_unlock failed"); + return (void *) 1l; + } + + return NULL; +} + + +static int +check_type (const char *mas, pthread_mutexattr_t *ma) +{ + int e; + + /* Check if a mutex will be elided. Lock elision can only be activated via + the tunables framework. By default, lock elision is disabled. */ + bool assume_elided_mutex = false; +#if HAVE_TUNABLES + int ma_type = PTHREAD_MUTEX_TIMED_NP; + if (ma != NULL) + { + e = pthread_mutexattr_gettype (ma, &ma_type); + if (e != 0) + { + printf ("pthread_mutexattr_gettype failed with %d (%m)\n", e); + return 1; + } + } + if (ma_type == PTHREAD_MUTEX_TIMED_NP) + { + /* This type of mutex can be elided if elision is enabled via the tunables + framework. Some tests below are failing if the mutex is elided. + Thus we only run those if we assume that the mutex won't be elided. */ + if (TUNABLE_GET_FULL (glibc, elision, enable, int32_t, NULL) == 1) + assume_elided_mutex = true; + } +#endif + + e = pthread_mutex_init (m, ma); + if (e != 0) + { +#ifdef ENABLE_PI + if (e == ENOTSUP) + { + puts ("PI mutexes unsupported"); + return 0; + } +#endif + printf ("1st mutex_init failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + printf ("immediate mutex_destroy failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_init (m, ma) != 0) + { + printf ("2nd mutex_init failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + printf ("1st mutex_lock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy, thus only test this if we don't assume + elision. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("mutex_destroy of self-locked mutex succeeded for %s\n", mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +mutex_destroy of self-locked mutex did not return EBUSY %s\n", + mas); + return 1; + } + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("1st mutex_unlock failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_trylock (m) != 0) + { + printf ("mutex_trylock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("mutex_destroy of self-trylocked mutex succeeded for %s\n", + mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +mutex_destroy of self-trylocked mutex did not return EBUSY %s\n", + mas); + return 1; + } + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("2nd mutex_unlock failed for %s\n", mas); + return 1; + } + + pthread_t th; + if (pthread_create (&th, NULL, tf, NULL) != 0) + { + puts ("1st create failed"); + return 1; + } + done = false; + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("1st barrier_wait failed"); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + printf ("2nd mutex_lock failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("3rd mutex_unlock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("mutex_destroy of condvar-used mutex succeeded for %s\n", + mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", mas); + return 1; + } + } + + done = true; + if (pthread_cond_signal (&c) != 0) + { + puts ("cond_signal failed"); + return 1; + } + + void *r; + if (pthread_join (th, &r) != 0) + { + puts ("join failed"); + return 1; + } + if (r != NULL) + { + puts ("thread didn't return NULL"); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + printf ("mutex_destroy after condvar-use failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_init (m, ma) != 0) + { + printf ("3rd mutex_init failed for %s\n", mas); + return 1; + } + + if (pthread_create (&th, NULL, tf, (void *) 1) != 0) + { + puts ("2nd create failed"); + return 1; + } + done = false; + + e = pthread_barrier_wait (&b); + if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD) + { + puts ("2nd barrier_wait failed"); + return 1; + } + + if (pthread_mutex_lock (m) != 0) + { + printf ("3rd mutex_lock failed for %s\n", mas); + return 1; + } + + if (pthread_mutex_unlock (m) != 0) + { + printf ("4th mutex_unlock failed for %s\n", mas); + return 1; + } + + /* Elided mutexes don't fail destroy. */ + if (assume_elided_mutex == false) + { + e = pthread_mutex_destroy (m); + if (e == 0) + { + printf ("2nd mutex_destroy of condvar-used mutex succeeded for %s\n", + mas); + return 1; + } + if (e != EBUSY) + { + printf ("\ +2nd mutex_destroy of condvar-used mutex did not return EBUSY for %s\n", + mas); + return 1; + } + } + + if (pthread_cancel (th) != 0) + { + puts ("cond_cancel failed"); + return 1; + } + + if (pthread_join (th, &r) != 0) + { + puts ("join failed"); + return 1; + } + if (r != PTHREAD_CANCELED) + { + puts ("thread not canceled"); + return 1; + } + + if (pthread_mutex_destroy (m) != 0) + { + printf ("mutex_destroy after condvar-canceled failed for %s\n", mas); + return 1; + } + + return 0; +} + + +static int +do_test (void) +{ + pthread_mutex_t mm; + m = &mm; + + if (pthread_barrier_init (&b, NULL, 2) != 0) + { + puts ("barrier_init failed"); + return 1; + } + + if (pthread_cond_init (&c, NULL) != 0) + { + puts ("cond_init failed"); + return 1; + } + + puts ("check normal mutex"); + int res = check_type ("normal", NULL); + + pthread_mutexattr_t ma; + if (pthread_mutexattr_init (&ma) != 0) + { + puts ("1st mutexattr_init failed"); + return 1; + } + if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_RECURSIVE) != 0) + { + puts ("1st mutexattr_settype failed"); + return 1; + } +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT)) + { + puts ("1st pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + puts ("check recursive mutex"); + res |= check_type ("recursive", &ma); + if (pthread_mutexattr_destroy (&ma) != 0) + { + puts ("1st mutexattr_destroy failed"); + return 1; + } + + if (pthread_mutexattr_init (&ma) != 0) + { + puts ("2nd mutexattr_init failed"); + return 1; + } + if (pthread_mutexattr_settype (&ma, PTHREAD_MUTEX_ERRORCHECK) != 0) + { + puts ("2nd mutexattr_settype failed"); + return 1; + } +#ifdef ENABLE_PI + if (pthread_mutexattr_setprotocol (&ma, PTHREAD_PRIO_INHERIT)) + { + puts ("2nd pthread_mutexattr_setprotocol failed"); + return 1; + } +#endif + puts ("check error-checking mutex"); + res |= check_type ("error-checking", &ma); + if (pthread_mutexattr_destroy (&ma) != 0) + { + puts ("2nd mutexattr_destroy failed"); + return 1; + } + + return res; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" |