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. */
21 /* Whether to enable locking.
22 Uncomment this to get a test program without locking, to verify that
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
47 #include "glthread/lock.h"
57 # define dbgprintf printf
59 # define dbgprintf if (0) printf
63 # define yield() thrd_yield ()
68 /* Returns a reference to the current thread as a pointer, for debugging. */
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 ().__))
75 /* On Solaris, thrd_t is merely an 'unsigned int'. */
76 # define thrd_current_pointer() ((void *) (uintptr_t) thrd_current ())
78 # define thrd_current_pointer() ((void *) thrd_current ())
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
];
91 static gl_rwlock_t fire_signal
[REPEAT_COUNT
];
93 static volatile int fire_signal_state
;
95 static once_flag once_control
;
97 static mtx_t performed_lock
;
102 ASSERT (mtx_lock (&performed_lock
) == thrd_success
);
104 ASSERT (mtx_unlock (&performed_lock
) == thrd_success
);
108 once_contender_thread (void *arg
)
110 int id
= (int) (intptr_t) arg
;
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
);
118 ASSERT (mtx_unlock (&ready_lock
[id
]) == thrd_success
);
120 if (repeat
== REPEAT_COUNT
)
123 dbgprintf ("Contender %p waiting for signal for round %d\n",
124 thrd_current_pointer (), repeat
);
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
]);
131 /* Wait for the signal to go. */
132 while (fire_signal_state
<= repeat
)
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
);
149 thrd_t threads
[THREAD_COUNT
];
151 /* Initialize all variables. */
152 for (i
= 0; i
< THREAD_COUNT
; i
++)
155 ASSERT (mtx_init (&ready_lock
[i
], mtx_plain
) == thrd_success
);
158 for (i
= 0; i
< REPEAT_COUNT
; i
++)
159 gl_rwlock_init (fire_signal
[i
]);
161 fire_signal_state
= 0;
165 /* Block all fire_signals. */
166 for (i
= REPEAT_COUNT
-1; i
>= 0; i
--)
167 gl_rwlock_wrlock (fire_signal
[i
]);
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
)
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
);
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
)
193 dbgprintf ("Main thread after synchronizing for round %d\n", repeat
);
197 /* Check that exactly one thread executed the once_execute()
203 if (repeat
== REPEAT_COUNT
)
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. */
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
);
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
);
223 gl_rwlock_unlock (fire_signal
[repeat
]);
225 fire_signal_state
= repeat
+ 1;
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 /* -------------------------------------------------------------------------- */
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
);
248 ASSERT (mtx_init (&performed_lock
, mtx_plain
) == thrd_success
);
250 printf ("Starting test_once ..."); fflush (stdout
);
252 printf (" OK\n"); fflush (stdout
);
254 return test_exit_status
;