xref: /xnu-10002.1.13/tests/os_refcnt.c (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions #include <darwintest.h>
2*1031c584SApple OSS Distributions #include <darwintest_utils.h>
3*1031c584SApple OSS Distributions #include <stdio.h>
4*1031c584SApple OSS Distributions #include <assert.h>
5*1031c584SApple OSS Distributions #include <setjmp.h>
6*1031c584SApple OSS Distributions 
7*1031c584SApple OSS Distributions #define DEVELOPMENT 1
8*1031c584SApple OSS Distributions #define DEBUG 0
9*1031c584SApple OSS Distributions #define XNU_KERNEL_PRIVATE 1
10*1031c584SApple OSS Distributions 
11*1031c584SApple OSS Distributions #define OS_REFCNT_DEBUG 1
12*1031c584SApple OSS Distributions #define STRESS_TESTS 0
13*1031c584SApple OSS Distributions 
14*1031c584SApple OSS Distributions #pragma clang diagnostic ignored "-Watomic-implicit-seq-cst"
15*1031c584SApple OSS Distributions #pragma clang diagnostic ignored "-Wc++98-compat"
16*1031c584SApple OSS Distributions 
17*1031c584SApple OSS Distributions void handle_panic(const char *func, char *str, ...);
18*1031c584SApple OSS Distributions #define panic(...) handle_panic(__func__, __VA_ARGS__)
19*1031c584SApple OSS Distributions 
20*1031c584SApple OSS Distributions #include "../libkern/os/refcnt.h"
21*1031c584SApple OSS Distributions #include "../libkern/os/refcnt.c"
22*1031c584SApple OSS Distributions 
23*1031c584SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
24*1031c584SApple OSS Distributions 
25*1031c584SApple OSS Distributions /* import some of the refcnt internal state for testing */
26*1031c584SApple OSS Distributions extern bool ref_debug_enable;
27*1031c584SApple OSS Distributions os_refgrp_decl_extern(global_ref_group);
28*1031c584SApple OSS Distributions 
29*1031c584SApple OSS Distributions T_GLOBAL_META(
30*1031c584SApple OSS Distributions 	T_META_NAMESPACE("os_refcnt"),
31*1031c584SApple OSS Distributions 	T_META_CHECK_LEAKS(false)
32*1031c584SApple OSS Distributions 	);
33*1031c584SApple OSS Distributions 
34*1031c584SApple OSS Distributions T_DECL(os_refcnt, "Basic atomic refcount")
35*1031c584SApple OSS Distributions {
36*1031c584SApple OSS Distributions 	struct os_refcnt rc;
37*1031c584SApple OSS Distributions 	os_ref_init(&rc, NULL);
38*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "refcount correctly initialized");
39*1031c584SApple OSS Distributions 
40*1031c584SApple OSS Distributions 	os_ref_retain(&rc);
41*1031c584SApple OSS Distributions 	os_ref_retain(&rc);
42*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
43*1031c584SApple OSS Distributions 
44*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release(&rc);
45*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
46*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
47*1031c584SApple OSS Distributions 
48*1031c584SApple OSS Distributions 	os_ref_release_live(&rc);
49*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "release_live decreased count");
50*1031c584SApple OSS Distributions 
51*1031c584SApple OSS Distributions 	x = os_ref_release(&rc);
52*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
53*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
54*1031c584SApple OSS Distributions 
55*1031c584SApple OSS Distributions 	os_ref_init(&rc, NULL);
56*1031c584SApple OSS Distributions 	T_ASSERT_TRUE(os_ref_retain_try(&rc), "try retained");
57*1031c584SApple OSS Distributions 
58*1031c584SApple OSS Distributions 	(void)os_ref_release(&rc);
59*1031c584SApple OSS Distributions 	(void)os_ref_release(&rc);
60*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "release");
61*1031c584SApple OSS Distributions 
62*1031c584SApple OSS Distributions 	T_ASSERT_FALSE(os_ref_retain_try(&rc), "try failed");
63*1031c584SApple OSS Distributions }
64*1031c584SApple OSS Distributions 
65*1031c584SApple OSS Distributions T_DECL(refcnt_raw, "Raw refcount")
66*1031c584SApple OSS Distributions {
67*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
68*1031c584SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
69*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "refcount correctly initialized");
70*1031c584SApple OSS Distributions 
71*1031c584SApple OSS Distributions 	os_ref_retain_raw(&rc, NULL);
72*1031c584SApple OSS Distributions 	os_ref_retain_raw(&rc, NULL);
73*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
74*1031c584SApple OSS Distributions 
75*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release_raw(&rc, NULL);
76*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
77*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
78*1031c584SApple OSS Distributions 
79*1031c584SApple OSS Distributions 	os_ref_release_live_raw(&rc, NULL);
80*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "release_live decreased count");
81*1031c584SApple OSS Distributions 
82*1031c584SApple OSS Distributions 	x = os_ref_release_raw(&rc, NULL);
83*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
84*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
85*1031c584SApple OSS Distributions 
86*1031c584SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
87*1031c584SApple OSS Distributions 	T_ASSERT_TRUE(os_ref_retain_try_raw(&rc, NULL), "try retained");
88*1031c584SApple OSS Distributions 
89*1031c584SApple OSS Distributions 	(void)os_ref_release_raw(&rc, NULL);
90*1031c584SApple OSS Distributions 	(void)os_ref_release_raw(&rc, NULL);
91*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "release");
92*1031c584SApple OSS Distributions 
93*1031c584SApple OSS Distributions 	T_ASSERT_FALSE(os_ref_retain_try_raw(&rc, NULL), "try failed");
94*1031c584SApple OSS Distributions }
95*1031c584SApple OSS Distributions 
96*1031c584SApple OSS Distributions T_DECL(refcnt_locked, "Locked refcount")
97*1031c584SApple OSS Distributions {
98*1031c584SApple OSS Distributions 	struct os_refcnt rc;
99*1031c584SApple OSS Distributions 	os_ref_init(&rc, NULL);
100*1031c584SApple OSS Distributions 
101*1031c584SApple OSS Distributions 	os_ref_retain_locked(&rc);
102*1031c584SApple OSS Distributions 	os_ref_retain_locked(&rc);
103*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
104*1031c584SApple OSS Distributions 
105*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release_locked(&rc);
106*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
107*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
108*1031c584SApple OSS Distributions 
109*1031c584SApple OSS Distributions 	(void)os_ref_release_locked(&rc);
110*1031c584SApple OSS Distributions 	x = os_ref_release_locked(&rc);
111*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
112*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
113*1031c584SApple OSS Distributions }
114*1031c584SApple OSS Distributions 
115*1031c584SApple OSS Distributions T_DECL(refcnt_raw_locked, "Locked raw refcount")
116*1031c584SApple OSS Distributions {
117*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
118*1031c584SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
119*1031c584SApple OSS Distributions 
120*1031c584SApple OSS Distributions 	os_ref_retain_locked_raw(&rc, NULL);
121*1031c584SApple OSS Distributions 	os_ref_retain_locked_raw(&rc, NULL);
122*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
123*1031c584SApple OSS Distributions 
124*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release_locked_raw(&rc, NULL);
125*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
126*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
127*1031c584SApple OSS Distributions 
128*1031c584SApple OSS Distributions 	(void)os_ref_release_locked_raw(&rc, NULL);
129*1031c584SApple OSS Distributions 	x = os_ref_release_locked_raw(&rc, NULL);
130*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
131*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
132*1031c584SApple OSS Distributions }
133*1031c584SApple OSS Distributions 
134*1031c584SApple OSS Distributions static void
do_bitwise_test(const os_ref_count_t bits)135*1031c584SApple OSS Distributions do_bitwise_test(const os_ref_count_t bits)
136*1031c584SApple OSS Distributions {
137*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
138*1031c584SApple OSS Distributions 	os_ref_count_t reserved = 0xaaaaaaaaU & ((1U << bits) - 1);
139*1031c584SApple OSS Distributions 
140*1031c584SApple OSS Distributions 	T_LOG("do_bitwise_test(nbits:%d, reserved:%#x)", bits, reserved);
141*1031c584SApple OSS Distributions 
142*1031c584SApple OSS Distributions 	os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
143*1031c584SApple OSS Distributions 
144*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "[%u bits] refcount initialized", bits);
145*1031c584SApple OSS Distributions 
146*1031c584SApple OSS Distributions 	os_ref_retain_mask(&rc, bits, NULL);
147*1031c584SApple OSS Distributions 	os_ref_retain_mask(&rc, bits, NULL);
148*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 3, "retain increased count");
149*1031c584SApple OSS Distributions 
150*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release_mask(&rc, bits, NULL);
151*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
152*1031c584SApple OSS Distributions 
153*1031c584SApple OSS Distributions 	os_ref_release_live_mask(&rc, bits, NULL);
154*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "release_live decreased count");
155*1031c584SApple OSS Distributions 
156*1031c584SApple OSS Distributions 	x = os_ref_release_mask(&rc, bits, NULL);
157*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "released");
158*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(x, 0, "returned released");
159*1031c584SApple OSS Distributions 
160*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(rc & ((1U << bits) - 1), reserved, "Reserved bits not modified");
161*1031c584SApple OSS Distributions 
162*1031c584SApple OSS Distributions 	os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
163*1031c584SApple OSS Distributions 	T_ASSERT_TRUE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try retained");
164*1031c584SApple OSS Distributions 	if (reserved) {
165*1031c584SApple OSS Distributions 		T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, reserved, NULL), "try reject");
166*1031c584SApple OSS Distributions 	}
167*1031c584SApple OSS Distributions 
168*1031c584SApple OSS Distributions 	(void)os_ref_release_mask(&rc, bits, NULL);
169*1031c584SApple OSS Distributions 	(void)os_ref_release_mask(&rc, bits, NULL);
170*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "release");
171*1031c584SApple OSS Distributions 
172*1031c584SApple OSS Distributions 	T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try fail");
173*1031c584SApple OSS Distributions 
174*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(os_ref_get_bits_mask(&rc, bits), reserved, "Reserved bits not modified");
175*1031c584SApple OSS Distributions }
176*1031c584SApple OSS Distributions 
177*1031c584SApple OSS Distributions T_DECL(refcnt_bitwise, "Bitwise refcount")
178*1031c584SApple OSS Distributions {
179*1031c584SApple OSS Distributions 	do_bitwise_test(0);
180*1031c584SApple OSS Distributions 	do_bitwise_test(1);
181*1031c584SApple OSS Distributions 	do_bitwise_test(8);
182*1031c584SApple OSS Distributions 	do_bitwise_test(26);
183*1031c584SApple OSS Distributions 
184*1031c584SApple OSS Distributions 	os_ref_atomic_t rc = 0xaaaaaaaa;
185*1031c584SApple OSS Distributions 
186*1031c584SApple OSS Distributions 	const os_ref_count_t nbits = 3;
187*1031c584SApple OSS Distributions 	const os_ref_count_t count = 5;
188*1031c584SApple OSS Distributions 	const os_ref_count_t bits = 7;
189*1031c584SApple OSS Distributions 	os_ref_init_count_mask(&rc, nbits, NULL, count, bits);
190*1031c584SApple OSS Distributions 
191*1031c584SApple OSS Distributions 	os_ref_count_t mask = (1U << nbits) - 1;
192*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(rc & mask, bits, "bits correctly initialized");
193*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(rc >> nbits, count, "count correctly initialized");
194*1031c584SApple OSS Distributions }
195*1031c584SApple OSS Distributions 
196*1031c584SApple OSS Distributions os_refgrp_decl(static, g1, "test group", NULL);
197*1031c584SApple OSS Distributions os_refgrp_decl_extern(g1);
198*1031c584SApple OSS Distributions 
199*1031c584SApple OSS Distributions T_DECL(refcnt_groups, "Group accounting")
200*1031c584SApple OSS Distributions {
201*1031c584SApple OSS Distributions #if OS_REFCNT_DEBUG
202*1031c584SApple OSS Distributions 	ref_debug_enable = true;
203*1031c584SApple OSS Distributions 
204*1031c584SApple OSS Distributions 	struct os_refcnt rc;
205*1031c584SApple OSS Distributions 	os_ref_init(&rc, &g1);
206*1031c584SApple OSS Distributions 
207*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_children, 1, "group attached");
208*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(global_ref_group.grp_children, 1, "global group attached");
209*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_count, 1, "group count");
210*1031c584SApple OSS Distributions 	T_ASSERT_EQ_ULLONG(g1.grp_retain_total, 1ULL, "group retains");
211*1031c584SApple OSS Distributions 	T_ASSERT_EQ_ULLONG(g1.grp_release_total, 0ULL, "group releases");
212*1031c584SApple OSS Distributions 
213*1031c584SApple OSS Distributions 	os_ref_retain(&rc);
214*1031c584SApple OSS Distributions 	os_ref_retain(&rc);
215*1031c584SApple OSS Distributions 	os_ref_release_live(&rc);
216*1031c584SApple OSS Distributions 	os_ref_release_live(&rc);
217*1031c584SApple OSS Distributions 
218*1031c584SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(g1.grp_retain_total, 3ULL, "group retains");
219*1031c584SApple OSS Distributions 	T_EXPECT_EQ_ULLONG(g1.grp_release_total, 2ULL, "group releases");
220*1031c584SApple OSS Distributions 
221*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release(&rc);
222*1031c584SApple OSS Distributions 	T_QUIET; T_ASSERT_EQ_UINT(x, 0, "released");
223*1031c584SApple OSS Distributions 
224*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_children, 0, "group detatched");
225*1031c584SApple OSS Distributions 	T_ASSERT_EQ_UINT(g1.grp_count, 0, "group count");
226*1031c584SApple OSS Distributions #else
227*1031c584SApple OSS Distributions 	T_SKIP("Refcount debugging disabled");
228*1031c584SApple OSS Distributions #endif
229*1031c584SApple OSS Distributions }
230*1031c584SApple OSS Distributions 
231*1031c584SApple OSS Distributions enum {
232*1031c584SApple OSS Distributions 	OSREF_UNDERFLOW    = 1,
233*1031c584SApple OSS Distributions 	OSREF_OVERFLOW     = 2,
234*1031c584SApple OSS Distributions 	OSREF_RETAIN       = 3,
235*1031c584SApple OSS Distributions 	OSREF_DEALLOC_LIVE = 4,
236*1031c584SApple OSS Distributions };
237*1031c584SApple OSS Distributions 
238*1031c584SApple OSS Distributions static jmp_buf jb;
239*1031c584SApple OSS Distributions static bool expect_panic = false;
240*1031c584SApple OSS Distributions 
241*1031c584SApple OSS Distributions void
handle_panic(const char * func,char * __unused str,...)242*1031c584SApple OSS Distributions handle_panic(const char *func, char *__unused str, ...)
243*1031c584SApple OSS Distributions {
244*1031c584SApple OSS Distributions 	int ret = -1;
245*1031c584SApple OSS Distributions 	if (!expect_panic) {
246*1031c584SApple OSS Distributions 		T_FAIL("unexpected panic from %s", func);
247*1031c584SApple OSS Distributions 		T_LOG("corrupt program state, aborting");
248*1031c584SApple OSS Distributions 		abort();
249*1031c584SApple OSS Distributions 	}
250*1031c584SApple OSS Distributions 	expect_panic = false;
251*1031c584SApple OSS Distributions 
252*1031c584SApple OSS Distributions 	if (strcmp(func, "os_ref_panic_underflow") == 0) {
253*1031c584SApple OSS Distributions 		ret = OSREF_UNDERFLOW;
254*1031c584SApple OSS Distributions 	} else if (strcmp(func, "os_ref_panic_overflow") == 0) {
255*1031c584SApple OSS Distributions 		ret = OSREF_OVERFLOW;
256*1031c584SApple OSS Distributions 	} else if (strcmp(func, "os_ref_panic_retain") == 0) {
257*1031c584SApple OSS Distributions 		ret = OSREF_RETAIN;
258*1031c584SApple OSS Distributions 	} else if (strcmp(func, "os_ref_panic_live") == 0) {
259*1031c584SApple OSS Distributions 		ret = OSREF_DEALLOC_LIVE;
260*1031c584SApple OSS Distributions 	} else {
261*1031c584SApple OSS Distributions 		T_LOG("unexpected panic from %s", func);
262*1031c584SApple OSS Distributions 	}
263*1031c584SApple OSS Distributions 
264*1031c584SApple OSS Distributions 	longjmp(jb, ret);
265*1031c584SApple OSS Distributions }
266*1031c584SApple OSS Distributions 
267*1031c584SApple OSS Distributions T_DECL(refcnt_underflow, "Underflow")
268*1031c584SApple OSS Distributions {
269*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
270*1031c584SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
271*1031c584SApple OSS Distributions 	(void)os_ref_release_raw(&rc, NULL);
272*1031c584SApple OSS Distributions 
273*1031c584SApple OSS Distributions 	int x = setjmp(jb);
274*1031c584SApple OSS Distributions 	if (x == 0) {
275*1031c584SApple OSS Distributions 		expect_panic = true;
276*1031c584SApple OSS Distributions 		(void)os_ref_release_raw(&rc, NULL);
277*1031c584SApple OSS Distributions 		T_FAIL("underflow not caught");
278*1031c584SApple OSS Distributions 	} else {
279*1031c584SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_UNDERFLOW, "underflow caught");
280*1031c584SApple OSS Distributions 	}
281*1031c584SApple OSS Distributions }
282*1031c584SApple OSS Distributions 
283*1031c584SApple OSS Distributions T_DECL(refcnt_overflow, "Overflow")
284*1031c584SApple OSS Distributions {
285*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
286*1031c584SApple OSS Distributions 	os_ref_init_count_raw(&rc, NULL, 0x0fffffffU);
287*1031c584SApple OSS Distributions 
288*1031c584SApple OSS Distributions 	int x = setjmp(jb);
289*1031c584SApple OSS Distributions 	if (x == 0) {
290*1031c584SApple OSS Distributions 		expect_panic = true;
291*1031c584SApple OSS Distributions 		(void)os_ref_retain_raw(&rc, NULL);
292*1031c584SApple OSS Distributions 		T_FAIL("overflow not caught");
293*1031c584SApple OSS Distributions 	} else {
294*1031c584SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_RETAIN, "overflow caught");
295*1031c584SApple OSS Distributions 	}
296*1031c584SApple OSS Distributions }
297*1031c584SApple OSS Distributions 
298*1031c584SApple OSS Distributions T_DECL(refcnt_resurrection, "Resurrection")
299*1031c584SApple OSS Distributions {
300*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
301*1031c584SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
302*1031c584SApple OSS Distributions 	os_ref_count_t n = os_ref_release_raw(&rc, NULL);
303*1031c584SApple OSS Distributions 
304*1031c584SApple OSS Distributions 	T_QUIET; T_EXPECT_EQ_UINT(n, 0, "reference not released");
305*1031c584SApple OSS Distributions 
306*1031c584SApple OSS Distributions 	int x = setjmp(jb);
307*1031c584SApple OSS Distributions 	if (x == 0) {
308*1031c584SApple OSS Distributions 		expect_panic = true;
309*1031c584SApple OSS Distributions 		(void)os_ref_retain_raw(&rc, NULL);
310*1031c584SApple OSS Distributions 		T_FAIL("resurrection not caught");
311*1031c584SApple OSS Distributions 	} else {
312*1031c584SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_RETAIN, "resurrection caught");
313*1031c584SApple OSS Distributions 	}
314*1031c584SApple OSS Distributions }
315*1031c584SApple OSS Distributions 
316*1031c584SApple OSS Distributions T_DECL(refcnt_dealloc_live, "Dealloc expected live object")
317*1031c584SApple OSS Distributions {
318*1031c584SApple OSS Distributions 	os_ref_atomic_t rc;
319*1031c584SApple OSS Distributions 	os_ref_init_raw(&rc, NULL);
320*1031c584SApple OSS Distributions 
321*1031c584SApple OSS Distributions 	expect_panic = true;
322*1031c584SApple OSS Distributions 	int x = setjmp(jb);
323*1031c584SApple OSS Distributions 	if (x == 0) {
324*1031c584SApple OSS Distributions 		expect_panic = true;
325*1031c584SApple OSS Distributions 		os_ref_release_live_raw(&rc, NULL);
326*1031c584SApple OSS Distributions 		T_FAIL("dealloc live not caught");
327*1031c584SApple OSS Distributions 	} else {
328*1031c584SApple OSS Distributions 		T_ASSERT_EQ_INT(x, OSREF_DEALLOC_LIVE, "dealloc live caught");
329*1031c584SApple OSS Distributions 	}
330*1031c584SApple OSS Distributions }
331*1031c584SApple OSS Distributions 
332*1031c584SApple OSS Distributions T_DECL(refcnt_initializer, "Static intializers")
333*1031c584SApple OSS Distributions {
334*1031c584SApple OSS Distributions 	struct os_refcnt rc = OS_REF_INITIALIZER;
335*1031c584SApple OSS Distributions 	os_ref_atomic_t rca = OS_REF_ATOMIC_INITIALIZER;
336*1031c584SApple OSS Distributions 
337*1031c584SApple OSS Distributions 	T_ASSERT_EQ_INT(0, os_ref_retain_try(&rc), NULL);
338*1031c584SApple OSS Distributions 	T_ASSERT_EQ_INT(0, os_ref_get_count_raw(&rca), NULL);
339*1031c584SApple OSS Distributions }
340*1031c584SApple OSS Distributions 
341*1031c584SApple OSS Distributions #if STRESS_TESTS
342*1031c584SApple OSS Distributions 
343*1031c584SApple OSS Distributions static const unsigned long iters = 1024 * 1024 * 32;
344*1031c584SApple OSS Distributions 
345*1031c584SApple OSS Distributions static void *
func(void * _rc)346*1031c584SApple OSS Distributions func(void *_rc)
347*1031c584SApple OSS Distributions {
348*1031c584SApple OSS Distributions 	struct os_refcnt *rc = _rc;
349*1031c584SApple OSS Distributions 	for (unsigned long i = 0; i < iters; i++) {
350*1031c584SApple OSS Distributions 		os_ref_retain(rc);
351*1031c584SApple OSS Distributions 		os_ref_release_live(rc);
352*1031c584SApple OSS Distributions 	}
353*1031c584SApple OSS Distributions 	return NULL;
354*1031c584SApple OSS Distributions }
355*1031c584SApple OSS Distributions 
356*1031c584SApple OSS Distributions T_DECL(refcnt_stress, "Stress test")
357*1031c584SApple OSS Distributions {
358*1031c584SApple OSS Distributions 	pthread_t th1, th2;
359*1031c584SApple OSS Distributions 
360*1031c584SApple OSS Distributions 	struct os_refcnt rc;
361*1031c584SApple OSS Distributions 	os_ref_init(&rc, NULL);
362*1031c584SApple OSS Distributions 
363*1031c584SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&th1, NULL, func, &rc), "pthread_create");
364*1031c584SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_create(&th2, NULL, func, &rc), "pthread_create");
365*1031c584SApple OSS Distributions 
366*1031c584SApple OSS Distributions 	void *r1, *r2;
367*1031c584SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_join(th1, &r1), "pthread_join");
368*1031c584SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(pthread_join(th2, &r2), "pthread_join");
369*1031c584SApple OSS Distributions 
370*1031c584SApple OSS Distributions 	os_ref_count_t x = os_ref_release(&rc);
371*1031c584SApple OSS Distributions 	T_ASSERT_EQ_INT(x, 0, "Consistent refcount");
372*1031c584SApple OSS Distributions }
373*1031c584SApple OSS Distributions 
374*1031c584SApple OSS Distributions #endif
375