source: mainline/kernel/generic/include/synch/spinlock.h

Last change on this file was 4f84ee42, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Fix a bug accidentally introduced in 4777e022

  • Property mode set to 100644
File size: 6.2 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/** @file
34 */
35
[06e1e95]36#ifndef KERN_SPINLOCK_H_
37#define KERN_SPINLOCK_H_
[f761f1eb]38
[78de83de]39#include <stdatomic.h>
40#include <stdbool.h>
[f761f1eb]41
[f43d8ce]42#include <arch/types.h>
43#include <assert.h>
44
45#define DEADLOCK_THRESHOLD 100000000
46
47#if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
48
49#include <log.h>
50
51#define DEADLOCK_PROBE_INIT(pname) size_t pname = 0
52
53#define DEADLOCK_PROBE(pname, value) \
54 if ((pname)++ > (value)) { \
55 (pname) = 0; \
56 log(LF_OTHER, LVL_WARN, \
57 "Deadlock probe %s: exceeded threshold %u\n" \
58 "cpu%u: function=%s, line=%u\n", \
59 #pname, (value), CPU->id, __func__, __LINE__); \
60 }
61
62#else /* CONFIG_DEBUG_SPINLOCK */
63
64#define DEADLOCK_PROBE_INIT(pname)
65#define DEADLOCK_PROBE(pname, value)
66
67#endif /* CONFIG_DEBUG_SPINLOCK */
[90c8b8d]68
[cc106e4]69typedef struct spinlock {
[f43d8ce]70#ifdef CONFIG_SMP
[78de83de]71 atomic_flag flag;
[a35b458]72
[2d93f1f9]73#ifdef CONFIG_DEBUG_SPINLOCK
[a000878c]74 const char *name;
[2b4a9f26]75#endif /* CONFIG_DEBUG_SPINLOCK */
[f43d8ce]76#endif
[e71a61d]77} spinlock_t;
[f761f1eb]78
[dc747e3]79/*
80 * SPINLOCK_DECLARE is to be used for dynamically allocated spinlocks,
81 * where the lock gets initialized in run time.
82 */
[90c8b8d]83#define SPINLOCK_DECLARE(lock_name) spinlock_t lock_name
84#define SPINLOCK_EXTERN(lock_name) extern spinlock_t lock_name
[dc747e3]85
[f43d8ce]86#ifdef CONFIG_SMP
87#ifdef CONFIG_DEBUG_SPINLOCK
88#define SPINLOCK_INITIALIZER(desc_name) { .name = (desc_name), .flag = ATOMIC_FLAG_INIT }
89#else
90#define SPINLOCK_INITIALIZER(desc_name) { .flag = ATOMIC_FLAG_INIT }
91#endif
92#else
93#define SPINLOCK_INITIALIZER(desc_name) {}
94#endif
95
[dc747e3]96/*
[2b4a9f26]97 * SPINLOCK_INITIALIZE and SPINLOCK_STATIC_INITIALIZE are to be used
98 * for statically allocated spinlocks. They declare (either as global
99 * or static) symbol and initialize the lock.
[dc747e3]100 */
[90c8b8d]101#define SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
[f43d8ce]102 spinlock_t lock_name = SPINLOCK_INITIALIZER(desc_name)
[90c8b8d]103
104#define SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
[f43d8ce]105 static spinlock_t lock_name = SPINLOCK_INITIALIZER(desc_name)
[53f9821]106
[f43d8ce]107#if defined(CONFIG_SMP) && defined(CONFIG_DEBUG_SPINLOCK)
108#define ASSERT_SPINLOCK(expr, lock) assert_verbose(expr, (lock)->name)
[2b4a9f26]109#else /* CONFIG_DEBUG_SPINLOCK */
[f43d8ce]110#define ASSERT_SPINLOCK(expr, lock) assert(expr)
[2b4a9f26]111#endif /* CONFIG_DEBUG_SPINLOCK */
[53f9821]112
[90c8b8d]113#define SPINLOCK_INITIALIZE(lock_name) \
114 SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
115
116#define SPINLOCK_STATIC_INITIALIZE(lock_name) \
117 SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
118
[4777e02]119#ifdef CONFIG_SMP
120
[2b4a9f26]121extern void spinlock_initialize(spinlock_t *, const char *);
[89ea2dc]122extern bool spinlock_trylock(spinlock_t *);
[f43d8ce]123extern void spinlock_lock(spinlock_t *);
124extern void spinlock_unlock(spinlock_t *);
[ffe4a87]125extern bool spinlock_locked(spinlock_t *);
[90c8b8d]126
[4777e02]127#else
128
129#include <preemption.h>
130
131static inline void spinlock_initialize(spinlock_t *l, const char *name)
132{
133}
134
135static inline bool spinlock_trylock(spinlock_t *l)
136{
[4f84ee42]137 preemption_disable();
[4777e02]138 return true;
139}
140
141static inline void spinlock_lock(spinlock_t *l)
142{
143 preemption_disable();
144}
145
146static inline void spinlock_unlock(spinlock_t *l)
147{
148 preemption_enable();
149}
150
151static inline bool spinlock_locked(spinlock_t *l)
152{
153 return true;
154}
155
156#endif
157
[2b4a9f26]158typedef struct {
[b076dfb]159 spinlock_t lock; /**< Spinlock */
160 bool guard; /**< Flag whether ipl is valid */
161 ipl_t ipl; /**< Original interrupt level */
162#ifdef CONFIG_DEBUG_SPINLOCK
163 _Atomic(struct cpu *) owner; /**< Which cpu currently owns this lock */
164#endif
[2b4a9f26]165} irq_spinlock_t;
166
167#define IRQ_SPINLOCK_DECLARE(lock_name) irq_spinlock_t lock_name
168#define IRQ_SPINLOCK_EXTERN(lock_name) extern irq_spinlock_t lock_name
169
170#define ASSERT_IRQ_SPINLOCK(expr, irq_lock) \
171 ASSERT_SPINLOCK(expr, &((irq_lock)->lock))
172
[f43d8ce]173#define IRQ_SPINLOCK_INITIALIZER(desc_name) \
174 { \
175 .lock = SPINLOCK_INITIALIZER(desc_name), \
[2b4a9f26]176 .guard = false, \
[f43d8ce]177 .ipl = 0, \
[2b4a9f26]178 }
179
[8aa9265]180/*
[f43d8ce]181 * IRQ_SPINLOCK_INITIALIZE and IRQ_SPINLOCK_STATIC_INITIALIZE are to be used
182 * for statically allocated interrupts-disabled spinlocks. They declare (either
183 * as global or static symbol) and initialize the lock.
[8aa9265]184 */
185#define IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, desc_name) \
[f43d8ce]186 irq_spinlock_t lock_name = IRQ_SPINLOCK_INITIALIZER(desc_name)
[8aa9265]187
188#define IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, desc_name) \
[f43d8ce]189 static irq_spinlock_t lock_name = IRQ_SPINLOCK_INITIALIZER(desc_name)
[8aa9265]190
[2b4a9f26]191#define IRQ_SPINLOCK_INITIALIZE(lock_name) \
192 IRQ_SPINLOCK_INITIALIZE_NAME(lock_name, #lock_name)
193
194#define IRQ_SPINLOCK_STATIC_INITIALIZE(lock_name) \
195 IRQ_SPINLOCK_STATIC_INITIALIZE_NAME(lock_name, #lock_name)
196
[a9f1372]197extern void irq_spinlock_initialize(irq_spinlock_t *, const char *);
198extern void irq_spinlock_lock(irq_spinlock_t *, bool);
199extern void irq_spinlock_unlock(irq_spinlock_t *, bool);
[89ea2dc]200extern bool irq_spinlock_trylock(irq_spinlock_t *);
[a9f1372]201extern void irq_spinlock_pass(irq_spinlock_t *, irq_spinlock_t *);
202extern void irq_spinlock_exchange(irq_spinlock_t *, irq_spinlock_t *);
[ffe4a87]203extern bool irq_spinlock_locked(irq_spinlock_t *);
[f761f1eb]204
205#endif
[b45c443]206
[06e1e95]207/** @}
[b45c443]208 */
Note: See TracBrowser for help on using the repository browser.