supersede: Add tests.
[gnulib.git] / tests / test-asyncsafe-spin2.c
blobcb0364f7ecfe96a281d23841d1406df3c1850888
1 /* Test of spin locks for communication between threads and signal handlers.
2 Copyright (C) 2005, 2008-2020 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 #if USE_ISOC_THREADS || USE_POSIX_THREADS || USE_ISOC_AND_POSIX_THREADS || USE_WINDOWS_THREADS
23 /* Whether to enable locking.
24 Uncomment this to get a test program without locking, to verify that
25 it crashes. */
26 #define ENABLE_LOCKING 1
28 /* Whether to help the scheduler through explicit yield().
29 Uncomment this to see if the operating system has a fair scheduler. */
30 #define EXPLICIT_YIELD 1
32 /* Whether to print debugging messages. */
33 #define ENABLE_DEBUGGING 0
35 /* Number of simultaneous threads. */
36 #define THREAD_COUNT 10
38 /* Number of operations performed in each thread. */
39 #if !(defined _WIN32 && ! defined __CYGWIN__) && HAVE_PTHREAD_H && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) && !defined __ibmxl__
40 /* The GCC built-ins are known to work fine. */
41 # define REPEAT_COUNT 5000
42 #else
43 /* This is quite high, because with a smaller count, say 50000, we often get
44 an "OK" result even with the racy implementation that we pick on Fedora 13
45 Linux/x86_64 (gcc 4.4). */
46 # define REPEAT_COUNT 100000
47 #endif
49 #include <signal.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
55 #include "asyncsafe-spin.h"
56 #if !ENABLE_LOCKING
57 # define asyncsafe_spin_init(lock) (void)(lock)
58 # define asyncsafe_spin_lock(lock, mask, saved_mask) \
59 ((void)(lock), (void)(mask), (void)(saved_mask))
60 # define asyncsafe_spin_unlock(lock, saved_mask) \
61 ((void)(lock), (void)(saved_mask))
62 # define asyncsafe_spin_destroy(lock) (void)(lock)
63 #endif
65 #include "glthread/lock.h"
66 #include "glthread/thread.h"
67 #include "glthread/yield.h"
69 #if HAVE_DECL_ALARM
70 # include <signal.h>
71 # include <unistd.h>
72 #endif
74 #include "atomic-int-gnulib.h"
76 #if ENABLE_DEBUGGING
77 # define dbgprintf printf
78 #else
79 # define dbgprintf if (0) printf
80 #endif
82 #if EXPLICIT_YIELD
83 # define yield() gl_thread_yield ()
84 #else
85 # define yield()
86 #endif
88 static sigset_t signals_to_block;
90 #define ACCOUNT_COUNT 4
92 static int account[ACCOUNT_COUNT];
94 static int
95 random_account (void)
97 return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT;
100 static void
101 check_accounts (void)
103 int i, sum;
105 sum = 0;
106 for (i = 0; i < ACCOUNT_COUNT; i++)
107 sum += account[i];
108 if (sum != ACCOUNT_COUNT * 1000)
109 abort ();
113 /* ------------------- Test use like normal locks ------------------- */
115 /* Test normal locks by having several bank accounts and several threads
116 which shuffle around money between the accounts and another thread
117 checking that all the money is still there. */
119 static asyncsafe_spinlock_t my_lock;
121 static void *
122 lock_mutator_thread (void *arg)
124 int repeat;
126 for (repeat = REPEAT_COUNT; repeat > 0; repeat--)
128 sigset_t saved_signals;
129 int i1, i2, value;
131 dbgprintf ("Mutator %p before lock\n", gl_thread_self_pointer ());
132 asyncsafe_spin_lock (&my_lock, &signals_to_block, &saved_signals);
133 dbgprintf ("Mutator %p after lock\n", gl_thread_self_pointer ());
135 i1 = random_account ();
136 i2 = random_account ();
137 value = ((unsigned int) rand () >> 3) % 10;
138 account[i1] += value;
139 account[i2] -= value;
141 dbgprintf ("Mutator %p before unlock\n", gl_thread_self_pointer ());
142 asyncsafe_spin_unlock (&my_lock, &saved_signals);
143 dbgprintf ("Mutator %p after unlock\n", gl_thread_self_pointer ());
145 dbgprintf ("Mutator %p before check lock\n", gl_thread_self_pointer ());
146 asyncsafe_spin_lock (&my_lock, &signals_to_block, &saved_signals);
147 check_accounts ();
148 asyncsafe_spin_unlock (&my_lock, &saved_signals);
149 dbgprintf ("Mutator %p after check unlock\n", gl_thread_self_pointer ());
151 yield ();
154 dbgprintf ("Mutator %p dying.\n", gl_thread_self_pointer ());
155 return NULL;
158 static struct atomic_int lock_checker_done;
160 static void *
161 lock_checker_thread (void *arg)
163 while (get_atomic_int_value (&lock_checker_done) == 0)
165 sigset_t saved_signals;
167 dbgprintf ("Checker %p before check lock\n", gl_thread_self_pointer ());
168 asyncsafe_spin_lock (&my_lock, &signals_to_block, &saved_signals);
169 check_accounts ();
170 asyncsafe_spin_unlock (&my_lock, &saved_signals);
171 dbgprintf ("Checker %p after check unlock\n", gl_thread_self_pointer ());
173 yield ();
176 dbgprintf ("Checker %p dying.\n", gl_thread_self_pointer ());
177 return NULL;
180 static void
181 test_asyncsafe_spin (void)
183 int i;
184 gl_thread_t checkerthread;
185 gl_thread_t threads[THREAD_COUNT];
187 /* Initialization. */
188 for (i = 0; i < ACCOUNT_COUNT; i++)
189 account[i] = 1000;
190 init_atomic_int (&lock_checker_done);
191 set_atomic_int_value (&lock_checker_done, 0);
193 /* Spawn the threads. */
194 checkerthread = gl_thread_create (lock_checker_thread, NULL);
195 for (i = 0; i < THREAD_COUNT; i++)
196 threads[i] = gl_thread_create (lock_mutator_thread, NULL);
198 /* Wait for the threads to terminate. */
199 for (i = 0; i < THREAD_COUNT; i++)
200 gl_thread_join (threads[i], NULL);
201 set_atomic_int_value (&lock_checker_done, 1);
202 gl_thread_join (checkerthread, NULL);
203 check_accounts ();
207 /* -------------------------------------------------------------------------- */
210 main ()
212 #if HAVE_DECL_ALARM
213 /* Declare failure if test takes too long, by using default abort
214 caused by SIGALRM. */
215 int alarm_value = 600;
216 signal (SIGALRM, SIG_DFL);
217 alarm (alarm_value);
218 #endif
220 sigemptyset (&signals_to_block);
221 sigaddset (&signals_to_block, SIGINT);
223 asyncsafe_spin_init (&my_lock);
225 printf ("Starting test_asyncsafe_spin ..."); fflush (stdout);
226 test_asyncsafe_spin ();
227 printf (" OK\n"); fflush (stdout);
229 return 0;
232 #else
234 /* No multithreading available. */
236 #include <stdio.h>
239 main ()
241 fputs ("Skipping test: multithreading not enabled\n", stderr);
242 return 77;
245 #endif