xref: /freebsd/share/man/man9/critical_enter.9 (revision 397e83df75e0fcd0d3fcb95ae4d794cb7600fc89)
1.\" Copyright (c) 2001,2002 John H. Baldwin <jhb@FreeBSD.org>
2.\"
3.\" Redistribution and use in source and binary forms, with or without
4.\" modification, are permitted provided that the following conditions
5.\" are met:
6.\" 1. Redistributions of source code must retain the above copyright
7.\"    notice, this list of conditions and the following disclaimer.
8.\" 2. Redistributions in binary form must reproduce the above copyright
9.\"    notice, this list of conditions and the following disclaimer in the
10.\"    documentation and/or other materials provided with the distribution.
11.\"
12.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
15.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
22.\" SUCH DAMAGE.
23.\"
24.Dd March 20, 2023
25.Dt CRITICAL_ENTER 9
26.Os
27.Sh NAME
28.Nm critical_enter ,
29.Nm critical_exit
30.Nd enter and exit a critical region
31.Sh SYNOPSIS
32.In sys/param.h
33.In sys/systm.h
34.Ft void
35.Fn critical_enter "void"
36.Ft void
37.Fn critical_exit "void"
38.Fn CRITICAL_ASSERT "struct thread *td"
39.Sh DESCRIPTION
40These functions are used to prevent preemption in a critical region of code.
41All that is guaranteed is that the thread currently executing on a CPU will
42not be preempted.
43Specifically, a thread in a critical region will not migrate to another CPU
44while it is in a critical region, nor will the current CPU switch to a
45different thread.
46The current CPU may still trigger faults and exceptions during a critical
47section; however, these faults are usually fatal.
48.Pp
49The CPU might also receive and handle interrupts within a critical section.
50When this occurs the interrupt exit will not result in a context switch, and
51execution will continue in the critical section.
52Thus, the net effect of a critical section on the current thread's execution is
53similar to running with interrupts disabled, except that timer interrupts and
54filtered interrupt handlers do not incur a latency penalty.
55.Pp
56The
57.Fn critical_enter
58and
59.Fn critical_exit
60functions manage a per-thread counter to handle nested critical sections.
61If a thread is made runnable that would normally preempt the current thread
62while the current thread is in a critical section,
63then the preemption will be deferred until the current thread exits the
64outermost critical section.
65.Pp
66Note that these functions do not provide any inter-CPU synchronization, data
67protection, or memory ordering guarantees, and thus should
68.Em not
69be used to protect shared data structures.
70.Pp
71These functions should be used with care as an unbound or infinite loop within
72a critical region will deadlock the CPU.
73Also, they should not be interlocked with operations on mutexes, sx locks,
74semaphores, or other synchronization primitives, as these primitives may
75require a context switch to operate.
76One exception to this is that spin mutexes include a critical section,
77so in certain cases critical sections may be interlocked with spin mutexes.
78.Pp
79Critical regions should be only as wide as necessary.
80That is, code which does not require the critical section to operate correctly
81should be excluded from its bounds whenever possible.
82Abuse of critical sections has an effect on overall system latency and timer
83precision, since disabling preemption will delay the execution of threaded
84interrupt handlers and
85.Xr callout 9
86events on the current CPU.
87.Pp
88The
89.Fn CRITICAL_ASSERT
90macro verifies that the provided thread
91.Fa td
92is currently executing in a critical section.
93It is a wrapper around
94.Xr KASSERT 9 .
95.Sh SEE ALSO
96.Xr callout 9 ,
97.Xr KASSERT 9 ,
98.Xr locking 9
99.Sh HISTORY
100These functions were introduced in
101.Fx 5.0 .
102