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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _BIRD_RCU_H_
|
|
|
|
#define _BIRD_RCU_H_
|
|
|
|
|
|
|
|
#include "lib/birdlib.h"
|
|
|
|
#include "lib/lists.h"
|
|
|
|
#include <stdatomic.h>
|
|
|
|
|
|
|
|
#define RCU_GP_PHASE 0x100000
|
|
|
|
#define RCU_NEST_MASK 0x0fffff
|
|
|
|
#define RCU_NEST_CNT 0x000001
|
|
|
|
|
|
|
|
extern _Atomic uint rcu_gp_ctl;
|
|
|
|
|
2023-01-19 09:49:47 +00:00
|
|
|
struct rcu_thread {
|
2021-11-12 21:58:40 +00:00
|
|
|
node n;
|
|
|
|
_Atomic uint ctl;
|
|
|
|
};
|
|
|
|
|
2023-01-19 09:49:47 +00:00
|
|
|
extern _Thread_local struct rcu_thread *this_rcu_thread;
|
2024-05-16 08:22:19 +00:00
|
|
|
extern _Thread_local uint rcu_blocked;
|
2021-11-12 21:58:40 +00:00
|
|
|
|
|
|
|
static inline void rcu_read_lock(void)
|
|
|
|
{
|
2023-01-19 09:49:47 +00:00
|
|
|
uint cmp = atomic_load_explicit(&this_rcu_thread->ctl, memory_order_acquire);
|
2021-11-12 21:58:40 +00:00
|
|
|
|
|
|
|
if (cmp & RCU_NEST_MASK)
|
2023-01-19 09:49:47 +00:00
|
|
|
atomic_store_explicit(&this_rcu_thread->ctl, cmp + RCU_NEST_CNT, memory_order_relaxed);
|
2021-11-12 21:58:40 +00:00
|
|
|
else
|
2023-01-19 09:49:47 +00:00
|
|
|
atomic_store(&this_rcu_thread->ctl, atomic_load_explicit(&rcu_gp_ctl, memory_order_acquire));
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void rcu_read_unlock(void)
|
|
|
|
{
|
2023-01-19 09:49:47 +00:00
|
|
|
atomic_fetch_sub(&this_rcu_thread->ctl, RCU_NEST_CNT);
|
2021-11-12 21:58:40 +00:00
|
|
|
}
|
|
|
|
|
2024-05-16 08:22:19 +00:00
|
|
|
static inline _Bool rcu_read_active(void)
|
|
|
|
{
|
|
|
|
return !!(atomic_load_explicit(&this_rcu_thread->ctl, memory_order_acquire) & RCU_NEST_MASK);
|
|
|
|
}
|
|
|
|
|
2021-11-12 21:58:40 +00:00
|
|
|
void synchronize_rcu(void);
|
|
|
|
|
|
|
|
/* Registering and unregistering a birdloop. To be called from birdloop implementation */
|
2023-01-19 09:49:47 +00:00
|
|
|
void rcu_thread_start(struct rcu_thread *);
|
|
|
|
void rcu_thread_stop(struct rcu_thread *);
|
2021-11-12 21:58:40 +00:00
|
|
|
|
|
|
|
/* Run this from resource init */
|
|
|
|
void rcu_init(void);
|
|
|
|
|
|
|
|
#endif
|