aboutsummaryrefslogtreecommitdiff
path: root/sysdeps
diff options
context:
space:
mode:
authorSamuel Thibault <samuel.thibault@ens-lyon.org>2020-02-09 17:00:39 +0000
committerSamuel Thibault <samuel.thibault@ens-lyon.org>2020-02-09 17:00:44 +0000
commit71d52ac4d65435791d8fa9f52abab7107ef7f7e8 (patch)
tree3166281a996db9450cc47a1b35a2fb54bc92f988 /sysdeps
parent900778283ac3cfbd274abc55840b5cdae9b7745f (diff)
downloadglibc-71d52ac4d65435791d8fa9f52abab7107ef7f7e8.tar
glibc-71d52ac4d65435791d8fa9f52abab7107ef7f7e8.tar.gz
glibc-71d52ac4d65435791d8fa9f52abab7107ef7f7e8.tar.bz2
glibc-71d52ac4d65435791d8fa9f52abab7107ef7f7e8.zip
pthread: Move spin tests from nptl to sysdeps/pthread
So they can be checked with htl too.
Diffstat (limited to 'sysdeps')
-rw-r--r--sysdeps/pthread/Makefile1
-rw-r--r--sysdeps/pthread/tst-spin1.c56
-rw-r--r--sysdeps/pthread/tst-spin2.c158
-rw-r--r--sysdeps/pthread/tst-spin3.c53
-rw-r--r--sysdeps/pthread/tst-spin4.c108
5 files changed, 376 insertions, 0 deletions
diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile
index db4d573070..6b66031706 100644
--- a/sysdeps/pthread/Makefile
+++ b/sysdeps/pthread/Makefile
@@ -44,5 +44,6 @@ 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 \
endif
diff --git a/sysdeps/pthread/tst-spin1.c b/sysdeps/pthread/tst-spin1.c
new file mode 100644
index 0000000000..3480f407c4
--- /dev/null
+++ b/sysdeps/pthread/tst-spin1.c
@@ -0,0 +1,56 @@
+/* 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>
+
+
+static int
+do_test (void)
+{
+ pthread_spinlock_t s;
+
+ if (pthread_spin_init (&s, PTHREAD_PROCESS_PRIVATE) != 0)
+ {
+ puts ("spin_init failed");
+ return 1;
+ }
+
+ if (pthread_spin_lock (&s) != 0)
+ {
+ puts ("spin_lock failed");
+ return 1;
+ }
+
+ if (pthread_spin_unlock (&s) != 0)
+ {
+ puts ("spin_unlock failed");
+ return 1;
+ }
+
+ if (pthread_spin_destroy (&s) != 0)
+ {
+ puts ("spin_destroy failed");
+ return 1;
+ }
+
+ return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/sysdeps/pthread/tst-spin2.c b/sysdeps/pthread/tst-spin2.c
new file mode 100644
index 0000000000..135228a958
--- /dev/null
+++ b/sysdeps/pthread/tst-spin2.c
@@ -0,0 +1,158 @@
+/* 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-spin2.XXXXXX";
+ char data[ps];
+ void *mem;
+ int fd;
+ pthread_spinlock_t *s;
+ pid_t pid;
+ char *p;
+ int err;
+
+ 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;
+ }
+
+ s = (pthread_spinlock_t *) (((uintptr_t) mem
+ + __alignof (pthread_spinlock_t))
+ & ~(__alignof (pthread_spinlock_t) - 1));
+ p = (char *) (s + 1);
+
+ if (pthread_spin_init (s, PTHREAD_PROCESS_SHARED) != 0)
+ {
+ puts ("spin_init failed");
+ return 1;
+ }
+
+ if (pthread_spin_lock (s) != 0)
+ {
+ puts ("spin_lock failed");
+ return 1;
+ }
+
+ err = pthread_spin_trylock (s);
+ if (err == 0)
+ {
+ puts ("1st spin_trylock succeeded");
+ return 1;
+ }
+ else if (err != EBUSY)
+ {
+ puts ("1st spin_trylock didn't return EBUSY");
+ return 1;
+ }
+
+ err = pthread_spin_unlock (s);
+ if (err != 0)
+ {
+ puts ("parent: spin_unlock failed");
+ return 1;
+ }
+
+ err = pthread_spin_trylock (s);
+ if (err != 0)
+ {
+ puts ("2nd spin_trylock failed");
+ return 1;
+ }
+
+ *p = 0;
+
+ puts ("going to fork now");
+ pid = fork ();
+ if (pid == -1)
+ {
+ puts ("fork failed");
+ return 1;
+ }
+ else if (pid == 0)
+ {
+ /* Play some lock ping-pong. It's our turn to unlock first. */
+ if ((*p)++ != 0)
+ {
+ puts ("child: *p != 0");
+ return 1;
+ }
+
+ if (pthread_spin_unlock (s) != 0)
+ {
+ puts ("child: 1st spin_unlock failed");
+ return 1;
+ }
+
+ puts ("child done");
+ }
+ else
+ {
+ if (pthread_spin_lock (s) != 0)
+ {
+ puts ("parent: 2nd spin_lock failed");
+ return 1;
+ }
+
+ puts ("waiting for child");
+
+ waitpid (pid, NULL, 0);
+
+ puts ("parent done");
+ }
+
+ return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
diff --git a/sysdeps/pthread/tst-spin3.c b/sysdeps/pthread/tst-spin3.c
new file mode 100644
index 0000000000..d64dab4443
--- /dev/null
+++ b/sysdeps/pthread/tst-spin3.c
@@ -0,0 +1,53 @@
+/* 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>
+
+static int do_test (void);
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
+
+static int
+do_test (void)
+{
+ pthread_spinlock_t s;
+
+ if (pthread_spin_init (&s, PTHREAD_PROCESS_PRIVATE) != 0)
+ {
+ puts ("spin_init failed");
+ return 1;
+ }
+
+ if (pthread_spin_lock (&s) != 0)
+ {
+ puts ("1st spin_lock failed");
+ return 1;
+ }
+
+ delayed_exit (1);
+
+ /* This call should never return. */
+ xpthread_spin_lock (&s);
+
+ puts ("2nd spin_lock returned");
+ return 1;
+}
diff --git a/sysdeps/pthread/tst-spin4.c b/sysdeps/pthread/tst-spin4.c
new file mode 100644
index 0000000000..99fe7aa6aa
--- /dev/null
+++ b/sysdeps/pthread/tst-spin4.c
@@ -0,0 +1,108 @@
+#include <pthread.h>
+#include <stdio.h>
+#include <unistd.h>
+
+static int count = 0;
+
+static void *
+thread_add_one (void *arg)
+{
+ int tmp;
+ pthread_spinlock_t *lock = (pthread_spinlock_t *) arg;
+
+ /* When do_test holds the lock for 1 sec, the two thread will be
+ in contention for the lock. */
+ if (pthread_spin_lock (lock) != 0)
+ {
+ puts ("thread_add_one(): spin_lock failed");
+ pthread_exit ((void *) 1l);
+ }
+
+ /* sleep 1s before modifying count */
+ tmp = count;
+ sleep (1);
+ count = tmp + 1;
+
+ if (pthread_spin_unlock (lock) != 0)
+ {
+ puts ("thread_add_one(): spin_unlock failed");
+ pthread_exit ((void *) 1l);
+ }
+
+ return NULL;
+}
+
+static int
+do_test (void)
+{
+ pthread_t thr1, thr2;
+ pthread_spinlock_t lock;
+ int tmp;
+
+ if (pthread_spin_init (&lock, PTHREAD_PROCESS_PRIVATE) != 0)
+ {
+ puts ("spin_init failed");
+ return 1;
+ }
+
+ if (pthread_spin_lock (&lock) != 0)
+ {
+ puts ("1st spin_lock failed");
+ return 1;
+ }
+
+ if (pthread_create (&thr1, NULL, thread_add_one, (void *) &lock) != 0)
+ {
+ puts ("1st pthread_create failed");
+ return 1;
+ }
+
+ if (pthread_create (&thr2, NULL, thread_add_one, (void *) &lock) != 0)
+ {
+ puts ("2nd pthread_create failed");
+ return 1;
+ }
+
+ /* sleep 1s before modifying count */
+ tmp = count;
+ sleep (1);
+ count = tmp + 1;
+
+ if (pthread_spin_unlock (&lock) != 0)
+ {
+ puts ("1st spin_unlock failed");
+ return 1;
+ }
+
+ void *status;
+ if (pthread_join (thr1, &status) != 0)
+ {
+ puts ("1st pthread_join failed");
+ return 1;
+ }
+ if (status != NULL)
+ {
+ puts ("failure in the 1st thread");
+ return 1;
+ }
+ if (pthread_join (thr2, &status) != 0)
+ {
+ puts ("2nd pthread_join failed");
+ return 1;
+ }
+ if (status != NULL)
+ {
+ puts ("failure in the 2nd thread");
+ return 1;
+ }
+
+ if (count != 3)
+ {
+ printf ("count is %d, should be 3\n", count);
+ return 1;
+ }
+ return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"