kern_cfglock.c revision 1.1.10.2 1 1.1.10.2 rmind /* $NetBSD: kern_cfglock.c,v 1.1.10.2 2011/03/05 20:55:13 rmind Exp $ */
2 1.1.10.2 rmind
3 1.1.10.2 rmind /*-
4 1.1.10.2 rmind * Copyright (c) 2002, 2006, 2007, 2008, 2009 The NetBSD Foundation, Inc.
5 1.1.10.2 rmind * All rights reserved.
6 1.1.10.2 rmind *
7 1.1.10.2 rmind * This code is derived from software contributed to The NetBSD Foundation
8 1.1.10.2 rmind * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.1.10.2 rmind * NASA Ames Research Center, and by Andrew Doran.
10 1.1.10.2 rmind *
11 1.1.10.2 rmind * Redistribution and use in source and binary forms, with or without
12 1.1.10.2 rmind * modification, are permitted provided that the following conditions
13 1.1.10.2 rmind * are met:
14 1.1.10.2 rmind * 1. Redistributions of source code must retain the above copyright
15 1.1.10.2 rmind * notice, this list of conditions and the following disclaimer.
16 1.1.10.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
17 1.1.10.2 rmind * notice, this list of conditions and the following disclaimer in the
18 1.1.10.2 rmind * documentation and/or other materials provided with the distribution.
19 1.1.10.2 rmind *
20 1.1.10.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1.10.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1.10.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1.10.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1.10.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1.10.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1.10.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1.10.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1.10.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1.10.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1.10.2 rmind * POSSIBILITY OF SUCH DAMAGE.
31 1.1.10.2 rmind */
32 1.1.10.2 rmind
33 1.1.10.2 rmind #include <sys/cdefs.h>
34 1.1.10.2 rmind __KERNEL_RCSID(0, "$NetBSD: kern_cfglock.c,v 1.1.10.2 2011/03/05 20:55:13 rmind Exp $");
35 1.1.10.2 rmind
36 1.1.10.2 rmind #include <sys/param.h>
37 1.1.10.2 rmind #include <sys/cpu.h>
38 1.1.10.2 rmind #include <sys/mutex.h>
39 1.1.10.2 rmind #include <sys/lwp.h>
40 1.1.10.2 rmind #include <sys/systm.h>
41 1.1.10.2 rmind
42 1.1.10.2 rmind static kmutex_t kernconfig_mutex;
43 1.1.10.2 rmind static lwp_t *kernconfig_lwp;
44 1.1.10.2 rmind static int kernconfig_recurse;
45 1.1.10.2 rmind
46 1.1.10.2 rmind /*
47 1.1.10.2 rmind * Functions for manipulating the kernel configuration lock. This
48 1.1.10.2 rmind * recursive lock should be used to protect all additions and removals
49 1.1.10.2 rmind * of kernel functionality, such as device configuration and loading
50 1.1.10.2 rmind * of modular kernel components.
51 1.1.10.2 rmind */
52 1.1.10.2 rmind
53 1.1.10.2 rmind void
54 1.1.10.2 rmind kernconfig_lock_init(void)
55 1.1.10.2 rmind {
56 1.1.10.2 rmind
57 1.1.10.2 rmind mutex_init(&kernconfig_mutex, MUTEX_DEFAULT, IPL_NONE);
58 1.1.10.2 rmind kernconfig_lwp = NULL;
59 1.1.10.2 rmind kernconfig_recurse = 0;
60 1.1.10.2 rmind }
61 1.1.10.2 rmind
62 1.1.10.2 rmind void
63 1.1.10.2 rmind kernconfig_lock(void)
64 1.1.10.2 rmind {
65 1.1.10.2 rmind lwp_t *my_lwp;
66 1.1.10.2 rmind
67 1.1.10.2 rmind /*
68 1.1.10.2 rmind * It's OK to check this unlocked, since it could only be set to
69 1.1.10.2 rmind * curlwp by the current thread itself, and not by an interrupt
70 1.1.10.2 rmind * or any other LWP.
71 1.1.10.2 rmind */
72 1.1.10.2 rmind KASSERT(!cpu_intr_p());
73 1.1.10.2 rmind my_lwp = curlwp;
74 1.1.10.2 rmind if (kernconfig_lwp == my_lwp) {
75 1.1.10.2 rmind kernconfig_recurse++;
76 1.1.10.2 rmind KASSERT(kernconfig_recurse > 1);
77 1.1.10.2 rmind } else {
78 1.1.10.2 rmind mutex_enter(&kernconfig_mutex);
79 1.1.10.2 rmind kernconfig_lwp = my_lwp;
80 1.1.10.2 rmind kernconfig_recurse = 1;
81 1.1.10.2 rmind }
82 1.1.10.2 rmind }
83 1.1.10.2 rmind
84 1.1.10.2 rmind void
85 1.1.10.2 rmind kernconfig_unlock(void)
86 1.1.10.2 rmind {
87 1.1.10.2 rmind
88 1.1.10.2 rmind KASSERT(kernconfig_is_held());
89 1.1.10.2 rmind KASSERT(kernconfig_recurse != 0);
90 1.1.10.2 rmind if (--kernconfig_recurse == 0) {
91 1.1.10.2 rmind kernconfig_lwp = NULL;
92 1.1.10.2 rmind mutex_exit(&kernconfig_mutex);
93 1.1.10.2 rmind }
94 1.1.10.2 rmind }
95 1.1.10.2 rmind
96 1.1.10.2 rmind bool
97 1.1.10.2 rmind kernconfig_is_held(void)
98 1.1.10.2 rmind {
99 1.1.10.2 rmind
100 1.1.10.2 rmind return mutex_owned(&kernconfig_mutex);
101 1.1.10.2 rmind }
102