2021-11-12 21:58:40 +00:00
|
|
|
/*
|
|
|
|
* BIRD Library -- Read-Copy-Update Basic Operations
|
|
|
|
*
|
|
|
|
* (c) 2021 Maria Matejka <mq@jmq.cz>
|
|
|
|
* (c) 2021 CZ.NIC z.s.p.o.
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
* Note: all the relevant patents shall be expired.
|
|
|
|
*
|
|
|
|
* Using the Supplementary Material for User-Level Implementations of Read-Copy-Update
|
|
|
|
* by Matthieu Desnoyers, Paul E. McKenney, Alan S. Stern, Michel R. Dagenais and Jonathan Walpole
|
|
|
|
* obtained from https://www.efficios.com/pub/rcu/urcu-supp-accepted.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lib/rcu.h"
|
|
|
|
#include "lib/io-loop.h"
|
|
|
|
#include "lib/locking.h"
|
|
|
|
|
2024-05-31 18:43:15 +00:00
|
|
|
_Atomic u64 rcu_global_phase = RCU_GP_PHASE;
|
|
|
|
_Thread_local struct rcu_thread this_rcu_thread;
|
2021-11-12 21:58:40 +00:00
|
|
|
|
2024-05-31 18:43:15 +00:00
|
|
|
static struct rcu_thread * _Atomic rcu_thread_list = NULL;
|
2021-11-12 21:58:40 +00:00
|
|
|
|
2024-05-31 18:43:15 +00:00
|
|
|
static _Atomic uint rcu_thread_spinlock = 0;
|
2021-11-12 21:58:40 +00:00
|
|
|
|
|
|
|
static int
|
2024-05-31 18:43:15 +00:00
|
|
|
rcu_critical(struct rcu_thread *t, u64 phase)
|
2021-11-12 21:58:40 +00:00
|
|
|
{
|
2024-05-31 18:43:15 +00:00
|
|
|
uint val = atomic_load_explicit(&t->ctl, memory_order_acquire);
|
|
|
|
return
|
|
|
|
(val & RCU_NEST_MASK) /* Active */
|
|
|
|
&& ((val & ~RCU_NEST_MASK) <= phase); /* In an older phase */
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
synchronize_rcu(void)
|
|
|
|
{
|
2024-05-31 18:43:15 +00:00
|
|
|
/* Increment phase */
|
|
|
|
u64 phase = atomic_fetch_add_explicit(&rcu_global_phase, RCU_GP_PHASE, memory_order_acq_rel);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
/* Spinlock */
|
|
|
|
while (atomic_exchange_explicit(&rcu_thread_spinlock, 1, memory_order_acq_rel))
|
|
|
|
birdloop_yield();
|
|
|
|
|
|
|
|
/* Check all threads */
|
2024-06-26 15:19:24 +00:00
|
|
|
bool critical = 0;
|
2024-05-31 18:43:15 +00:00
|
|
|
for (struct rcu_thread * _Atomic *tp = &rcu_thread_list, *t;
|
|
|
|
t = atomic_load_explicit(tp, memory_order_acquire);
|
|
|
|
tp = &t->next)
|
|
|
|
/* Found a critical */
|
|
|
|
if (critical = rcu_critical(t, phase))
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Unlock */
|
|
|
|
ASSERT_DIE(atomic_exchange_explicit(&rcu_thread_spinlock, 0, memory_order_acq_rel));
|
|
|
|
|
|
|
|
/* Done if no critical */
|
|
|
|
if (!critical)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Wait and retry if critical */
|
|
|
|
birdloop_yield();
|
|
|
|
}
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-05-31 18:43:15 +00:00
|
|
|
rcu_thread_start(void)
|
2021-11-12 21:58:40 +00:00
|
|
|
{
|
2024-05-31 18:43:15 +00:00
|
|
|
/* Insert this thread to the thread list, no spinlock is needed */
|
|
|
|
struct rcu_thread *next = atomic_load_explicit(&rcu_thread_list, memory_order_acquire);
|
|
|
|
do atomic_store_explicit(&this_rcu_thread.next, next, memory_order_relaxed);
|
|
|
|
while (!atomic_compare_exchange_strong_explicit(
|
|
|
|
&rcu_thread_list, &next, &this_rcu_thread,
|
|
|
|
memory_order_acq_rel, memory_order_acquire));
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2024-05-31 18:43:15 +00:00
|
|
|
rcu_thread_stop(void)
|
2021-11-12 21:58:40 +00:00
|
|
|
{
|
2024-05-31 18:43:15 +00:00
|
|
|
/* Spinlock */
|
|
|
|
while (atomic_exchange_explicit(&rcu_thread_spinlock, 1, memory_order_acq_rel))
|
|
|
|
birdloop_yield();
|
|
|
|
|
|
|
|
/* Find this thread */
|
|
|
|
for (struct rcu_thread * _Atomic *tp = &rcu_thread_list, *t;
|
|
|
|
t = atomic_load_explicit(tp, memory_order_acquire);
|
|
|
|
tp = &t->next)
|
|
|
|
if (t == &this_rcu_thread)
|
|
|
|
{
|
|
|
|
/* Remove this thread */
|
|
|
|
atomic_store_explicit(tp, atomic_load_explicit(&t->next, memory_order_acquire), memory_order_release);
|
|
|
|
|
|
|
|
/* Unlock and go */
|
|
|
|
ASSERT_DIE(atomic_exchange_explicit(&rcu_thread_spinlock, 0, memory_order_acq_rel));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bug("Failed to find a stopped rcu thread");
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rcu_init(void)
|
|
|
|
{
|
2024-05-31 18:43:15 +00:00
|
|
|
rcu_thread_start();
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|