1*67035d4bSanton.\" $OpenBSD: mutex.9,v 1.25 2019/11/04 18:18:03 anton Exp $ 295534e8aSpedro.\" 3fcbf5195Sjmc.\" Copyright (c) 2005 Pedro Martelletto <pedro@ambientworks.net> 495534e8aSpedro.\" All rights reserved. 595534e8aSpedro.\" 695534e8aSpedro.\" Permission to use, copy, modify, and distribute this software for any 795534e8aSpedro.\" purpose with or without fee is hereby granted, provided that the above 895534e8aSpedro.\" copyright notice and this permission notice appear in all copies. 995534e8aSpedro.\" 1095534e8aSpedro.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 1195534e8aSpedro.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 1295534e8aSpedro.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 1395534e8aSpedro.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 1495534e8aSpedro.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 1595534e8aSpedro.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 1695534e8aSpedro.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 1795534e8aSpedro.\" 18*67035d4bSanton.Dd $Mdocdate: November 4 2019 $ 1995534e8aSpedro.Dt MUTEX 9 2095534e8aSpedro.Os 2195534e8aSpedro.Sh NAME 2295534e8aSpedro.Nm mutex , 2395534e8aSpedro.Nm mtx_init , 246b3b6211Svisa.Nm mtx_init_flags , 2595534e8aSpedro.Nm mtx_enter , 26b7266c42Sderaadt.Nm mtx_enter_try , 27ad1eb2f1Sdlg.Nm mtx_leave , 28ad1eb2f1Sdlg.Nm MUTEX_ASSERT_LOCKED , 29ad1eb2f1Sdlg.Nm MUTEX_ASSERT_UNLOCKED , 306b3b6211Svisa.Nm MUTEX_INITIALIZER , 316b3b6211Svisa.Nm MUTEX_INITIALIZER_FLAGS 3295534e8aSpedro.Nd interface to CPU mutexes 3395534e8aSpedro.Sh SYNOPSIS 34dddd2645Sschwarze.In sys/mutex.h 3595534e8aSpedro.Ft void 3695534e8aSpedro.Fn mtx_init "struct mutex *mtxp" "int wantipl" 3795534e8aSpedro.Ft void 386b3b6211Svisa.Fn mtx_init_flags "struct mutex *mtxp" "int wantipl" "const char *name" \ 396b3b6211Svisa"int flags" 406b3b6211Svisa.Ft void 4195534e8aSpedro.Fn mtx_enter "struct mutex *mtxp" 42761ff03cSdlg.Ft int 43761ff03cSdlg.Fn mtx_enter_try "struct mutex *mtxp" 4495534e8aSpedro.Ft void 4595534e8aSpedro.Fn mtx_leave "struct mutex *mtxp" 462245e4c4Soga.Fn MUTEX_ASSERT_LOCKED "struct mutex *mtxp" 472245e4c4Soga.Fn MUTEX_ASSERT_UNLOCKED "struct mutex *mtxp" 48f27d3bc2Sdlg.Fn MUTEX_INITIALIZER "int wantipl" 496b3b6211Svisa.Fn MUTEX_INITIALIZER_FLAGS "int wantipl" "const char *name" "int flags" 5095534e8aSpedro.Sh DESCRIPTION 5195534e8aSpedroThe 5295534e8aSpedro.Nm 5395534e8aSpedroset of functions provides a non-recursive, interrupt-aware spinning mechanism 5495534e8aSpedroto ensure mutual exclusion between different CPUs. 5595534e8aSpedro.Pp 5695534e8aSpedroThe 5795534e8aSpedro.Fn mtx_init 5895534e8aSpedrofunction is used to initiate the mutex pointed to by 5995534e8aSpedro.Fa mtxp . 602217a74eSteduWhen acquired, the mutex will cause the processor interrupt level to be raised 6195534e8aSpedroto 622217a74eStedu.Fa wantipl 632217a74eSteduif necessary. 6495534e8aSpedro.Pp 6595534e8aSpedroThe 666b3b6211Svisa.Fn mtx_init_flags 676b3b6211Svisamacro is similar to 686b3b6211Svisa.Fn mtx_init , 696b3b6211Svisabut it additionally accepts parameters for 706b3b6211Svisa.Xr witness 4 . 716b3b6211SvisaThe pointer 726b3b6211Svisa.Fa name 736b3b6211Svisadifferentiates a lock type. 746b3b6211SvisaTwo mutexes have the same lock type only if they have been created by the same 756b3b6211Svisaoccurrence of 766b3b6211Svisa.Fn mtx_init_flags 776b3b6211Svisawith the same pointer 786b3b6211Svisa.Fa name . 796b3b6211SvisaThe 806b3b6211Svisa.Fa flags 816b3b6211Svisaparameter is a bitwise OR of the following options: 826b3b6211Svisa.Bl -tag -width MTX_NOWITNESS -offset indent 836b3b6211Svisa.It Dv MTX_DUPOK 846b3b6211SvisaPrevents 856b3b6211Svisa.Xr witness 4 866b3b6211Svisafrom logging when a CPU acquires more than one lock of this lock type. 876b3b6211Svisa.It Dv MTX_NOWITNESS 886b3b6211SvisaInstructs 896b3b6211Svisa.Xr witness 4 906b3b6211Svisato ignore this lock. 916b3b6211Svisa.El 926b3b6211Svisa.Pp 936b3b6211SvisaThe 9495534e8aSpedro.Fn mtx_enter 9595534e8aSpedrofunction acquires a mutex, spinning if necessary. 9695534e8aSpedro.Pp 9795534e8aSpedroThe 98761ff03cSdlg.Fn mtx_enter_try 99761ff03cSdlgfunction attempts to acquire a mutex. 100761ff03cSdlg.Pp 101761ff03cSdlgThe 10295534e8aSpedro.Fn mtx_leave 10395534e8aSpedrofunction releases a mutex. 10495534e8aSpedroIn case the acquisition of the mutex caused the interrupt level to be changed, 10595534e8aSpedroit is then restored. 1062245e4c4Soga.Pp 1072245e4c4SogaThe 1082245e4c4Soga.Fn MUTEX_ASSERT_LOCKED 1092245e4c4Sogaand 1102245e4c4Soga.Fn MUTEX_ASSERT_UNLOCKED 1112245e4c4Sogamacros may be used to assert that a mutex is held locked or unlocked by 1127a5a2541Sjmcthe current CPU. 113f27d3bc2Sdlg.Pp 114f27d3bc2SdlgA mutex declaration may be initialised with the 115f27d3bc2Sdlg.Fn MUTEX_INITIALIZER 116f27d3bc2Sdlgmacro. 117f27d3bc2SdlgWhen acquired, the mutex will cause the processor interrupt level to be raised 118f27d3bc2Sdlgto 119f27d3bc2Sdlg.Fa wantipl 120f27d3bc2Sdlgif necessary. 1216b3b6211Svisa.Pp 1226b3b6211SvisaThe 1236b3b6211Svisa.Fn MUTEX_INITIALIZER_FLAGS 1246b3b6211Svisamacro is similar to 1256b3b6211Svisa.Fn MUTEX_INITIALIZER , 1266b3b6211Svisabut it additionally accepts parameters for 1276b3b6211Svisa.Xr witness 4 . 1286b3b6211SvisaSee the 1296b3b6211Svisa.Fn mtx_init_flags 1306b3b6211Svisamacro for details. 131654df495Sdlg.Sh CONTEXT 132654df495Sdlg.Fn mtx_init 1336b3b6211Svisaand 1346b3b6211Svisa.Fn mtx_init_flags 135654df495Sdlgcan be called during autoconf, from process context, or from interrupt 136654df495Sdlgcontext. 137654df495Sdlg.Pp 138654df495Sdlg.Fn mtx_enter , 139654df495Sdlg.Fn mtx_enter_try , 140654df495Sdlgand 141654df495Sdlg.Fn mtx_leave 142654df495Sdlgcan be called during autoconf, from process context, or from any 143654df495Sdlginterrupt context at or below the interrupt level 144654df495Sdlg.Fa mtxp 145654df495Sdlgwas initialised with. 1466165e501Sdlg.Sh RETURN VALUES 1476165e501SdlgThe 1486165e501Sdlg.Fn mtx_enter_try 149b1bc1f25Sjmcfunction will return non-zero if it succeeds in acquiring the mutex 1506165e501Sdlg.Fa mtxp , 1516165e501Sdlgotherwise it will return 0. 15295534e8aSpedro.Sh SEE ALSO 153*67035d4bSanton.Xr witness 4 , 15496b9b86cSdlg.Xr msleep 9 , 155f798b271Sjsg.Xr rwlock 9 , 15695534e8aSpedro.Xr spl 9 15795534e8aSpedro.Sh HISTORY 15895534e8aSpedroThe 15995534e8aSpedro.Nm 16095534e8aSpedrofunctions first appeared in 16195534e8aSpedro.Ox 3.6 . 16295534e8aSpedro.Sh AUTHORS 16395534e8aSpedroThe 16495534e8aSpedro.Nm 16595534e8aSpedrofunctions were written by 166f0641c22Sschwarze.An Artur Grabowski Aq Mt art@openbsd.org . 16795534e8aSpedro.Sh CAVEATS 168c508eb99SjsgAs these are spinning locks, don't sleep while holding one. 169c508eb99Sjsg.Pp 17095534e8aSpedroMultiple mutexes may be nested, but not interleaved. 17195534e8aSpedroThis is okay: 17295534e8aSpedro.Bd -literal -offset indent 17395534e8aSpedromtx_enter(foo); 17495534e8aSpedromtx_enter(bar); 17595534e8aSpedromtx_leave(bar); 17695534e8aSpedromtx_leave(foo); 17795534e8aSpedro.Ed 17895534e8aSpedro.Pp 17995534e8aSpedroWhile this is 18095534e8aSpedro.Fa not : 18195534e8aSpedro.Bd -literal -offset indent 18295534e8aSpedromtx_enter(foo); 18395534e8aSpedromtx_enter(bar); 18495534e8aSpedromtx_leave(foo); 18595534e8aSpedromtx_leave(bar); 18695534e8aSpedro.Ed 187