DPDK  17.11.10
rte_spinlock.h
Go to the documentation of this file.
1 /*-
2  * BSD LICENSE
3  *
4  * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of Intel Corporation nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef _RTE_SPINLOCK_H_
35 #define _RTE_SPINLOCK_H_
36 
50 #include <rte_lcore.h>
51 #ifdef RTE_FORCE_INTRINSICS
52 #include <rte_common.h>
53 #endif
54 #include <rte_pause.h>
55 
59 typedef struct {
60  volatile int locked;
62 
66 #define RTE_SPINLOCK_INITIALIZER { 0 }
67 
74 static inline void
76 {
77  sl->locked = 0;
78 }
79 
86 static inline void
88 
89 #ifdef RTE_FORCE_INTRINSICS
90 static inline void
92 {
93  int exp = 0;
94 
95  while (!__atomic_compare_exchange_n(&sl->locked, &exp, 1, 0,
96  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
97  while (__atomic_load_n(&sl->locked, __ATOMIC_RELAXED))
98  rte_pause();
99  exp = 0;
100  }
101 }
102 #endif
103 
110 static inline void
112 
113 #ifdef RTE_FORCE_INTRINSICS
114 static inline void
116 {
117  __atomic_store_n(&sl->locked, 0, __ATOMIC_RELEASE);
118 }
119 #endif
120 
129 static inline int
131 
132 #ifdef RTE_FORCE_INTRINSICS
133 static inline int
135 {
136  int exp = 0;
137  return __atomic_compare_exchange_n(&sl->locked, &exp, 1,
138  0, /* disallow spurious failure */
139  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
140 }
141 #endif
142 
151 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
152 {
153  return __atomic_load_n(&sl->locked, __ATOMIC_ACQUIRE);
154 }
155 
162 static inline int rte_tm_supported(void);
163 
177 static inline void
179 
187 static inline void
189 
206 static inline int
208 
212 typedef struct {
214  volatile int user;
215  volatile int count;
217 
221 #define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
222 
230 {
231  rte_spinlock_init(&slr->sl);
232  slr->user = -1;
233  slr->count = 0;
234 }
235 
243 {
244  int id = rte_gettid();
245 
246  if (slr->user != id) {
247  rte_spinlock_lock(&slr->sl);
248  slr->user = id;
249  }
250  slr->count++;
251 }
259 {
260  if (--(slr->count) == 0) {
261  slr->user = -1;
262  rte_spinlock_unlock(&slr->sl);
263  }
264 
265 }
266 
276 {
277  int id = rte_gettid();
278 
279  if (slr->user != id) {
280  if (rte_spinlock_trylock(&slr->sl) == 0)
281  return 0;
282  slr->user = id;
283  }
284  slr->count++;
285  return 1;
286 }
287 
288 
302 static inline void rte_spinlock_recursive_lock_tm(
304 
312 static inline void rte_spinlock_recursive_unlock_tm(
314 
331 static inline int rte_spinlock_recursive_trylock_tm(
333 
334 #endif /* _RTE_SPINLOCK_H_ */
static int rte_spinlock_recursive_trylock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:275
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:229
static void rte_spinlock_recursive_lock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:242
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:75
static int rte_tm_supported(void)
volatile int locked
Definition: rte_spinlock.h:60
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:151
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:314
static void rte_spinlock_recursive_unlock(rte_spinlock_recursive_t *slr)
Definition: rte_spinlock.h:258