DPDK  16.04.0
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 
58 typedef struct {
59  volatile int locked;
61 
65 #define RTE_SPINLOCK_INITIALIZER { 0 }
66 
73 static inline void
75 {
76  sl->locked = 0;
77 }
78 
85 static inline void
87 
88 #ifdef RTE_FORCE_INTRINSICS
89 static inline void
91 {
92  while (__sync_lock_test_and_set(&sl->locked, 1))
93  while(sl->locked)
94  rte_pause();
95 }
96 #endif
97 
104 static inline void
106 
107 #ifdef RTE_FORCE_INTRINSICS
108 static inline void
110 {
111  __sync_lock_release(&sl->locked);
112 }
113 #endif
114 
123 static inline int
125 
126 #ifdef RTE_FORCE_INTRINSICS
127 static inline int
129 {
130  return __sync_lock_test_and_set(&sl->locked,1) == 0;
131 }
132 #endif
133 
142 static inline int rte_spinlock_is_locked (rte_spinlock_t *sl)
143 {
144  return sl->locked;
145 }
146 
153 static inline int rte_tm_supported(void);
154 
168 static inline void
170 
178 static inline void
180 
197 static inline int
199 
203 typedef struct {
205  volatile int user;
206  volatile int count;
208 
212 #define RTE_SPINLOCK_RECURSIVE_INITIALIZER {RTE_SPINLOCK_INITIALIZER, -1, 0}
213 
221 {
222  rte_spinlock_init(&slr->sl);
223  slr->user = -1;
224  slr->count = 0;
225 }
226 
234 {
235  int id = rte_gettid();
236 
237  if (slr->user != id) {
238  rte_spinlock_lock(&slr->sl);
239  slr->user = id;
240  }
241  slr->count++;
242 }
250 {
251  if (--(slr->count) == 0) {
252  slr->user = -1;
253  rte_spinlock_unlock(&slr->sl);
254  }
255 
256 }
257 
267 {
268  int id = rte_gettid();
269 
270  if (slr->user != id) {
271  if (rte_spinlock_trylock(&slr->sl) == 0)
272  return 0;
273  slr->user = id;
274  }
275  slr->count++;
276  return 1;
277 }
278 
279 
293 static inline void rte_spinlock_recursive_lock_tm(
295 
303 static inline void rte_spinlock_recursive_unlock_tm(
305 
322 static inline int rte_spinlock_recursive_trylock_tm(
324 
325 #endif /* _RTE_SPINLOCK_H_ */