xv6, line by line
kernel/spinlock.h

kernel/spinlock.h

C header · 8 lines · annotated 100% · kernel · upstream

Included by 20 fileskernel/bio.c, kernel/console.c, kernel/exec.c, kernel/file.c, kernel/fs.c, kernel/kalloc.c, kernel/log.c, kernel/pipe.c, kernel/printk.c, kernel/proc.c, kernel/sleeplock.c, kernel/spinlock.c, kernel/syscall.c, kernel/sysfile.c, kernel/sysproc.c, kernel/trap.c, kernel/uart.c, kernel/virtio_disk.c, kernel/vm.c, user/init.c
About this file

The definition of xv6’s basic lock, the spinlock. Any kernel data that more than one CPU (or an interrupt handler) can touch is guarded by one of these: the process table entries, the free-page list, the console buffer, the tick counter, and so on.

The struct is tiny: one word that says whether the lock is held, plus two fields that exist only to help find bugs. All the logic lives in kernel/spinlock.c.

Read next: kernel/spinlock.c, then kernel/sleeplock.h for the lock used for long waits.

1// Mutual exclusion lock.
2struct spinlock {
3 uint locked; // Is the lock held?
5 // For debugging:
6 char *name; // Name of lock.
7 struct cpu *cpu; // The cpu holding the lock.
8};