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