File: thread-api-tests.c

package info (click to toggle)
mmlib 1.4.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,716 kB
  • sloc: ansic: 18,071; makefile: 431; sh: 135; python: 63
file content (542 lines) | stat: -rw-r--r-- 14,356 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
   @mindmaze_header@
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <check.h>
#include <stdatomic.h>

#include "api-testcases.h"
#include "mmpredefs.h"
#include "mmsysio.h"
#include "mmthread.h"
#include "mmtime.h"
#include "tests-child-proc.h"
#include "threaddata-manipulation.h"

#define EXPECTED_VALUE	(int)0xdeadbeef
#define NUM_CONCURRENCY	12

static
int mutex_type_flags[] = {
	0,
	MM_THR_PSHARED,
};
#define NUM_MUTEX_TYPE	MM_NELEM(mutex_type_flags)
#define FIRST_PSHARED_MUTEX_TYPE	1

static
void* simple_write_proc(void* arg)
{
	int* pval = arg;

	*pval = EXPECTED_VALUE;

	return NULL;
}


/*
 * Test that a write in another thread affect shared data
 */
START_TEST(data_write_in_thread)
{
	int value = 0;
	mm_thread_t thid;

	ck_assert(mm_thr_create(&thid, simple_write_proc, &value) == 0);
	mm_thr_join(thid, NULL);

	ck_assert(value == EXPECTED_VALUE);
}
END_TEST


/**************************************************************************
 *                                                                        *
 *                         mutex tests                                    *
 *                                                                        *
 **************************************************************************/
static
void* protected_write_threadproc(void* arg)
{
	run_write_shared_data(arg);
	return NULL;
}


static
void init_shared_write_data(struct shared_write_data* shdata, int mutex_flags,
                            int num_runner, int num_iter, bool do_sleep)
{
	shdata->sleep_in_touch = do_sleep;
	shdata->num_iteration = num_iter;
	shdata->value = SHARED_WRITE_INIT_VALUE;
	shdata->failed = false;
	shdata->num_runner_remaining = num_runner;

	ck_assert(mm_thr_mutex_init(&shdata->mutex, mutex_flags) == 0);
}


/**
 * runtest_mutex_protection_on_write() - test a concurrent write on shared data
 * @mutex_flags: flags to use for mutex in the test
 * @num_iter:   number of times each runner has to touch the shared data
 * @do_sleep:   true if runner has to sleep between setting shared data and restore
 *
 * This function initialized the shared data and mutex and spawn the thread
 * that will concurrently modify the shared data (protected by mutex). If
 * the mutex protection fails, one (or several) thread will see inconsistent
 * state in shared data and will report failure in shdata->failed.
 */
static
void runtest_mutex_protection_on_write(int mutex_flags, int num_iter, bool do_sleep)
{
	int i, num_thread;
	mm_thread_t thid[NUM_CONCURRENCY];
	struct shared_write_data shdata;

	init_shared_write_data(&shdata, mutex_flags, NUM_CONCURRENCY, num_iter, do_sleep);

	// Spawn all thread for the test
	num_thread = 0;
	for (i = 0; i < NUM_CONCURRENCY; i++) {
		if (mm_thr_create(&thid[i], protected_write_threadproc, &shdata) != 0)
			break;

		num_thread++;
	}


	// Join only threads that have been created
	for (i = 0; i < num_thread; i++)
		mm_thr_join(thid[i], NULL);

	ck_assert(num_thread == NUM_CONCURRENCY);
	ck_assert(shdata.num_runner_remaining == 0);
	ck_assert(shdata.failed == false);
}


/**
 * runtest_mutex_on_pshared_write() - test a concurrent write on shared data
 * @shdata:     structure holding test setup and shared data
 * @mutex_flags: flags to use for mutex in the test
 *
 * This function initialized the shared data and mutex and spawn the thread
 * that will concurrently modify the shared data (protected by mutex). If
 * the mutex protection fails, one (or several) thread will see inconsistent
 * state in shared data and will report failure in shdata->failed.
 */
