RUNQUEUE(9)
π NAME π
okay bestie, welcome to the RUNQUEUE(9) page from the Kernel Developer's Manual β¨ β it's literally all about choosethread, procrunnable, remrunqueue, and setrunqueue, aka managing the queue of runnable processes π
β¨ SYNOPSIS β¨
#include <sys/param.h>
#include <sys/proc.h>
extern struct rq itqueues[];
extern struct rq rtqueues[];
extern struct rq queues[];
extern struct rq idqueues[];
struct thread *
choosethread(void);
int
procrunnable(void);
void
remrunqueue(struct thread *td);
void
setrunqueue(struct thread *td);
π DESCRIPTION π
okay so picture this: the run queue is made of four priority queues β itqueues for interrupt threads, rtqueues for realtime priority processes, queues for time sharing processes, and idqueues for idle priority processes π¦
Each priority queue is an array of NQS queue header structures. Every queue header identifies a list of runnable processes that all have equal priority. Each queue also has a single word containing a bit mask that identifies non-empty queues, so the kernel can select a process quickly. Those bit-mask words are named itqueuebits, rtqueuebits, queuebits, and idqueuebits. The run queues are protected by the sched_lock mutex, periodt π₯
procrunnable()β returns zero if there are no runnable processes other than the idle process. If there is at least one runnable process other than the idle process, it returns a non-zero value. Note that thesched_lockmutex does not need to be held when this function is called. There's a small race window where one CPU may place a process on the run queue when there are currently no other runnable processes while another CPU is calling this function. In that case the second CPU will simply travel through the idle loop one additional time before noticing that there is a runnable process. This works because idle CPUs are not halted in SMP systems. If idle CPUs are halted in SMP systems, then this race condition might have more serious repercussions in the losing case, andprocrunnable()may have to require that thesched_lockmutex be acquired. yikes πchoosethread()β returns the highest priority runnable thread. If there are no runnable threads, then the idle thread is returned. This function is called bycpu_switch()andcpu_throw()to determine which thread to switch to.choosethread()must be called with thesched_lockmutex held πsetrunqueue()β adds the threadtdto the tail of the appropriate queue in the proper priority queue. The thread must be runnable, i.e.p_statmust be set toSRUN. This function must be called with thesched_lockmutex held πΈremrunqueue()β removes threadtdfrom its run queue.
π heads up: iftdis not on a run queue, then the kernel willpanic(9). This function must be called with thesched_lockmutex held.
π SEE ALSO π
cpu_switch(9), scheduler(9), sleepqueue(9) β go read those too, iykyk π
This page's source/version is Debian, dated August 15, 2010 β mwah, bestie π«Άπ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- Copyright (c) 2000-2001 John H. Baldwin <jhb@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms