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