13af31f78SSascha Wildner.\" 23af31f78SSascha Wildner.\" Copyright (c) 2006 The DragonFly Project. All rights reserved. 33af31f78SSascha Wildner.\" 43af31f78SSascha Wildner.\" Redistribution and use in source and binary forms, with or without 53af31f78SSascha Wildner.\" modification, are permitted provided that the following conditions 63af31f78SSascha Wildner.\" are met: 73af31f78SSascha Wildner.\" 83af31f78SSascha Wildner.\" 1. Redistributions of source code must retain the above copyright 93af31f78SSascha Wildner.\" notice, this list of conditions and the following disclaimer. 103af31f78SSascha Wildner.\" 2. Redistributions in binary form must reproduce the above copyright 113af31f78SSascha Wildner.\" notice, this list of conditions and the following disclaimer in 123af31f78SSascha Wildner.\" the documentation and/or other materials provided with the 133af31f78SSascha Wildner.\" distribution. 143af31f78SSascha Wildner.\" 3. Neither the name of The DragonFly Project nor the names of its 153af31f78SSascha Wildner.\" contributors may be used to endorse or promote products derived 163af31f78SSascha Wildner.\" from this software without specific, prior written permission. 173af31f78SSascha Wildner.\" 183af31f78SSascha Wildner.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 193af31f78SSascha Wildner.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 203af31f78SSascha Wildner.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 213af31f78SSascha Wildner.\" FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 223af31f78SSascha Wildner.\" COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 233af31f78SSascha Wildner.\" INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 243af31f78SSascha Wildner.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 253af31f78SSascha Wildner.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 263af31f78SSascha Wildner.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 273af31f78SSascha Wildner.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 283af31f78SSascha Wildner.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 293af31f78SSascha Wildner.\" SUCH DAMAGE. 303af31f78SSascha Wildner.\" 31deff95cbSSascha Wildner.Dd April 10, 2010 323af31f78SSascha Wildner.Dt CRIT_ENTER 9 333af31f78SSascha Wildner.Os 343af31f78SSascha Wildner.Sh NAME 353af31f78SSascha Wildner.Nm crit_enter , 363af31f78SSascha Wildner.Nm crit_enter_gd , 373af31f78SSascha Wildner.Nm crit_enter_id , 383af31f78SSascha Wildner.Nm crit_exit , 393af31f78SSascha Wildner.Nm crit_exit_gd , 403af31f78SSascha Wildner.Nm crit_exit_id 413af31f78SSascha Wildner.Nd enter and exit a critical section 423af31f78SSascha Wildner.Sh SYNOPSIS 433af31f78SSascha Wildner.In sys/thread2.h 443af31f78SSascha Wildner.Ft void 453af31f78SSascha Wildner.Fn crit_enter "void" 463af31f78SSascha Wildner.Ft void 473af31f78SSascha Wildner.Fn crit_exit "void" 483af31f78SSascha Wildner.Ft void 493af31f78SSascha Wildner.Fn crit_enter_gd "globaldata_t gd" 503af31f78SSascha Wildner.Ft void 513af31f78SSascha Wildner.Fn crit_exit_gd "globaldata_t gd" 523af31f78SSascha Wildner.Ft void 533af31f78SSascha Wildner.Fn crit_enter_id "const char *id" 543af31f78SSascha Wildner.Ft void 553af31f78SSascha Wildner.Fn crit_exit_id "const char *id" 563af31f78SSascha Wildner.Sh DESCRIPTION 573af31f78SSascha WildnerThe 583af31f78SSascha Wildner.Fn crit_enter 593af31f78SSascha Wildnerand 603af31f78SSascha Wildner.Fn crit_exit 613af31f78SSascha Wildnerfunctions are used to enter and exit a critical section of code. 62dca05b43SNuno AntunesEntering a critical section will disallow preemption of the currently 63dca05b43SNuno Antunesrunning thread on the current CPU for the duration of the critical section. 64dca05b43SNuno AntunesWhile a critical section is active, interrupts and IPIs are also prevented 65dca05b43SNuno Antunesfrom executing on the current CPU. 663af31f78SSascha WildnerInstead, the interrupt code marks the interrupt as deferred and immediately 67dca05b43SNuno Antunesreturns (without scheduling any interrupt thread). 68dca05b43SNuno AntunesIf an interrupt or an IPI is deferred in this way, it will be processed upon 69dca05b43SNuno Antunesleaving the critical section. 70dca05b43SNuno Antunes.Pp 713af31f78SSascha WildnerIt is possible for a thread to sleep while holding a critical section, 723af31f78SSascha Wildnerhowever this results in the critical section being given up for the time of 733af31f78SSascha Wildnerthe sleep and being reacquired after waking up. 743af31f78SSascha Wildner.Pp 753af31f78SSascha WildnerIf the current CPU's globaldata pointer is available, 763af31f78SSascha Wildner.Fn crit_enter_gd 773af31f78SSascha Wildnerand 783af31f78SSascha Wildner.Fn crit_exit_gd 793af31f78SSascha Wildnermay be used to reduce the amount of generated code. 803af31f78SSascha Wildner.Pp 813af31f78SSascha WildnerCritical sections are per-CPU entities. 821be1f152SSascha WildnerThey are typically used to interlock operations local to the CPU. 833af31f78SSascha WildnerA critical section on one CPU will not prevent an interrupt or IPI from 843221afbeSHasso Tepperoccurring on some other CPU. 853af31f78SSascha WildnerIf cross-CPU interlocks are required the more heavy weight 863af31f78SSascha Wildner.Xr spinlock 9 873af31f78SSascha Wildneror 883af31f78SSascha Wildner.Xr serializer 9 893af31f78SSascha Wildnerlock is recommended instead. 903af31f78SSascha Wildner.Pp 913af31f78SSascha WildnerUnlike spinlocks and serializer locks, critical sections can be nested. 923af31f78SSascha Wildner.Sh DEBUGGING CRITICAL SECTIONS 933af31f78SSascha WildnerKernels compiled with 943af31f78SSascha Wildner.Dv DEBUG_CRIT_SECTIONS 953af31f78SSascha Wildnerwill report any 963af31f78SSascha Wildner.Fn crit_exit 973af31f78SSascha Wildnercalls that are made from a different function than the 983af31f78SSascha Wildner.Fn crit_enter 993af31f78SSascha Wildnerthat they are unnesting. 1003af31f78SSascha WildnerThe 1013af31f78SSascha Wildner.Fn crit_enter_id 1023af31f78SSascha Wildnerand 1033af31f78SSascha Wildner.Fn crit_exit_id 1043af31f78SSascha Wildnerfunctions can be used to specify a fixed ID in cases where this is done 1053af31f78SSascha Wildneron purpose. 1063af31f78SSascha WildnerIdentifiers must be string pointers but the debug code only checks the 1073af31f78SSascha Wildnerpointer address, it does not do a 1083af31f78SSascha Wildner.Fn strcmp 1093af31f78SSascha Wildnerto validate the ID. 110deff95cbSSascha Wildner.Sh FILES 111deff95cbSSascha WildnerThe critical section implementation is in 112deff95cbSSascha Wildner.Pa /sys/sys/thread2.h . 1133af31f78SSascha Wildner.Sh SEE ALSO 114*c04308e8SMarkus Pfeiffer.Xr locking 9 , 1153af31f78SSascha Wildner.Xr serializer 9 , 1163af31f78SSascha Wildner.Xr spinlock 9 1173af31f78SSascha Wildner.Sh HISTORY 1183af31f78SSascha WildnerThese functions were introduced in 1193af31f78SSascha Wildner.Dx 1.0 . 120