source: mainline/kernel/generic/src/synch/spinlock.c@ 0b5203b

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

XXX to NOTE

  • Property mode set to 100644
File size: 8.6 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f761f1eb]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
[e88eb48]29/** @addtogroup kernel_sync
[b45c443]30 * @{
31 */
32
[cf26ba9]33/**
[b45c443]34 * @file
[b60c582]35 * @brief Spinlocks.
[cf26ba9]36 */
[b60c582]37
[5e04b48d]38#include <synch/spinlock.h>
[23684b7]39#include <atomic.h>
[05882233]40#include <barrier.h>
[5e04b48d]41#include <arch.h>
[c842f04]42#include <preemption.h>
[bab75df6]43#include <stdio.h>
[5e04b48d]44#include <debug.h>
[2d93f1f9]45#include <symtab.h>
[311929ec]46#include <stacktrace.h>
[1066041]47#include <cpu.h>
[f761f1eb]48
[5f85c91]49#ifdef CONFIG_SMP
[f761f1eb]50
[5e04b48d]51/** Initialize spinlock
52 *
53 * @param sl Pointer to spinlock_t structure.
[90c8b8d]54 *
[5e04b48d]55 */
[a000878c]56void spinlock_initialize(spinlock_t *lock, const char *name)
[f761f1eb]57{
[78de83de]58 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
[2d93f1f9]59#ifdef CONFIG_DEBUG_SPINLOCK
[90c8b8d]60 lock->name = name;
61#endif
[f761f1eb]62}
63
[90c8b8d]64#ifdef CONFIG_DEBUG_SPINLOCK
65
[5e04b48d]66/** Lock spinlock
67 *
68 * Lock spinlock.
[1b20da0]69 * This version has limitted ability to report
[5e04b48d]70 * possible occurence of deadlock.
71 *
[90c8b8d]72 * @param lock Pointer to spinlock_t structure.
73 *
[5e04b48d]74 */
[90c8b8d]75void spinlock_lock_debug(spinlock_t *lock)
[f761f1eb]76{
[98000fb]77 size_t i = 0;
[05e2a7ad]78 bool deadlock_reported = false;
[a35b458]79
[c842f04]80 preemption_disable();
[78de83de]81 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
[d9cf9d5f]82 /*
[90c8b8d]83 * We need to be careful about particular locks
84 * which are directly used to report deadlocks
85 * via printf() (and recursively other functions).
86 * This conserns especially printf_lock and the
87 * framebuffer lock.
[c263c77]88 *
89 * Any lock whose name is prefixed by "*" will be
90 * ignored by this deadlock detection routine
91 * as this might cause an infinite recursion.
92 * We trust our code that there is no possible deadlock
93 * caused by these locks (except when an exception
94 * is triggered for instance by printf()).
95 *
96 * We encountered false positives caused by very
97 * slow framebuffer interaction (especially when
98 * run in a simulator) that caused problems with both
99 * printf_lock and the framebuffer lock.
[d9cf9d5f]100 */
[c263c77]101 if (lock->name[0] == '*')
102 continue;
[a35b458]103
[14df080]104 if (i++ > DEADLOCK_THRESHOLD) {
[7e752b2]105 printf("cpu%u: looping on spinlock %p:%s, "
106 "caller=%p (%s)\n", CPU->id, lock, lock->name,
107 (void *) CALLER, symtab_fmt_name_lookup(CALLER));
[311929ec]108 stack_trace();
[a35b458]109
[f761f1eb]110 i = 0;
[05e2a7ad]111 deadlock_reported = true;
[f761f1eb]112 }
113 }
[a35b458]114
[05e2a7ad]115 if (deadlock_reported)
[8ed4014]116 printf("cpu%u: not deadlocked\n", CPU->id);
[f761f1eb]117}
[90c8b8d]118
[13108f24]119/** Unlock spinlock
120 *
121 * Unlock spinlock.
122 *
123 * @param sl Pointer to spinlock_t structure.
124 */
125void spinlock_unlock_debug(spinlock_t *lock)
126{
[ffe4a87]127 ASSERT_SPINLOCK(spinlock_locked(lock), lock);
[a35b458]128
[78de83de]129 atomic_flag_clear_explicit(&lock->flag, memory_order_release);
[13108f24]130 preemption_enable();
131}
132
[f761f1eb]133#endif
134
[5e04b48d]135/** Lock spinlock conditionally
136 *
[2b4a9f26]137 * Lock spinlock conditionally. If the spinlock is not available
138 * at the moment, signal failure.
[5e04b48d]139 *
[90c8b8d]140 * @param lock Pointer to spinlock_t structure.
[5e04b48d]141 *
142 * @return Zero on failure, non-zero otherwise.
[90c8b8d]143 *
[5e04b48d]144 */
[89ea2dc]145bool spinlock_trylock(spinlock_t *lock)
[f761f1eb]146{
[c842f04]147 preemption_disable();
[78de83de]148 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
[a35b458]149
[89ea2dc]150 if (!ret)
[c842f04]151 preemption_enable();
[a35b458]152
[89ea2dc]153 return ret;
[f761f1eb]154}
155
[ffe4a87]156/** Find out whether the spinlock is currently locked.
157 *
158 * @param lock Spinlock.
159 * @return True if the spinlock is locked, false otherwise.
160 */
161bool spinlock_locked(spinlock_t *lock)
162{
[95d45482]163 // NOTE: Atomic flag doesn't support simple atomic read (by design),
164 // so instead we test_and_set and then clear if necessary.
165 // This function is only used inside assert, so we don't need
166 // any preemption_disable/enable here.
[78de83de]167
168 bool ret = atomic_flag_test_and_set_explicit(&lock->flag, memory_order_relaxed);
169 if (!ret)
170 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
171 return ret;
[ffe4a87]172}
173
[f761f1eb]174#endif
[b45c443]175
[a9f1372]176/** Initialize interrupts-disabled spinlock
177 *
178 * @param lock IRQ spinlock to be initialized.
179 * @param name IRQ spinlock name.
180 *
181 */
182void irq_spinlock_initialize(irq_spinlock_t *lock, const char *name)
183{
184 spinlock_initialize(&(lock->lock), name);
185 lock->guard = false;
186 lock->ipl = 0;
187}
188
189/** Lock interrupts-disabled spinlock
190 *
191 * Lock a spinlock which requires disabled interrupts.
192 *
193 * @param lock IRQ spinlock to be locked.
[9fe9d296]194 * @param irq_dis If true, disables interrupts before locking the spinlock.
195 * If false, interrupts are expected to be already disabled.
[a9f1372]196 *
197 */
198void irq_spinlock_lock(irq_spinlock_t *lock, bool irq_dis)
199{
200 if (irq_dis) {
201 ipl_t ipl = interrupts_disable();
202 spinlock_lock(&(lock->lock));
[a35b458]203
[a9f1372]204 lock->guard = true;
205 lock->ipl = ipl;
206 } else {
207 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
[a35b458]208
[a9f1372]209 spinlock_lock(&(lock->lock));
210 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
211 }
212}
213
214/** Unlock interrupts-disabled spinlock
215 *
216 * Unlock a spinlock which requires disabled interrupts.
217 *
218 * @param lock IRQ spinlock to be unlocked.
219 * @param irq_res If true, interrupts are restored to previously
220 * saved interrupt level.
221 *
222 */
223void irq_spinlock_unlock(irq_spinlock_t *lock, bool irq_res)
224{
225 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
[a35b458]226
[a9f1372]227 if (irq_res) {
228 ASSERT_IRQ_SPINLOCK(lock->guard, lock);
[a35b458]229
[a9f1372]230 lock->guard = false;
231 ipl_t ipl = lock->ipl;
[a35b458]232
[a9f1372]233 spinlock_unlock(&(lock->lock));
234 interrupts_restore(ipl);
235 } else {
236 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
237 spinlock_unlock(&(lock->lock));
238 }
239}
240
241/** Lock interrupts-disabled spinlock
242 *
243 * Lock an interrupts-disabled spinlock conditionally. If the
244 * spinlock is not available at the moment, signal failure.
245 * Interrupts are expected to be already disabled.
246 *
247 * @param lock IRQ spinlock to be locked conditionally.
248 *
249 * @return Zero on failure, non-zero otherwise.
250 *
251 */
[89ea2dc]252bool irq_spinlock_trylock(irq_spinlock_t *lock)
[a9f1372]253{
254 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
[89ea2dc]255 bool ret = spinlock_trylock(&(lock->lock));
[a35b458]256
[89ea2dc]257 ASSERT_IRQ_SPINLOCK((!ret) || (!lock->guard), lock);
258 return ret;
[a9f1372]259}
260
261/** Pass lock from one interrupts-disabled spinlock to another
262 *
263 * Pass lock from one IRQ spinlock to another IRQ spinlock
264 * without enabling interrupts during the process.
265 *
266 * The first IRQ spinlock is supposed to be locked.
267 *
268 * @param unlock IRQ spinlock to be unlocked.
269 * @param lock IRQ spinlock to be locked.
270 *
271 */
272void irq_spinlock_pass(irq_spinlock_t *unlock, irq_spinlock_t *lock)
273{
274 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
[a35b458]275
[a9f1372]276 /* Pass guard from unlock to lock */
277 bool guard = unlock->guard;
278 ipl_t ipl = unlock->ipl;
279 unlock->guard = false;
[a35b458]280
[a9f1372]281 spinlock_unlock(&(unlock->lock));
282 spinlock_lock(&(lock->lock));
[a35b458]283
[a9f1372]284 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
[a35b458]285
[a9f1372]286 if (guard) {
287 lock->guard = true;
288 lock->ipl = ipl;
289 }
290}
291
292/** Hand-over-hand locking of interrupts-disabled spinlocks
293 *
294 * Implement hand-over-hand locking between two interrupts-disabled
295 * spinlocks without enabling interrupts during the process.
296 *
297 * The first IRQ spinlock is supposed to be locked.
298 *
299 * @param unlock IRQ spinlock to be unlocked.
300 * @param lock IRQ spinlock to be locked.
301 *
302 */
303void irq_spinlock_exchange(irq_spinlock_t *unlock, irq_spinlock_t *lock)
304{
305 ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
[a35b458]306
[a9f1372]307 spinlock_lock(&(lock->lock));
308 ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
[a35b458]309
[a9f1372]310 /* Pass guard from unlock to lock */
311 if (unlock->guard) {
312 lock->guard = true;
313 lock->ipl = unlock->ipl;
314 unlock->guard = false;
315 }
[a35b458]316
[a9f1372]317 spinlock_unlock(&(unlock->lock));
318}
319
[ffe4a87]320/** Find out whether the IRQ spinlock is currently locked.
321 *
322 * @param lock IRQ spinlock.
323 * @return True if the IRQ spinlock is locked, false otherwise.
324 */
325bool irq_spinlock_locked(irq_spinlock_t *ilock)
326{
327 return spinlock_locked(&ilock->lock);
328}
329
[cc73a8a1]330/** @}
[b45c443]331 */
Note: See TracBrowser for help on using the repository browser.