Skip to content

thread-rcu: Fix incorrect grace period counting #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion thread-rcu/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CFLAGS = -Wall
CFLAGS += -g
CFLAGS += -std=c11
CFLAGS += -std=gnu11
CFLAGS += -D'N_READERS=100'
CFLAGS += -D'N_UPDATE_RUN=5'
CFLAGS += -fsanitize=thread
Expand Down
44 changes: 27 additions & 17 deletions thread-rcu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static inline void thread_barrier(struct barrier_struct *b, size_t n)
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "rcu.h"

Expand All @@ -55,34 +56,42 @@ static atomic_uint grace_periods[GP_IDX_MAX];
static void *reader_func(void *argv)
{
struct test *tmp;
unsigned int local_gp_idx;
unsigned int old_prev_count;
struct timespec ts = {
.tv_sec = 0,
.tv_nsec = 30000L,
};

if (rcu_init())
abort();

thread_barrier(&test_barrier, N_READERS + 1);
nanosleep(&ts, NULL);

rcu_read_lock();

tmp = rcu_dereference(dut);

if (tmp->count != atomic_load_explicit(&prev_count, memory_order_acquire)) {
old_prev_count = atomic_exchange_explicit(&prev_count, tmp->count,
memory_order_release);
if (tmp->count != old_prev_count)
atomic_fetch_add_explicit(&gp_idx, 1, memory_order_release);
if (atomic_load_explicit(&gp_idx, memory_order_acquire) >
N_UPDATE_RUN) {
fprintf(stderr, "grace period index (%u) is over bound (%u).\n",
atomic_load_explicit(&gp_idx, memory_order_acquire),
N_UPDATE_RUN);
abort();
}
old_prev_count = atomic_load_explicit(&prev_count, memory_order_acquire);
if (old_prev_count < tmp->count) {
atomic_compare_exchange_strong(&prev_count, &old_prev_count,
tmp->count);
} else if (tmp->count < old_prev_count) {
fprintf(stderr,
"old count (%u) should not be larger than new one (%u).\n",
old_prev_count, tmp->count);
abort();
}

atomic_fetch_add_explicit(
&grace_periods[atomic_load_explicit(&gp_idx, memory_order_acquire)], 1,
memory_order_relaxed);
local_gp_idx = atomic_load_explicit(&gp_idx, memory_order_acquire);
if (local_gp_idx > N_UPDATE_RUN) {
fprintf(stderr, "grace period index (%u) is over bound (%u).\n",
local_gp_idx, N_UPDATE_RUN);
abort();
}
atomic_fetch_add_explicit(&grace_periods[local_gp_idx], 1,
memory_order_relaxed);

rcu_read_unlock();

Expand All @@ -103,6 +112,7 @@ static void *updater_func(void *argv)
newval->count = i;
oldp = rcu_assign_pointer(dut, newval);
synchronize_rcu();
atomic_fetch_add_explicit(&gp_idx, 1, memory_order_release);
free(oldp);
}

Expand Down Expand Up @@ -140,8 +150,8 @@ int main(int argc, char *argv[])

if (total != N_READERS)
fprintf(stderr,
"The Sum of records in the array of grace period(s) (%u) is "
"not the same with number of reader(s) (%u)\n",
"The sum of records in the array of grace period(s) (%u)\n"
"is not the same with number of reader(s) (%u)\n",
total, N_READERS);

return 0;
Expand Down
2 changes: 1 addition & 1 deletion thread-rcu/rcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ struct rcu_data {
} while (0)
#define rcu_next(np) \
((struct rcu_node *) (READ_ONCE((np)->__next_rcu_nesting) & ~0x3))
#define rcu_next_mask(nrn) ((struct rcu_node *) ((uintptr_t)(nrn) & ~0x3))
#define rcu_next_mask(nrn) ((struct rcu_node *) ((uintptr_t) (nrn) & ~0x3))

static struct rcu_data rcu_data = {
.nr_thread = 0,
Expand Down