Files
subhobhai943andClaude Opus 5 f9400f6ac4 feat(kernel): Add RB-tree/kfifo/hashtable, wait queues, futex, tiny RCU, page cache and KTest harness
Expands the kernel-and-TTY core with the primitives the higher layers have
been missing, plus an in-kernel harness that proves they work.

Data structures (lib/):
  - rbtree.c: Linux-compatible red-black tree with parent pointers, full
    insert/erase rebalancing, in-order iteration and an invariant validator.
  - kfifo.c: power-of-two byte ring with free-running indices, so the wrap
    path costs one mask instead of a branch per byte.
  - hashtable.c: separately-chained table with FNV-1a hashing, string or
    u64 keys, and rehash-on-growth at a 3/4 load factor.

Kernel subsystems:
  - kernel/wait.c: wait queues and one-shot completion barriers, backed by a
    static sleeper pool so a blocking path never re-enters the allocator.
  - kernel/futex.c: 32-bucket futex hash with value re-check, requeue, and an
    fmutex_t sleeping mutex built on the uncontended-fastpath protocol.
  - kernel/rcu.c: tiny RCU with reader nesting, deferred callbacks, kfree_rcu
    and rcu_assign_pointer/rcu_dereference publish barriers.
  - mm/page_cache.c: write-back block page cache, 4 KB pages indexed by
    (device, page), CLOCK second-chance eviction and dirty write-back.
  - kernel/ktest.c: KUnit-style harness with 6 built-in suites, 21 cases and
    735 assertions covering every subsystem above (all passing under QEMU).

LazyBox userland:
  - coreutils.c: sort, uniq, cut, tr, rev, tac, nl, seq, diff, xxd, du,
    factor, sum and truncate, reading through the VFS and handling the
    zero-length synthetic nodes that procfs and sysfs expose.
  - ktest, rcuinfo, futexinfo, waitinfo and pagecache diagnostics applets.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01L5yKJT4Wo2cV3rfPrwY5MP
2026-08-24 23:38:59 +05:30

59 lines
1.7 KiB
C

#ifndef _KERNEL_RCU_H
#define _KERNEL_RCU_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
// Tiny RCU (Read-Copy-Update).
//
// Readers are free: rcu_read_lock() only bumps a per-CPU nesting depth, so a
// lookup never contends with an update. Writers publish a new version with
// rcu_assign_pointer() and defer reclamation of the old one until every reader
// that could still hold a reference has finished — a "grace period".
#define RCU_MAX_CALLBACKS 64
typedef void (*rcu_callback_t)(void* arg);
typedef struct {
uint64_t grace_periods;
uint64_t callbacks_queued;
uint64_t callbacks_invoked;
uint64_t read_locks;
uint32_t nesting_depth;
uint32_t pending;
} rcu_stats_t;
void rcu_init(void);
void rcu_read_lock(void);
void rcu_read_unlock(void);
bool rcu_read_lock_held(void);
// Block until every reader active at call time has left its critical section.
void rcu_synchronize(void);
// Queue `func(arg)` to run once the current grace period elapses.
int call_rcu(rcu_callback_t func, void* arg);
// Free `ptr` after a grace period (the common case for call_rcu).
int kfree_rcu(void* ptr);
// Advance the state machine; called from the timer tick and from rcu_synchronize.
void rcu_process_callbacks(void);
uint64_t rcu_get_grace_period(void);
rcu_stats_t rcu_get_stats(void);
void rcu_dump(void);
// Publish/consume helpers. The barriers keep the compiler from reordering the
// initialisation of an object past the store that makes it visible.
#define rcu_assign_pointer(p, v) \
do { __atomic_store_n(&(p), (v), __ATOMIC_RELEASE); } while (0)
#define rcu_dereference(p) \
__atomic_load_n(&(p), __ATOMIC_ACQUIRE)
#endif // _KERNEL_RCU_H