Zephyr API Documentation 4.3.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
spinlock.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
11
12#ifndef ZEPHYR_INCLUDE_SPINLOCK_H_
13#define ZEPHYR_INCLUDE_SPINLOCK_H_
14
15#include <errno.h>
16#include <stdbool.h>
17
18#include <zephyr/arch/cpu.h>
19#include <zephyr/sys/atomic.h>
20#include <zephyr/sys/__assert.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
33
34struct z_spinlock_key {
35 int key;
36};
37
45struct k_spinlock {
49#ifdef CONFIG_SMP
50#ifdef CONFIG_TICKET_SPINLOCKS
51 /*
52 * Ticket spinlocks are conceptually two atomic variables,
53 * one indicating the current FIFO head (spinlock owner),
54 * and the other indicating the current FIFO tail.
55 * Spinlock is acquired in the following manner:
56 * - current FIFO tail value is atomically incremented while it's
57 * original value is saved as a "ticket"
58 * - we spin until the FIFO head becomes equal to the ticket value
59 *
60 * Spinlock is released by atomic increment of the FIFO head
61 */
62 atomic_t owner;
63 atomic_t tail;
64#else
65 atomic_t locked;
66#endif /* CONFIG_TICKET_SPINLOCKS */
67#endif /* CONFIG_SMP */
68
69#ifdef CONFIG_SPIN_VALIDATE
70 /* Stores the thread that holds the lock with the locking CPU
71 * ID in the bottom two bits.
72 */
73 uintptr_t thread_cpu;
74#ifdef CONFIG_SPIN_LOCK_TIME_LIMIT
75 /* Stores the time (in cycles) when a lock was taken
76 */
77 uint32_t lock_time;
78#endif /* CONFIG_SPIN_LOCK_TIME_LIMIT */
79#endif /* CONFIG_SPIN_VALIDATE */
80
81#if defined(CONFIG_NONZERO_SPINLOCK_SIZE) && !defined(CONFIG_SMP) && !defined(CONFIG_SPIN_VALIDATE)
82 /* Add a dummy field to guarantee the spinlock has a non-zero
83 * size. If neither CONFIG_SMP nor CONFIG_SPIN_VALIDATE are
84 * defined then the k_spinlock struct would otherwise have no
85 * members and sizeof(k_spinlock) would be 0 in C and 1 in C++.
86 *
87 * That size difference causes problems when the k_spinlock
88 * is embedded into another struct like k_msgq, because C and
89 * C++ will have different ideas on the offsets of the members
90 * that come after the k_spinlock member.
91 */
92 char dummy;
93#endif
97};
98
99/* There's a spinlock validation framework available when asserts are
100 * enabled. It adds a relatively hefty overhead (about 3k or so) to
101 * kernel code size, don't use on platforms known to be small.
102 */
103#ifdef CONFIG_SPIN_VALIDATE
104bool z_spin_lock_valid(struct k_spinlock *l);
105bool z_spin_unlock_valid(struct k_spinlock *l);
106void z_spin_lock_set_owner(struct k_spinlock *l);
107BUILD_ASSERT(CONFIG_MP_MAX_NUM_CPUS <= 4, "Too many CPUs for mask");
108
109# ifdef CONFIG_KERNEL_COHERENCE
110bool z_spin_lock_mem_coherent(struct k_spinlock *l);
111# endif /* CONFIG_KERNEL_COHERENCE */
112
113#endif /* CONFIG_SPIN_VALIDATE */
114
126typedef struct z_spinlock_key k_spinlock_key_t;
127
128static ALWAYS_INLINE void z_spinlock_validate_pre(struct k_spinlock *l)
129{
130 ARG_UNUSED(l);
131#ifdef CONFIG_SPIN_VALIDATE
132 __ASSERT(z_spin_lock_valid(l), "Invalid spinlock %p", l);
133#ifdef CONFIG_KERNEL_COHERENCE
134 __ASSERT_NO_MSG(z_spin_lock_mem_coherent(l));
135#endif
136#endif
137}
138
139static ALWAYS_INLINE void z_spinlock_validate_post(struct k_spinlock *l)
140{
141 ARG_UNUSED(l);
142#ifdef CONFIG_SPIN_VALIDATE
143 z_spin_lock_set_owner(l);
144#if defined(CONFIG_SPIN_LOCK_TIME_LIMIT) && (CONFIG_SPIN_LOCK_TIME_LIMIT != 0)
145 l->lock_time = sys_clock_cycle_get_32();
146#endif /* CONFIG_SPIN_LOCK_TIME_LIMIT */
147#endif /* CONFIG_SPIN_VALIDATE */
148}
149
182{
183 ARG_UNUSED(l);
185
186 /* Note that we need to use the underlying arch-specific lock
187 * implementation. The "irq_lock()" API in SMP context is
188 * actually a wrapper for a global spinlock!
189 */
190 k.key = arch_irq_lock();
191
192 z_spinlock_validate_pre(l);
193#ifdef CONFIG_SMP
194#ifdef CONFIG_TICKET_SPINLOCKS
195 /*
196 * Enqueue ourselves to the end of a spinlock waiters queue
197 * receiving a ticket
198 */
199 atomic_val_t ticket = atomic_inc(&l->tail);
200 /* Spin until our ticket is served */
201 while (atomic_get(&l->owner) != ticket) {
203 }
204#else
205 while (!atomic_cas(&l->locked, 0, 1)) {
207 }
208#endif /* CONFIG_TICKET_SPINLOCKS */
209#endif /* CONFIG_SMP */
210 z_spinlock_validate_post(l);
211
212 return k;
213}
214
230{
231 int key = arch_irq_lock();
232
233 z_spinlock_validate_pre(l);
234#ifdef CONFIG_SMP
235#ifdef CONFIG_TICKET_SPINLOCKS
236 /*
237 * atomic_get and atomic_cas operations below are not executed
238 * simultaneously.
239 * So in theory k_spin_trylock can lock an already locked spinlock.
240 * To reproduce this the following conditions should be met after we
241 * executed atomic_get and before we executed atomic_cas:
242 *
243 * - spinlock needs to be taken 0xffff_..._ffff + 1 times
244 * (which requires 0xffff_..._ffff number of CPUs, as k_spin_lock call
245 * is blocking) or
246 * - spinlock needs to be taken and released 0xffff_..._ffff times and
247 * then taken again
248 *
249 * In real-life systems this is considered non-reproducible given that
250 * required actions need to be done during this tiny window of several
251 * CPU instructions (which execute with interrupt locked,
252 * so no preemption can happen here)
253 */
254 atomic_val_t ticket_val = atomic_get(&l->owner);
255
256 if (!atomic_cas(&l->tail, ticket_val, ticket_val + 1)) {
257 goto busy;
258 }
259#else
260 if (!atomic_cas(&l->locked, 0, 1)) {
261 goto busy;
262 }
263#endif /* CONFIG_TICKET_SPINLOCKS */
264#endif /* CONFIG_SMP */
265 z_spinlock_validate_post(l);
266
267 k->key = key;
268
269 return 0;
270
271#ifdef CONFIG_SMP
272busy:
273 arch_irq_unlock(key);
274 return -EBUSY;
275#endif /* CONFIG_SMP */
276}
277
301{
302 ARG_UNUSED(l);
303#ifdef CONFIG_SPIN_VALIDATE
304 __ASSERT(z_spin_unlock_valid(l), "Not my spinlock %p", l);
305
306#if defined(CONFIG_SPIN_LOCK_TIME_LIMIT) && (CONFIG_SPIN_LOCK_TIME_LIMIT != 0)
307 uint32_t delta = sys_clock_cycle_get_32() - l->lock_time;
308
309 __ASSERT(delta < CONFIG_SPIN_LOCK_TIME_LIMIT,
310 "Spin lock %p held %u cycles, longer than limit of %u cycles",
311 l, delta, CONFIG_SPIN_LOCK_TIME_LIMIT);
312#endif /* CONFIG_SPIN_LOCK_TIME_LIMIT */
313#endif /* CONFIG_SPIN_VALIDATE */
314
315#ifdef CONFIG_SMP
316#ifdef CONFIG_TICKET_SPINLOCKS
317 /* Give the spinlock to the next CPU in a FIFO */
318 (void)atomic_inc(&l->owner);
319#else
320 /* Strictly we don't need atomic_clear() here (which is an
321 * exchange operation that returns the old value). We are always
322 * setting a zero and (because we hold the lock) know the existing
323 * state won't change due to a race. But some architectures need
324 * a memory barrier when used like this, and we don't have a
325 * Zephyr framework for that.
326 */
327 (void)atomic_clear(&l->locked);
328#endif /* CONFIG_TICKET_SPINLOCKS */
329#endif /* CONFIG_SMP */
330 arch_irq_unlock(key.key);
331}
332
336
337#if defined(CONFIG_SMP) && defined(CONFIG_TEST)
338/*
339 * @brief Checks if spinlock is held by some CPU, including the local CPU.
340 * This API shouldn't be used outside the tests for spinlock
341 *
342 * @param l A pointer to the spinlock
343 * @retval true - if spinlock is held by some CPU; false - otherwise
344 */
345static ALWAYS_INLINE bool z_spin_is_locked(struct k_spinlock *l)
346{
347#ifdef CONFIG_TICKET_SPINLOCKS
348 atomic_val_t ticket_val = atomic_get(&l->owner);
349
350 return !atomic_cas(&l->tail, ticket_val, ticket_val);
351#else
352 return l->locked;
353#endif /* CONFIG_TICKET_SPINLOCKS */
354}
355#endif /* defined(CONFIG_SMP) && defined(CONFIG_TEST) */
356
357/* Internal function: releases the lock, but leaves local interrupts disabled */
358static ALWAYS_INLINE void k_spin_release(struct k_spinlock *l)
359{
360 ARG_UNUSED(l);
361#ifdef CONFIG_SPIN_VALIDATE
362 __ASSERT(z_spin_unlock_valid(l), "Not my spinlock %p", l);
363#endif
364#ifdef CONFIG_SMP
365#ifdef CONFIG_TICKET_SPINLOCKS
366 (void)atomic_inc(&l->owner);
367#else
368 (void)atomic_clear(&l->locked);
369#endif /* CONFIG_TICKET_SPINLOCKS */
370#endif /* CONFIG_SMP */
371}
372
373#if defined(CONFIG_SPIN_VALIDATE) && defined(__GNUC__)
374static ALWAYS_INLINE void z_spin_onexit(__maybe_unused k_spinlock_key_t *k)
375{
376 __ASSERT(k->key, "K_SPINLOCK exited with goto, break or return, "
377 "use K_SPINLOCK_BREAK instead.");
378}
379#define K_SPINLOCK_ONEXIT __attribute__((__cleanup__(z_spin_onexit)))
380#else
381#define K_SPINLOCK_ONEXIT
382#endif
383
387
394#define K_SPINLOCK_BREAK continue
395
437#define K_SPINLOCK(lck) \
438 for (k_spinlock_key_t __i K_SPINLOCK_ONEXIT = {}, __key = k_spin_lock(lck); !__i.key; \
439 k_spin_unlock((lck), __key), __i.key = 1)
440
442
443#ifdef __cplusplus
444}
445#endif
446
447#endif /* ZEPHYR_INCLUDE_SPINLOCK_H_ */
uint32_t sys_clock_cycle_get_32(void)
static ALWAYS_INLINE unsigned int arch_irq_lock(void)
Disable all interrupts on the local CPU.
Definition irq.h:168
static ALWAYS_INLINE void arch_irq_unlock(unsigned int key)
Definition irq.h:176
void arch_spin_relax(void)
Perform architecture specific processing within spin loops.
long atomic_t
Definition atomic_types.h:15
atomic_t atomic_val_t
Definition atomic_types.h:16
System error numbers.
atomic_val_t atomic_get(const atomic_t *target)
Atomic get.
atomic_val_t atomic_clear(atomic_t *target)
Atomic clear.
atomic_val_t atomic_inc(atomic_t *target)
Atomic increment.
bool atomic_cas(atomic_t *target, atomic_val_t old_value, atomic_val_t new_value)
Atomic compare-and-set.
static ALWAYS_INLINE int k_spin_trylock(struct k_spinlock *l, k_spinlock_key_t *k)
Attempt to lock a spinlock.
Definition spinlock.h:229
static ALWAYS_INLINE void k_spin_unlock(struct k_spinlock *l, k_spinlock_key_t key)
Unlock a spin lock.
Definition spinlock.h:299
static ALWAYS_INLINE k_spinlock_key_t k_spin_lock(struct k_spinlock *l)
Lock a spinlock.
Definition spinlock.h:181
struct z_spinlock_key k_spinlock_key_t
Spinlock key type.
Definition spinlock.h:126
#define EBUSY
Mount device busy.
Definition errno.h:54
#define ALWAYS_INLINE
Definition common.h:160
#define BUILD_ASSERT(EXPR, MSG...)
Definition llvm.h:51
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
Kernel Spin Lock.
Definition spinlock.h:45