1 /* Testing race while enabling lock elision.
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
24 #include <support/support.h>
25 #include <support/xthread.h>
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;
36 for (i
= 0; i
< iteration_count
; i
++)
38 if ((uintptr_t) arg
== 0)
40 xpthread_mutex_destroy (&mutex
);
41 xpthread_mutex_init (&mutex
, NULL
);
44 xpthread_barrier_wait (&barrier
);
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 reset 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
);
58 xpthread_barrier_wait (&barrier
);
67 printf ("Starting %d threads to run %lld iterations.\n",
68 thread_count
, iteration_count
);
70 pthread_t
*threads
= xmalloc (thread_count
* sizeof (pthread_t
));
71 xpthread_barrier_init (&barrier
, NULL
, thread_count
);
72 xpthread_mutex_init (&mutex
, NULL
);
74 for (i
= 0; i
< thread_count
; i
++)
75 threads
[i
] = xpthread_create (NULL
, thr_func
, (void *) (uintptr_t) i
);
77 for (i
= 0; i
< thread_count
; i
++)
78 xpthread_join (threads
[i
]);
80 xpthread_barrier_destroy (&barrier
);
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 },
92 cmdline_process (int c
)
94 long long int arg
= strtoll (optarg
, NULL
, 0);
99 iteration_count
= arg
;
102 if (arg
> 0 && arg
< 100)
107 #define CMDLINE_PROCESS cmdline_process
109 #include <support/test-driver.c>