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

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

Make spinlock_lock/unlock into proper functions in all configurations

  • Property mode set to 100644
File size: 4.9 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
[5e04b48d]39#include <synch/spinlock.h>
[23684b7]40#include <atomic.h>
[05882233]41#include <barrier.h>
[5e04b48d]42#include <arch.h>
[c842f04]43#include <preemption.h>
[bab75df6]44#include <stdio.h>
[5e04b48d]45#include <debug.h>
[2d93f1f9]46#include <symtab.h>
[311929ec]47#include <stacktrace.h>
[1066041]48#include <cpu.h>
[f761f1eb]49
[5e04b48d]50/** Initialize spinlock
51 *
52 * @param sl Pointer to spinlock_t structure.
[90c8b8d]53 *
[5e04b48d]54 */
[a000878c]55void spinlock_initialize(spinlock_t *lock, const char *name)
[f761f1eb]56{
[f43d8ce]57#ifdef CONFIG_SMP
[78de83de]58 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
[2d93f1f9]59#ifdef CONFIG_DEBUG_SPINLOCK
[90c8b8d]60 lock->name = name;
61#endif
[f43d8ce]62#endif
[f761f1eb]63}
64
[5e04b48d]65/** Lock spinlock
66 *
[90c8b8d]67 * @param lock Pointer to spinlock_t structure.
68 *
[5e04b48d]69 */
[f43d8ce]70void spinlock_lock(spinlock_t *lock)
[f761f1eb]71{
[f43d8ce]72 preemption_disable();
73
74#ifdef CONFIG_SMP
[05e2a7ad]75 bool deadlock_reported = false;
[f43d8ce]76 size_t i = 0;
[a35b458]77
[78de83de]78 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
[f43d8ce]79#ifdef CONFIG_DEBUG_SPINLOCK
[d9cf9d5f]80 /*
[90c8b8d]81 * We need to be careful about particular locks
82 * which are directly used to report deadlocks
83 * via printf() (and recursively other functions).
84 * This conserns especially printf_lock and the
85 * framebuffer lock.
[c263c77]86 *
87 * Any lock whose name is prefixed by "*" will be
88 * ignored by this deadlock detection routine
89 * as this might cause an infinite recursion.
90 * We trust our code that there is no possible deadlock
91 * caused by these locks (except when an exception
92 * is triggered for instance by printf()).
93 *
94 * We encountered false positives caused by very
95 * slow framebuffer interaction (especially when
96 * run in a simulator) that caused problems with both
97 * printf_lock and the framebuffer lock.
[d9cf9d5f]98 */
[c263c77]99 if (lock->name[0] == '*')
100 continue;
[a35b458]101
[14df080]102 if (i++ > DEADLOCK_THRESHOLD) {
[7e752b2]103 printf("cpu%u: looping on spinlock %p:%s, "
104 "caller=%p (%s)\n", CPU->id, lock, lock->name,
105 (void *) CALLER, symtab_fmt_name_lookup(CALLER));
[311929ec]106 stack_trace();
[a35b458]107
[f761f1eb]108 i = 0;
[05e2a7ad]109 deadlock_reported = true;
[f761f1eb]110 }
[f43d8ce]111#endif
[f761f1eb]112 }
[a35b458]113
[f43d8ce]114 /* Avoid compiler warning with debug disabled. */
115 (void) i;
116
[05e2a7ad]117 if (deadlock_reported)
[8ed4014]118 printf("cpu%u: not deadlocked\n", CPU->id);
[f43d8ce]119
120#endif
[f761f1eb]121}
[90c8b8d]122
[13108f24]123/** Unlock spinlock
124 *
125 * @param sl Pointer to spinlock_t structure.
126 */
[f43d8ce]127void spinlock_unlock(spinlock_t *lock)
[13108f24]128{
[f43d8ce]129#ifdef CONFIG_SMP
130#ifdef CONFIG_DEBUG_SPINLOCK
[ffe4a87]131 ASSERT_SPINLOCK(spinlock_locked(lock), lock);
[f43d8ce]132#endif
[a35b458]133
[78de83de]134 atomic_flag_clear_explicit(&lock->flag, memory_order_release);
[f43d8ce]135#endif
136
[13108f24]137 preemption_enable();
138}
139
[f43d8ce]140/**
[2b4a9f26]141 * Lock spinlock conditionally. If the spinlock is not available
142 * at the moment, signal failure.
[5e04b48d]143 *
[90c8b8d]144 * @param lock Pointer to spinlock_t structure.
[5e04b48d]145 *
[f43d8ce]146 * @return true on success.
[90c8b8d]147 *
[5e04b48d]148 */
[89ea2dc]149bool spinlock_trylock(spinlock_t *lock)
[f761f1eb]150{
[c842f04]151 preemption_disable();
[f43d8ce]152
153#ifdef CONFIG_SMP
[78de83de]154 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
[a35b458]155
[89ea2dc]156 if (!ret)
[c842f04]157 preemption_enable();
[a35b458]158
[89ea2dc]159 return ret;
[f43d8ce]160#else
161 return true;
162#endif
[f761f1eb]163}
164
[ffe4a87]165/** Find out whether the spinlock is currently locked.
166 *
167 * @param lock Spinlock.
168 * @return True if the spinlock is locked, false otherwise.
169 */
170bool spinlock_locked(spinlock_t *lock)
171{
[f43d8ce]172#ifdef CONFIG_SMP
[95d45482]173 // NOTE: Atomic flag doesn't support simple atomic read (by design),
174 // so instead we test_and_set and then clear if necessary.
175 // This function is only used inside assert, so we don't need
176 // any preemption_disable/enable here.
[78de83de]177
178 bool ret = atomic_flag_test_and_set_explicit(&lock->flag, memory_order_relaxed);
179 if (!ret)
180 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
181 return ret;
[f43d8ce]182#else
183 return true;
[f761f1eb]184#endif
[f43d8ce]185}
[b45c443]186
[cc73a8a1]187/** @}
[b45c443]188 */
Note: See TracBrowser for help on using the repository browser.