xref: /freebsd/share/man/man9/critical_enter.9 (revision a7623790fb345e6dc986dfd31df0ace115e6f2e4)
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.\" $FreeBSD$
25.\"
26.Dd October 5, 2005
27.Dt CRITICAL_ENTER 9
28.Os
29.Sh NAME
30.Nm critical_enter ,
31.Nm critical_exit
32.Nd enter and exit a critical region
33.Sh SYNOPSIS
34.In sys/param.h
35.In sys/systm.h
36.Ft void
37.Fn critical_enter "void"
38.Ft void
39.Fn critical_exit "void"
40.Sh DESCRIPTION
41These functions are used to prevent preemption in a critical region of code.
42All that is guaranteed is that the thread currently executing on a CPU will
43not be preempted.
44Specifically, a thread in a critical region will not migrate to another
45CPU while it is in a critical region.
46The current CPU may still trigger faults and exceptions during a critical
47section; however, these faults are usually fatal.
48.Pp
49The
50.Fn critical_enter
51and
52.Fn critical_exit
53functions manage a per-thread counter to handle nested critical sections.
54If a thread is made runnable that would normally preempt the current thread
55while the current thread is in a critical section,
56then the preemption will be deferred until the current thread exits the
57outermost critical section.
58.Pp
59Note that these functions are not required to provide any inter-CPU
60synchronization, data protection, or memory ordering guarantees and thus
61should
62.Em not
63be used to protect shared data structures.
64.Pp
65These functions should be used with care as an infinite loop within a
66critical region will deadlock the CPU.
67Also, they should not be interlocked with operations on mutexes, sx locks,
68semaphores, or other synchronization primitives.
69One exception to this is that spin mutexes include a critical section,
70so in certain cases critical sections may be interlocked with spin mutexes.
71.Sh HISTORY
72These functions were introduced in
73.Fx 5.0 .
74