DPDK  19.11.14
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  while (__atomic_load_n(&sl->locked, __ATOMIC_RELAXED))
69  rte_pause();
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 
100 static inline int
102 
103 #ifdef RTE_FORCE_INTRINSICS
104 static 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 
122 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
123 {
124  return __atomic_load_n(&sl->locked, __ATOMIC_ACQUIRE);
125 }
126 
133 static inline int rte_tm_supported(void);
134 
148 static inline void
150 
158 static inline void
160 
177 static inline int
179 
183 typedef 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 
273 static inline void rte_spinlock_recursive_lock_tm(
275 
283 static inline void rte_spinlock_recursive_unlock_tm(
285 
302 static inline int rte_spinlock_recursive_trylock_tm(
304 
305 #endif /* _RTE_SPINLOCK_H_ */
static int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:246
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_init(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:200
static void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:213
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 int rte_spinlock_trylock_tm(rte_spinlock_t *sl)
static void rte_pause(void)
static void rte_spinlock_recursive_unlock_tm(rte_spinlock_recursive_t *slr)
static int rte_spinlock_recursive_trylock_tm(rte_spinlock_recursive_t *slr)
static int rte_spinlock_is_locked(rte_spinlock_t *sl)
Definition: rte_spinlock.h:122
static int rte_spinlock_trylock(rte_spinlock_t *sl)
static void rte_spinlock_lock_tm(rte_spinlock_t *sl)
static int rte_gettid(void)
Definition: rte_eal.h:458
static void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:229