xref: /xnu-8020.140.41/libkern/os/refcnt.h (revision 27b03b360a988dfd3dfdf34262bb0042026747cc)
1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions  * Copyright (c) 2017 Apple Inc. All rights reserved.
3*27b03b36SApple OSS Distributions  *
4*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions  *
6*27b03b36SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions  *
15*27b03b36SApple OSS Distributions  * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions  *
18*27b03b36SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions  * limitations under the License.
25*27b03b36SApple OSS Distributions  *
26*27b03b36SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions  */
28*27b03b36SApple OSS Distributions 
29*27b03b36SApple OSS Distributions #ifndef _OS_REFCNT_H_
30*27b03b36SApple OSS Distributions #define _OS_REFCNT_H_
31*27b03b36SApple OSS Distributions 
32*27b03b36SApple OSS Distributions /*
33*27b03b36SApple OSS Distributions  * os_refcnt reference counting API
34*27b03b36SApple OSS Distributions  *
35*27b03b36SApple OSS Distributions  * Two flavors are provided: atomic and locked. Atomic internally uses C11 atomic
36*27b03b36SApple OSS Distributions  * operations and requires no external synchronization, whereas the locked flavor
37*27b03b36SApple OSS Distributions  * assumes the refcnt object is locked by the caller. It is NOT safe to
38*27b03b36SApple OSS Distributions  * mix-and-match locked and atomic calls.
39*27b03b36SApple OSS Distributions  *
40*27b03b36SApple OSS Distributions  * 'refgrp's are a way to (hierarchically) group like refcount objects for
41*27b03b36SApple OSS Distributions  * debugging purposes. The group keeps track of the total number and aggregate
42*27b03b36SApple OSS Distributions  * reference count of member refcounts, and the "rlog=" boot-arg is used to enable
43*27b03b36SApple OSS Distributions  * refcount logging by group name. Named groups can be created explicitly with
44*27b03b36SApple OSS Distributions  * os_refgrp_decl(), or implicitly by passing NULL for the refgrp when
45*27b03b36SApple OSS Distributions  * initializing a refcnt object. In the latter case, the group name is the same as
46*27b03b36SApple OSS Distributions  * the function enclosing the init call. Groups are only available on DEV or DEBUG
47*27b03b36SApple OSS Distributions  * builds, and are otherwise compiled out.
48*27b03b36SApple OSS Distributions  */
49*27b03b36SApple OSS Distributions 
50*27b03b36SApple OSS Distributions #include <stdatomic.h>
51*27b03b36SApple OSS Distributions #include <stdbool.h>
52*27b03b36SApple OSS Distributions #include <os/base.h>
53*27b03b36SApple OSS Distributions 
54*27b03b36SApple OSS Distributions struct os_refcnt;
55*27b03b36SApple OSS Distributions struct os_refgrp;
56*27b03b36SApple OSS Distributions typedef struct os_refcnt os_refcnt_t;
57*27b03b36SApple OSS Distributions 
58*27b03b36SApple OSS Distributions /* type of the internal counter */
59*27b03b36SApple OSS Distributions typedef uint32_t os_ref_count_t;
60*27b03b36SApple OSS Distributions typedef _Atomic(os_ref_count_t) os_ref_atomic_t;
61*27b03b36SApple OSS Distributions 
62*27b03b36SApple OSS Distributions /*
63*27b03b36SApple OSS Distributions  * OS_REF_INITIALIZER
64*27b03b36SApple OSS Distributions  * OS_REF_ATOMIC_INITIALIZER
65*27b03b36SApple OSS Distributions  *
66*27b03b36SApple OSS Distributions  * Static initializers that create refcnt objects with safe initial values for use
67*27b03b36SApple OSS Distributions  * between declaration and initialization (os_ref*_init()). Equivalent to zeroing.
68*27b03b36SApple OSS Distributions  */
69*27b03b36SApple OSS Distributions 
70*27b03b36SApple OSS Distributions #ifndef KERNEL
71*27b03b36SApple OSS Distributions # include <stdlib.h>
72*27b03b36SApple OSS Distributions # include <stdio.h>
73*27b03b36SApple OSS Distributions # ifndef __improbable
74*27b03b36SApple OSS Distributions #  define __improbable(x) x
75*27b03b36SApple OSS Distributions # endif
76*27b03b36SApple OSS Distributions # ifndef panic
77*27b03b36SApple OSS Distributions #  define panic(x, ...) do { fprintf(stderr, x, __VA_ARGS__); abort(); } while (0)
78*27b03b36SApple OSS Distributions # endif
79*27b03b36SApple OSS Distributions #endif
80*27b03b36SApple OSS Distributions 
81*27b03b36SApple OSS Distributions #ifndef OS_REFCNT_DEBUG
82*27b03b36SApple OSS Distributions # if DEVELOPMENT || DEBUG
83*27b03b36SApple OSS Distributions #  define OS_REFCNT_DEBUG 1
84*27b03b36SApple OSS Distributions # else
85*27b03b36SApple OSS Distributions #  define OS_REFCNT_DEBUG 0
86*27b03b36SApple OSS Distributions # endif
87*27b03b36SApple OSS Distributions #endif
88*27b03b36SApple OSS Distributions 
89*27b03b36SApple OSS Distributions #if __has_attribute(diagnose_if)
90*27b03b36SApple OSS Distributions # define os_error_if(cond, msg) __attribute__((diagnose_if((cond), (msg), "error")))
91*27b03b36SApple OSS Distributions #else
92*27b03b36SApple OSS Distributions # define os_error_if(...)
93*27b03b36SApple OSS Distributions #endif
94*27b03b36SApple OSS Distributions 
95*27b03b36SApple OSS Distributions __BEGIN_DECLS
96*27b03b36SApple OSS Distributions 
97*27b03b36SApple OSS Distributions /*
98*27b03b36SApple OSS Distributions  * os_ref_init: initialize an os_refcnt with a count of 1
99*27b03b36SApple OSS Distributions  * os_ref_init_count: initialize an os_refcnt with a specific count >= 1
100*27b03b36SApple OSS Distributions  */
101*27b03b36SApple OSS Distributions #define os_ref_init(rc, grp) os_ref_init_count((rc), (grp), 1)
102*27b03b36SApple OSS Distributions static void os_ref_init_count(struct os_refcnt *, struct os_refgrp *, os_ref_count_t count)
103*27b03b36SApple OSS Distributions os_error_if(count == 0, "Reference count must be non-zero initialized");
104*27b03b36SApple OSS Distributions 
105*27b03b36SApple OSS Distributions /*
106*27b03b36SApple OSS Distributions  * os_refgrp_decl(qual, var, name, parent): declare a refgroup object 'var' with
107*27b03b36SApple OSS Distributions  *   given name string and parent group.
108*27b03b36SApple OSS Distributions  */
109*27b03b36SApple OSS Distributions 
110*27b03b36SApple OSS Distributions /*
111*27b03b36SApple OSS Distributions  *
112*27b03b36SApple OSS Distributions  * os_ref_retain: acquire a reference (increment reference count by 1) atomically.
113*27b03b36SApple OSS Distributions  *
114*27b03b36SApple OSS Distributions  * os_ref_release: release a reference (decrement reference count) atomically and
115*27b03b36SApple OSS Distributions  *		return the new count. Memory is synchronized such that the dealloc block
116*27b03b36SApple OSS Distributions  *		(i.e. code handling the final release() == 0 call) sees up-to-date memory
117*27b03b36SApple OSS Distributions  *		with respect to all prior release()s on the same refcnt object. This
118*27b03b36SApple OSS Distributions  *		memory ordering is sufficient for most use cases.
119*27b03b36SApple OSS Distributions  *
120*27b03b36SApple OSS Distributions  * os_ref_release_relaxed: same as release() but with weaker relaxed memory ordering.
121*27b03b36SApple OSS Distributions  *		This can be used when the dealloc block is already synchronized with other
122*27b03b36SApple OSS Distributions  *		accesses to the object (for example, with a lock).
123*27b03b36SApple OSS Distributions  *
124*27b03b36SApple OSS Distributions  * os_ref_release_live: release a reference that is guaranteed not to be the last one.
125*27b03b36SApple OSS Distributions  */
126*27b03b36SApple OSS Distributions static void os_ref_retain(struct os_refcnt *);
127*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_release(struct os_refcnt *) OS_WARN_RESULT;
128*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_release_relaxed(struct os_refcnt *) OS_WARN_RESULT;
129*27b03b36SApple OSS Distributions static void os_ref_release_live(struct os_refcnt *);
130*27b03b36SApple OSS Distributions 
131*27b03b36SApple OSS Distributions /*
132*27b03b36SApple OSS Distributions  * os_ref_retain_try: a variant of atomic retain that fails for objects with a
133*27b03b36SApple OSS Distributions  *		zero reference count. The caller must therefore ensure that the object
134*27b03b36SApple OSS Distributions  *		remains alive for any possible retain_try() caller, usually by using a
135*27b03b36SApple OSS Distributions  *		lock protecting both the retain and dealloc paths. This variant is useful
136*27b03b36SApple OSS Distributions  *		for objects stored in a collection, because no lock is required on the
137*27b03b36SApple OSS Distributions  *		release() side until the object is deallocated.
138*27b03b36SApple OSS Distributions  */
139*27b03b36SApple OSS Distributions static bool os_ref_retain_try(struct os_refcnt *) OS_WARN_RESULT;
140*27b03b36SApple OSS Distributions 
141*27b03b36SApple OSS Distributions /*
142*27b03b36SApple OSS Distributions  * os_ref_retain_locked: acquire a reference on an object protected by a held
143*27b03b36SApple OSS Distributions  *		lock. The caller must ensure mutual exclusivity of retain_locked() and
144*27b03b36SApple OSS Distributions  *		release_locked() calls on the same object.
145*27b03b36SApple OSS Distributions  *
146*27b03b36SApple OSS Distributions  * os_ref_release_locked: release a reference on an object protected by a held
147*27b03b36SApple OSS Distributions  *		lock.
148*27b03b36SApple OSS Distributions  */
149*27b03b36SApple OSS Distributions static void os_ref_retain_locked(struct os_refcnt *);
150*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_release_locked(struct os_refcnt *) OS_WARN_RESULT;
151*27b03b36SApple OSS Distributions 
152*27b03b36SApple OSS Distributions /*
153*27b03b36SApple OSS Distributions  * os_ref_get_count: return the current reference count. This is unsafe for
154*27b03b36SApple OSS Distributions  *		synchronization.
155*27b03b36SApple OSS Distributions  */
156*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_get_count(struct os_refcnt *rc);
157*27b03b36SApple OSS Distributions 
158*27b03b36SApple OSS Distributions 
159*27b03b36SApple OSS Distributions #if XNU_KERNEL_PRIVATE
160*27b03b36SApple OSS Distributions #pragma GCC visibility push(hidden)
161*27b03b36SApple OSS Distributions 
162*27b03b36SApple OSS Distributions /*
163*27b03b36SApple OSS Distributions  * Raw API that uses a plain atomic counter (os_ref_atomic_t) and a separate
164*27b03b36SApple OSS Distributions  * refgroup. This can be used in situations where the refcount object must be
165*27b03b36SApple OSS Distributions  * fixed size, for example for embedding in structures with ABI stability
166*27b03b36SApple OSS Distributions  * requirements.
167*27b03b36SApple OSS Distributions  */
168*27b03b36SApple OSS Distributions 
169*27b03b36SApple OSS Distributions #define os_ref_init_raw(rc, grp) os_ref_init_count_raw((rc), (grp), 1)
170*27b03b36SApple OSS Distributions static void os_ref_init_count_raw(os_ref_atomic_t *, struct os_refgrp *, os_ref_count_t count)
171*27b03b36SApple OSS Distributions os_error_if(count == 0, "Reference count must be non-zero initialized");
172*27b03b36SApple OSS Distributions static void os_ref_retain_floor(struct os_refcnt *, os_ref_count_t f)
173*27b03b36SApple OSS Distributions os_error_if(!__builtin_constant_p(f) || f == 0, "refcount floor must be >= 1");
174*27b03b36SApple OSS Distributions static void os_ref_retain_raw(os_ref_atomic_t *, struct os_refgrp *);
175*27b03b36SApple OSS Distributions static void os_ref_retain_floor_raw(os_ref_atomic_t *, os_ref_count_t f, struct os_refgrp *)
176*27b03b36SApple OSS Distributions os_error_if(!__builtin_constant_p(f) || f == 0, "refcount floor must be >= 1");
177*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_release_raw(os_ref_atomic_t *, struct os_refgrp *) OS_WARN_RESULT;
178*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_release_raw_relaxed(os_ref_atomic_t *, struct os_refgrp *) OS_WARN_RESULT;
179*27b03b36SApple OSS Distributions static void os_ref_release_live_raw(os_ref_atomic_t *, struct os_refgrp *);
180*27b03b36SApple OSS Distributions static bool os_ref_retain_try_raw(os_ref_atomic_t *, struct os_refgrp *) OS_WARN_RESULT;
181*27b03b36SApple OSS Distributions static bool os_ref_retain_floor_try_raw(os_ref_atomic_t *, os_ref_count_t f, struct os_refgrp *) OS_WARN_RESULT
182*27b03b36SApple OSS Distributions     os_error_if(!__builtin_constant_p(f) || f == 0, "refcount floor must be >= 1");
183*27b03b36SApple OSS Distributions static void os_ref_retain_locked_raw(os_ref_atomic_t *, struct os_refgrp *);
184*27b03b36SApple OSS Distributions static void os_ref_retain_floor_locked_raw(os_ref_atomic_t *, os_ref_count_t f, struct os_refgrp *)
185*27b03b36SApple OSS Distributions os_error_if(!__builtin_constant_p(f) || f == 0, "refcount floor must be >= 1");
186*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_release_locked_raw(os_ref_atomic_t *, struct os_refgrp *) OS_WARN_RESULT;
187*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_get_count_raw(os_ref_atomic_t *rc);
188*27b03b36SApple OSS Distributions 
189*27b03b36SApple OSS Distributions 
190*27b03b36SApple OSS Distributions /*
191*27b03b36SApple OSS Distributions  * Bitwise API: like the raw API, but allows some bits in the refcount value to be
192*27b03b36SApple OSS Distributions  * reserved for other purposes. 'b' defines the number of trailing (LSB) reserved
193*27b03b36SApple OSS Distributions  * bits, which the refcnt_raw API will never modify (except at init()).
194*27b03b36SApple OSS Distributions  *
195*27b03b36SApple OSS Distributions  * It is assumed that users of this API always use atomic ops on the
196*27b03b36SApple OSS Distributions  * os_ref_atomic_t (or hold a lock for the locked variants), and never modify the
197*27b03b36SApple OSS Distributions  * top (32 - 'b') bits.
198*27b03b36SApple OSS Distributions  *
199*27b03b36SApple OSS Distributions  * Due to guard bits, the maximum reference count is 2^(28 - 'b') - 1, and the
200*27b03b36SApple OSS Distributions  * maximum 'b' is 26 bits. This API can also be used just to limit the max
201*27b03b36SApple OSS Distributions  * refcount.
202*27b03b36SApple OSS Distributions  *
203*27b03b36SApple OSS Distributions  * The "*_raw_mask" APIs return the raw bit pattern of the refcount (with a type
204*27b03b36SApple OSS Distributions  * of uint32_t), that the caller is supposed to decode. Other APIs that return
205*27b03b36SApple OSS Distributions  * os_ref_count_t return a normalized refcount where the trailing bits have been
206*27b03b36SApple OSS Distributions  * removed.
207*27b03b36SApple OSS Distributions  *
208*27b03b36SApple OSS Distributions  * "locked" variants aren't provided as the point of these interfaces
209*27b03b36SApple OSS Distributions  * is to combine flags into a refcount and be able to manipulate both
210*27b03b36SApple OSS Distributions  * atomically with respect to each other.
211*27b03b36SApple OSS Distributions  */
212*27b03b36SApple OSS Distributions 
213*27b03b36SApple OSS Distributions /* Initialize the reference count and reserved bits */
214*27b03b36SApple OSS Distributions #define os_ref_init_mask(rc, b, grp, bits) os_ref_init_count_mask((rc), (b), (grp), 1, bits)
215*27b03b36SApple OSS Distributions void os_ref_init_count_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp,
216*27b03b36SApple OSS Distributions     os_ref_count_t init_count, uint32_t init_bits)
217*27b03b36SApple OSS Distributions os_error_if(init_count == 0, "Reference count must be non-zero initialized")
218*27b03b36SApple OSS Distributions os_error_if(b > 26, "Bitwise reference count limited to 26 bits")
219*27b03b36SApple OSS Distributions os_error_if(init_bits >= (1U << b), "Bits out of range");
220*27b03b36SApple OSS Distributions 
221*27b03b36SApple OSS Distributions static uint32_t os_ref_get_raw_mask(os_ref_atomic_t *rc);
222*27b03b36SApple OSS Distributions static uint32_t os_ref_get_bits_mask(os_ref_atomic_t *rc, uint32_t b);
223*27b03b36SApple OSS Distributions static os_ref_count_t os_ref_get_count_mask(os_ref_atomic_t *rc, uint32_t b);
224*27b03b36SApple OSS Distributions 
225*27b03b36SApple OSS Distributions static void
226*27b03b36SApple OSS Distributions os_ref_retain_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp);
227*27b03b36SApple OSS Distributions static void
228*27b03b36SApple OSS Distributions os_ref_retain_acquire_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp);
229*27b03b36SApple OSS Distributions static uint32_t
230*27b03b36SApple OSS Distributions os_ref_retain_try_mask(os_ref_atomic_t *, uint32_t b, uint32_t reject_mask,
231*27b03b36SApple OSS Distributions     struct os_refgrp *grp) OS_WARN_RESULT;
232*27b03b36SApple OSS Distributions static bool
233*27b03b36SApple OSS Distributions os_ref_retain_try_acquire_mask(os_ref_atomic_t *, uint32_t b, uint32_t reject_mask,
234*27b03b36SApple OSS Distributions     struct os_refgrp *grp) OS_WARN_RESULT;
235*27b03b36SApple OSS Distributions 
236*27b03b36SApple OSS Distributions static uint32_t
237*27b03b36SApple OSS Distributions os_ref_release_raw_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp) OS_WARN_RESULT;
238*27b03b36SApple OSS Distributions static uint32_t
239*27b03b36SApple OSS Distributions os_ref_release_raw_relaxed_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp) OS_WARN_RESULT;
240*27b03b36SApple OSS Distributions static os_ref_count_t
241*27b03b36SApple OSS Distributions os_ref_release_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp) OS_WARN_RESULT;
242*27b03b36SApple OSS Distributions static os_ref_count_t
243*27b03b36SApple OSS Distributions os_ref_release_relaxed_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp) OS_WARN_RESULT;
244*27b03b36SApple OSS Distributions static uint32_t
245*27b03b36SApple OSS Distributions os_ref_release_live_raw_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp);
246*27b03b36SApple OSS Distributions static void
247*27b03b36SApple OSS Distributions os_ref_release_live_mask(os_ref_atomic_t *rc, uint32_t b, struct os_refgrp *grp);
248*27b03b36SApple OSS Distributions 
249*27b03b36SApple OSS Distributions #pragma GCC visibility pop
250*27b03b36SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
251*27b03b36SApple OSS Distributions 
252*27b03b36SApple OSS Distributions __END_DECLS
253*27b03b36SApple OSS Distributions 
254*27b03b36SApple OSS Distributions #include <os/refcnt_internal.h>
255*27b03b36SApple OSS Distributions #endif
256