DPDK  19.02.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  while (__sync_lock_test_and_set(&sl->locked, 1))
65  while(sl->locked)
66  rte_pause();
67 }
68 #endif
69 
76 static inline void
78 
79 #ifdef RTE_FORCE_INTRINSICS
80 static inline void
82 {
83  __sync_lock_release(&sl->locked);
84 }
85 #endif
86 
95 static inline int
97 
98 #ifdef RTE_FORCE_INTRINSICS
99 static inline int
101 {
102  return __sync_lock_test_and_set(&sl->locked,1) == 0;
103 }
104 #endif
105 
114 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
115 {
116  return sl->locked;
117 }
118 
125 static inline int rte_tm_supported(void);
126 
140 static inline void
142 
150 static inline void
152 
169 static inline int
171 
175 typedef struct {
177  volatile int user;
178  volatile int count;
180 
184 #define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
185 
193 {
194  rte_spinlock_init(&slr->sl);
195  slr->user = -1;
196  slr->count = 0;
197 }
198 
206 {
207  int id = rte_gettid();
208 
209  if (slr->user != id) {
210  rte_spinlock_lock(&slr->sl);
211  slr->user = id;
212  }
213  slr->count++;
214 }
222 {
223  if (--(slr->count) == 0) {
224  slr->user = -1;
225  rte_spinlock_unlock(&slr->sl);
226  }
227 
228 }
229 
239 {
240  int id = rte_gettid();
241 
242  if (slr->user != id) {
243  if (rte_spinlock_trylock(&slr->sl) == 0)
244  return 0;
245  slr->user = id;
246  }
247  slr->count++;
248  return 1;
249 }
250 
251 
265 static inline void rte_spinlock_recursive_lock_tm(
267 
275 static inline void rte_spinlock_recursive_unlock_tm(
277 
294 static inline int rte_spinlock_recursive_trylock_tm(
296 
297 #endif /* _RTE_SPINLOCK_H_ */