static
void runtest_mutex_on_pshared_write(int mutex_flags,
                                         bool sleep_in_touch, int num_iter)
{
	int i, num_proc = 0;
	int shm_fd, num_runner_remaining = 0;
	mm_pid_t pid[NUM_CONCURRENCY];
	struct shared_write_data* shdata;
	void* map = NULL;
	bool failed = true;
	struct mm_remap_fd fdmap;
	char* argv[] = {TESTS_CHILD_BIN, "run_write_shared_data", "mapfile-3-4096", NULL};

	if ((shm_fd = mm_anon_shm()) == -1
	  || mm_ftruncate(shm_fd, MM_PAGESZ)
	  || !(map = mm_mapfile(shm_fd, 0, MM_PAGESZ, MM_MAP_RDWR|MM_MAP_SHARED)) ) {
		goto exit;
	}

	shdata = map;
	init_shared_write_data(shdata, mutex_flags, NUM_CONCURRENCY, num_iter, sleep_in_touch);

	// Spawn all children for the test
	fdmap.child_fd = 3;
	fdmap.parent_fd = shm_fd;
	num_proc = 0;
	for (i = 0; i < NUM_CONCURRENCY; i++) {
		if (mm_spawn(&pid[i], argv[0], 1, &fdmap, 0, argv, NULL))
			break;

		num_proc++;
	}

	// Wait that all children finish
	for (i = 0; i < num_proc; i++)
		mm_wait_process(pid[i], NULL);

	failed = shdata->failed;
	num_runner_remaining = shdata->num_runner_remaining;

exit:
	mm_unmap(map);
	if (shm_fd != -1)
		mm_close(shm_fd);

	ck_assert(num_proc == NUM_CONCURRENCY);
	ck_assert(num_runner_remaining == 0);
	ck_assert(failed == false);
}


/*
 * perform concurrent modification on a shared data protected by mutex. The
 * access protected by the mutex should restore the value as it at the
 * beginning if the mutex protection works.
 */
START_TEST(mutex_protection_on_write_normal)
{
	runtest_mutex_protection_on_write(mutex_type_flags[_i], 100000, false);
}
END_TEST


/*
 * Same test as mutex_protection_on_write_normal but introduce a sleep while
 * a thread is holding the lock and is modifying shared data. This increases
 * further the probability of contended lock situation.
 */
START_TEST(mutex_protection_on_write_sleep)
{
	runtest_mutex_protection_on_write(mutex_type_flags[_i], 10, true);
}
END_TEST


START_TEST(mutex_protection_on_pshared_write_normal)
{
	runtest_mutex_on_pshared_write(mutex_type_flags[_i], false, 100000);
}
END_TEST


START_TEST(mutex_protection_on_pshared_write_sleep)
{
	runtest_mutex_on_pshared_write(mutex_type_flags[_i], true, 10);
}
END_TEST


static const int robust_sleep_on_lock_cases[] = {false, true};
#define NUM_ROBUST_CASES MM_NELEM(robust_sleep_on_lock_cases)
#define EXPECTED_CRASH_ITER	(NUM_CONCURRENCY/2)

START_TEST(robust_mutex)
{
	int i, num_proc = 0;
	int num_iter = 0, num_iter_finished = 0, detected_crash_iter = 0;
	int shm_fd;
	mm_pid_t pid[NUM_CONCURRENCY];
	struct robust_mutex_write_data* rdata;
	void* map = NULL;
	struct mm_remap_fd fdmap;
	char* argv[] = {TESTS_CHILD_BIN, "run_robust_mutex_write_data", "mapfile-3-4096", NULL};

	if ((shm_fd = mm_anon_shm()) == -1
	  || mm_ftruncate(shm_fd, MM_PAGESZ)
	  || !(map = mm_mapfile(shm_fd, 0, MM_PAGESZ, MM_MAP_RDWR|MM_MAP_SHARED)) ) {
		goto exit;
	}

	rdata = map;
	rdata->iter = 0;
	rdata->iter_finished = 0;
	rdata->sleep_after_first_lock = robust_sleep_on_lock_cases[_i];
	rdata->crash_at_iter = EXPECTED_CRASH_ITER;
	rdata->detected_iter_after_crash = -1;
	ck_assert(mm_thr_mutex_init(&rdata->mutex, MM_THR_PSHARED) == 0);

	// Spawn all children for the test
	fdmap.child_fd = 3;
	fdmap.parent_fd = shm_fd;
	num_proc = 0;
	for (i = 0; i < NUM_CONCURRENCY; i++) {
		if (mm_spawn(&pid[i], argv[0], 1, &fdmap, 0, argv, NULL))
			break;

		num_proc++;
	}

	// Wait that all children finish
	for (i = 0; i < num_proc; i++)
		mm_wait_process(pid[i], NULL);

	num_iter = rdata->iter;
	num_iter_finished = rdata->iter_finished;
	detected_crash_iter = rdata->detected_iter_after_crash;

exit:
	mm_unmap(map);
	if (shm_fd != -1)
		mm_close(shm_fd);

	ck_assert(num_proc == NUM_CONCURRENCY);
	ck_assert(num_iter == NUM_CONCURRENCY);
	ck_assert(num_iter_finished == NUM_CONCURRENCY-1);
	ck_assert(detected_crash_iter == EXPECTED_CRASH_ITER);
}
END_TEST

