DPDK 21.11.9
rte_spinlock.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5#ifndef _RTE_SPINLOCK_H_
6#define _RTE_SPINLOCK_H_
7
21#include <rte_lcore.h>
22#ifdef RTE_FORCE_INTRINSICS
23#include <rte_common.h>
24#endif
25#include <rte_pause.h>
26
30typedef struct {
31 volatile int locked;
33
37#define RTE_SPINLOCK_INITIALIZER { 0 }
38
45static inline void
47{
48 sl->locked = 0;
49}
50
57static inline void
59
60#ifdef RTE_FORCE_INTRINSICS
61static inline void
63{
64 int exp = 0;
65
66 while (!__atomic_compare_exchange_n(&sl->locked, &exp, 1, 0,
67 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
68 rte_wait_until_equal_32((volatile uint32_t *)&sl->locked,
69 0, __ATOMIC_RELAXED);
70 exp = 0;
71 }
72}
73#endif
74
81static inline void
83
84#ifdef RTE_FORCE_INTRINSICS
85static inline void
87{
88 __atomic_store_n(&sl->locked, 0, __ATOMIC_RELEASE);
89}
90#endif
91
100static inline int
102
103#ifdef RTE_FORCE_INTRINSICS
104static inline int
106{
107 int exp = 0;
108 return __atomic_compare_exchange_n(&sl->locked, &exp, 1,
109 0, /* disallow spurious failure */
110 __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
111}
112#endif
113
123{
124 return __atomic_load_n(&sl->locked, __ATOMIC_ACQUIRE);
125}
126
133static inline int rte_tm_supported(void);
134
148static inline void
150
158static inline void
160
177static inline int
179
183typedef struct {
185 volatile int user;
186 volatile int count;
188
192#define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
193
201{
202 rte_spinlock_init(&slr->sl);
203 slr->user = -1;
204 slr->count = 0;
205}
206
214{
215 int id = rte_gettid();
216
217 if (slr->user != id) {
218 rte_spinlock_lock(&slr->sl);
219 slr->user = id;
220 }
221 slr->count++;
222}
230{
231 if (--(slr->count) == 0) {
232 slr->user = -1;
233 rte_spinlock_unlock(&slr->sl);
234 }
235
236}
237
247{
248 int id = rte_gettid();
249
250 if (slr->user != id) {
251 if (rte_spinlock_trylock(&slr->sl) == 0)
252 return 0;
253 slr->user = id;
254 }
255 slr->count++;
256 return 1;
257}
258
259
275
285
304
305#endif /* _RTE_SPINLOCK_H_ */
static int rte_gettid(void)
Definition: rte_eal.h:445
static __rte_always_inline void rte_wait_until_equal_32(volatile uint32_t *addr, uint32_t expected, int memorder)
Definition: rte_pause.h:96
static int rte_tm_supported(void)
static int rte_spinlock_trylock_tm(rte_spinlock_t *sl)
static int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:246
static void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:213
static void rte_spinlock_recursive_lock_tm(rte_spinlock_recursive_t *slr)
static int rte_spinlock_recursive_trylock_tm(rte_spinlock_recursive_t *slr)
static void rte_spinlock_unlock(rte_spinlock_t *sl)
static int rte_spinlock_trylock(rte_spinlock_t *sl)
static void rte_spinlock_recursive_unlock_tm(rte_spinlock_recursive_t *slr)
static void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:229
static void rte_spinlock_lock(rte_spinlock_t *sl)
static void rte_spinlock_lock_tm(rte_spinlock_t *sl)
static void rte_spinlock_recursive_init(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:200
static void rte_spinlock_unlock_tm(rte_spinlock_t *sl)
static int rte_spinlock_is_locked(rte_spinlock_t *sl)
Definition: rte_spinlock.h:122
static void rte_spinlock_init(rte_spinlock_t *sl)
Definition: rte_spinlock.h:46
volatile int locked
Definition: rte_spinlock.h:31