remove extra parentheses in return statement
[dpdk.git] / lib / librte_eal / common / include / arch / x86 / rte_spinlock.h
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_X86_64_H_
35 #define _RTE_SPINLOCK_X86_64_H_
36
37 #ifdef __cplusplus
38 extern "C" {
39 #endif
40
41 #include "generic/rte_spinlock.h"
42 #include "rte_rtm.h"
43 #include "rte_cpuflags.h"
44 #include "rte_branch_prediction.h"
45 #include "rte_common.h"
46
47 #define RTE_RTM_MAX_RETRIES (10)
48 #define RTE_XABORT_LOCK_BUSY (0xff)
49
50 #ifndef RTE_FORCE_INTRINSICS
51 static inline void
52 rte_spinlock_lock(rte_spinlock_t *sl)
53 {
54         int lock_val = 1;
55         asm volatile (
56                         "1:\n"
57                         "xchg %[locked], %[lv]\n"
58                         "test %[lv], %[lv]\n"
59                         "jz 3f\n"
60                         "2:\n"
61                         "pause\n"
62                         "cmpl $0, %[locked]\n"
63                         "jnz 2b\n"
64                         "jmp 1b\n"
65                         "3:\n"
66                         : [locked] "=m" (sl->locked), [lv] "=q" (lock_val)
67                         : "[lv]" (lock_val)
68                         : "memory");
69 }
70
71 static inline void
72 rte_spinlock_unlock (rte_spinlock_t *sl)
73 {
74         int unlock_val = 0;
75         asm volatile (
76                         "xchg %[locked], %[ulv]\n"
77                         : [locked] "=m" (sl->locked), [ulv] "=q" (unlock_val)
78                         : "[ulv]" (unlock_val)
79                         : "memory");
80 }
81
82 static inline int
83 rte_spinlock_trylock (rte_spinlock_t *sl)
84 {
85         int lockval = 1;
86
87         asm volatile (
88                         "xchg %[locked], %[lockval]"
89                         : [locked] "=m" (sl->locked), [lockval] "=q" (lockval)
90                         : "[lockval]" (lockval)
91                         : "memory");
92
93         return lockval == 0;
94 }
95 #endif
96
97 static uint8_t rtm_supported; /* cache the flag to avoid the overhead
98                                  of the rte_cpu_get_flag_enabled function */
99
100 static inline void __attribute__((constructor))
101 rte_rtm_init(void)
102 {
103         rtm_supported = rte_cpu_get_flag_enabled(RTE_CPUFLAG_RTM);
104 }
105
106 static inline int rte_tm_supported(void)
107 {
108         return rtm_supported;
109 }
110
111 static inline int
112 rte_try_tm(volatile int *lock)
113 {
114         if (!rtm_supported)
115                 return 0;
116
117         int retries = RTE_RTM_MAX_RETRIES;
118
119         while (likely(retries--)) {
120
121                 unsigned int status = rte_xbegin();
122
123                 if (likely(RTE_XBEGIN_STARTED == status)) {
124                         if (unlikely(*lock))
125                                 rte_xabort(RTE_XABORT_LOCK_BUSY);
126                         else
127                                 return 1;
128                 }
129                 while (*lock)
130                         rte_pause();
131
132                 if ((status & RTE_XABORT_EXPLICIT) &&
133                         (RTE_XABORT_CODE(status) == RTE_XABORT_LOCK_BUSY))
134                         continue;
135
136                 if ((status & RTE_XABORT_RETRY) == 0) /* do not retry */
137                         break;
138         }
139         return 0;
140 }
141
142 static inline void
143 rte_spinlock_lock_tm(rte_spinlock_t *sl)
144 {
145         if (likely(rte_try_tm(&sl->locked)))
146                 return;
147
148         rte_spinlock_lock(sl); /* fall-back */
149 }
150
151 static inline int
152 rte_spinlock_trylock_tm(rte_spinlock_t *sl)
153 {
154         if (likely(rte_try_tm(&sl->locked)))
155                 return 1;
156
157         return rte_spinlock_trylock(sl);
158 }
159
160 static inline void
161 rte_spinlock_unlock_tm(rte_spinlock_t *sl)
162 {
163         if (unlikely(sl->locked))
164                 rte_spinlock_unlock(sl);
165         else
166                 rte_xend();
167 }
168
169 static inline void
170 rte_spinlock_recursive_lock_tm(rte_spinlock_recursive_t *slr)
171 {
172         if (likely(rte_try_tm(&slr->sl.locked)))
173                 return;
174
175         rte_spinlock_recursive_lock(slr); /* fall-back */
176 }
177
178 static inline void
179 rte_spinlock_recursive_unlock_tm(rte_spinlock_recursive_t *slr)
180 {
181         if (unlikely(slr->sl.locked))
182                 rte_spinlock_recursive_unlock(slr);
183         else
184                 rte_xend();
185 }
186
187 static inline int
188 rte_spinlock_recursive_trylock_tm(rte_spinlock_recursive_t *slr)
189 {
190         if (likely(rte_try_tm(&slr->sl.locked)))
191                 return 1;
192
193         return rte_spinlock_recursive_trylock(slr);
194 }
195
196
197 #ifdef __cplusplus
198 }
199 #endif
200
201 #endif /* _RTE_SPINLOCK_X86_64_H_ */