DPDK  18.11.11
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
50 {
51  rwl->cnt = 0;
52 }
53 
60 static inline void
62 {
63  int32_t x;
64  int success = 0;
65 
66  while (success == 0) {
67  x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
68  /* write lock is held */
69  if (x < 0) {
70  rte_pause();
71  continue;
72  }
73  success = __atomic_compare_exchange_n(&rwl->cnt, &x, x + 1, 1,
74  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
75  }
76 }
77 
84 static inline void
86 {
87  __atomic_fetch_sub(&rwl->cnt, 1, __ATOMIC_RELEASE);
88 }
89 
96 static inline void
98 {
99  int32_t x;
100  int success = 0;
101 
102  while (success == 0) {
103  x = __atomic_load_n(&rwl->cnt, __ATOMIC_RELAXED);
104  /* a lock is held */
105  if (x != 0) {
106  rte_pause();
107  continue;
108  }
109  success = __atomic_compare_exchange_n(&rwl->cnt, &x, -1, 1,
110  __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
111  }
112 }
113 
120 static inline void
122 {
123  __atomic_store_n(&rwl->cnt, 0, __ATOMIC_RELEASE);
124 }
125 
139 static inline void
141 
148 static inline void
150 
164 static inline void
166 
173 static inline void
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif /* _RTE_RWLOCK_H_ */
static void rte_rwlock_write_lock_tm(rte_rwlock_t *rwl)
static void rte_rwlock_read_lock(rte_rwlock_t *rwl)
Definition: rte_rwlock.h:61
static void rte_rwlock_read_lock_tm(rte_rwlock_t *rwl)
static void rte_rwlock_read_unlock(rte_rwlock_t *rwl)
Definition: rte_rwlock.h:85
static void rte_pause(void)
volatile int32_t cnt
Definition: rte_rwlock.h:34
static void rte_rwlock_write_unlock(rte_rwlock_t *rwl)
Definition: rte_rwlock.h:121
static void rte_rwlock_read_unlock_tm(rte_rwlock_t *rwl)
static void rte_rwlock_write_lock(rte_rwlock_t *rwl)
Definition: rte_rwlock.h:97
static void rte_rwlock_init(rte_rwlock_t *rwl)
Definition: rte_rwlock.h:49
static void rte_rwlock_write_unlock_tm(rte_rwlock_t *rwl)