2013-12-13 06:41:02 +08:00
|
|
|
#include "test/jemalloc_test.h"
|
|
|
|
|
2017-01-20 13:41:41 +08:00
|
|
|
#define NTHREADS 2
|
|
|
|
#define NINCRS 2000000
|
2013-12-13 06:41:02 +08:00
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_mtx_basic) {
|
2013-12-13 06:41:02 +08:00
|
|
|
mtx_t mtx;
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(mtx_init(&mtx), "Unexpected mtx_init() failure");
|
2013-12-13 06:41:02 +08:00
|
|
|
mtx_lock(&mtx);
|
|
|
|
mtx_unlock(&mtx);
|
|
|
|
mtx_fini(&mtx);
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
mtx_t mtx;
|
|
|
|
unsigned x;
|
|
|
|
} thd_start_arg_t;
|
|
|
|
|
|
|
|
static void *
|
2017-01-16 08:56:30 +08:00
|
|
|
thd_start(void *varg) {
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_start_arg_t *arg = (thd_start_arg_t *)varg;
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < NINCRS; i++) {
|
|
|
|
mtx_lock(&arg->mtx);
|
|
|
|
arg->x++;
|
|
|
|
mtx_unlock(&arg->mtx);
|
|
|
|
}
|
2017-01-20 10:15:45 +08:00
|
|
|
return NULL;
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-16 08:56:30 +08:00
|
|
|
TEST_BEGIN(test_mtx_race) {
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_start_arg_t arg;
|
|
|
|
thd_t thds[NTHREADS];
|
|
|
|
unsigned i;
|
|
|
|
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_false(mtx_init(&arg.mtx), "Unexpected mtx_init() failure");
|
2013-12-13 06:41:02 +08:00
|
|
|
arg.x = 0;
|
2017-01-16 08:56:30 +08:00
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_create(&thds[i], thd_start, (void *)&arg);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
|
|
|
for (i = 0; i < NTHREADS; i++) {
|
2013-12-13 06:41:02 +08:00
|
|
|
thd_join(thds[i], NULL);
|
2017-01-16 08:56:30 +08:00
|
|
|
}
|
2020-02-19 06:39:06 +08:00
|
|
|
expect_u_eq(arg.x, NTHREADS * NINCRS,
|
2013-12-13 06:41:02 +08:00
|
|
|
"Race-related counter corruption");
|
|
|
|
}
|
|
|
|
TEST_END
|
|
|
|
|
|
|
|
int
|
2017-01-16 08:56:30 +08:00
|
|
|
main(void) {
|
2017-01-20 10:15:45 +08:00
|
|
|
return test(
|
2013-12-13 06:41:02 +08:00
|
|
|
test_mtx_basic,
|
2017-01-20 10:15:45 +08:00
|
|
|
test_mtx_race);
|
2013-12-13 06:41:02 +08:00
|
|
|
}
|