source: mainline/kernel/generic/src/synch/irq_spinlock.c@ 95658c9

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 95658c9 was 95658c9, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Put irq_spinlock_*() functions in a separate file

  • Property mode set to 100644
File size: 5.3 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
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.
16 *
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.
27 */
28
29/** @addtogroup kernel_sync
30 * @{
31 */
32
33/**
34 * @file
35 * @brief IRQ Spinlocks.
36 */
37
38#include <synch/spinlock.h>
39
40/** Initialize interrupts-disabled spinlock
41 *
42 * @param lock IRQ spinlock to be initialized.
43 * @param name IRQ spinlock name.
44 *
45 */
46void irq_spinlock_initialize(irq_spinlock_t *lock, const char *name)
47{
48 spinlock_initialize(&(lock->lock), name);
49 lock->guard = false;
50 lock->ipl = 0;
51}
52
53/** Lock interrupts-disabled spinlock
54 *
55 * Lock a spinlock which requires disabled interrupts.
56 *
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.
60 *
61 */
62void irq_spinlock_lock(irq_spinlock_t *lock, bool irq_dis)
63{
64 if (irq_dis) {
65 ipl_t ipl = interrupts_disable();
66 spinlock_lock(&(lock->lock));
67
68 lock->guard = true;
69 lock->ipl = ipl;
70 } else {
71 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
72
73 spinlock_lock(&(lock->lock));
74 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
75 }
76}
77
78/** Unlock interrupts-disabled spinlock
79 *
80 * Unlock a spinlock which requires disabled interrupts.
81 *
82 * @param lock IRQ spinlock to be unlocked.
83 * @param irq_res If true, interrupts are restored to previously
84 * saved interrupt level.
85 *
86 */
87void irq_spinlock_unlock(irq_spinlock_t *lock, bool irq_res)
88{
89 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
90
91 if (irq_res) {
92 ASSERT_IRQ_SPINLOCK(lock->guard, lock);
93
94 lock->guard = false;
95 ipl_t ipl = lock->ipl;
96
97 spinlock_unlock(&(lock->lock));
98 interrupts_restore(ipl);
99 } else {
100 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
101 spinlock_unlock(&(lock->lock));
102 }
103}
104
105/** Lock interrupts-disabled spinlock
106 *
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.
110 *
111 * @param lock IRQ spinlock to be locked conditionally.
112 *
113 * @return Zero on failure, non-zero otherwise.
114 *
115 */
116bool irq_spinlock_trylock(irq_spinlock_t *lock)
117{
118 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
119 bool ret = spinlock_trylock(&(lock->lock));
120
121 ASSERT_IRQ_SPINLOCK((!ret) || (!lock->guard), lock);
122 return ret;
123}
124
125/** Pass lock from one interrupts-disabled spinlock to another
126 *
127 * Pass lock from one IRQ spinlock to another IRQ spinlock
128 * without enabling interrupts during the process.
129 *
130 * The first IRQ spinlock is supposed to be locked.
131 *
132 * @param unlock IRQ spinlock to be unlocked.
133 * @param lock IRQ spinlock to be locked.
134 *
135 */
136void irq_spinlock_pass(irq_spinlock_t *unlock, irq_spinlock_t *lock)
137{
138 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
139
140 /* Pass guard from unlock to lock */
141 bool guard = unlock->guard;
142 ipl_t ipl = unlock->ipl;
143 unlock->guard = false;
144
145 spinlock_unlock(&(unlock->lock));
146 spinlock_lock(&(lock->lock));
147
148 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
149
150 if (guard) {
151 lock->guard = true;
152 lock->ipl = ipl;
153 }
154}
155
156/** Hand-over-hand locking of interrupts-disabled spinlocks
157 *
158 * Implement hand-over-hand locking between two interrupts-disabled
159 * spinlocks without enabling interrupts during the process.
160 *
161 * The first IRQ spinlock is supposed to be locked.
162 *
163 * @param unlock IRQ spinlock to be unlocked.
164 * @param lock IRQ spinlock to be locked.
165 *
166 */
167void irq_spinlock_exchange(irq_spinlock_t *unlock, irq_spinlock_t *lock)
168{
169 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
170
171 spinlock_lock(&(lock->lock));
172 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
173
174 /* Pass guard from unlock to lock */
175 if (unlock->guard) {
176 lock->guard = true;
177 lock->ipl = unlock->ipl;
178 unlock->guard = false;
179 }
180
181 spinlock_unlock(&(unlock->lock));
182}
183
184/** Find out whether the IRQ spinlock is currently locked.
185 *
186 * @param lock IRQ spinlock.
187 * @return True if the IRQ spinlock is locked, false otherwise.
188 */
189bool irq_spinlock_locked(irq_spinlock_t *ilock)
190{
191 return spinlock_locked(&ilock->lock);
192}
193
194/** @}
195 */
Note: See TracBrowser for help on using the repository browser.