source: mainline/kernel/generic/src/synch/spinlock.c@ 8addb24a

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

Turn spin look hint into a function

  • Property mode set to 100644
File size: 5.0 KB
RevLine 
[f761f1eb]1/*
[df4ed85]2 * Copyright (c) 2001-2004 Jakub Jermar
[f43d8ce]3 * Copyright (c) 2023 Jiří Zárevúcky
[f761f1eb]4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
[e88eb48]30/** @addtogroup kernel_sync
[b45c443]31 * @{
32 */
33
[cf26ba9]34/**
[b45c443]35 * @file
[b60c582]36 * @brief Spinlocks.
[cf26ba9]37 */
[b60c582]38
[2b264c4]39#include <arch/asm.h>
[5e04b48d]40#include <synch/spinlock.h>
[23684b7]41#include <atomic.h>
[05882233]42#include <barrier.h>
[5e04b48d]43#include <arch.h>
[c842f04]44#include <preemption.h>
[bab75df6]45#include <stdio.h>
[5e04b48d]46#include <debug.h>
[2d93f1f9]47#include <symtab.h>
[311929ec]48#include <stacktrace.h>
[1066041]49#include <cpu.h>
[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{
[f43d8ce]58#ifdef CONFIG_SMP
[78de83de]59 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
[2d93f1f9]60#ifdef CONFIG_DEBUG_SPINLOCK
[90c8b8d]61 lock->name = name;
62#endif
[f43d8ce]63#endif
[f761f1eb]64}
65
[5e04b48d]66/** Lock spinlock
67 *
[90c8b8d]68 * @param lock Pointer to spinlock_t structure.
69 *
[5e04b48d]70 */
[f43d8ce]71void spinlock_lock(spinlock_t *lock)
[f761f1eb]72{
[f43d8ce]73 preemption_disable();
74
75#ifdef CONFIG_SMP
[05e2a7ad]76 bool deadlock_reported = false;
[f43d8ce]77 size_t i = 0;
[a35b458]78
[78de83de]79 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
[8addb24a]80 cpu_spin_hint();
[2b264c4]81
[f43d8ce]82#ifdef CONFIG_DEBUG_SPINLOCK
[d9cf9d5f]83 /*
[90c8b8d]84 * We need to be careful about particular locks
85 * which are directly used to report deadlocks
86 * via printf() (and recursively other functions).
87 * This conserns especially printf_lock and the
88 * framebuffer lock.
[c263c77]89 *
90 * Any lock whose name is prefixed by "*" will be
91 * ignored by this deadlock detection routine
92 * as this might cause an infinite recursion.
93 * We trust our code that there is no possible deadlock
94 * caused by these locks (except when an exception
95 * is triggered for instance by printf()).
96 *
97 * We encountered false positives caused by very
98 * slow framebuffer interaction (especially when
99 * run in a simulator) that caused problems with both
100 * printf_lock and the framebuffer lock.
[d9cf9d5f]101 */
[c263c77]102 if (lock->name[0] == '*')
103 continue;
[a35b458]104
[14df080]105 if (i++ > DEADLOCK_THRESHOLD) {
[7e752b2]106 printf("cpu%u: looping on spinlock %p:%s, "
107 "caller=%p (%s)\n", CPU->id, lock, lock->name,
108 (void *) CALLER, symtab_fmt_name_lookup(CALLER));
[311929ec]109 stack_trace();
[a35b458]110
[f761f1eb]111 i = 0;
[05e2a7ad]112 deadlock_reported = true;
[f761f1eb]113 }
[f43d8ce]114#endif
[f761f1eb]115 }
[a35b458]116
[f43d8ce]117 /* Avoid compiler warning with debug disabled. */
118 (void) i;
119
[05e2a7ad]120 if (deadlock_reported)
[8ed4014]121 printf("cpu%u: not deadlocked\n", CPU->id);
[f43d8ce]122
123#endif
[f761f1eb]124}
[90c8b8d]125
[13108f24]126/** Unlock spinlock
127 *
128 * @param sl Pointer to spinlock_t structure.
129 */
[f43d8ce]130void spinlock_unlock(spinlock_t *lock)
[13108f24]131{
[f43d8ce]132#ifdef CONFIG_SMP
133#ifdef CONFIG_DEBUG_SPINLOCK
[ffe4a87]134 ASSERT_SPINLOCK(spinlock_locked(lock), lock);
[f43d8ce]135#endif
[a35b458]136
[78de83de]137 atomic_flag_clear_explicit(&lock->flag, memory_order_release);
[f43d8ce]138#endif
139
[13108f24]140 preemption_enable();
141}
142
[f43d8ce]143/**
[2b4a9f26]144 * Lock spinlock conditionally. If the spinlock is not available
145 * at the moment, signal failure.
[5e04b48d]146 *
[90c8b8d]147 * @param lock Pointer to spinlock_t structure.
[5e04b48d]148 *
[f43d8ce]149 * @return true on success.
[90c8b8d]150 *
[5e04b48d]151 */
[89ea2dc]152bool spinlock_trylock(spinlock_t *lock)
[f761f1eb]153{
[c842f04]154 preemption_disable();
[f43d8ce]155
156#ifdef CONFIG_SMP
[78de83de]157 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
[a35b458]158
[89ea2dc]159 if (!ret)
[c842f04]160 preemption_enable();
[a35b458]161
[89ea2dc]162 return ret;
[f43d8ce]163#else
164 return true;
165#endif
[f761f1eb]166}
167
[ffe4a87]168/** Find out whether the spinlock is currently locked.
169 *
170 * @param lock Spinlock.
171 * @return True if the spinlock is locked, false otherwise.
172 */
173bool spinlock_locked(spinlock_t *lock)
174{
[f43d8ce]175#ifdef CONFIG_SMP
[95d45482]176 // NOTE: Atomic flag doesn't support simple atomic read (by design),
177 // so instead we test_and_set and then clear if necessary.
178 // This function is only used inside assert, so we don't need
179 // any preemption_disable/enable here.
[78de83de]180
181 bool ret = atomic_flag_test_and_set_explicit(&lock->flag, memory_order_relaxed);
182 if (!ret)
183 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
184 return ret;
[f43d8ce]185#else
186 return true;
[f761f1eb]187#endif
[f43d8ce]188}
[b45c443]189
[cc73a8a1]190/** @}
[b45c443]191 */
Note: See TracBrowser for help on using the repository browser.