DPDK  17.08.2
rte_rwlock.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_RWLOCK_H_
35 #define _RTE_RWLOCK_H_
36 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 #include <rte_common.h>
54 #include <rte_atomic.h>
55 #include <rte_pause.h>
56 
62 typedef struct {
63  volatile int32_t cnt;
64 } rte_rwlock_t;
65 
69 #define RTE_RWLOCK_INITIALIZER { 0 }
70 
77 static inline void
79 {
80  rwl->cnt = 0;
81 }
82 
89 static inline void
91 {
92  int32_t x;
93  int success = 0;
94 
95  while (success == 0) {
96  x = rwl->cnt;
97  /* write lock is held */
98  if (x < 0) {
99  rte_pause();
100  continue;
101  }
102  success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt,
103  x, x + 1);
104  }
105 }
106 
113 static inline void
115 {
116  rte_atomic32_dec((rte_atomic32_t *)(intptr_t)&rwl->cnt);
117 }
118 
125 static inline void
127 {
128  int32_t x;
129  int success = 0;
130 
131  while (success == 0) {
132  x = rwl->cnt;
133  /* a lock is held */
134  if (x != 0) {
135  rte_pause();
136  continue;
137  }
138  success = rte_atomic32_cmpset((volatile uint32_t *)&rwl->cnt,
139  0, -1);
140  }
141 }
142 
149 static inline void
151 {
152  rte_atomic32_inc((rte_atomic32_t *)(intptr_t)&rwl->cnt);
153 }
154 
168 static inline void
170 
177 static inline void
179 
193 static inline void
195 
202 static inline void
204 
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif /* _RTE_RWLOCK_H_ */