Line data Source code
1 : /* t-lock.c - Check the lock functions
2 : * Copyright (C) 2013, 2015 g10 Code GmbH
3 : *
4 : * This file is part of libgpg-error.
5 : *
6 : * libgpg-error is free software; you can redistribute it and/or
7 : * modify it under the terms of the GNU Lesser General Public License
8 : * as published by the Free Software Foundation; either version 2.1 of
9 : * the License, or (at your option) any later version.
10 : *
11 : * libgpg-error is distributed in the hope that it will be useful, but
12 : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : * Lesser General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU Lesser General Public
17 : * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18 : */
19 :
20 : #if HAVE_CONFIG_H
21 : # include <config.h>
22 : #endif
23 :
24 : #include <stdio.h>
25 : #include <stdlib.h>
26 : #include <string.h>
27 : #include <assert.h>
28 : #include <sys/types.h>
29 : #include <unistd.h>
30 : #ifdef _WIN32
31 : # include <windows.h>
32 : # include <time.h>
33 : #else
34 : # include <pthread.h>
35 : #endif
36 :
37 : #define PGM "t-lock"
38 :
39 : #include "t-common.h"
40 :
41 : #ifdef _WIN32
42 : # define THREAD_RET_TYPE DWORD WINAPI
43 : # define THREAD_RET_VALUE 0
44 : #else
45 : # define THREAD_RET_TYPE void *
46 : # define THREAD_RET_VALUE NULL
47 : #endif
48 :
49 :
50 : /* Our tests works by having a a couple of accountant threads which do
51 : random transactions between accounts and a revision threads which
52 : checks that the balance of all accounts is invariant. The idea for
53 : this check is due to Bruno Haible. */
54 : #define N_ACCOUNT 8
55 : #define ACCOUNT_VALUE 42
56 : static int account[N_ACCOUNT];
57 : GPGRT_LOCK_DEFINE (accounts_lock);
58 :
59 : /* Number of transactions done by each accountant. */
60 : #define N_TRANSACTIONS 1000
61 :
62 : /* Number of accountants to run. */
63 : #define N_ACCOUNTANTS 5
64 :
65 : /* Maximum transaction value. A quite low value is used so that we
66 : would get an integer overflow. */
67 : #define MAX_TRANSACTION_VALUE 50
68 :
69 : /* Flag to tell the revision thread to finish. */
70 : static volatile int stop_revision_thread;
71 :
72 :
73 : /* Initialze all accounts. */
74 : static void
75 : init_accounts (void)
76 : {
77 : int i;
78 :
79 8 : for (i=0; i < N_ACCOUNT; i++)
80 8 : account[i] = ACCOUNT_VALUE;
81 : }
82 :
83 :
84 : /* Check that the sum of all accounts matches the intial sum. */
85 : static void
86 3957 : check_accounts (void)
87 : {
88 : int i, sum;
89 :
90 : sum = 0;
91 35613 : for (i = 0; i < N_ACCOUNT; i++)
92 31656 : sum += account[i];
93 3957 : if (sum != N_ACCOUNT * ACCOUNT_VALUE)
94 0 : die ("accounts out of balance");
95 3957 : }
96 :
97 :
98 : static void
99 0 : print_accounts (void)
100 : {
101 : int i;
102 :
103 0 : for (i=0; i < N_ACCOUNT; i++)
104 0 : printf ("account %d: %6d\n", i, account[i]);
105 0 : }
106 :
107 :
108 : #if defined(_WIN32) || defined(USE_POSIX_THREADS)
109 : /* Get a a random integer value in the range 0 to HIGH. */
110 : static unsigned int
111 : get_rand (int high)
112 : {
113 15000 : return (unsigned int)(1+(int)((double)(high+1)*rand ()/(RAND_MAX+1.0))) - 1;
114 : }
115 :
116 :
117 : /* Pick a random account. Note that this fucntion is not
118 : thread-safe. */
119 : static int
120 : pick_account (void)
121 : {
122 : return get_rand (N_ACCOUNT - 1);
123 : }
124 :
125 :
126 : /* Pick a random value for a transaction. This is not thread-safe. */
127 : static int
128 : pick_value (void)
129 : {
130 : return get_rand (MAX_TRANSACTION_VALUE);
131 : }
132 :
133 :
134 : /* This is the revision department. */
135 : static THREAD_RET_TYPE
136 3 : revision_thread (void *arg)
137 : {
138 : gpg_err_code_t rc;
139 : int i = 0;
140 :
141 : (void)arg;
142 :
143 3959 : while (!stop_revision_thread)
144 : {
145 3953 : rc = gpgrt_lock_lock (&accounts_lock);
146 3953 : if (rc)
147 0 : fail ("gpgrt_lock_lock failed at %d: %s", __LINE__, gpg_strerror (rc));
148 :
149 3953 : check_accounts ();
150 3953 : rc = gpgrt_lock_unlock (&accounts_lock);
151 3953 : if (rc)
152 0 : fail ("gpgrt_lock_unlock failed at %d: %s", __LINE__,gpg_strerror (rc));
153 3953 : if (!(++i%7))
154 564 : gpgrt_yield ();
155 : }
156 3 : return THREAD_RET_VALUE;
157 : }
158 :
159 :
160 : /* This is one of our accountants. */
161 : static THREAD_RET_TYPE
162 27 : accountant_thread (void *arg)
163 : {
164 : gpg_err_code_t rc;
165 : int i;
166 : int acc1, acc2;
167 : int value;
168 :
169 : (void)arg;
170 :
171 : #ifdef _WIN32
172 : srand (time(NULL)*getpid()); /* Windows needs it per thread. */
173 : #endif
174 14786 : for (i = 0; i < N_TRANSACTIONS; i++)
175 : {
176 14771 : rc = gpgrt_lock_lock (&accounts_lock);
177 15000 : if (rc)
178 0 : fail ("gpgrt_lock_lock failed at %d: %s", __LINE__, gpg_strerror (rc));
179 :
180 : acc1 = pick_account ();
181 : acc2 = pick_account ();
182 : value = pick_value ();
183 15000 : account[acc1] += value;
184 15000 : account[acc2] -= value;
185 :
186 15000 : rc = gpgrt_lock_unlock (&accounts_lock);
187 14818 : if (rc)
188 0 : fail ("gpgrt_lock_unlock failed at %d: %s", __LINE__,gpg_strerror (rc));
189 14762 : if (i && !(i%8))
190 1857 : gpgrt_yield ();
191 : }
192 15 : return THREAD_RET_VALUE;
193 : }
194 : #endif /*_WIN32||USE_POSIX_THREADS*/
195 :
196 :
197 : static void
198 3 : run_test (void)
199 : {
200 : #ifdef _WIN32
201 : HANDLE rthread;
202 : HANDLE athreads[N_ACCOUNTANTS];
203 : int i;
204 : int rc;
205 :
206 : stop_revision_thread = 0;
207 : rthread = CreateThread (NULL, 0, revision_thread, NULL, 0, NULL);
208 : if (!rthread)
209 : die ("error creating revision thread: rc=%d", (int)GetLastError ());
210 :
211 : for (i=0; i < N_ACCOUNTANTS; i++)
212 : {
213 : athreads[i] = CreateThread (NULL, 0, accountant_thread, NULL, 0, NULL);
214 : if (!athreads[i])
215 : die ("error creating accountant thread %d: rc=%d",
216 : i, (int)GetLastError ());
217 : }
218 :
219 : for (i=0; i < N_ACCOUNTANTS; i++)
220 : {
221 : rc = WaitForSingleObject (athreads[i], INFINITE);
222 : if (rc == WAIT_OBJECT_0)
223 : show ("accountant thread %d has terminated", i);
224 : else
225 : fail ("waiting for accountant thread %d failed: %d",
226 : i, (int)GetLastError ());
227 : CloseHandle (athreads[i]);
228 : }
229 : stop_revision_thread = 1;
230 :
231 : rc = WaitForSingleObject (rthread, INFINITE);
232 : if (rc == WAIT_OBJECT_0)
233 : show ("revision thread has terminated");
234 : else
235 : fail ("waiting for revision thread failed: %d", (int)GetLastError ());
236 : CloseHandle (rthread);
237 :
238 : #else /*!_WIN32*/
239 : # ifdef USE_POSIX_THREADS
240 : pthread_t rthread;
241 : pthread_t athreads[N_ACCOUNTANTS];
242 : int i;
243 :
244 3 : stop_revision_thread = 0;
245 3 : pthread_create (&rthread, NULL, revision_thread, NULL);
246 :
247 18 : for (i=0; i < N_ACCOUNTANTS; i++)
248 15 : pthread_create (&athreads[i], NULL, accountant_thread, NULL);
249 :
250 15 : for (i=0; i < N_ACCOUNTANTS; i++)
251 : {
252 15 : pthread_join (athreads[i], NULL);
253 15 : show ("accountant thread %d has terminated", i);
254 : }
255 :
256 3 : stop_revision_thread = 1;
257 3 : pthread_join (rthread, NULL);
258 3 : show ("revision thread has terminated");
259 : # else /*!USE_POSIX_THREADS*/
260 : verbose++;
261 : show ("no thread support - skipping test\n", PGM);
262 : verbose--;
263 : # endif /*!USE_POSIX_THREADS*/
264 : #endif /*!_WIN32*/
265 :
266 3 : gpgrt_lock_destroy (&accounts_lock);
267 3 : }
268 :
269 :
270 : int
271 1 : main (int argc, char **argv)
272 : {
273 : int last_argc = -1;
274 : int rc;
275 :
276 1 : if (argc)
277 : {
278 1 : argc--; argv++;
279 : }
280 1 : while (argc && last_argc != argc )
281 : {
282 : last_argc = argc;
283 0 : if (!strcmp (*argv, "--help"))
284 : {
285 0 : puts (
286 : "usage: ./t-lock [options]\n"
287 : "\n"
288 : "Options:\n"
289 : " --verbose Show what is going on\n"
290 : " --debug Flyswatter\n"
291 : );
292 0 : exit (0);
293 : }
294 0 : if (!strcmp (*argv, "--verbose"))
295 : {
296 0 : verbose = 1;
297 0 : argc--; argv++;
298 : }
299 0 : else if (!strcmp (*argv, "--debug"))
300 : {
301 0 : verbose = debug = 1;
302 0 : argc--; argv++;
303 : }
304 : }
305 :
306 1 : srand (time(NULL)*getpid());
307 :
308 1 : if (!gpg_error_check_version (GPG_ERROR_VERSION))
309 : {
310 0 : die ("gpg_error_check_version returned an error");
311 : errorcount++;
312 : }
313 :
314 : init_accounts ();
315 1 : check_accounts ();
316 1 : run_test ();
317 1 : check_accounts ();
318 : /* Run a second time to check deinit code. */
319 1 : run_test ();
320 1 : check_accounts ();
321 : /* And a third time to test an explicit init. */
322 1 : rc = gpgrt_lock_init (&accounts_lock);
323 1 : if (rc)
324 0 : fail ("gpgrt_lock_init failed at %d: %s", __LINE__, gpg_strerror (rc));
325 1 : run_test ();
326 1 : check_accounts ();
327 1 : if (verbose)
328 0 : print_accounts ();
329 :
330 1 : return errorcount ? 1 : 0;
331 : }
|