xref: /freebsd/share/man/man9/critical_enter.9 (revision 5521ff5a4d1929056e7ffc982fac3341ca54df7c)
1.\" Copyright (c) 2001 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 critical_enter ,
32.Nm critical_exit
33.Nd enter and exit a critical region
34.Sh SYNOPSIS
35.Fd #include <sys/types.h>
36.Fd #include <sys/systm.h>
37.Ft critical_t
38.Fn critical_enter "void"
39.Ft void
40.Fn critical_exit "critical_t savecrit"
41.Sh DESCRIPTION
42These functions are used to prevent preemption in a critcal region of code.
43All that is guaranteed is that the current CPU will not be preempted by an
44external interrupt.
45The current CPU may still trigger faults and exceptions during a critical
46section.
47The
48.Fn critical_enter
49function returns an opaque value of type
50.Vt critical_t .
51This value must be passed to the
52.Fn critical_exit
53function when leaving the critical region.
54.Pp
55Note that these functions are not required to provide any inter-CPU
56synchronization, data protection, or memory ordering guarantees and thus
57should
58.Em not
59be used to protect shared data structures.
60.Pp
61These functions should be used with care as an infinite loop within a
62critical region will deadlock the CPU.
63.Sh RETURN VALUES
64The
65.Fn critical_enter
66function returns an opaque value that must be passed to a corresponding call to
67.Fn critical_exit .
68.Sh EXAMPLES
69This example demonstrates the use of
70.Fn critical_enter
71and
72.Fn critical_exit
73to guarantee atomic access to the DMA controller.
74.Bd -literal -offset indent
75int
76isa_dmastatus(int chan)
77{
78        u_long  cnt = 0;
79        int     ffport, waport;
80        u_long  low1, high1, low2, high2;
81        critical_t savecrit;
82
83	...
84        savecrit = critical_enter();
85        outb(ffport, 0);
86        low1 = inb(waport);
87        high1 = inb(waport);
88        outb(ffport, 0);
89        low2 = inb(waport);
90        high2 = inb(waport);
91        critical_exit(savecrit);
92	...
93}
94.Ed
95.Sh HISTORY
96These functions were introduced in
97.Fx 5.0 .
98