Upgrade to latest version of awk. Significant changes can be found in
[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  * $DragonFly: src/sys/sys/sysref2.h,v 1.1 2007/04/29 01:29:31 dillon Exp $
35  */
36 /*
37  * System resource registration, reference counter, and allocation
38  * management subsystem.
39  *
40  * All major system resources use this structure and API to associate
41  * a machine-unique sysref with a major system resource structure, to
42  * reference count that structure, to register the structure and provide
43  * a dictionary lookup feature for remote access, and to provide an
44  * allocation and release backend.
45  */
46
47 #ifndef _SYS_SYSREF2_H_
48 #define _SYS_SYSREF2_H_
49
50 #ifndef _SYS_SYSREF_H_
51 #include <sys/sysref.h>
52 #endif
53 #ifndef _MACHINE_ATOMIC_H_
54 #include <machine/atomic.h>
55 #endif
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
68 sysref_get(struct sysref *sr)
69 {
70         atomic_add_int(&sr->refcnt, 1);
71 }
72
73 /*
74  * 1->0 transitions, and transitions occuring with negative ref counts,
75  * require special handling.  All other transitions can be done with a
76  * trivial locked bus cycle.
77  */
78 static __inline
79 void
80 sysref_put(struct sysref *sr)
81 {
82         int count = sr->refcnt;
83
84         if (count <= 1 || atomic_cmpset_int(&sr->refcnt, count, count - 1) == 0)
85                 _sysref_put(sr);
86 }
87
88 static __inline
89 int
90 sysref_isactive(struct sysref *sr)
91 {
92         return(sr->refcnt > 0);
93 }
94
95 static __inline
96 int
97 sysref_isinactive(struct sysref *sr)
98 {
99         return(sr->refcnt <= 0);
100 }
101
102 #ifdef _KERNEL
103
104 void sysref_init(struct sysref *sr, struct sysref_class *);
105 void *sysref_alloc(struct sysref_class *class);
106 void sysref_activate(struct sysref *);
107
108 #endif
109
110 #endif