2 * Copyright (c) 2020 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 #ifndef _SYS_EXISLOCK2_H_
35 #define _SYS_EXISLOCK2_H_
37 #ifndef _SYS_GLOBALDATA_H_
38 #include <sys/globaldata.h>
40 #ifndef _MACHINE_THREAD_H_
41 #include <machine/thread.h>
45 * Initialize the structure. To reduce confusion we also have exis_setlive()
46 * when repurposing a structure, which does the same thing.
49 exis_init(exislock_t *xlk)
51 xlk->pseudo_ticks = 0;
55 exis_setlive(exislock_t *xlk)
57 xlk->pseudo_ticks = 0;
61 * pcpu exis lock API. Enter and and exit a type-safe critical section.
64 exis_hold_gd(globaldata_t gd)
70 exis_drop_gd(globaldata_t gd)
72 if (--gd->gd_exislockcnt == 0)
89 * poll whether the object is usable or not. A value >= 0 indicates that
90 * the (possibly cached) object is usable.
92 * This call returns the approximate number of pseudo_ticks remaining until
93 * the object becomes unusable, +/- one.
95 * The actual value returns is either >= 0, or a negative number. Caller
96 * should refrain from trying to interpret values >= 0 other than the fact
99 * Negative numbers indicate the number of pseudo_ticks which have occurred
100 * since the object became unusable. Various negative values trigger
104 exis_poll(exislock_t *xlk)
106 long val = xlk->pseudo_ticks;
111 return (val - pseudo_ticks);
115 * Return the current state. Note that the NOTCACHED state persists for
116 * two pseudo_ticks. This is done because the global pseudo_ticks counter
117 * can concurrently increment by 1 (but no more than 1) during a type-safe
120 * The state can transition even while holding a type-safe critical section,
121 * but sequencing is designed such that this does not cause any problems.
124 exis_state(exislock_t *xlk)
126 long val = xlk->pseudo_ticks;
131 val = val - pseudo_ticks;
135 return EXIS_NOTCACHED;
136 return EXIS_TERMINATE;
140 * Returns non-zero if the structure is usable (either LIVE or CACHED).
142 * WARNING! The structure is not considered to be usable if it is in
143 * an UNCACHED state, but if it is CACHED and transitions to
144 * UNCACHED during a type-safe critical section it does remain
145 * usable for the duration of that type-safe critical section.
148 exis_usable(exislock_t *xlk)
150 return (exis_poll(xlk) >= 0);
154 * Returns non-zero if the structure can be destroyed
157 exis_freeable(exislock_t *xlk)
159 return (exis_poll(xlk) <= -2);
163 * If the structure is in a LIVE or CACHED state, or if it was CACHED and
164 * concurrently transitioned to NOTCACHED in the same type-safe critical
165 * section, the state will be reset to a CACHED(n) state and non-zero is
168 * Otherwise 0 is returned and no action is taken.
171 exis_cache(exislock_t *xlk, long n)
173 long val = xlk->pseudo_ticks;
174 long pticks = pseudo_ticks;
181 * avoid cache line ping-pong
184 if (xlk->pseudo_ticks != pticks) {
186 xlk->pseudo_ticks = pticks;
194 * The current state of the structure is ignored and the srtucture is
195 * placed in a CACHED(0) state. It will automatically sequence through
196 * the NOTCACHED and TERMINATE states as psuedo_ticks increments.
198 * The NOTCACHED state is an indeterminant state, since the pseudo_ticks
199 * counter might already be armed for increment, it can increment at least
200 * once while code is inside an exis_hold(). The TERMINATE state occurs
201 * at the second tick.
203 * If the caller repurposes the structure, it is usually a good idea to
204 * place it back into a LIVE state by calling exis_setlive().
207 exis_terminate(exislock_t *xlk)
209 xlk->pseudo_ticks = pseudo_ticks - 1;
212 #endif /* !_SYS_EXISLOCK2_H_ */