The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
spinlock_t Struct Reference

kernel locking rules: More...

#include </home/runner/work/ginnOS-docs/ginnOS-docs/ginnOS/src/kernel/sync/spinlock.h>

Data Fields

volatile uint32_t next
volatile uint32_t owner
volatile uint32_t holder

Detailed Description

kernel locking rules:

  • syscalls run with interrupts enabled (int 0x80 is a trap gate) and the timer can switch processes anywhere in kernel code, so any data shared between processes needs a lock, not just data shared with IRQs.
  • IRQ handlers run with interrupts masked and must never sleep.
  • short critical sections, and anything an IRQ handler touches: a spinlock with spin_lock_irqsave / spin_unlock_irqrestore. examples: heap, pmm, process table, pipes, keyboard ring, console, the reference counts of open-file descriptions (kernel/vfs/file.c).
  • long sections in process context (disk and filesystem I/O, a whole console write): a mutex_t (kernel/sync/mutex.h), which sleeps instead of masking interrupts.
  • to wait for a condition guarded by a spinlock, test it with the lock held and sleep with wait_queue_block_locked(), which releases the lock only once the process is queued, so a wakeup cannot be lost. re-test in a loop after waking.
  • nothing can block before the scheduler is active (the first /bin/sh runs on the boot stack until it starts a child); see scheduler_is_active().
  • never sleep (mutex, wait queue) while holding a spinlock, except through wait_queue_block_locked() on that same lock.
  • the lock order: a lock on the left can be held while the lock on its right is taken, never the other way round. sched_lock is the innermost: code that holds it takes no other lock. a mutex -> any spinlock signal_lock (signal.c) -> the process table lock -> sched_lock the lock of a wait_queue_block_locked() caller -> sched_lock a futex bucket lock -> sched_lock the page lock of a user space (vma.h) -> pmm_lock the page lock of a user space -> the TLB flush lock (tlb.c); code that holds the flush lock takes no other lock heap_lock -> pmm_lock (heap growth)
  • a core that waits for a lock does the TLB flush that another core asks of it (tlb_poll), so a holder can wait for the other cores to flush.
  • use spin_lock / spin_unlock only where interrupts are masked already (an IRQ handler, or inside an irqsave lock). they do not change the interrupt state.
  • "interrupts are masked" is not a lock: it stops only this core. data that two cores can change needs a spinlock.
  • lock assertions (KASSERT) are compiled out under NDEBUG; they only fire in make debug builds. a ticket spinlock, as Linux had before its queued spinlocks. a core takes the next ticket with one atomic add and spins until the owner field comes to its ticket, so the cores get the lock in the order they asked for it and no core waits for ever.

the lock does not stop the timer from switching to another process, so a holder must have interrupts masked on its core: use the irqsave variants, or spin_lock() when interrupts are off already. a process that holds a spinlock must never sleep (wait_queue_block_locked() is the one exception: it releases the lock). a spinlock. zero-initialized means unlocked.

Field Documentation

◆ next

volatile uint32_t spinlock_t::next

◆ owner

volatile uint32_t spinlock_t::owner

◆ holder

volatile uint32_t spinlock_t::holder

The documentation for this struct was generated from the following file: