1 #include "test/jemalloc_test.h"
2 
3 #define NTHREADS	2
4 #define NINCRS		2000000
5 
6 TEST_BEGIN(test_mtx_basic) {
7 	mtx_t mtx;
8 
9 	assert_false(mtx_init(&mtx), "Unexpected mtx_init() failure");
10 	mtx_lock(&mtx);
11 	mtx_unlock(&mtx);
12 	mtx_fini(&mtx);
13 }
14 TEST_END
15 
16 typedef struct {
double_eq_rel(double a,double b,double max_rel_err,double max_abs_err)17 	mtx_t		mtx;
18 	unsigned	x;
19 } thd_start_arg_t;
20 
21 static void *
22 thd_start(void *varg) {
23 	thd_start_arg_t *arg = (thd_start_arg_t *)varg;
24 	unsigned i;
25 
26 	for (i = 0; i < NINCRS; i++) {
27 		mtx_lock(&arg->mtx);
factorial(unsigned x)28 		arg->x++;
29 		mtx_unlock(&arg->mtx);
30 	}
31 	return NULL;
32 }
33 
34 TEST_BEGIN(test_mtx_race) {
35 	thd_start_arg_t arg;
36 	thd_t thds[NTHREADS];
37 	unsigned i;
38 
TEST_BEGIN(test_ln_gamma_factorial)39 	assert_false(mtx_init(&arg.mtx), "Unexpected mtx_init() failure");
40 	arg.x = 0;
41 	for (i = 0; i < NTHREADS; i++) {
42 		thd_create(&thds[i], thd_start, (void *)&arg);
43 	}
44 	for (i = 0; i < NTHREADS; i++) {
45 		thd_join(thds[i], NULL);
46 	}
47 	assert_u_eq(arg.x, NTHREADS * NINCRS,
48 	    "Race-related counter corruption");
49 }
50 TEST_END
51 
52 int
53 main(void) {
54 	return test(
55 	    test_mtx_basic,
56 	    test_mtx_race);
57 }
58