gdb - Local mods (compile)
[dragonfly.git] / sys / sys / sysref2.h
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 _SYS_SYSREF_H_
49 #include <sys/sysref.h>
50 #endif
51 #include <machine/atomic.h>
52
53 void _sysref_put(struct sysref *);
54
55 /*
56  * Normally only 1+ transitions can occur.  Note that a negative ref count
57  * is used as a marker to indicate that a structure is undergoing deletion
58  * and that temporary refs during the process of deletion may occur.
59  *
60  * The get interface is the primary reference counting interface.
61  */
62 static __inline
63 void
64 sysref_get(struct sysref *sr)
65 {
66         KKASSERT((sr->flags & SRF_PUTAWAY) == 0);
67         atomic_add_int(&sr->refcnt, 1);
68 }
69
70 /*
71  * 1->0 transitions, and transitions occuring with negative ref counts,
72  * require special handling.  All other transitions can be done with a
73  * trivial locked bus cycle.
74  */
75 static __inline
76 void
77 sysref_put(struct sysref *sr)
78 {
79         int count = sr->refcnt;
80
81         KKASSERT((sr->flags & SRF_PUTAWAY) == 0);
82         if (count <= 1 || atomic_cmpset_int(&sr->refcnt, count, count - 1) == 0)
83                 _sysref_put(sr);
84 }
85
86 /*
87  * Return true of the object is fully active
88  */
89 static __inline
90 int
91 sysref_isactive(struct sysref *sr)
92 {
93         return(sr->refcnt > 0);
94 }
95
96 /*
97  * Return true of the object is being deactivated
98  */
99 static __inline
100 int
101 sysref_isinactive(struct sysref *sr)
102 {
103         return(sr->refcnt <= 0);
104 }
105
106 /*
107  * Return true if we control the last deactivation ref on the object
108  */
109 static __inline
110 int
111 sysref_islastdeactivation(struct sysref *sr)
112 {
113         return(sr->refcnt == -0x40000000);
114 }
115
116 #ifdef _KERNEL
117
118 void sysref_init(struct sysref *sr, struct sysref_class *);
119 void *sysref_alloc(struct sysref_class *class);
120 void sysref_activate(struct sysref *);
121
122 #endif
123
124 #endif