]> git.proxmox.com Git - rustc.git/blame - src/jemalloc/test/unit/mtx.c
New upstream version 1.22.1+dfsg1
[rustc.git] / src / jemalloc / test / unit / mtx.c
CommitLineData
1a4d82fc
JJ
1#include "test/jemalloc_test.h"
2
3#define NTHREADS 2
4#define NINCRS 2000000
5
6TEST_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}
15TEST_END
16
17typedef struct {
18 mtx_t mtx;
19 unsigned x;
20} thd_start_arg_t;
21
22static void *
23thd_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
36TEST_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}
51TEST_END
52
53int
54main(void)
55{
56
57 return (test(
58 test_mtx_basic,
59 test_mtx_race));
60}