]> git.ipfire.org Git - thirdparty/glibc.git/blob - nptl/tst-mutex10.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / nptl / tst-mutex10.c
1 /* Testing race while enabling lock elision.
2 Copyright (C) 2018-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdint.h>
21 #include <pthread.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <support/support.h>
25 #include <support/xthread.h>
26
27 static pthread_barrier_t barrier;
28 static pthread_mutex_t mutex;
29 static long long int iteration_count = 1000000;
30 static unsigned int thread_count = 3;
31
32 static void *
33 thr_func (void *arg)
34 {
35 long long int i;
36 for (i = 0; i < iteration_count; i++)
37 {
38 if ((uintptr_t) arg == 0)
39 {
40 xpthread_mutex_destroy (&mutex);
41 xpthread_mutex_init (&mutex, NULL);
42 }
43
44 xpthread_barrier_wait (&barrier);
45
46 /* Test if enabling lock elision works if it is enabled concurrently.
47 There was a race in FORCE_ELISION macro which leads to either
48 pthread_mutex_destroy returning EBUSY as the owner was recorded
49 by pthread_mutex_lock - in "normal mutex" code path - but was not
50 resetted in pthread_mutex_unlock - in "elision" code path.
51 Or it leads to the assertion in nptl/pthread_mutex_lock.c:
52 assert (mutex->__data.__owner == 0);
53 Please ensure that the test is run with lock elision:
54 export GLIBC_TUNABLES=glibc.elision.enable=1 */
55 xpthread_mutex_lock (&mutex);
56 xpthread_mutex_unlock (&mutex);
57
58 xpthread_barrier_wait (&barrier);
59 }
60 return NULL;
61 }
62
63 static int
64 do_test (void)
65 {
66 unsigned int i;
67 printf ("Starting %d threads to run %lld iterations.\n",
68 thread_count, iteration_count);
69
70 pthread_t *threads = xmalloc (thread_count * sizeof (pthread_t));
71 xpthread_barrier_init (&barrier, NULL, thread_count);
72 xpthread_mutex_init (&mutex, NULL);
73
74 for (i = 0; i < thread_count; i++)
75 threads[i] = xpthread_create (NULL, thr_func, (void *) (uintptr_t) i);
76
77 for (i = 0; i < thread_count; i++)
78 xpthread_join (threads[i]);
79
80 xpthread_barrier_destroy (&barrier);
81 free (threads);
82
83 return EXIT_SUCCESS;
84 }
85
86 #define OPT_ITERATIONS 10000
87 #define OPT_THREADS 10001
88 #define CMDLINE_OPTIONS \
89 { "iterations", required_argument, NULL, OPT_ITERATIONS }, \
90 { "threads", required_argument, NULL, OPT_THREADS },
91 static void
92 cmdline_process (int c)
93 {
94 long long int arg = strtoll (optarg, NULL, 0);
95 switch (c)
96 {
97 case OPT_ITERATIONS:
98 if (arg > 0)
99 iteration_count = arg;
100 break;
101 case OPT_THREADS:
102 if (arg > 0 && arg < 100)
103 thread_count = arg;
104 break;
105 }
106 }
107 #define CMDLINE_PROCESS cmdline_process
108 #define TIMEOUT 50
109 #include <support/test-driver.c>