/**************************************************************************
 *                                                                        *
 *                     condition variable tests                           *
 *                                                                        *
 **************************************************************************/
static
void* notif_var_threadproc(void* arg)
{
	run_notif_data(arg);
	return NULL;
}


static
void init_notif_data(struct notif_data* ndata, int mutex_flags)
{
	memset(ndata, 0, sizeof(*ndata));

	mm_thr_mutex_init(&ndata->mutex, mutex_flags);
	mm_thr_cond_init(&ndata->cv1, mutex_flags);
	mm_thr_cond_init(&ndata->cv2, mutex_flags);
}


static
void deinit_notif_data(struct notif_data* ndata)
{
	mm_thr_mutex_deinit(&ndata->mutex);
	mm_thr_cond_deinit(&ndata->cv1);
	mm_thr_cond_deinit(&ndata->cv2);
}


static
void do_notif_and_inspect(struct notif_data* ndata, bool do_broadcast,
                          int num_runner)
{
	mm_thr_mutex_lock(&ndata->mutex);

	// Wait for all started thread be ready
	while (ndata->nwaiter < num_runner)
		mm_thr_cond_wait(&ndata->cv1, &ndata->mutex);

	if (!do_broadcast) {
		// Notify one thread to wakeup and give it time respond
		ndata->todo = true;
		mm_thr_cond_signal(&ndata->cv2);
		mm_thr_mutex_unlock(&ndata->mutex);
		mm_relative_sleep_ms(500);
		mm_thr_mutex_lock(&ndata->mutex);
	}

	// Copy done value for later inspection and notify all threads to
	// quit
	ndata->quit = true;
	mm_thr_cond_broadcast(&ndata->cv2);
	mm_thr_mutex_unlock(&ndata->mutex);
}


/**
 * runtest_signal_or_broadcast_thread() - test thread notification
 * @mutex_flags: flags to use for mutex in the test
 * @do_broadcast: if true, use broadcast instead of signal
 */
static
void runtest_signal_or_broadcast_thread(int mutex_flags, bool do_broadcast)
{
	int i, num_thread;
	mm_thread_t thid[NUM_CONCURRENCY];
	struct notif_data ndata;

	init_notif_data(&ndata, mutex_flags);

	// Spawn all thread for the test
	num_thread = 0;
	for (i = 0; i < NUM_CONCURRENCY; i++) {
		if (mm_thr_create(&thid[i], notif_var_threadproc, &ndata) != 0)
			break;

		num_thread++;
	}

	do_notif_and_inspect(&ndata, do_broadcast, num_thread);

	// Join only threads that have been created
	for (i = 0; i < num_thread; i++)
		mm_thr_join(thid[i], NULL);

	// Verify expected outcome
	if (do_broadcast)
		ck_assert(ndata.numquit == NUM_CONCURRENCY);
	else
		ck_assert(ndata.done >= 1);

	ck_assert(num_thread == NUM_CONCURRENCY);

	deinit_notif_data(&ndata);
}


/**
 * runtest_signal_or_broadcast_thread() - test thread notification
 * @mutex_flags: flags to use for mutex in the test
 * @do_broadcast: if true, use broadcast instead of signal
 */
