1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 /*
35 * System resource registration, reference counter, and allocation
36 * management subsystem.
37 *
38 * All major system resources use this structure and API to associate
39 * a machine-unique sysref with a major system resource structure, to
40 * reference count that structure, to register the structure and provide
41 * a dictionary lookup feature for remote access, and to provide an
42 * allocation and release backend.
43 */
44
45 #ifndef _SYS_SYSREF2_H_
46 #define _SYS_SYSREF2_H_
47
48 #ifndef _KERNEL
49 #error "This file should not be included by userland programs."
50 #endif
51
52 #ifndef _SYS_SYSREF_H_
53 #include <sys/sysref.h>
54 #endif
55 #include <machine/atomic.h>
56
57 void _sysref_put(struct sysref *);
58
59 /*
60 * Normally only 1+ transitions can occur. Note that a negative ref count
61 * is used as a marker to indicate that a structure is undergoing deletion
62 * and that temporary refs during the process of deletion may occur.
63 *
64 * The get interface is the primary reference counting interface.
65 */
66 static __inline
67 void
sysref_get(struct sysref * sr)68 sysref_get(struct sysref *sr)
69 {
70 KKASSERT((sr->flags & SRF_PUTAWAY) == 0);
71 atomic_add_int(&sr->refcnt, 1);
72 }
73
74 /*
75 * 1->0 transitions, and transitions occuring with negative ref counts,
76 * require special handling. All other transitions can be done with a
77 * trivial locked bus cycle.
78 */
79 static __inline
80 void
sysref_put(struct sysref * sr)81 sysref_put(struct sysref *sr)
82 {
83 int count = sr->refcnt;
84
85 KKASSERT((sr->flags & SRF_PUTAWAY) == 0);
86 if (count <= 1 || atomic_cmpset_int(&sr->refcnt, count, count - 1) == 0)
87 _sysref_put(sr);
88 }
89
90 /*
91 * Return true of the object is fully active
92 */
93 static __inline
94 int
sysref_isactive(struct sysref * sr)95 sysref_isactive(struct sysref *sr)
96 {
97 return(sr->refcnt > 0);
98 }
99
100 /*
101 * Return true of the object is being deactivated
102 */
103 static __inline
104 int
sysref_isinactive(struct sysref * sr)105 sysref_isinactive(struct sysref *sr)
106 {
107 return(sr->refcnt <= 0);
108 }
109
110 /*
111 * Return true if we control the last deactivation ref on the object
112 */
113 static __inline
114 int
sysref_islastdeactivation(struct sysref * sr)115 sysref_islastdeactivation(struct sysref *sr)
116 {
117 return(sr->refcnt == -0x40000000);
118 }
119
120 #ifdef _KERNEL
121
122 void sysref_init(struct sysref *sr, struct sysref_class *);
123 void *sysref_alloc(struct sysref_class *class);
124 void sysref_activate(struct sysref *);
125
126 #endif
127
128 #endif /* !_SYS_SYSREF2_H_ */
129