Skip to content

Commit 8d03152

Browse files
Doug Fosterdavid-marchand
authored andcommitted
rcu: refactor thread register and unregister
This simplifies the implementation of rte_rcu_qsbr_thread_register() and rte_rcu_thread_unregister() functions. The simplified implementation is easier to read. Signed-off-by: Doug Foster <[email protected]> Reviewed-by: Honnappa Nagarahalli <[email protected]> Reviewed-by: Wathsala Vithanage <[email protected]>
1 parent 93f8d73 commit 8d03152

File tree

2 files changed

+23
-55
lines changed

2 files changed

+23
-55
lines changed

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ Don Provan <[email protected]>
368368
Don Wallwork <[email protected]>
369369
Doug Dziggel <[email protected]>
370370
Douglas Flint <[email protected]>
371+
Doug Foster <[email protected]>
371372
Dr. David Alan Gilbert <[email protected]>
372373
Drocula Lambda <[email protected]>
373374
Dror Birkman <[email protected]>

lib/rcu/rte_rcu_qsbr.c

Lines changed: 22 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ rte_rcu_qsbr_init(struct rte_rcu_qsbr *v, uint32_t max_threads)
8181
int
8282
rte_rcu_qsbr_thread_register(struct rte_rcu_qsbr *v, unsigned int thread_id)
8383
{
84-
unsigned int i, id, success;
85-
uint64_t old_bmap, new_bmap;
84+
unsigned int i, id;
85+
uint64_t old_bmap;
8686

8787
if (v == NULL || thread_id >= v->max_threads) {
8888
RCU_LOG(ERR, "Invalid input parameter");
@@ -97,31 +97,15 @@ rte_rcu_qsbr_thread_register(struct rte_rcu_qsbr *v, unsigned int thread_id)
9797
id = thread_id & __RTE_QSBR_THRID_MASK;
9898
i = thread_id >> __RTE_QSBR_THRID_INDEX_SHIFT;
9999

100-
/* Make sure that the counter for registered threads does not
101-
* go out of sync. Hence, additional checks are required.
102-
*/
103-
/* Check if the thread is already registered */
104-
old_bmap = rte_atomic_load_explicit(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
105-
rte_memory_order_relaxed);
106-
if (old_bmap & 1UL << id)
107-
return 0;
100+
/* Add the thread to the bitmap of registered threads */
101+
old_bmap = rte_atomic_fetch_or_explicit(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
102+
(1UL << id), rte_memory_order_release);
108103

109-
do {
110-
new_bmap = old_bmap | (1UL << id);
111-
success = rte_atomic_compare_exchange_strong_explicit(
112-
__RTE_QSBR_THRID_ARRAY_ELM(v, i),
113-
&old_bmap, new_bmap,
114-
rte_memory_order_release, rte_memory_order_relaxed);
115-
116-
if (success)
117-
rte_atomic_fetch_add_explicit(&v->num_threads,
118-
1, rte_memory_order_relaxed);
119-
else if (old_bmap & (1UL << id))
120-
/* Someone else registered this thread.
121-
* Counter should not be incremented.
122-
*/
123-
return 0;
124-
} while (success == 0);
104+
/* Increment the number of threads registered only if the thread was not already
105+
* registered
106+
*/
107+
if (!(old_bmap & (1UL << id)))
108+
rte_atomic_fetch_add_explicit(&v->num_threads, 1, rte_memory_order_relaxed);
125109

126110
return 0;
127111
}
@@ -132,8 +116,8 @@ rte_rcu_qsbr_thread_register(struct rte_rcu_qsbr *v, unsigned int thread_id)
132116
int
133117
rte_rcu_qsbr_thread_unregister(struct rte_rcu_qsbr *v, unsigned int thread_id)
134118
{
135-
unsigned int i, id, success;
136-
uint64_t old_bmap, new_bmap;
119+
unsigned int i, id;
120+
uint64_t old_bmap;
137121

138122
if (v == NULL || thread_id >= v->max_threads) {
139123
RCU_LOG(ERR, "Invalid input parameter");
@@ -148,35 +132,18 @@ rte_rcu_qsbr_thread_unregister(struct rte_rcu_qsbr *v, unsigned int thread_id)
148132
id = thread_id & __RTE_QSBR_THRID_MASK;
149133
i = thread_id >> __RTE_QSBR_THRID_INDEX_SHIFT;
150134

151-
/* Make sure that the counter for registered threads does not
152-
* go out of sync. Hence, additional checks are required.
135+
/* Make sure any loads of the shared data structure are
136+
* completed before removal of the thread from the bitmap of
137+
* reporting threads.
153138
*/
154-
/* Check if the thread is already unregistered */
155-
old_bmap = rte_atomic_load_explicit(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
156-
rte_memory_order_relaxed);
157-
if (!(old_bmap & (1UL << id)))
158-
return 0;
139+
old_bmap = rte_atomic_fetch_and_explicit(__RTE_QSBR_THRID_ARRAY_ELM(v, i),
140+
~(1UL << id), rte_memory_order_release);
159141

160-
do {
161-
new_bmap = old_bmap & ~(1UL << id);
162-
/* Make sure any loads of the shared data structure are
163-
* completed before removal of the thread from the list of
164-
* reporting threads.
165-
*/
166-
success = rte_atomic_compare_exchange_strong_explicit(
167-
__RTE_QSBR_THRID_ARRAY_ELM(v, i),
168-
&old_bmap, new_bmap,
169-
rte_memory_order_release, rte_memory_order_relaxed);
170-
171-
if (success)
172-
rte_atomic_fetch_sub_explicit(&v->num_threads,
173-
1, rte_memory_order_relaxed);
174-
else if (!(old_bmap & (1UL << id)))
175-
/* Someone else unregistered this thread.
176-
* Counter should not be incremented.
177-
*/
178-
return 0;
179-
} while (success == 0);
142+
/* Decrement the number of threads unregistered only if the thread was not already
143+
* unregistered
144+
*/
145+
if (old_bmap & (1UL << id))
146+
rte_atomic_fetch_sub_explicit(&v->num_threads, 1, rte_memory_order_relaxed);
180147

181148
return 0;
182149
}

0 commit comments

Comments
 (0)