GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
MI_SWITCH(9) FreeBSD Kernel Developer's Manual MI_SWITCH(9)

mi_switchswitch to another thread context

#include <sys/param.h>
#include <sys/proc.h>

void
mi_switch(int flags);

The () function implements the machine-independent prelude to a thread context switch. It is the single entry point for every context switch and is called from only a few distinguished places in the kernel. The context switch is, by necessity, always performed by the switched thread, even when the switch is initiated from elsewhere; e.g. preemption requested via Inter-Processor Interrupt (IPI).

The various major uses of () can be enumerated as follows:

  1. From within a function such as sleepq_wait(9) or () when the current thread voluntarily relinquishes the CPU to wait for some resource or lock to become available.
  2. Involuntary preemption due to arrival of a higher-priority thread.
  3. At the tail end of critical_exit(9), if preemption was deferred due to the critical section.
  4. Within the TDA_SCHED AST handler, when rescheduling before the return to usermode was requested. There are several reasons for this, a notable one coming from () when the running thread has exceeded its time slice.
  5. In the signal handling code (see issignal(9)) if a signal is delivered that causes a process to stop.
  6. In () where a thread needs to stop execution due to the suspension state of the process as a whole.
  7. In kern_yield(9) when a thread wants to voluntarily relinquish the processor.

The flags argument to () indicates the context switch type. One of the following must be passed:

Switch due to delayed preemption after exiting a critical section.
Switch after propagating scheduling priority to the owner of a resource.
Begin waiting on a sleepqueue(9).
Yield call.
Rescheduling was requested.
Switch from the idle thread.
A kernel thread which handles interrupts has finished work and must wait for interrupts to schedule additional work.
Thread suspended.
Preemption by a higher-priority thread, initiated by a remote processor.
Idle thread preempted, initiated by a remote processor.
The running thread has been bound to another processor and must be switched out.

In addition to the switch type, callers must specify the nature of the switch by performing a bitwise OR with one of the SW_VOL or SW_INVOL flags, but not both. Respectively, these flags denote whether the context switch is voluntary or involuntary on the part of the current thread. For an involuntary context switch in which the running thread is being preempted, the caller should also pass the SW_PREEMPT flag.

Upon entry to (), the current thread must be holding its assigned thread lock. It may be unlocked as part of the context switch. After they have been rescheduled and execution resumes, threads will exit mi_switch() with their thread lock unlocked.

() records the amount of time the current thread has been running before handing control over to the scheduler, via (). After selecting a new thread to run, the scheduler will call cpu_switch() to perform the low-level context switch.

() is the machine-dependent function that performs the actual switch from the running thread oldtd to the chosen thread newtd.

cpu_switch(9), cpu_throw(9), critical_exit(9), issignal(9), kern_yield(9), mutex(9), pmap(9), sleepqueue(9), thread_exit(9)

January 7, 2025 FreeBSD 14.3-RELEASE

Search for    or go to Top of page |  Section 9 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.