White space
[dragonfly.git] / sys / kern / lwkt_serialize.c
blob28a2cc7621d20f34006074be4368c6eec4d61eff
1 /*
2 * Copyright (c) 2005 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:
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.
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.
34 * $DragonFly: src/sys/kern/lwkt_serialize.c,v 1.13 2008/05/02 10:57:33 sephe Exp $
37 * This API provides a fast locked-bus-cycle-based serializer. It's
38 * basically a low level NON-RECURSIVE exclusive lock that can be held across
39 * a blocking condition. It is NOT a mutex.
41 * This serializer is primarily designed for low level situations and
42 * interrupt/device interaction. There are two primary facilities. First,
43 * the serializer facility itself. Second, an integrated interrupt handler
44 * disablement facility.
47 #include "opt_serializer.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/proc.h>
53 #include <sys/rtprio.h>
54 #include <sys/queue.h>
55 #include <sys/thread2.h>
56 #include <sys/serialize.h>
57 #include <sys/sysctl.h>
58 #include <sys/ktr.h>
59 #include <sys/kthread.h>
60 #include <machine/cpu.h>
61 #include <sys/lock.h>
62 #include <sys/caps.h>
64 #define SLZ_KTR_STRING "slz=%p"
65 #define SLZ_KTR_ARG_SIZE (sizeof(void *))
67 #ifndef KTR_SERIALIZER
68 #define KTR_SERIALIZER KTR_ALL
69 #endif
71 KTR_INFO_MASTER(slz);
72 KTR_INFO(KTR_SERIALIZER, slz, enter, 0, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
73 KTR_INFO(KTR_SERIALIZER, slz, sleep, 1, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
74 KTR_INFO(KTR_SERIALIZER, slz, exit, 2, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
75 KTR_INFO(KTR_SERIALIZER, slz, wakeup, 3, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
76 KTR_INFO(KTR_SERIALIZER, slz, try, 4, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
77 KTR_INFO(KTR_SERIALIZER, slz, tryfail, 5, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
78 KTR_INFO(KTR_SERIALIZER, slz, tryok, 6, SLZ_KTR_STRING, SLZ_KTR_ARG_SIZE);
80 #define logslz(name, slz) KTR_LOG(slz_ ## name, slz)
82 static void lwkt_serialize_sleep(void *info);
83 static void lwkt_serialize_wakeup(void *info);
85 void
86 lwkt_serialize_init(lwkt_serialize_t s)
88 atomic_intr_init(&s->interlock);
89 #ifdef INVARIANTS
90 s->last_td = (void *)-4;
91 #endif
92 s->sleep_cnt = 0;
93 s->tryfail_cnt = 0;
94 s->enter_cnt = 0;
95 s->try_cnt = 0;
98 void
99 lwkt_serialize_enter(lwkt_serialize_t s)
101 #ifdef INVARIANTS
102 KKASSERT(s->last_td != curthread);
103 #endif
104 logslz(enter, s);
105 atomic_intr_cond_enter(&s->interlock, lwkt_serialize_sleep, s);
106 #ifdef INVARIANTS
107 s->last_td = curthread;
108 #endif
109 #ifdef PROFILE_SERIALIZER
110 s->enter_cnt++;
111 #endif
115 * Returns non-zero on success
118 lwkt_serialize_try(lwkt_serialize_t s)
120 int error;
122 #ifdef INVARIANTS
123 KKASSERT(s->last_td != curthread);
124 #endif
125 #ifdef PROFILE_SERIALIZER
126 s->try_cnt++;
127 #endif
128 logslz(try, s);
129 if ((error = atomic_intr_cond_try(&s->interlock)) == 0) {
130 #ifdef INVARIANTS
131 s->last_td = curthread;
132 #endif
133 logslz(tryok, s);
134 return(1);
136 #ifdef PROFILE_SERIALIZER
137 s->tryfail_cnt++;
138 #endif
139 logslz(tryfail, s);
140 return (0);
143 void
144 lwkt_serialize_exit(lwkt_serialize_t s)
146 #ifdef INVARIANTS
147 KKASSERT(s->last_td == curthread);
148 s->last_td = (void *)-2;
149 #endif
150 logslz(exit, s);
151 atomic_intr_cond_exit(&s->interlock, lwkt_serialize_wakeup, s);
155 * Interrupt handler disablement support, used by drivers. Non-stackable
156 * (uses bit 30).
158 void
159 lwkt_serialize_handler_disable(lwkt_serialize_t s)
161 atomic_intr_handler_disable(&s->interlock);
164 void
165 lwkt_serialize_handler_enable(lwkt_serialize_t s)
167 atomic_intr_handler_enable(&s->interlock);
170 void
171 lwkt_serialize_handler_call(lwkt_serialize_t s, void (*func)(void *, void *),
172 void *arg, void *frame)
175 * note: a return value of 0 indicates that the interrupt handler is
176 * enabled.
178 if (atomic_intr_handler_is_enabled(&s->interlock) == 0) {
179 logslz(enter, s);
180 atomic_intr_cond_enter(&s->interlock, lwkt_serialize_sleep, s);
181 #ifdef INVARIANTS
182 s->last_td = curthread;
183 #endif
184 #ifdef PROFILE_SERIALIZER
185 s->enter_cnt++;
186 #endif
187 if (atomic_intr_handler_is_enabled(&s->interlock) == 0)
188 func(arg, frame);
189 #ifdef INVARIANTS
190 KKASSERT(s->last_td == curthread);
191 s->last_td = (void *)-2;
192 #endif
193 logslz(exit, s);
194 atomic_intr_cond_exit(&s->interlock, lwkt_serialize_wakeup, s);
199 * Similar to handler_call but does not block. Returns 0 on success,
200 * and 1 on failure.
203 lwkt_serialize_handler_try(lwkt_serialize_t s, void (*func)(void *, void *),
204 void *arg, void *frame)
207 * note: a return value of 0 indicates that the interrupt handler is
208 * enabled.
210 if (atomic_intr_handler_is_enabled(&s->interlock) == 0) {
211 #ifdef PROFILE_SERIALIZER
212 s->try_cnt++;
213 #endif
214 logslz(try, s);
215 if (atomic_intr_cond_try(&s->interlock) == 0) {
216 #ifdef INVARIANTS
217 s->last_td = curthread;
218 #endif
219 logslz(tryok, s);
220 func(arg, frame);
221 #ifdef INVARIANTS
222 KKASSERT(s->last_td == curthread);
223 s->last_td = (void *)-2;
224 #endif
225 logslz(exit, s);
226 atomic_intr_cond_exit(&s->interlock, lwkt_serialize_wakeup, s);
227 return(0);
230 #ifdef PROFILE_SERIALIZER
231 s->tryfail_cnt++;
232 #endif
233 logslz(tryfail, s);
234 return(1);
239 * Helper functions
241 * It is possible to race an interrupt which acquires and releases the
242 * bit, then calls wakeup before we actually go to sleep, so we
243 * need to check that the interlock is still acquired from within
244 * a critical section prior to sleeping.
246 static void
247 lwkt_serialize_sleep(void *info)
249 lwkt_serialize_t s = info;
250 crit_enter();
251 tsleep_interlock(s);
252 if (atomic_intr_cond_test(&s->interlock) != 0) {
253 #ifdef PROFILE_SERIALIZER
254 s->sleep_cnt++;
255 #endif
256 logslz(sleep, s);
257 tsleep(s, 0, "slize", 0);
259 crit_exit();
262 static void
263 lwkt_serialize_wakeup(void *info)
265 logslz(wakeup, info);
266 wakeup(info);