kernel/sleeplock.h
Included by 12 files
kernel/bio.c, kernel/console.c, kernel/file.c, kernel/fs.c, kernel/log.c, kernel/pipe.c, kernel/printk.c, kernel/sleeplock.c, kernel/sysfile.c, kernel/uart.c, kernel/virtio_disk.c, user/init.cAbout this file
The definition of the sleep lock, xv6’s lock for long waits. A process that finds a
sleep lock taken gives up the CPU instead of spinning, so the lock can be held across slow
operations such as reading a disk block. xv6 uses sleep locks for disk buffers
(kernel/buf.h), in-memory inodes (kernel/file.h) and the UART’s transmit side
(kernel/uart.c).
Read before: kernel/spinlock.h. Read next: kernel/sleeplock.c.
A held flag, guarded by a spinlock
locked plays the same role as in a spinlock, but it is an ordinary variable,
not changed with atomic instructions. Instead, every read and write of locked (and
pid) happens while holding the small spinlock lk. That spinlock is held only for
a few instructions at a time, inside acquiresleep, releasesleep and
holdingsleep; the sleep lock itself can be held for as long as the holder likes,
including while it sleeps.
name and pid are for debugging, like name and cpu in a spinlock. Recording the
holder by PID (process ID) rather than by CPU makes sense here: a process that holds a sleep
lock may sleep and later continue on a different CPU, but it stays the same process.
1 while some process holds the lock. Protected by lk.
The spinlock that makes checking and setting locked one atomic step. (Waiters
use the address of the whole struct sleeplock as their
wait channel.)
The lock’s name, for debugging.
PID of the holding process, or 0. holdingsleep compares it with the caller’s PID.