xref: /freebsd/share/man/man9/critical_enter.9 (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
1.\" Copyright (c) 2001,2002 John H. Baldwin <jhb@FreeBSD.org>
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd March 22, 2001
28.Dt CRITICAL_ENTER 9
29.Os
30.Sh NAME
31.Nm cpu_critical_enter ,
32.Nm cpu_critical_exit ,
33.Nm critical_enter ,
34.Nm critical_exit
35.Nd enter and exit a critical region
36.Sh SYNOPSIS
37.In sys/param.h
38.In sys/proc.h
39.In sys/systm.h
40.In machine/critical.h
41.Ft void
42.Fn cpu_critical_enter "void"
43.Ft void
44.Fn cpu_critical_exit "void"
45.Ft void
46.Fn critical_enter "void"
47.Ft void
48.Fn critical_exit "void"
49.Sh DESCRIPTION
50These functions are used to prevent preemption in a critical region of code.
51All that is guaranteed is that the thread currently executing on a CPU will
52not be preempted.
53Specifically, a thread in a critical region will not migrate to another
54CPU while it is in a critical region.
55The current CPU may still trigger faults and exceptions during a critical
56section; however, these faults are usually fatal.
57.Pp
58The
59.Fn cpu_critical_enter
60and
61.Fn cpu_critical_exit
62functions provide the machine dependent disabling of preemption, normally
63by disabling interrupts on the local CPU.
64.Pp
65The
66.Fn critical_enter
67and
68.Fn critical_exit
69functions provide a machine independent wrapper around the machine dependent
70API.
71This wrapper currently saves state regarding nested critical sections.
72Nearly all code should use these versions of the API.
73.Pp
74Note that these functions are not required to provide any inter-CPU
75synchronization, data protection, or memory ordering guarantees and thus
76should
77.Em not
78be used to protect shared data structures.
79.Pp
80These functions should be used with care as an infinite loop within a
81critical region will deadlock the CPU.
82Also, they should not be interlocked with operations on mutexes, sx locks,
83semaphores, or other synchronization primitives.
84.Sh EXAMPLES
85This example demonstrates the use of
86.Fn critical_enter
87and
88.Fn critical_exit
89to guarantee atomic access to the DMA controller.
90.Bd -literal -offset indent
91int
92isa_dmastatus(int chan)
93{
94        u_long  cnt = 0;
95        int     ffport, waport;
96        u_long  low1, high1, low2, high2;
97
98	...
99        critical_enter();
100        outb(ffport, 0);
101        low1 = inb(waport);
102        high1 = inb(waport);
103        outb(ffport, 0);
104        low2 = inb(waport);
105        high2 = inb(waport);
106        critical_exit();
107	...
108}
109.Ed
110.Sh HISTORY
111These functions were introduced in
112.Fx 5.0 .
113