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
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 <synch/spinlock.h>
40#include <atomic.h>
41#include <barrier.h>
42#include <arch.h>
43#include <preemption.h>
44#include <stdio.h>
45#include <debug.h>
46#include <symtab.h>
47#include <stacktrace.h>
48#include <cpu.h>
49
50/** Initialize spinlock
51 *
52 * @param sl Pointer to spinlock_t structure.
53 *
54 */
55void spinlock_initialize(spinlock_t *lock, const char *name)
56{
57#ifdef CONFIG_SMP
58 atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
59#ifdef CONFIG_DEBUG_SPINLOCK
60 lock->name = name;
61#endif
62#endif
63}
64
65/** Lock spinlock
66 *
67 * @param lock Pointer to spinlock_t structure.
68 *
69 */
70void spinlock_lock(spinlock_t *lock)
71{
72 preemption_disable();
73
74#ifdef CONFIG_SMP
75 bool deadlock_reported = false;
76 size_t i = 0;
77
78 while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
79#ifdef CONFIG_DEBUG_SPINLOCK
80 /*
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.
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.
98 */
99 if (lock->name[0] == '*')
100 continue;
101
102 if (i++ > DEADLOCK_THRESHOLD) {
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));
106 stack_trace();
107
108 i = 0;
109 deadlock_reported = true;
110 }
111#endif
112 }
113
114 /* Avoid compiler warning with debug disabled. */
115 (void) i;
116
117 if (deadlock_reported)
118 printf("cpu%u: not deadlocked\n", CPU->id);
119
120#endif
121}
122
123/** Unlock spinlock
124 *
125 * @param sl Pointer to spinlock_t structure.
126 */
127void spinlock_unlock(spinlock_t *lock)
128{
129#ifdef CONFIG_SMP
130#ifdef CONFIG_DEBUG_SPINLOCK
131 ASSERT_SPINLOCK(spinlock_locked(lock), lock);
132#endif
133
134 atomic_flag_clear_explicit(&lock->flag, memory_order_release);
135#endif
136
137 preemption_enable();
138}
139
140/**
141 * Lock spinlock conditionally. If the spinlock is not available
142 * at the moment, signal failure.
143 *
144 * @param lock Pointer to spinlock_t structure.
145 *
146 * @return true on success.
147 *
148 */
149bool spinlock_trylock(spinlock_t *lock)
150{
151 preemption_disable();
152
153#ifdef CONFIG_SMP
154 bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
155
156 if (!ret)
157 preemption_enable();
158
159 return ret;
160#else
161 return true;
162#endif
163}
164
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{
172#ifdef CONFIG_SMP
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.
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;
182#else
183 return true;
184#endif
185}
186
187/** @}
188 */
Note: See TracBrowser for help on using the repository browser.