Files
SUB-OS/include/kernel/wait.h
T
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

66 lines
2.0 KiB
C

#ifndef _KERNEL_WAIT_H
#define _KERNEL_WAIT_H
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <lib/list.h>
#include <kernel/sync.h>
#include <kernel/task.h>
// Wait queues and completions.
//
// SUB-OS runs a cooperative round-robin scheduler, so a sleeper parks itself
// on the queue, marks its task BLOCKED and yields until a waker flips the
// entry's `woken` flag. Timed waits fall back to the PIT tick counter.
typedef struct wait_entry {
task_t* task;
volatile bool woken;
struct list_head node;
} wait_entry_t;
typedef struct wait_queue {
char name[24];
struct list_head waiters;
spinlock_t lock;
uint64_t wakeups;
uint64_t sleeps;
uint32_t nr_waiting;
} wait_queue_t;
void wait_queue_init(wait_queue_t* wq, const char* name);
// Park the caller until woken. Returns 0 on wake, -1 if the queue is full.
int wait_event(wait_queue_t* wq);
// Park the caller for at most `timeout_ticks` PIT ticks (100 Hz).
// Returns 0 if woken, 1 if the wait timed out.
int wait_event_timeout(wait_queue_t* wq, uint64_t timeout_ticks);
// Wake the longest-waiting sleeper, or every sleeper. Returns the count woken.
int wake_up(wait_queue_t* wq);
int wake_up_all(wait_queue_t* wq);
uint32_t wait_queue_waiters(const wait_queue_t* wq);
// ---------------------------------------------------------------------------
// Completions: one-shot "this is done" barriers built on a wait queue.
// ---------------------------------------------------------------------------
typedef struct completion {
wait_queue_t wq;
volatile bool done;
} completion_t;
void completion_init(completion_t* c, const char* name);
void wait_for_completion(completion_t* c);
int wait_for_completion_timeout(completion_t* c, uint64_t timeout_ticks);
void complete(completion_t* c);
void complete_all(completion_t* c);
bool completion_done(const completion_t* c);
void wait_subsystem_init(void);
void wait_dump_stats(void);
#endif // _KERNEL_WAIT_H