1 #include "test/jemalloc_test.h"
2
3 static const bool config_tcache =
4 #ifdef JEMALLOC_TCACHE
5 true
6 #else
7 false
8 #endif
9 ;
10
11 void *
thd_start(void * arg)12 thd_start(void *arg)
13 {
14 int err;
15 size_t sz;
16 bool e0, e1;
17
18 sz = sizeof(bool);
19 if ((err = mallctl("thread.tcache.enabled", (void *)&e0, &sz, NULL,
20 0))) {
21 if (err == ENOENT) {
22 assert_false(config_tcache,
23 "ENOENT should only be returned if tcache is "
24 "disabled");
25 }
26 goto label_ENOENT;
27 }
28
29 if (e0) {
30 e1 = false;
31 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
32 (void *)&e1, sz), 0, "Unexpected mallctl() error");
33 assert_true(e0, "tcache should be enabled");
34 }
35
36 e1 = true;
37 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
38 (void *)&e1, sz), 0, "Unexpected mallctl() error");
39 assert_false(e0, "tcache should be disabled");
40
41 e1 = true;
42 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
43 (void *)&e1, sz), 0, "Unexpected mallctl() error");
44 assert_true(e0, "tcache should be enabled");
45
46 e1 = false;
47 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
48 (void *)&e1, sz), 0, "Unexpected mallctl() error");
49 assert_true(e0, "tcache should be enabled");
50
51 e1 = false;
52 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
53 (void *)&e1, sz), 0, "Unexpected mallctl() error");
54 assert_false(e0, "tcache should be disabled");
55
56 free(malloc(1));
57 e1 = true;
58 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
59 (void *)&e1, sz), 0, "Unexpected mallctl() error");
60 assert_false(e0, "tcache should be disabled");
61
62 free(malloc(1));
63 e1 = true;
64 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
65 (void *)&e1, sz), 0, "Unexpected mallctl() error");
66 assert_true(e0, "tcache should be enabled");
67
68 free(malloc(1));
69 e1 = false;
70 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
71 (void *)&e1, sz), 0, "Unexpected mallctl() error");
72 assert_true(e0, "tcache should be enabled");
73
74 free(malloc(1));
75 e1 = false;
76 assert_d_eq(mallctl("thread.tcache.enabled", (void *)&e0, &sz,
77 (void *)&e1, sz), 0, "Unexpected mallctl() error");
78 assert_false(e0, "tcache should be disabled");
79
80 free(malloc(1));
81 return (NULL);
82 label_ENOENT:
83 test_skip("\"thread.tcache.enabled\" mallctl not available");
84 return (NULL);
85 }
86
TEST_BEGIN(test_main_thread)87 TEST_BEGIN(test_main_thread)
88 {
89
90 thd_start(NULL);
91 }
92 TEST_END
93
TEST_BEGIN(test_subthread)94 TEST_BEGIN(test_subthread)
95 {
96 thd_t thd;
97
98 thd_create(&thd, thd_start, NULL);
99 thd_join(thd, NULL);
100 }
101 TEST_END
102
103 int
main(void)104 main(void)
105 {
106
107 /* Run tests multiple times to check for bad interactions. */
108 return (test(
109 test_main_thread,
110 test_subthread,
111 test_main_thread,
112 test_subthread,
113 test_main_thread));
114 }
115