xref: /redis-3.2.3/deps/jemalloc/test/unit/tsd.c (revision 82154ffa)
1 #include "test/jemalloc_test.h"
2 
3 #define	THREAD_DATA 0x72b65c10
4 
5 typedef unsigned int data_t;
6 
7 static bool data_cleanup_executed;
8 
9 void
10 data_cleanup(void *arg)
11 {
12 	data_t *data = (data_t *)arg;
13 
14 	assert_x_eq(*data, THREAD_DATA,
15 	    "Argument passed into cleanup function should match tsd value");
16 	data_cleanup_executed = true;
17 }
18 
19 malloc_tsd_protos(, data, data_t)
20 malloc_tsd_externs(data, data_t)
21 #define	DATA_INIT 0x12345678
22 malloc_tsd_data(, data, data_t, DATA_INIT)
23 malloc_tsd_funcs(, data, data_t, DATA_INIT, data_cleanup)
24 
25 static void *
26 thd_start(void *arg)
27 {
28 	data_t d = (data_t)(uintptr_t)arg;
29 	assert_x_eq(*data_tsd_get(), DATA_INIT,
30 	    "Initial tsd get should return initialization value");
31 
32 	data_tsd_set(&d);
33 	assert_x_eq(*data_tsd_get(), d,
34 	    "After tsd set, tsd get should return value that was set");
35 
36 	d = 0;
37 	assert_x_eq(*data_tsd_get(), (data_t)(uintptr_t)arg,
38 	    "Resetting local data should have no effect on tsd");
39 
40 	return (NULL);
41 }
42 
43 TEST_BEGIN(test_tsd_main_thread)
44 {
45 
46 	thd_start((void *) 0xa5f3e329);
47 }
48 TEST_END
49 
50 TEST_BEGIN(test_tsd_sub_thread)
51 {
52 	thd_t thd;
53 
54 	data_cleanup_executed = false;
55 	thd_create(&thd, thd_start, (void *)THREAD_DATA);
56 	thd_join(thd, NULL);
57 	assert_true(data_cleanup_executed,
58 	    "Cleanup function should have executed");
59 }
60 TEST_END
61 
62 int
63 main(void)
64 {
65 
66 	data_tsd_boot();
67 
68 	return (test(
69 	    test_tsd_main_thread,
70 	    test_tsd_sub_thread));
71 }
72