Put irq_spinlock_*() functions in a separate file
[helenos.git] / kernel / generic / src / synch / irq_spinlock.c
blob974444849bc0a1173bc47b709e88068434fd283a
1 /*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 /** @addtogroup kernel_sync
30 * @{
33 /**
34 * @file
35 * @brief IRQ Spinlocks.
38 #include <synch/spinlock.h>
40 /** Initialize interrupts-disabled spinlock
42 * @param lock IRQ spinlock to be initialized.
43 * @param name IRQ spinlock name.
46 void irq_spinlock_initialize(irq_spinlock_t *lock, const char *name)
48 spinlock_initialize(&(lock->lock), name);
49 lock->guard = false;
50 lock->ipl = 0;
53 /** Lock interrupts-disabled spinlock
55 * Lock a spinlock which requires disabled interrupts.
57 * @param lock IRQ spinlock to be locked.
58 * @param irq_dis If true, disables interrupts before locking the spinlock.
59 * If false, interrupts are expected to be already disabled.
62 void irq_spinlock_lock(irq_spinlock_t *lock, bool irq_dis)
64 if (irq_dis) {
65 ipl_t ipl = interrupts_disable();
66 spinlock_lock(&(lock->lock));
68 lock->guard = true;
69 lock->ipl = ipl;
70 } else {
71 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
73 spinlock_lock(&(lock->lock));
74 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
78 /** Unlock interrupts-disabled spinlock
80 * Unlock a spinlock which requires disabled interrupts.
82 * @param lock IRQ spinlock to be unlocked.
83 * @param irq_res If true, interrupts are restored to previously
84 * saved interrupt level.
87 void irq_spinlock_unlock(irq_spinlock_t *lock, bool irq_res)
89 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
91 if (irq_res) {
92 ASSERT_IRQ_SPINLOCK(lock->guard, lock);
94 lock->guard = false;
95 ipl_t ipl = lock->ipl;
97 spinlock_unlock(&(lock->lock));
98 interrupts_restore(ipl);
99 } else {
100 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
101 spinlock_unlock(&(lock->lock));
105 /** Lock interrupts-disabled spinlock
107 * Lock an interrupts-disabled spinlock conditionally. If the
108 * spinlock is not available at the moment, signal failure.
109 * Interrupts are expected to be already disabled.
111 * @param lock IRQ spinlock to be locked conditionally.
113 * @return Zero on failure, non-zero otherwise.
116 bool irq_spinlock_trylock(irq_spinlock_t *lock)
118 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
119 bool ret = spinlock_trylock(&(lock->lock));
121 ASSERT_IRQ_SPINLOCK((!ret) || (!lock->guard), lock);
122 return ret;
125 /** Pass lock from one interrupts-disabled spinlock to another
127 * Pass lock from one IRQ spinlock to another IRQ spinlock
128 * without enabling interrupts during the process.
130 * The first IRQ spinlock is supposed to be locked.
132 * @param unlock IRQ spinlock to be unlocked.
133 * @param lock IRQ spinlock to be locked.
136 void irq_spinlock_pass(irq_spinlock_t *unlock, irq_spinlock_t *lock)
138 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
140 /* Pass guard from unlock to lock */
141 bool guard = unlock->guard;
142 ipl_t ipl = unlock->ipl;
143 unlock->guard = false;
145 spinlock_unlock(&(unlock->lock));
146 spinlock_lock(&(lock->lock));
148 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
150 if (guard) {
151 lock->guard = true;
152 lock->ipl = ipl;
156 /** Hand-over-hand locking of interrupts-disabled spinlocks
158 * Implement hand-over-hand locking between two interrupts-disabled
159 * spinlocks without enabling interrupts during the process.
161 * The first IRQ spinlock is supposed to be locked.
163 * @param unlock IRQ spinlock to be unlocked.
164 * @param lock IRQ spinlock to be locked.
167 void irq_spinlock_exchange(irq_spinlock_t *unlock, irq_spinlock_t *lock)
169 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
171 spinlock_lock(&(lock->lock));
172 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
174 /* Pass guard from unlock to lock */
175 if (unlock->guard) {
176 lock->guard = true;
177 lock->ipl = unlock->ipl;
178 unlock->guard = false;
181 spinlock_unlock(&(unlock->lock));
184 /** Find out whether the IRQ spinlock is currently locked.
186 * @param lock IRQ spinlock.
187 * @return True if the IRQ spinlock is locked, false otherwise.
189 bool irq_spinlock_locked(irq_spinlock_t *ilock)
191 return spinlock_locked(&ilock->lock);
194 /** @}