openat: don’t close (-1)
[gnulib.git] / tests / test-call_once2.c
blobbb780bb67eed909d4a13168f883888f2423df98b
1 /* Test of locking in multithreaded situations.
2 Copyright (C) 2005, 2008-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2005. */
19 #include <config.h>
21 /* Whether to enable locking.
22 Uncomment this to get a test program without locking, to verify that
23 it crashes. */
24 #define ENABLE_LOCKING 1
26 /* Whether to help the scheduler through explicit thrd_yield().
27 Uncomment this to see if the operating system has a fair scheduler. */
28 #define EXPLICIT_YIELD 1
30 /* Whether to print debugging messages. */
31 #define ENABLE_DEBUGGING 0
33 /* Number of simultaneous threads. */
34 #define THREAD_COUNT 10
36 /* Number of operations performed in each thread.
37 This is quite high, because with a smaller count, say 5000, we often get
38 an "OK" result even without ENABLE_LOCKING (on Linux/x86). */
39 #define REPEAT_COUNT 50000
41 #include <threads.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
47 #include "glthread/lock.h"
49 #if HAVE_DECL_ALARM
50 # include <signal.h>
51 # include <unistd.h>
52 #endif
54 #include "macros.h"
56 #if ENABLE_DEBUGGING
57 # define dbgprintf printf
58 #else
59 # define dbgprintf if (0) printf
60 #endif
62 #if EXPLICIT_YIELD
63 # define yield() thrd_yield ()
64 #else
65 # define yield()
66 #endif
68 /* Returns a reference to the current thread as a pointer, for debugging. */
69 #if defined __MVS__
70 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
71 The first three bytes of this field appear to uniquely identify a
72 pthread_t, though not necessarily representing a pointer. */
73 # define thrd_current_pointer() (*((void **) thrd_current ().__))
74 #elif defined __sun
75 /* On Solaris, thrd_t is merely an 'unsigned int'. */
76 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
77 #else
78 # define thrd_current_pointer() ((void *) thrd_current ())
79 #endif
82 /* ------------------------ Test once-only execution ------------------------ */
84 /* Test once-only execution by having several threads attempt to grab a
85 once-only task simultaneously (triggered by releasing a read-write lock). */
87 static once_flag fresh_once = ONCE_FLAG_INIT;
88 static int ready[THREAD_COUNT];
89 static mtx_t ready_lock[THREAD_COUNT];
90 #if ENABLE_LOCKING
91 static gl_rwlock_t fire_signal[REPEAT_COUNT];
92 #else
93 static volatile int fire_signal_state;
94 #endif
95 static once_flag once_control;
96 static int performed;
97 static mtx_t performed_lock;
99 static void
100 once_execute (void)
102 ASSERT (mtx_lock (&performed_lock) == thrd_success);
103 performed++;
104 ASSERT (mtx_unlock (&performed_lock) == thrd_success);
107 static int
108 once_contender_thread (void *arg)
110 int id = (int) (intptr_t) arg;
111 int repeat;
113 for (repeat = 0; repeat <= REPEAT_COUNT; repeat++)
115 /* Tell the main thread that we're ready. */
116 ASSERT (mtx_lock (&ready_lock[id]) == thrd_success);
117 ready[id] = 1;
118 ASSERT (mtx_unlock (&ready_lock[id]) == thrd_success);
120 if (repeat == REPEAT_COUNT)
121 break;
123 dbgprintf ("Contender %p waiting for signal for round %d\n",
124 thrd_current_pointer (), repeat);
125 #if ENABLE_LOCKING
126 /* Wait for the signal to go. */
127 gl_rwlock_rdlock (fire_signal[repeat]);
128 /* And don't hinder the others (if the scheduler is unfair). */
129 gl_rwlock_unlock (fire_signal[repeat]);
130 #else
131 /* Wait for the signal to go. */
132 while (fire_signal_state <= repeat)
133 yield ();
134 #endif
135 dbgprintf ("Contender %p got the signal for round %d\n",
136 thrd_current_pointer (), repeat);
138 /* Contend for execution. */
139 call_once (&once_control, once_execute);
142 return 0;
145 static void
146 test_once (void)
148 int i, repeat;
149 thrd_t threads[THREAD_COUNT];
151 /* Initialize all variables. */
152 for (i = 0; i < THREAD_COUNT; i++)
154 ready[i] = 0;
155 ASSERT (mtx_init (&ready_lock[i], mtx_plain) == thrd_success);
157 #if ENABLE_LOCKING
158 for (i = 0; i < REPEAT_COUNT; i++)
159 gl_rwlock_init (fire_signal[i]);
160 #else
161 fire_signal_state = 0;
162 #endif
164 #if ENABLE_LOCKING
165 /* Block all fire_signals. */
166 for (i = REPEAT_COUNT-1; i >= 0; i--)
167 gl_rwlock_wrlock (fire_signal[i]);
168 #endif
170 /* Spawn the threads. */
171 for (i = 0; i < THREAD_COUNT; i++)
172 ASSERT (thrd_create (&threads[i],
173 once_contender_thread, (void *) (intptr_t) i)
174 == thrd_success);
176 for (repeat = 0; repeat <= REPEAT_COUNT; repeat++)
178 /* Wait until every thread is ready. */
179 dbgprintf ("Main thread before synchronizing for round %d\n", repeat);
180 for (;;)
182 int ready_count = 0;
183 for (i = 0; i < THREAD_COUNT; i++)
185 ASSERT (mtx_lock (&ready_lock[i]) == thrd_success);
186 ready_count += ready[i];
187 ASSERT (mtx_unlock (&ready_lock[i]) == thrd_success);
189 if (ready_count == THREAD_COUNT)
190 break;
191 yield ();
193 dbgprintf ("Main thread after synchronizing for round %d\n", repeat);
195 if (repeat > 0)
197 /* Check that exactly one thread executed the once_execute()
198 function. */
199 if (performed != 1)
200 abort ();
203 if (repeat == REPEAT_COUNT)
204 break;
206 /* Preparation for the next round: Initialize once_control. */
207 memcpy (&once_control, &fresh_once, sizeof (once_flag));
209 /* Preparation for the next round: Reset the performed counter. */
210 performed = 0;
212 /* Preparation for the next round: Reset the ready flags. */
213 for (i = 0; i < THREAD_COUNT; i++)
215 ASSERT (mtx_lock (&ready_lock[i]) == thrd_success);
216 ready[i] = 0;
217 ASSERT (mtx_unlock (&ready_lock[i]) == thrd_success);
220 /* Signal all threads simultaneously. */
221 dbgprintf ("Main thread giving signal for round %d\n", repeat);
222 #if ENABLE_LOCKING
223 gl_rwlock_unlock (fire_signal[repeat]);
224 #else
225 fire_signal_state = repeat + 1;
226 #endif
229 /* Wait for the threads to terminate. */
230 for (i = 0; i < THREAD_COUNT; i++)
231 ASSERT (thrd_join (threads[i], NULL) == thrd_success);
235 /* -------------------------------------------------------------------------- */
238 main ()
240 #if HAVE_DECL_ALARM
241 /* Declare failure if test takes too long, by using default abort
242 caused by SIGALRM. */
243 int alarm_value = 600;
244 signal (SIGALRM, SIG_DFL);
245 alarm (alarm_value);
246 #endif
248 ASSERT (mtx_init (&performed_lock, mtx_plain) == thrd_success);
250 printf ("Starting test_once ..."); fflush (stdout);
251 test_once ();
252 printf (" OK\n"); fflush (stdout);
254 return test_exit_status;