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