DPDK  17.08.2
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  while (__sync_lock_test_and_set(&sl->locked, 1))
94  while(sl->locked)
95  rte_pause();
96 }
97 #endif
98 
105 static inline void
107 
108 #ifdef RTE_FORCE_INTRINSICS
109 static inline void
111 {
112  __sync_lock_release(&sl->locked);
113 }
114 #endif
115 
124 static inline int
126 
127 #ifdef RTE_FORCE_INTRINSICS
128 static inline int
130 {
131  return __sync_lock_test_and_set(&sl->locked,1) == 0;
132 }
133 #endif
134 
143 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
144 {
145  return sl->locked;
146 }
147 
154 static inline int rte_tm_supported(void);
155 
169 static inline void
171 
179 static inline void
181 
198 static inline int
200 
204 typedef struct {
206  volatile int user;
207  volatile int count;
209 
213 #define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
214 
222 {
223  rte_spinlock_init(&slr->sl);
224  slr->user = -1;
225  slr->count = 0;
226 }
227 
235 {
236  int id = rte_gettid();
237 
238  if (slr->user != id) {
239  rte_spinlock_lock(&slr->sl);
240  slr->user = id;
241  }
242  slr->count++;
243 }
251 {
252  if (--(slr->count) == 0) {
253  slr->user = -1;
254  rte_spinlock_unlock(&slr->sl);
255  }
256 
257 }
258 
268 {
269  int id = rte_gettid();
270 
271  if (slr->user != id) {
272  if (rte_spinlock_trylock(&slr->sl) == 0)
273  return 0;
274  slr->user = id;
275  }
276  slr->count++;
277  return 1;
278 }
279 
280 
294 static inline void rte_spinlock_recursive_lock_tm(
296 
304 static inline void rte_spinlock_recursive_unlock_tm(
306 
323 static inline int rte_spinlock_recursive_trylock_tm(
325 
326 #endif /* _RTE_SPINLOCK_H_ */