DPDK  23.03.0
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_lock_annotations.h>
26 #include <rte_pause.h>
27 
31 typedef struct __rte_lockable {
32  volatile int locked;
34 
38 #define RTE_SPINLOCK_INITIALIZER { 0 }
39 
46 static inline void
48 {
49  sl->locked = 0;
50 }
51 
58 static inline void
60  __rte_exclusive_lock_function(sl);
61 
62 #ifdef RTE_FORCE_INTRINSICS
63 static inline void
65  __rte_no_thread_safety_analysis
66 {
67  int exp = 0;
68 
69  while (!__atomic_compare_exchange_n(&sl->locked, &exp, 1, 0,
70  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
71  rte_wait_until_equal_32((volatile uint32_t *)&sl->locked,
72  0, __ATOMIC_RELAXED);
73  exp = 0;
74  }
75 }
76 #endif
77 
84 static inline void
86  __rte_unlock_function(sl);
87 
88 #ifdef RTE_FORCE_INTRINSICS
89 static inline void
91  __rte_no_thread_safety_analysis
92 {
93  __atomic_store_n(&sl->locked, 0, __ATOMIC_RELEASE);
94 }
95 #endif
96 
106 static inline int
108  __rte_exclusive_trylock_function(1, sl);
109 
110 #ifdef RTE_FORCE_INTRINSICS
111 static inline int
113  __rte_no_thread_safety_analysis
114 {
115  int exp = 0;
116  return __atomic_compare_exchange_n(&sl->locked, &exp, 1,
117  0, /* disallow spurious failure */
118  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
119 }
120 #endif
121 
130 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
131 {
132  return __atomic_load_n(&sl->locked, __ATOMIC_ACQUIRE);
133 }
134 
141 static inline int rte_tm_supported(void);
142 
156 static inline void
158  __rte_exclusive_lock_function(sl);
159 
167 static inline void
169  __rte_unlock_function(sl);
170 
188 static inline int
190  __rte_exclusive_trylock_function(1, sl);
191 
195 typedef struct {
197  volatile int user;
198  volatile int count;
200 
204 #define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
205 
213 {
214  rte_spinlock_init(&slr->sl);
215  slr->user = -1;
216  slr->count = 0;
217 }
218 
226  __rte_no_thread_safety_analysis
227 {
228  int id = rte_gettid();
229 
230  if (slr->user != id) {
231  rte_spinlock_lock(&slr->sl);
232  slr->user = id;
233  }
234  slr->count++;
235 }
243  __rte_no_thread_safety_analysis
244 {
245  if (--(slr->count) == 0) {
246  slr->user = -1;
247  rte_spinlock_unlock(&slr->sl);
248  }
249 
250 }
251 
262  __rte_no_thread_safety_analysis
263 {
264  int id = rte_gettid();
265 
266  if (slr->user != id) {
267  if (rte_spinlock_trylock(&slr->sl) == 0)
268  return 0;
269  slr->user = id;
270  }
271  slr->count++;
272  return 1;
273 }
274 
275 
289 static inline void rte_spinlock_recursive_lock_tm(
291 
299 static inline void rte_spinlock_recursive_unlock_tm(
301 
319 static inline int rte_spinlock_recursive_trylock_tm(
321 
322 #endif /* _RTE_SPINLOCK_H_ */
static __rte_warn_unused_result int rte_spinlock_trylock_tm(rte_spinlock_t *sl) sl)
static void rte_spinlock_recursive_lock_tm(rte_spinlock_recursive_t *slr)
static void rte_spinlock_lock(rte_spinlock_t *sl)
static void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis
Definition: rte_spinlock.h:242
static __rte_warn_unused_result int rte_spinlock_recursive_trylock_tm(rte_spinlock_recursive_t *slr)
static void rte_spinlock_recursive_init(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:212
static void rte_spinlock_unlock(rte_spinlock_t *sl)
static void rte_spinlock_unlock_tm(rte_spinlock_t *sl)
static __rte_warn_unused_result int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis
Definition: rte_spinlock.h:261
static void rte_spinlock_init(rte_spinlock_t *sl)
Definition: rte_spinlock.h:47
static int rte_tm_supported(void)
volatile int locked
Definition: rte_spinlock.h:32
static __rte_warn_unused_result int rte_spinlock_trylock(rte_spinlock_t *sl) sl)
static void rte_spinlock_recursive_unlock_tm(rte_spinlock_recursive_t *slr)
static int rte_spinlock_is_locked(rte_spinlock_t *sl)
Definition: rte_spinlock.h:130
static void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr) __rte_no_thread_safety_analysis
Definition: rte_spinlock.h:225
static __rte_always_inline void rte_wait_until_equal_32(volatile uint32_t *addr, uint32_t expected, int memorder)
Definition: rte_pause.h:95
static void rte_spinlock_lock_tm(rte_spinlock_t *sl)
static int rte_gettid(void)
Definition: rte_eal.h:444
#define __rte_warn_unused_result
Definition: rte_common.h:250