1 /* Test of POSIX spin locks.
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. */
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
26 #define ENABLE_LOCKING 1
28 /* Whether to help the scheduler through explicit sched_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 #define REPEAT_COUNT 50000
57 #include "atomic-int-posix.h"
60 # define dbgprintf printf
62 # define dbgprintf if (0) printf
66 # define yield() sched_yield ()
71 /* Returns a reference to the current thread as a pointer, for debugging. */
73 /* On IBM z/OS, pthread_t is a struct with an 8-byte '__' field.
74 The first three bytes of this field appear to uniquely identify a
75 pthread_t, though not necessarily representing a pointer. */
76 # define pthread_self_pointer() (*((void **) pthread_self ().__))
78 # define pthread_self_pointer() ((void *) (uintptr_t) pthread_self ())
81 #define ACCOUNT_COUNT 4
83 static int account
[ACCOUNT_COUNT
];
88 return ((unsigned int) rand () >> 3) % ACCOUNT_COUNT
;
97 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
99 if (sum
!= ACCOUNT_COUNT
* 1000)
104 /* ------------------- Test use like normal locks ------------------- */
106 /* Test normal locks by having several bank accounts and several threads
107 which shuffle around money between the accounts and another thread
108 checking that all the money is still there. */
110 static pthread_spinlock_t my_lock
;
113 lock_mutator_thread (void *arg
)
117 for (repeat
= REPEAT_COUNT
; repeat
> 0; repeat
--)
121 dbgprintf ("Mutator %p before lock\n", pthread_self_pointer ());
122 ASSERT (pthread_spin_lock (&my_lock
) == 0);
123 dbgprintf ("Mutator %p after lock\n", pthread_self_pointer ());
125 i1
= random_account ();
126 i2
= random_account ();
127 value
= ((unsigned int) rand () >> 3) % 10;
128 account
[i1
] += value
;
129 account
[i2
] -= value
;
131 dbgprintf ("Mutator %p before unlock\n", pthread_self_pointer ());
132 ASSERT (pthread_spin_unlock (&my_lock
) == 0);
133 dbgprintf ("Mutator %p after unlock\n", pthread_self_pointer ());
135 dbgprintf ("Mutator %p before check lock\n", pthread_self_pointer ());
136 ASSERT (pthread_spin_lock (&my_lock
) == 0);
138 ASSERT (pthread_spin_unlock (&my_lock
) == 0);
139 dbgprintf ("Mutator %p after check unlock\n", pthread_self_pointer ());
144 dbgprintf ("Mutator %p dying.\n", pthread_self_pointer ());
148 static struct atomic_int lock_checker_done
;
151 lock_checker_thread (void *arg
)
153 while (get_atomic_int_value (&lock_checker_done
) == 0)
155 dbgprintf ("Checker %p before check lock\n", pthread_self_pointer ());
156 ASSERT (pthread_spin_lock (&my_lock
) == 0);
158 ASSERT (pthread_spin_unlock (&my_lock
) == 0);
159 dbgprintf ("Checker %p after check unlock\n", pthread_self_pointer ());
164 dbgprintf ("Checker %p dying.\n", pthread_self_pointer ());
169 test_pthread_spin (void)
172 pthread_t checkerthread
;
173 pthread_t threads
[THREAD_COUNT
];
175 /* Initialization. */
176 for (i
= 0; i
< ACCOUNT_COUNT
; i
++)
178 init_atomic_int (&lock_checker_done
);
179 set_atomic_int_value (&lock_checker_done
, 0);
181 /* Spawn the threads. */
182 ASSERT (pthread_create (&checkerthread
, NULL
, lock_checker_thread
, NULL
)
184 for (i
= 0; i
< THREAD_COUNT
; i
++)
185 ASSERT (pthread_create (&threads
[i
], NULL
, lock_mutator_thread
, NULL
) == 0);
187 /* Wait for the threads to terminate. */
188 for (i
= 0; i
< THREAD_COUNT
; i
++)
189 ASSERT (pthread_join (threads
[i
], NULL
) == 0);
190 set_atomic_int_value (&lock_checker_done
, 1);
191 ASSERT (pthread_join (checkerthread
, NULL
) == 0);
196 /* -------------------------------------------------------------------------- */
202 /* Declare failure if test takes too long, by using default abort
203 caused by SIGALRM. */
204 int alarm_value
= 600;
205 signal (SIGALRM
, SIG_DFL
);
209 ASSERT (pthread_spin_init (&my_lock
, 0) == 0);
211 printf ("Starting test_pthread_spin ..."); fflush (stdout
);
212 test_pthread_spin ();
213 printf (" OK\n"); fflush (stdout
);
220 /* No multithreading available. */
227 fputs ("Skipping test: multithreading not enabled\n", stderr
);