1.\" 2.\" Copyright (c) 2006 The DragonFly Project. 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.\" 8.\" 1. Redistributions of source code must retain the above copyright 9.\" notice, this list of conditions and the following disclaimer. 10.\" 2. Redistributions in binary form must reproduce the above copyright 11.\" notice, this list of conditions and the following disclaimer in 12.\" the documentation and/or other materials provided with the 13.\" distribution. 14.\" 3. Neither the name of The DragonFly Project nor the names of its 15.\" contributors may be used to endorse or promote products derived 16.\" from this software without specific, prior written permission. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 24.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 28.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.Dd April 10, 2010 32.Dt CRIT_ENTER 9 33.Os 34.Sh NAME 35.Nm crit_enter , 36.Nm crit_enter_gd , 37.Nm crit_enter_id , 38.Nm crit_exit , 39.Nm crit_exit_gd , 40.Nm crit_exit_id 41.Nd enter and exit a critical section 42.Sh SYNOPSIS 43.In sys/thread2.h 44.Ft void 45.Fn crit_enter "void" 46.Ft void 47.Fn crit_exit "void" 48.Ft void 49.Fn crit_enter_gd "globaldata_t gd" 50.Ft void 51.Fn crit_exit_gd "globaldata_t gd" 52.Ft void 53.Fn crit_enter_id "const char *id" 54.Ft void 55.Fn crit_exit_id "const char *id" 56.Sh DESCRIPTION 57The 58.Fn crit_enter 59and 60.Fn crit_exit 61functions are used to enter and exit a critical section of code. 62Entering a critical section will disallow preemption of the currently 63running thread on the current CPU for the duration of the critical section. 64While a critical section is active, interrupts and IPIs are also prevented 65from executing on the current CPU. 66Instead, the interrupt code marks the interrupt as deferred and immediately 67returns (without scheduling any interrupt thread). 68If an interrupt or an IPI is deferred in this way, it will be processed upon 69leaving the critical section. 70.Pp 71It is possible for a thread to sleep while holding a critical section, 72however this results in the critical section being given up for the time of 73the sleep and being reacquired after waking up. 74.Pp 75If the current CPU's globaldata pointer is available, 76.Fn crit_enter_gd 77and 78.Fn crit_exit_gd 79may be used to reduce the amount of generated code. 80.Pp 81Critical sections are per-CPU entities. 82They are typically used to interlock operations local to the CPU. 83A critical section on one CPU will not prevent an interrupt or IPI from 84occurring on some other CPU. 85If cross-CPU interlocks are required the more heavy weight 86.Xr spinlock 9 87or 88.Xr serializer 9 89lock is recommended instead. 90.Pp 91Unlike spinlocks and serializer locks, critical sections can be nested. 92.Sh DEBUGGING CRITICAL SECTIONS 93Kernels compiled with 94.Dv DEBUG_CRIT_SECTIONS 95will report any 96.Fn crit_exit 97calls that are made from a different function than the 98.Fn crit_enter 99that they are unnesting. 100The 101.Fn crit_enter_id 102and 103.Fn crit_exit_id 104functions can be used to specify a fixed ID in cases where this is done 105on purpose. 106Identifiers must be string pointers but the debug code only checks the 107pointer address, it does not do a 108.Fn strcmp 109to validate the ID. 110.Sh FILES 111The critical section implementation is in 112.Pa /sys/sys/thread2.h . 113.Sh SEE ALSO 114.Xr locking 9 , 115.Xr serializer 9 , 116.Xr spinlock 9 117.Sh HISTORY 118These functions were introduced in 119.Dx 1.0 . 120