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

ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b596d0d 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
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * Copyright (c) 2023 Jiří Zárevúcky
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
30/** @addtogroup kernel_sync
31 * @{
32 */
33
34/**
35 * @file
36 * @brief Spinlocks.
37 */
38
39#include <arch/asm.h>
40#include <synch/spinlock.h>
41#include <atomic.h>
42#include <barrier.h>
43#include <arch.h>
44#include <preemption.h>
45#include <stdio.h>
46#include <debug.h>
47#include <symtab.h>
48#include <stacktrace.h>
49#include <cpu.h>
50
51/** Initialize spinlock
52 *
53 * @param sl Pointer to spinlock_t structure.
54 *
55 */
56void spinlock_initialize(spinlock_t *lock, const char *name)
57{
58#ifdef CONFIG_SMP
59 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
60#ifdef CONFIG_DEBUG_SPINLOCK
61 lock->name = name;
62#endif
63#endif
64}
65
66/** Lock spinlock
67 *
68 * @param lock Pointer to spinlock_t structure.
69 *
70 */
71void spinlock_lock(spinlock_t *lock)
72{
73 preemption_disable();
74
75#ifdef CONFIG_SMP
76 bool deadlock_reported = false;
77 size_t i = 0;
78
79 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
80 cpu_spin_hint();
81
82#ifdef CONFIG_DEBUG_SPINLOCK
83 /*
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.
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.
101 */
102 if (lock->name[0] == '*')
103 continue;
104
105 if (i++ > DEADLOCK_THRESHOLD) {
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));
109 stack_trace();
110
111 i = 0;
112 deadlock_reported = true;
113 }
114#endif
115 }
116
117 /* Avoid compiler warning with debug disabled. */
118 (void) i;
119
120 if (deadlock_reported)
121 printf("cpu%u: not deadlocked\n", CPU->id);
122
123#endif
124}
125
126/** Unlock spinlock
127 *
128 * @param sl Pointer to spinlock_t structure.
129 */
130void spinlock_unlock(spinlock_t *lock)
131{
132#ifdef CONFIG_SMP
133#ifdef CONFIG_DEBUG_SPINLOCK
134 ASSERT_SPINLOCK(spinlock_locked(lock), lock);
135#endif
136
137 atomic_flag_clear_explicit(&lock->flag, memory_order_release);
138#endif
139
140 preemption_enable();
141}
142
143/**
144 * Lock spinlock conditionally. If the spinlock is not available
145 * at the moment, signal failure.
146 *
147 * @param lock Pointer to spinlock_t structure.
148 *
149 * @return true on success.
150 *
151 */
152bool spinlock_trylock(spinlock_t *lock)
153{
154 preemption_disable();
155
156#ifdef CONFIG_SMP
157 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
158
159 if (!ret)
160 preemption_enable();
161
162 return ret;
163#else
164 return true;
165#endif
166}
167
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{
175#ifdef CONFIG_SMP
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.
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;
185#else
186 return true;
187#endif
188}
189
190/** @}
191 */
Note: See TracBrowser for help on using the repository browser.