DPDK
19.02.0
Main Page
Related Pages
Data Structures
Files
Examples
File List
Globals
lib
librte_eal
common
include
generic
rte_rwlock.h
Go to the documentation of this file.
1
/* SPDX-License-Identifier: BSD-3-Clause
2
* Copyright(c) 2010-2014 Intel Corporation
3
*/
4
5
#ifndef _RTE_RWLOCK_H_
6
#define _RTE_RWLOCK_H_
7
20
#ifdef __cplusplus
21
extern
"C"
{
22
#endif
23
24
#include <
rte_common.h
>
25
#include <
rte_atomic.h
>
26
#include <
rte_pause.h
>
27
33
typedef
struct
{
34
volatile
int32_t
cnt
;
35
}
rte_rwlock_t
;
36
40
#define RTE_RWLOCK_INITIALIZER { 0 }
41
48
static
inline
void
49
rte_rwlock_init
(
rte_rwlock_t
*rwl)
50
{
51
rwl->
cnt
= 0;
52
}
53
60
static
inline
void
61
rte_rwlock_read_lock
(
rte_rwlock_t
*rwl)
62
{
63
int32_t x;
64
int
success = 0;
65
66
while
(success == 0) {
67
x = rwl->
cnt
;
68
/* write lock is held */
69
if
(x < 0) {
70
rte_pause
();
71
continue
;
72
}
73
success =
rte_atomic32_cmpset
((
volatile
uint32_t *)&rwl->
cnt
,
74
(uint32_t)x, (uint32_t)(x + 1));
75
}
76
}
77
91
static
inline
__rte_experimental
int
92
rte_rwlock_read_trylock
(
rte_rwlock_t
*rwl)
93
{
94
int32_t x;
95
int
success = 0;
96
97
while
(success == 0) {
98
x = rwl->
cnt
;
99
/* write lock is held */
100
if
(x < 0)
101
return
-EBUSY;
102
success =
rte_atomic32_cmpset
((
volatile
uint32_t *)&rwl->
cnt
,
103
(uint32_t)x, (uint32_t)(x + 1));
104
}
105
return
0;
106
}
107
114
static
inline
void
115
rte_rwlock_read_unlock
(
rte_rwlock_t
*rwl)
116
{
117
rte_atomic32_dec
((
rte_atomic32_t
*)(intptr_t)&rwl->
cnt
);
118
}
119
133
static
inline
__rte_experimental
int
134
rte_rwlock_write_trylock
(
rte_rwlock_t
*rwl)
135
{
136
int32_t x;
137
138
x = rwl->
cnt
;
139
if
(x != 0 ||
rte_atomic32_cmpset
((
volatile
uint32_t *)&rwl->
cnt
,
140
0, (uint32_t)-1) == 0)
141
return
-EBUSY;
142
143
return
0;
144
}
145
152
static
inline
void
153
rte_rwlock_write_lock
(
rte_rwlock_t
*rwl)
154
{
155
int32_t x;
156
int
success = 0;
157
158
while
(success == 0) {
159
x = rwl->
cnt
;
160
/* a lock is held */
161
if
(x != 0) {
162
rte_pause
();
163
continue
;
164
}
165
success =
rte_atomic32_cmpset
((
volatile
uint32_t *)&rwl->
cnt
,
166
0, (uint32_t)-1);
167
}
168
}
169
176
static
inline
void
177
rte_rwlock_write_unlock
(
rte_rwlock_t
*rwl)
178
{
179
rte_atomic32_inc
((
rte_atomic32_t
*)(intptr_t)&rwl->
cnt
);
180
}
181
195
static
inline
void
196
rte_rwlock_read_lock_tm
(
rte_rwlock_t
*rwl);
197
204
static
inline
void
205
rte_rwlock_read_unlock_tm
(
rte_rwlock_t
*rwl);
206
220
static
inline
void
221
rte_rwlock_write_lock_tm
(
rte_rwlock_t
*rwl);
222
229
static
inline
void
230
rte_rwlock_write_unlock_tm
(
rte_rwlock_t
*rwl);
231
232
#ifdef __cplusplus
233
}
234
#endif
235
236
#endif
/* _RTE_RWLOCK_H_ */
Generated by
1.8.1.2