static
void runtest_signal_or_broadcast_process(int mutex_flags, bool do_broadcast)
{
	int i, num_proc = 0, value_done = 0, value_numquit = 0;
	struct notif_data* ndata;
	mm_pid_t pid[NUM_CONCURRENCY];
	int shm_fd;
	void* map = NULL;
	struct mm_remap_fd fdmap;
	char* argv[] = {TESTS_CHILD_BIN, "run_notif_data", "mapfile-3-4096", NULL};

	if ((shm_fd = mm_anon_shm()) == -1
	  || mm_ftruncate(shm_fd, MM_PAGESZ)
	  || !(map = mm_mapfile(shm_fd, 0, MM_PAGESZ, MM_MAP_RDWR|MM_MAP_SHARED)) ) {
		goto exit;
	}

	ndata = map;
	init_notif_data(ndata, mutex_flags);

	// Spawn all children for the test
	fdmap.child_fd = 3;
	fdmap.parent_fd = shm_fd;
	num_proc = 0;
	for (i = 0; i < NUM_CONCURRENCY; i++) {
		if (mm_spawn(&pid[i], argv[0], 1, &fdmap, 0, argv, NULL))
			break;

		num_proc++;
	}

	do_notif_and_inspect(ndata, do_broadcast, num_proc);

	// Wait that all children finish
	for (i = 0; i < num_proc; i++)
		mm_wait_process(pid[i], NULL);

	value_numquit = ndata->numquit;
	value_done = ndata->done;
	deinit_notif_data(ndata);

exit:
	mm_unmap(map);
	if (shm_fd != -1)
		mm_close(shm_fd);

	// Verify expected outcome
	if (do_broadcast)
		ck_assert(value_numquit == NUM_CONCURRENCY);
	else
		ck_assert(value_done >= 1);

	ck_assert(num_proc == NUM_CONCURRENCY);
}


START_TEST(signal_thread_data)
{
	runtest_signal_or_broadcast_thread(mutex_type_flags[_i], false);
}
END_TEST


START_TEST(broadcast_thread_data)
{
	runtest_signal_or_broadcast_thread(mutex_type_flags[_i], true);
}
END_TEST


START_TEST(signal_pshared_data)
{
	runtest_signal_or_broadcast_process(mutex_type_flags[_i], false);
}
END_TEST


START_TEST(broadcast_pshared_data)
{
	runtest_signal_or_broadcast_process(mutex_type_flags[_i], true);
}
END_TEST

/**************************************************************************
 *                                                                        *
 *                           one-time init tests                          *
 *                                                                        *
 **************************************************************************/
static atomic_int once_val1 = 0;
static atomic_int once_val2 = 0;
static mm_thr_once_t once = MM_THR_ONCE_INIT;

static
void one_time_proc(void)
{
	int readval;

	readval = atomic_fetch_add(&once_val1, 1);
	mm_relative_sleep_ms(1);
	atomic_fetch_add(&once_val2, readval+1);
}


static
void* once_test_proc(void* data)
{
	(void)data;

	mm_thr_once(&once, one_time_proc);

	return NULL;
}


START_TEST(concurrent_once)
{
	mm_thread_t thid[NUM_CONCURRENCY];
	int i;

	for (i = 0; i < MM_NELEM(thid); i++)
		ck_assert(mm_thr_create(&thid[i], once_test_proc, NULL) == 0);

	for (i = 0; i < MM_NELEM(thid); i++)
		mm_thr_join(thid[i], NULL);

	ck_assert_int_eq(once_val1, 1);
	ck_assert_int_eq(once_val2, 1);
}
END_TEST


/**************************************************************************
 *                                                                        *
 *                          Test suite setup                              *
 *                                                                        *
 **************************************************************************/
LOCAL_SYMBOL
TCase* create_thread_tcase(void)
{
	TCase *tc = tcase_create("thread");
	tcase_add_test(tc, data_write_in_thread);
	tcase_add_loop_test(tc, mutex_protection_on_write_normal, 0, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, mutex_protection_on_write_sleep, 0, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, mutex_protection_on_pshared_write_normal, FIRST_PSHARED_MUTEX_TYPE, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, mutex_protection_on_pshared_write_sleep, FIRST_PSHARED_MUTEX_TYPE, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, robust_mutex, 0, NUM_ROBUST_CASES);
	tcase_add_loop_test(tc, signal_thread_data, 0, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, broadcast_thread_data, 0, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, signal_pshared_data, FIRST_PSHARED_MUTEX_TYPE, NUM_MUTEX_TYPE);
	tcase_add_loop_test(tc, broadcast_pshared_data, FIRST_PSHARED_MUTEX_TYPE, NUM_MUTEX_TYPE);
	tcase_add_test(tc, concurrent_once);

	return tc;
}