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