DPDK  22.07.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_pause.h>
26 
30 typedef struct {
31  volatile int locked;
33 
37 #define RTE_SPINLOCK_INITIALIZER { 0 }
38 
45 static inline void
47 {
48  sl->locked = 0;
49 }
50 
57 static inline void
59 
60 #ifdef RTE_FORCE_INTRINSICS
61 static 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 
81 static inline void
83 
84 #ifdef RTE_FORCE_INTRINSICS
85 static inline void
87 {
88  __atomic_store_n(&sl->locked, 0, __ATOMIC_RELEASE);
89 }
90 #endif
91 
101 static inline int
103 
104 #ifdef RTE_FORCE_INTRINSICS
105 static inline int
107 {
108  int exp = 0;
109  return __atomic_compare_exchange_n(&sl->locked, &exp, 1,
110  0, /* disallow spurious failure */
111  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
112 }
113 #endif
114 
123 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
124 {
125  return __atomic_load_n(&sl->locked, __ATOMIC_ACQUIRE);
126 }
127 
134 static inline int rte_tm_supported(void);
135 
149 static inline void
151 
159 static inline void
161 
179 static inline int
181 
185 typedef struct {
187  volatile int user;
188  volatile int count;
190 
194 #define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
195 
203 {
204  rte_spinlock_init(&slr->sl);
205  slr->user = -1;
206  slr->count = 0;
207 }
208 
216 {
217  int id = rte_gettid();
218 
219  if (slr->user != id) {
220  rte_spinlock_lock(&slr->sl);
221  slr->user = id;
222  }
223  slr->count++;
224 }
232 {
233  if (--(slr->count) == 0) {
234  slr->user = -1;
235  rte_spinlock_unlock(&slr->sl);
236  }
237 
238 }
239 
250 {
251  int id = rte_gettid();
252 
253  if (slr->user != id) {
254  if (rte_spinlock_trylock(&slr->sl) == 0)
255  return 0;
256  slr->user = id;
257  }
258  slr->count++;
259  return 1;
260 }
261 
262 
276 static inline void rte_spinlock_recursive_lock_tm(
278 
286 static inline void rte_spinlock_recursive_unlock_tm(
288 
306 static inline int rte_spinlock_recursive_trylock_tm(
308 
309 #endif /* _RTE_SPINLOCK_H_ */
static void rte_spinlock_recursive_lock_tm(rte_spinlock_recursive_t *slr)
static void rte_spinlock_lock(rte_spinlock_t *sl)
static __rte_warn_unused_result int rte_spinlock_trylock(rte_spinlock_t *sl)
static __rte_warn_unused_result int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:249
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:202
static void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:215
static void rte_spinlock_unlock(rte_spinlock_t *sl)
static void rte_spinlock_unlock_tm(rte_spinlock_t *sl)
static void rte_spinlock_init(rte_spinlock_t *sl)
Definition: rte_spinlock.h:46
static int rte_tm_supported(void)
volatile int locked
Definition: rte_spinlock.h:31
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:123
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 void rte_spinlock_lock_tm(rte_spinlock_t *sl)
static __rte_warn_unused_result int rte_spinlock_trylock_tm(rte_spinlock_t *sl)
static int rte_gettid(void)
Definition: rte_eal.h:445
#define __rte_warn_unused_result
Definition: rte_common.h:253
static void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:231