1*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
2*bbb1b6f9SApple OSS Distributions #include <darwintest_utils.h>
3*bbb1b6f9SApple OSS Distributions #include <stdio.h>
4*bbb1b6f9SApple OSS Distributions #include <assert.h>
5*bbb1b6f9SApple OSS Distributions #include <setjmp.h>
6*bbb1b6f9SApple OSS Distributions #include <os/tsd.h>
7*bbb1b6f9SApple OSS Distributions
8*bbb1b6f9SApple OSS Distributions #define DEVELOPMENT 1
9*bbb1b6f9SApple OSS Distributions #define DEBUG 0
10*bbb1b6f9SApple OSS Distributions #define XNU_KERNEL_PRIVATE 1
11*bbb1b6f9SApple OSS Distributions
12*bbb1b6f9SApple OSS Distributions #define OS_REFCNT_DEBUG 1
13*bbb1b6f9SApple OSS Distributions #define STRESS_TESTS 0
14*bbb1b6f9SApple OSS Distributions #define __zpercpu
15*bbb1b6f9SApple OSS Distributions
16*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Watomic-implicit-seq-cst"
17*bbb1b6f9SApple OSS Distributions #pragma clang diagnostic ignored "-Wc++98-compat"
18*bbb1b6f9SApple OSS Distributions
19*bbb1b6f9SApple OSS Distributions __abortlike
20*bbb1b6f9SApple OSS Distributions void handle_panic(const char *func, char *str, ...);
21*bbb1b6f9SApple OSS Distributions #define panic(...) handle_panic(__func__, __VA_ARGS__)
22*bbb1b6f9SApple OSS Distributions
23*bbb1b6f9SApple OSS Distributions #define ZPERCPU_STRIDE 128
24*bbb1b6f9SApple OSS Distributions
25*bbb1b6f9SApple OSS Distributions static inline int
zpercpu_count(void)26*bbb1b6f9SApple OSS Distributions zpercpu_count(void)
27*bbb1b6f9SApple OSS Distributions {
28*bbb1b6f9SApple OSS Distributions static int n;
29*bbb1b6f9SApple OSS Distributions if (__improbable(n == 0)) {
30*bbb1b6f9SApple OSS Distributions n = dt_ncpu();
31*bbb1b6f9SApple OSS Distributions }
32*bbb1b6f9SApple OSS Distributions return n;
33*bbb1b6f9SApple OSS Distributions }
34*bbb1b6f9SApple OSS Distributions
35*bbb1b6f9SApple OSS Distributions static inline void
thread_wakeup(void * event)36*bbb1b6f9SApple OSS Distributions thread_wakeup(void *event)
37*bbb1b6f9SApple OSS Distributions {
38*bbb1b6f9SApple OSS Distributions abort();
39*bbb1b6f9SApple OSS Distributions }
40*bbb1b6f9SApple OSS Distributions
41*bbb1b6f9SApple OSS Distributions #define zalloc_percpu(zone, flags) \
42*bbb1b6f9SApple OSS Distributions (uint64_t _Atomic *)calloc((size_t)zpercpu_count(), ZPERCPU_STRIDE)
43*bbb1b6f9SApple OSS Distributions
44*bbb1b6f9SApple OSS Distributions #define zfree_percpu(zone, ptr) \
45*bbb1b6f9SApple OSS Distributions free(ptr)
46*bbb1b6f9SApple OSS Distributions
47*bbb1b6f9SApple OSS Distributions static inline uint64_t _Atomic *
zpercpu_get_cpu(uint64_t _Atomic * ptr,int cpu)48*bbb1b6f9SApple OSS Distributions zpercpu_get_cpu(uint64_t _Atomic *ptr, int cpu)
49*bbb1b6f9SApple OSS Distributions {
50*bbb1b6f9SApple OSS Distributions return (uint64_t _Atomic *)((uintptr_t)ptr + (uintptr_t)cpu * ZPERCPU_STRIDE);
51*bbb1b6f9SApple OSS Distributions }
52*bbb1b6f9SApple OSS Distributions
53*bbb1b6f9SApple OSS Distributions #define zpercpu_get(ptr) zpercpu_get_cpu(ptr, 0)
54*bbb1b6f9SApple OSS Distributions
55*bbb1b6f9SApple OSS Distributions #define zpercpu_foreach_cpu(cpu) \
56*bbb1b6f9SApple OSS Distributions for (int cpu = 0, __n = zpercpu_count(); cpu < __n; cpu++)
57*bbb1b6f9SApple OSS Distributions
58*bbb1b6f9SApple OSS Distributions #define zpercpu_foreach(cpu) \
59*bbb1b6f9SApple OSS Distributions for (int cpu = 0, __n = zpercpu_count(); cpu < __n; cpu++)
60*bbb1b6f9SApple OSS Distributions
61*bbb1b6f9SApple OSS Distributions #define cpu_number() (int)_os_cpu_number()
62*bbb1b6f9SApple OSS Distributions
63*bbb1b6f9SApple OSS Distributions #include "../libkern/os/refcnt.h"
64*bbb1b6f9SApple OSS Distributions #include "../libkern/os/refcnt.c"
65*bbb1b6f9SApple OSS Distributions
66*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
67*bbb1b6f9SApple OSS Distributions
68*bbb1b6f9SApple OSS Distributions /* import some of the refcnt internal state for testing */
69*bbb1b6f9SApple OSS Distributions extern bool ref_debug_enable;
70*bbb1b6f9SApple OSS Distributions os_refgrp_decl_extern(global_ref_group);
71*bbb1b6f9SApple OSS Distributions
72*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
73*bbb1b6f9SApple OSS Distributions T_META_NAMESPACE("os_refcnt"),
74*bbb1b6f9SApple OSS Distributions T_META_CHECK_LEAKS(false)
75*bbb1b6f9SApple OSS Distributions );
76*bbb1b6f9SApple OSS Distributions
77*bbb1b6f9SApple OSS Distributions T_DECL(os_refcnt, "Basic atomic refcount")
78*bbb1b6f9SApple OSS Distributions {
79*bbb1b6f9SApple OSS Distributions struct os_refcnt rc;
80*bbb1b6f9SApple OSS Distributions os_ref_init(&rc, NULL);
81*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "refcount correctly initialized");
82*bbb1b6f9SApple OSS Distributions
83*bbb1b6f9SApple OSS Distributions os_ref_retain(&rc);
84*bbb1b6f9SApple OSS Distributions os_ref_retain(&rc);
85*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
86*bbb1b6f9SApple OSS Distributions
87*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release(&rc);
88*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
89*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
90*bbb1b6f9SApple OSS Distributions
91*bbb1b6f9SApple OSS Distributions os_ref_release_live(&rc);
92*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 1, "release_live decreased count");
93*bbb1b6f9SApple OSS Distributions
94*bbb1b6f9SApple OSS Distributions x = os_ref_release(&rc);
95*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
96*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
97*bbb1b6f9SApple OSS Distributions
98*bbb1b6f9SApple OSS Distributions os_ref_init(&rc, NULL);
99*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(os_ref_retain_try(&rc), "try retained");
100*bbb1b6f9SApple OSS Distributions
101*bbb1b6f9SApple OSS Distributions (void)os_ref_release(&rc);
102*bbb1b6f9SApple OSS Distributions (void)os_ref_release(&rc);
103*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "release");
104*bbb1b6f9SApple OSS Distributions
105*bbb1b6f9SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try(&rc), "try failed");
106*bbb1b6f9SApple OSS Distributions }
107*bbb1b6f9SApple OSS Distributions
108*bbb1b6f9SApple OSS Distributions T_DECL(os_pcpu_refcnt, "Basic atomic refcount")
109*bbb1b6f9SApple OSS Distributions {
110*bbb1b6f9SApple OSS Distributions dispatch_queue_t rq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
111*bbb1b6f9SApple OSS Distributions dispatch_group_t g = dispatch_group_create();
112*bbb1b6f9SApple OSS Distributions os_pcpu_ref_t rc;
113*bbb1b6f9SApple OSS Distributions
114*bbb1b6f9SApple OSS Distributions os_pcpu_ref_init(&rc, NULL);
115*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), OS_REFCNT_MAX_COUNT,
116*bbb1b6f9SApple OSS Distributions "refcount correctly initialized");
117*bbb1b6f9SApple OSS Distributions
118*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
119*bbb1b6f9SApple OSS Distributions os_pcpu_ref_retain(rc, NULL);
120*bbb1b6f9SApple OSS Distributions });
121*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
122*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(os_pcpu_ref_retain_try(rc, NULL), "try succeeded");
123*bbb1b6f9SApple OSS Distributions });
124*bbb1b6f9SApple OSS Distributions dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
125*bbb1b6f9SApple OSS Distributions
126*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), OS_REFCNT_MAX_COUNT,
127*bbb1b6f9SApple OSS Distributions "retain increased count");
128*bbb1b6f9SApple OSS Distributions
129*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_kill(rc, NULL), 2,
130*bbb1b6f9SApple OSS Distributions "kill decreased count");
131*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), 2,
132*bbb1b6f9SApple OSS Distributions "kill decreased count");
133*bbb1b6f9SApple OSS Distributions
134*bbb1b6f9SApple OSS Distributions T_ASSERT_FALSE(os_pcpu_ref_retain_try(rc, NULL), "try failed");
135*bbb1b6f9SApple OSS Distributions
136*bbb1b6f9SApple OSS Distributions os_pcpu_ref_release_live(rc, NULL);
137*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), 1, "release_live decreased count");
138*bbb1b6f9SApple OSS Distributions
139*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_release(rc, NULL), 0, "returned released");
140*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_pcpu_ref_count(rc), 0, "released");
141*bbb1b6f9SApple OSS Distributions
142*bbb1b6f9SApple OSS Distributions os_pcpu_ref_destroy(&rc, NULL);
143*bbb1b6f9SApple OSS Distributions }
144*bbb1b6f9SApple OSS Distributions
145*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_raw, "Raw refcount")
146*bbb1b6f9SApple OSS Distributions {
147*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
148*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
149*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "refcount correctly initialized");
150*bbb1b6f9SApple OSS Distributions
151*bbb1b6f9SApple OSS Distributions os_ref_retain_raw(&rc, NULL);
152*bbb1b6f9SApple OSS Distributions os_ref_retain_raw(&rc, NULL);
153*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
154*bbb1b6f9SApple OSS Distributions
155*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release_raw(&rc, NULL);
156*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
157*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
158*bbb1b6f9SApple OSS Distributions
159*bbb1b6f9SApple OSS Distributions os_ref_release_live_raw(&rc, NULL);
160*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 1, "release_live decreased count");
161*bbb1b6f9SApple OSS Distributions
162*bbb1b6f9SApple OSS Distributions x = os_ref_release_raw(&rc, NULL);
163*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
164*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
165*bbb1b6f9SApple OSS Distributions
166*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
167*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(os_ref_retain_try_raw(&rc, NULL), "try retained");
168*bbb1b6f9SApple OSS Distributions
169*bbb1b6f9SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
170*bbb1b6f9SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
171*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "release");
172*bbb1b6f9SApple OSS Distributions
173*bbb1b6f9SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try_raw(&rc, NULL), "try failed");
174*bbb1b6f9SApple OSS Distributions }
175*bbb1b6f9SApple OSS Distributions
176*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_locked, "Locked refcount")
177*bbb1b6f9SApple OSS Distributions {
178*bbb1b6f9SApple OSS Distributions struct os_refcnt rc;
179*bbb1b6f9SApple OSS Distributions os_ref_init(&rc, NULL);
180*bbb1b6f9SApple OSS Distributions
181*bbb1b6f9SApple OSS Distributions os_ref_retain_locked(&rc);
182*bbb1b6f9SApple OSS Distributions os_ref_retain_locked(&rc);
183*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 3, "retain increased count");
184*bbb1b6f9SApple OSS Distributions
185*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release_locked(&rc);
186*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 2, "release decreased count");
187*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
188*bbb1b6f9SApple OSS Distributions
189*bbb1b6f9SApple OSS Distributions (void)os_ref_release_locked(&rc);
190*bbb1b6f9SApple OSS Distributions x = os_ref_release_locked(&rc);
191*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count(&rc), 0, "released");
192*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
193*bbb1b6f9SApple OSS Distributions }
194*bbb1b6f9SApple OSS Distributions
195*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_raw_locked, "Locked raw refcount")
196*bbb1b6f9SApple OSS Distributions {
197*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
198*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
199*bbb1b6f9SApple OSS Distributions
200*bbb1b6f9SApple OSS Distributions os_ref_retain_locked_raw(&rc, NULL);
201*bbb1b6f9SApple OSS Distributions os_ref_retain_locked_raw(&rc, NULL);
202*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 3, "retain increased count");
203*bbb1b6f9SApple OSS Distributions
204*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release_locked_raw(&rc, NULL);
205*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 2, "release decreased count");
206*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
207*bbb1b6f9SApple OSS Distributions
208*bbb1b6f9SApple OSS Distributions (void)os_ref_release_locked_raw(&rc, NULL);
209*bbb1b6f9SApple OSS Distributions x = os_ref_release_locked_raw(&rc, NULL);
210*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_raw(&rc), 0, "released");
211*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
212*bbb1b6f9SApple OSS Distributions }
213*bbb1b6f9SApple OSS Distributions
214*bbb1b6f9SApple OSS Distributions static void
do_bitwise_test(const os_ref_count_t bits)215*bbb1b6f9SApple OSS Distributions do_bitwise_test(const os_ref_count_t bits)
216*bbb1b6f9SApple OSS Distributions {
217*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
218*bbb1b6f9SApple OSS Distributions os_ref_count_t reserved = 0xaaaaaaaaU & ((1U << bits) - 1);
219*bbb1b6f9SApple OSS Distributions
220*bbb1b6f9SApple OSS Distributions T_LOG("do_bitwise_test(nbits:%d, reserved:%#x)", bits, reserved);
221*bbb1b6f9SApple OSS Distributions
222*bbb1b6f9SApple OSS Distributions os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
223*bbb1b6f9SApple OSS Distributions
224*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "[%u bits] refcount initialized", bits);
225*bbb1b6f9SApple OSS Distributions
226*bbb1b6f9SApple OSS Distributions os_ref_retain_mask(&rc, bits, NULL);
227*bbb1b6f9SApple OSS Distributions os_ref_retain_mask(&rc, bits, NULL);
228*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 3, "retain increased count");
229*bbb1b6f9SApple OSS Distributions
230*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release_mask(&rc, bits, NULL);
231*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 2, "release returned correct count");
232*bbb1b6f9SApple OSS Distributions
233*bbb1b6f9SApple OSS Distributions os_ref_release_live_mask(&rc, bits, NULL);
234*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 1, "release_live decreased count");
235*bbb1b6f9SApple OSS Distributions
236*bbb1b6f9SApple OSS Distributions x = os_ref_release_mask(&rc, bits, NULL);
237*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "released");
238*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(x, 0, "returned released");
239*bbb1b6f9SApple OSS Distributions
240*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(rc & ((1U << bits) - 1), reserved, "Reserved bits not modified");
241*bbb1b6f9SApple OSS Distributions
242*bbb1b6f9SApple OSS Distributions os_ref_init_count_mask(&rc, bits, NULL, 1, reserved);
243*bbb1b6f9SApple OSS Distributions T_ASSERT_TRUE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try retained");
244*bbb1b6f9SApple OSS Distributions if (reserved) {
245*bbb1b6f9SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, reserved, NULL), "try reject");
246*bbb1b6f9SApple OSS Distributions }
247*bbb1b6f9SApple OSS Distributions
248*bbb1b6f9SApple OSS Distributions (void)os_ref_release_mask(&rc, bits, NULL);
249*bbb1b6f9SApple OSS Distributions (void)os_ref_release_mask(&rc, bits, NULL);
250*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(os_ref_get_count_mask(&rc, bits), 0, "release");
251*bbb1b6f9SApple OSS Distributions
252*bbb1b6f9SApple OSS Distributions T_ASSERT_FALSE(os_ref_retain_try_mask(&rc, bits, 0, NULL), "try fail");
253*bbb1b6f9SApple OSS Distributions
254*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(os_ref_get_bits_mask(&rc, bits), reserved, "Reserved bits not modified");
255*bbb1b6f9SApple OSS Distributions }
256*bbb1b6f9SApple OSS Distributions
257*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_bitwise, "Bitwise refcount")
258*bbb1b6f9SApple OSS Distributions {
259*bbb1b6f9SApple OSS Distributions do_bitwise_test(0);
260*bbb1b6f9SApple OSS Distributions do_bitwise_test(1);
261*bbb1b6f9SApple OSS Distributions do_bitwise_test(8);
262*bbb1b6f9SApple OSS Distributions do_bitwise_test(26);
263*bbb1b6f9SApple OSS Distributions
264*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc = 0xaaaaaaaa;
265*bbb1b6f9SApple OSS Distributions
266*bbb1b6f9SApple OSS Distributions const os_ref_count_t nbits = 3;
267*bbb1b6f9SApple OSS Distributions const os_ref_count_t count = 5;
268*bbb1b6f9SApple OSS Distributions const os_ref_count_t bits = 7;
269*bbb1b6f9SApple OSS Distributions os_ref_init_count_mask(&rc, nbits, NULL, count, bits);
270*bbb1b6f9SApple OSS Distributions
271*bbb1b6f9SApple OSS Distributions os_ref_count_t mask = (1U << nbits) - 1;
272*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(rc & mask, bits, "bits correctly initialized");
273*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(rc >> nbits, count, "count correctly initialized");
274*bbb1b6f9SApple OSS Distributions }
275*bbb1b6f9SApple OSS Distributions
276*bbb1b6f9SApple OSS Distributions os_refgrp_decl(static, g1, "test group", NULL);
277*bbb1b6f9SApple OSS Distributions os_refgrp_decl_extern(g1);
278*bbb1b6f9SApple OSS Distributions
279*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_groups, "Group accounting")
280*bbb1b6f9SApple OSS Distributions {
281*bbb1b6f9SApple OSS Distributions #if OS_REFCNT_DEBUG
282*bbb1b6f9SApple OSS Distributions ref_debug_enable = true;
283*bbb1b6f9SApple OSS Distributions
284*bbb1b6f9SApple OSS Distributions struct os_refcnt rc;
285*bbb1b6f9SApple OSS Distributions os_ref_init(&rc, &g1);
286*bbb1b6f9SApple OSS Distributions
287*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_children, 1, "group attached");
288*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(global_ref_group.grp_children, 1, "global group attached");
289*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_count, 1, "group count");
290*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_ULLONG(g1.grp_retain_total, 1ULL, "group retains");
291*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_ULLONG(g1.grp_release_total, 0ULL, "group releases");
292*bbb1b6f9SApple OSS Distributions
293*bbb1b6f9SApple OSS Distributions os_ref_retain(&rc);
294*bbb1b6f9SApple OSS Distributions os_ref_retain(&rc);
295*bbb1b6f9SApple OSS Distributions os_ref_release_live(&rc);
296*bbb1b6f9SApple OSS Distributions os_ref_release_live(&rc);
297*bbb1b6f9SApple OSS Distributions
298*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ_ULLONG(g1.grp_retain_total, 3ULL, "group retains");
299*bbb1b6f9SApple OSS Distributions T_EXPECT_EQ_ULLONG(g1.grp_release_total, 2ULL, "group releases");
300*bbb1b6f9SApple OSS Distributions
301*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release(&rc);
302*bbb1b6f9SApple OSS Distributions T_QUIET; T_ASSERT_EQ_UINT(x, 0, "released");
303*bbb1b6f9SApple OSS Distributions
304*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_children, 0, "group detatched");
305*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_UINT(g1.grp_count, 0, "group count");
306*bbb1b6f9SApple OSS Distributions #else
307*bbb1b6f9SApple OSS Distributions T_SKIP("Refcount debugging disabled");
308*bbb1b6f9SApple OSS Distributions #endif
309*bbb1b6f9SApple OSS Distributions }
310*bbb1b6f9SApple OSS Distributions
311*bbb1b6f9SApple OSS Distributions enum {
312*bbb1b6f9SApple OSS Distributions OSREF_UNDERFLOW = 1,
313*bbb1b6f9SApple OSS Distributions OSREF_OVERFLOW = 2,
314*bbb1b6f9SApple OSS Distributions OSREF_RETAIN = 3,
315*bbb1b6f9SApple OSS Distributions OSREF_DEALLOC_LIVE = 4,
316*bbb1b6f9SApple OSS Distributions };
317*bbb1b6f9SApple OSS Distributions
318*bbb1b6f9SApple OSS Distributions static jmp_buf jb;
319*bbb1b6f9SApple OSS Distributions static bool expect_panic = false;
320*bbb1b6f9SApple OSS Distributions
321*bbb1b6f9SApple OSS Distributions void
handle_panic(const char * func,char * __unused str,...)322*bbb1b6f9SApple OSS Distributions handle_panic(const char *func, char *__unused str, ...)
323*bbb1b6f9SApple OSS Distributions {
324*bbb1b6f9SApple OSS Distributions int ret = -1;
325*bbb1b6f9SApple OSS Distributions if (!expect_panic) {
326*bbb1b6f9SApple OSS Distributions T_FAIL("unexpected panic from %s", func);
327*bbb1b6f9SApple OSS Distributions T_LOG("corrupt program state, aborting");
328*bbb1b6f9SApple OSS Distributions abort();
329*bbb1b6f9SApple OSS Distributions }
330*bbb1b6f9SApple OSS Distributions expect_panic = false;
331*bbb1b6f9SApple OSS Distributions
332*bbb1b6f9SApple OSS Distributions if (strcmp(func, "os_ref_panic_underflow") == 0) {
333*bbb1b6f9SApple OSS Distributions ret = OSREF_UNDERFLOW;
334*bbb1b6f9SApple OSS Distributions } else if (strcmp(func, "os_ref_panic_overflow") == 0) {
335*bbb1b6f9SApple OSS Distributions ret = OSREF_OVERFLOW;
336*bbb1b6f9SApple OSS Distributions } else if (strcmp(func, "os_ref_panic_retain") == 0) {
337*bbb1b6f9SApple OSS Distributions ret = OSREF_RETAIN;
338*bbb1b6f9SApple OSS Distributions } else if (strcmp(func, "os_ref_panic_live") == 0) {
339*bbb1b6f9SApple OSS Distributions ret = OSREF_DEALLOC_LIVE;
340*bbb1b6f9SApple OSS Distributions } else {
341*bbb1b6f9SApple OSS Distributions T_LOG("unexpected panic from %s", func);
342*bbb1b6f9SApple OSS Distributions }
343*bbb1b6f9SApple OSS Distributions
344*bbb1b6f9SApple OSS Distributions longjmp(jb, ret);
345*bbb1b6f9SApple OSS Distributions }
346*bbb1b6f9SApple OSS Distributions
347*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_underflow, "Underflow")
348*bbb1b6f9SApple OSS Distributions {
349*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
350*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
351*bbb1b6f9SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
352*bbb1b6f9SApple OSS Distributions
353*bbb1b6f9SApple OSS Distributions int x = setjmp(jb);
354*bbb1b6f9SApple OSS Distributions if (x == 0) {
355*bbb1b6f9SApple OSS Distributions expect_panic = true;
356*bbb1b6f9SApple OSS Distributions (void)os_ref_release_raw(&rc, NULL);
357*bbb1b6f9SApple OSS Distributions T_FAIL("underflow not caught");
358*bbb1b6f9SApple OSS Distributions } else {
359*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_UNDERFLOW, "underflow caught");
360*bbb1b6f9SApple OSS Distributions }
361*bbb1b6f9SApple OSS Distributions }
362*bbb1b6f9SApple OSS Distributions
363*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_overflow, "Overflow")
364*bbb1b6f9SApple OSS Distributions {
365*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
366*bbb1b6f9SApple OSS Distributions os_ref_init_count_raw(&rc, NULL, 0x0fffffffU);
367*bbb1b6f9SApple OSS Distributions
368*bbb1b6f9SApple OSS Distributions int x = setjmp(jb);
369*bbb1b6f9SApple OSS Distributions if (x == 0) {
370*bbb1b6f9SApple OSS Distributions expect_panic = true;
371*bbb1b6f9SApple OSS Distributions (void)os_ref_retain_raw(&rc, NULL);
372*bbb1b6f9SApple OSS Distributions T_FAIL("overflow not caught");
373*bbb1b6f9SApple OSS Distributions } else {
374*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_OVERFLOW, "overflow caught");
375*bbb1b6f9SApple OSS Distributions }
376*bbb1b6f9SApple OSS Distributions }
377*bbb1b6f9SApple OSS Distributions
378*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_resurrection, "Resurrection")
379*bbb1b6f9SApple OSS Distributions {
380*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
381*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
382*bbb1b6f9SApple OSS Distributions os_ref_count_t n = os_ref_release_raw(&rc, NULL);
383*bbb1b6f9SApple OSS Distributions
384*bbb1b6f9SApple OSS Distributions T_QUIET; T_EXPECT_EQ_UINT(n, 0, "reference not released");
385*bbb1b6f9SApple OSS Distributions
386*bbb1b6f9SApple OSS Distributions int x = setjmp(jb);
387*bbb1b6f9SApple OSS Distributions if (x == 0) {
388*bbb1b6f9SApple OSS Distributions expect_panic = true;
389*bbb1b6f9SApple OSS Distributions (void)os_ref_retain_raw(&rc, NULL);
390*bbb1b6f9SApple OSS Distributions T_FAIL("resurrection not caught");
391*bbb1b6f9SApple OSS Distributions } else {
392*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_RETAIN, "resurrection caught");
393*bbb1b6f9SApple OSS Distributions }
394*bbb1b6f9SApple OSS Distributions }
395*bbb1b6f9SApple OSS Distributions
396*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_dealloc_live, "Dealloc expected live object")
397*bbb1b6f9SApple OSS Distributions {
398*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
399*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
400*bbb1b6f9SApple OSS Distributions
401*bbb1b6f9SApple OSS Distributions expect_panic = true;
402*bbb1b6f9SApple OSS Distributions int x = setjmp(jb);
403*bbb1b6f9SApple OSS Distributions if (x == 0) {
404*bbb1b6f9SApple OSS Distributions expect_panic = true;
405*bbb1b6f9SApple OSS Distributions os_ref_release_live_raw(&rc, NULL);
406*bbb1b6f9SApple OSS Distributions T_FAIL("dealloc live not caught");
407*bbb1b6f9SApple OSS Distributions } else {
408*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(x, OSREF_DEALLOC_LIVE, "dealloc live caught");
409*bbb1b6f9SApple OSS Distributions }
410*bbb1b6f9SApple OSS Distributions }
411*bbb1b6f9SApple OSS Distributions
412*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_initializer, "Static intializers")
413*bbb1b6f9SApple OSS Distributions {
414*bbb1b6f9SApple OSS Distributions struct os_refcnt rc = OS_REF_INITIALIZER;
415*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rca = OS_REF_ATOMIC_INITIALIZER;
416*bbb1b6f9SApple OSS Distributions
417*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(0, os_ref_retain_try(&rc), NULL);
418*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(0, os_ref_get_count_raw(&rca), NULL);
419*bbb1b6f9SApple OSS Distributions }
420*bbb1b6f9SApple OSS Distributions
421*bbb1b6f9SApple OSS Distributions #if STRESS_TESTS
422*bbb1b6f9SApple OSS Distributions
423*bbb1b6f9SApple OSS Distributions static unsigned pcpu_perf_step = 0;
424*bbb1b6f9SApple OSS Distributions
425*bbb1b6f9SApple OSS Distributions static void
worker_ref(os_ref_atomic_t * rc,unsigned long * count)426*bbb1b6f9SApple OSS Distributions worker_ref(os_ref_atomic_t *rc, unsigned long *count)
427*bbb1b6f9SApple OSS Distributions {
428*bbb1b6f9SApple OSS Distributions unsigned long n = 0;
429*bbb1b6f9SApple OSS Distributions
430*bbb1b6f9SApple OSS Distributions while (os_atomic_load(&pcpu_perf_step, relaxed) == 0) {
431*bbb1b6f9SApple OSS Distributions }
432*bbb1b6f9SApple OSS Distributions
433*bbb1b6f9SApple OSS Distributions while (os_atomic_load(&pcpu_perf_step, relaxed) == 1) {
434*bbb1b6f9SApple OSS Distributions os_ref_retain_raw(rc, NULL);
435*bbb1b6f9SApple OSS Distributions os_ref_release_live_raw(rc, NULL);
436*bbb1b6f9SApple OSS Distributions n++;
437*bbb1b6f9SApple OSS Distributions }
438*bbb1b6f9SApple OSS Distributions
439*bbb1b6f9SApple OSS Distributions os_atomic_add(count, n, relaxed);
440*bbb1b6f9SApple OSS Distributions }
441*bbb1b6f9SApple OSS Distributions
442*bbb1b6f9SApple OSS Distributions static void
worker_pcpu_ref(os_pcpu_ref_t rc,unsigned long * count)443*bbb1b6f9SApple OSS Distributions worker_pcpu_ref(os_pcpu_ref_t rc, unsigned long *count)
444*bbb1b6f9SApple OSS Distributions {
445*bbb1b6f9SApple OSS Distributions unsigned long n = 0;
446*bbb1b6f9SApple OSS Distributions
447*bbb1b6f9SApple OSS Distributions while (os_atomic_load(&pcpu_perf_step, relaxed) == 0) {
448*bbb1b6f9SApple OSS Distributions }
449*bbb1b6f9SApple OSS Distributions
450*bbb1b6f9SApple OSS Distributions while (os_atomic_load(&pcpu_perf_step, relaxed) == 1) {
451*bbb1b6f9SApple OSS Distributions os_pcpu_ref_retain(rc, NULL);
452*bbb1b6f9SApple OSS Distributions os_pcpu_ref_release_live(rc, NULL);
453*bbb1b6f9SApple OSS Distributions n++;
454*bbb1b6f9SApple OSS Distributions }
455*bbb1b6f9SApple OSS Distributions
456*bbb1b6f9SApple OSS Distributions os_atomic_add(count, n, relaxed);
457*bbb1b6f9SApple OSS Distributions }
458*bbb1b6f9SApple OSS Distributions
459*bbb1b6f9SApple OSS Distributions #define PCPU_BENCH_LEN 2
460*bbb1b6f9SApple OSS Distributions
461*bbb1b6f9SApple OSS Distributions static void
warmup_thread_pool(dispatch_group_t g,dispatch_queue_t rq)462*bbb1b6f9SApple OSS Distributions warmup_thread_pool(dispatch_group_t g, dispatch_queue_t rq)
463*bbb1b6f9SApple OSS Distributions {
464*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 1, relaxed);
465*bbb1b6f9SApple OSS Distributions
466*bbb1b6f9SApple OSS Distributions zpercpu_foreach_cpu(cpu) {
467*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
468*bbb1b6f9SApple OSS Distributions while (os_atomic_load(&pcpu_perf_step, relaxed) == 1) {
469*bbb1b6f9SApple OSS Distributions }
470*bbb1b6f9SApple OSS Distributions });
471*bbb1b6f9SApple OSS Distributions }
472*bbb1b6f9SApple OSS Distributions
473*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 0, relaxed);
474*bbb1b6f9SApple OSS Distributions dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
475*bbb1b6f9SApple OSS Distributions }
476*bbb1b6f9SApple OSS Distributions
477*bbb1b6f9SApple OSS Distributions T_DECL(pcpu_perf, "Performance per-cpu")
478*bbb1b6f9SApple OSS Distributions {
479*bbb1b6f9SApple OSS Distributions os_ref_atomic_t rc;
480*bbb1b6f9SApple OSS Distributions os_pcpu_ref_t prc;
481*bbb1b6f9SApple OSS Distributions __block unsigned long count = 0;
482*bbb1b6f9SApple OSS Distributions double scale = PCPU_BENCH_LEN * 1e6;
483*bbb1b6f9SApple OSS Distributions dispatch_queue_t rq = dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0);
484*bbb1b6f9SApple OSS Distributions dispatch_group_t g = dispatch_group_create();
485*bbb1b6f9SApple OSS Distributions
486*bbb1b6f9SApple OSS Distributions os_ref_init_raw(&rc, NULL);
487*bbb1b6f9SApple OSS Distributions os_pcpu_ref_init(&prc, NULL);
488*bbb1b6f9SApple OSS Distributions
489*bbb1b6f9SApple OSS Distributions T_LOG("uncontended benchmark");
490*bbb1b6f9SApple OSS Distributions
491*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
492*bbb1b6f9SApple OSS Distributions worker_ref(&rc, &count);
493*bbb1b6f9SApple OSS Distributions });
494*bbb1b6f9SApple OSS Distributions
495*bbb1b6f9SApple OSS Distributions count = 0;
496*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 1, relaxed);
497*bbb1b6f9SApple OSS Distributions sleep(PCPU_BENCH_LEN);
498*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 0, relaxed);
499*bbb1b6f9SApple OSS Distributions dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
500*bbb1b6f9SApple OSS Distributions
501*bbb1b6f9SApple OSS Distributions T_PASS("%.2fM rounds per thread per second (atomic)", count / scale);
502*bbb1b6f9SApple OSS Distributions
503*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
504*bbb1b6f9SApple OSS Distributions worker_pcpu_ref(prc, &count);
505*bbb1b6f9SApple OSS Distributions });
506*bbb1b6f9SApple OSS Distributions
507*bbb1b6f9SApple OSS Distributions count = 0;
508*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 1, relaxed);
509*bbb1b6f9SApple OSS Distributions sleep(PCPU_BENCH_LEN);
510*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 0, relaxed);
511*bbb1b6f9SApple OSS Distributions dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
512*bbb1b6f9SApple OSS Distributions
513*bbb1b6f9SApple OSS Distributions T_PASS("%.2fM rounds per thread per second (pcpu)", count / scale);
514*bbb1b6f9SApple OSS Distributions
515*bbb1b6f9SApple OSS Distributions T_LOG("contended benchmark");
516*bbb1b6f9SApple OSS Distributions
517*bbb1b6f9SApple OSS Distributions warmup_thread_pool(g, rq);
zpercpu_foreach_cpu(cpu)518*bbb1b6f9SApple OSS Distributions zpercpu_foreach_cpu(cpu) {
519*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
520*bbb1b6f9SApple OSS Distributions worker_ref(&rc, &count);
521*bbb1b6f9SApple OSS Distributions });
522*bbb1b6f9SApple OSS Distributions }
523*bbb1b6f9SApple OSS Distributions
524*bbb1b6f9SApple OSS Distributions count = 0;
525*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 1, relaxed);
526*bbb1b6f9SApple OSS Distributions sleep(PCPU_BENCH_LEN);
527*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 0, relaxed);
528*bbb1b6f9SApple OSS Distributions dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
529*bbb1b6f9SApple OSS Distributions
530*bbb1b6f9SApple OSS Distributions T_PASS("%.2fM rounds per thread per second (atomic)", count / (zpercpu_count() * scale));
531*bbb1b6f9SApple OSS Distributions
532*bbb1b6f9SApple OSS Distributions warmup_thread_pool(g, rq);
zpercpu_foreach_cpu(cpu)533*bbb1b6f9SApple OSS Distributions zpercpu_foreach_cpu(cpu) {
534*bbb1b6f9SApple OSS Distributions dispatch_group_async(g, rq, ^{
535*bbb1b6f9SApple OSS Distributions worker_pcpu_ref(prc, &count);
536*bbb1b6f9SApple OSS Distributions });
537*bbb1b6f9SApple OSS Distributions }
538*bbb1b6f9SApple OSS Distributions
539*bbb1b6f9SApple OSS Distributions count = 0;
540*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 1, relaxed);
541*bbb1b6f9SApple OSS Distributions sleep(PCPU_BENCH_LEN);
542*bbb1b6f9SApple OSS Distributions os_atomic_store(&pcpu_perf_step, 0, relaxed);
543*bbb1b6f9SApple OSS Distributions dispatch_group_wait(g, DISPATCH_TIME_FOREVER);
544*bbb1b6f9SApple OSS Distributions
545*bbb1b6f9SApple OSS Distributions T_PASS("%.2fM rounds per thread per second (pcpu)", count / (zpercpu_count() * scale));
546*bbb1b6f9SApple OSS Distributions
547*bbb1b6f9SApple OSS Distributions (void)os_pcpu_ref_kill(prc, NULL);
548*bbb1b6f9SApple OSS Distributions os_pcpu_ref_destroy(&prc, NULL);
549*bbb1b6f9SApple OSS Distributions }
550*bbb1b6f9SApple OSS Distributions
551*bbb1b6f9SApple OSS Distributions static const unsigned long iters = 1024 * 1024 * 32;
552*bbb1b6f9SApple OSS Distributions
553*bbb1b6f9SApple OSS Distributions static void *
func(void * _rc)554*bbb1b6f9SApple OSS Distributions func(void *_rc)
555*bbb1b6f9SApple OSS Distributions {
556*bbb1b6f9SApple OSS Distributions struct os_refcnt *rc = _rc;
557*bbb1b6f9SApple OSS Distributions for (unsigned long i = 0; i < iters; i++) {
558*bbb1b6f9SApple OSS Distributions os_ref_retain(rc);
559*bbb1b6f9SApple OSS Distributions os_ref_release_live(rc);
560*bbb1b6f9SApple OSS Distributions }
561*bbb1b6f9SApple OSS Distributions return NULL;
562*bbb1b6f9SApple OSS Distributions }
563*bbb1b6f9SApple OSS Distributions
564*bbb1b6f9SApple OSS Distributions T_DECL(refcnt_stress, "Stress test")
565*bbb1b6f9SApple OSS Distributions {
566*bbb1b6f9SApple OSS Distributions pthread_t th1, th2;
567*bbb1b6f9SApple OSS Distributions
568*bbb1b6f9SApple OSS Distributions struct os_refcnt rc;
569*bbb1b6f9SApple OSS Distributions os_ref_init(&rc, NULL);
570*bbb1b6f9SApple OSS Distributions
571*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&th1, NULL, func, &rc), "pthread_create");
572*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_create(&th2, NULL, func, &rc), "pthread_create");
573*bbb1b6f9SApple OSS Distributions
574*bbb1b6f9SApple OSS Distributions void *r1, *r2;
575*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(th1, &r1), "pthread_join");
576*bbb1b6f9SApple OSS Distributions T_ASSERT_POSIX_ZERO(pthread_join(th2, &r2), "pthread_join");
577*bbb1b6f9SApple OSS Distributions
578*bbb1b6f9SApple OSS Distributions os_ref_count_t x = os_ref_release(&rc);
579*bbb1b6f9SApple OSS Distributions T_ASSERT_EQ_INT(x, 0, "Consistent refcount");
580*bbb1b6f9SApple OSS Distributions }
581*bbb1b6f9SApple OSS Distributions
582*bbb1b6f9SApple OSS Distributions #endif
583