1*877332c4SJoyce McIntosh.\" 2*877332c4SJoyce McIntosh.\" The contents of this file are subject to the terms of the 3*877332c4SJoyce McIntosh.\" Common Development and Distribution License (the "License"). 4*877332c4SJoyce McIntosh.\" You may not use this file except in compliance with the License. 5*877332c4SJoyce McIntosh.\" 6*877332c4SJoyce McIntosh.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 7*877332c4SJoyce McIntosh.\" or http://www.opensolaris.org/os/licensing. 8*877332c4SJoyce McIntosh.\" See the License for the specific language governing permissions 9*877332c4SJoyce McIntosh.\" and limitations under the License. 10*877332c4SJoyce McIntosh.\" 11*877332c4SJoyce McIntosh.\" When distributing Covered Code, include this CDDL HEADER in each 12*877332c4SJoyce McIntosh.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. 13*877332c4SJoyce McIntosh.\" If applicable, add the following below this CDDL HEADER, with the 14*877332c4SJoyce McIntosh.\" fields enclosed by brackets "[]" replaced with your own identifying 15*877332c4SJoyce McIntosh.\" information: Portions Copyright [yyyy] [name of copyright owner] 16*877332c4SJoyce McIntosh.\" 17*877332c4SJoyce McIntosh.\" 18c10c16deSRichard Lowe.\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved 19*877332c4SJoyce McIntosh.\" Copyright 2018 Nexenta Systems, Inc. 20*877332c4SJoyce McIntosh.\" 21*877332c4SJoyce McIntosh.Dd July 30, 2018 22*877332c4SJoyce McIntosh.Dt SEMAPHORE 9F 23*877332c4SJoyce McIntosh.Os 24*877332c4SJoyce McIntosh.Sh NAME 25*877332c4SJoyce McIntosh.Nm semaphore , 26*877332c4SJoyce McIntosh.Nm sema_init , 27*877332c4SJoyce McIntosh.Nm sema_destroy , 28*877332c4SJoyce McIntosh.Nm sema_p , 29*877332c4SJoyce McIntosh.Nm sema_p_sig , 30*877332c4SJoyce McIntosh.Nm sema_v , 31*877332c4SJoyce McIntosh.Nm sema_tryp 32*877332c4SJoyce McIntosh.Nd semaphore functions 33*877332c4SJoyce McIntosh.Sh SYNOPSIS 34*877332c4SJoyce McIntosh.In sys/ksynch.h 35*877332c4SJoyce McIntosh.Ft void 36*877332c4SJoyce McIntosh.Fo sema_init 37*877332c4SJoyce McIntosh.Fa "ksema_t *sp" 38*877332c4SJoyce McIntosh.Fa "uint_t val" 39*877332c4SJoyce McIntosh.Fa "char *name" 40*877332c4SJoyce McIntosh.Fa "ksema_type_t type" 41*877332c4SJoyce McIntosh.Fa "void *arg" 42*877332c4SJoyce McIntosh.Fc 43*877332c4SJoyce McIntosh.Ft void 44*877332c4SJoyce McIntosh.Fo sema_destroy 45*877332c4SJoyce McIntosh.Fa "ksema_t *sp" 46*877332c4SJoyce McIntosh.Fc 47*877332c4SJoyce McIntosh.Ft void 48*877332c4SJoyce McIntosh.Fo sema_p 49*877332c4SJoyce McIntosh.Fa "ksema_t *sp" 50*877332c4SJoyce McIntosh.Fc 51*877332c4SJoyce McIntosh.Ft void 52*877332c4SJoyce McIntosh.Fo sema_v 53*877332c4SJoyce McIntosh.Fa "ksema_t *sp" 54*877332c4SJoyce McIntosh.Fc 55*877332c4SJoyce McIntosh.Ft int 56*877332c4SJoyce McIntosh.Fo sema_p_sig 57*877332c4SJoyce McIntosh.Fa "ksema_t *sp" 58*877332c4SJoyce McIntosh.Fc 59*877332c4SJoyce McIntosh.Ft int 60*877332c4SJoyce McIntosh.Fo sema_tryp 61*877332c4SJoyce McIntosh.Fa "ksema_t *sp" 62*877332c4SJoyce McIntosh.Fc 63*877332c4SJoyce McIntosh.Sh INTERFACE LEVEL 64*877332c4SJoyce McIntoshillumos DDI specific (illumos DDI). 65*877332c4SJoyce McIntosh.Sh PARAMETERS 66*877332c4SJoyce McIntosh.Bl -tag -width Ds 67*877332c4SJoyce McIntosh.It Fa sp 68*877332c4SJoyce McIntoshA pointer to a semaphore, type 69*877332c4SJoyce McIntosh.Vt ksema_t . 70*877332c4SJoyce McIntosh.It Fa val 71c10c16deSRichard LoweInitial value for semaphore. 72*877332c4SJoyce McIntosh.It Fa name 73*877332c4SJoyce McIntoshDescriptive string. 74*877332c4SJoyce McIntoshThis is obsolete and should be 75*877332c4SJoyce McIntosh.Dv NULL . 76*877332c4SJoyce McIntosh.Po Non- Ns 77*877332c4SJoyce McIntosh.Dv NULL 78*877332c4SJoyce McIntoshstrings are legal, but they are a waste of kernel memory. 79*877332c4SJoyce McIntosh.Pc 80*877332c4SJoyce McIntosh.It Fa type 81*877332c4SJoyce McIntoshVariant type of the semaphore. 82*877332c4SJoyce McIntoshCurrently, only 83*877332c4SJoyce McIntosh.Dv SEMA_DRIVER 84*877332c4SJoyce McIntoshis supported. 85*877332c4SJoyce McIntosh.It Fa arg 86*877332c4SJoyce McIntoshType-specific argument; should be 87*877332c4SJoyce McIntosh.Dv NULL . 88*877332c4SJoyce McIntosh.El 89*877332c4SJoyce McIntosh.Sh DESCRIPTION 90*877332c4SJoyce McIntoshThese functions implement counting semaphores as described by Dijkstra. 91*877332c4SJoyce McIntoshA semaphore has a value which is atomically decremented by 92*877332c4SJoyce McIntosh.Fn sema_p 93*877332c4SJoyce McIntoshand atomically incremented by 94*877332c4SJoyce McIntosh.Fn sema_v . 95*877332c4SJoyce McIntoshThe value must always be greater than or equal to zero. 96*877332c4SJoyce McIntoshIf 97*877332c4SJoyce McIntosh.Fn sema_p 98*877332c4SJoyce McIntoshis called and the value is zero, the calling thread is blocked until another 99*877332c4SJoyce McIntoshthread performs a 100*877332c4SJoyce McIntosh.Fn sema_v 101c10c16deSRichard Loweoperation on the semaphore. 102*877332c4SJoyce McIntosh.Pp 103*877332c4SJoyce McIntoshSemaphores are initialized by calling 104*877332c4SJoyce McIntosh.Fn sema_init . 105*877332c4SJoyce McIntoshThe argument, 106*877332c4SJoyce McIntosh.Fa val , 107*877332c4SJoyce McIntoshgives the initial value for the semaphore. 108*877332c4SJoyce McIntoshThe semaphore storage is provided by the caller but more may be dynamically 109*877332c4SJoyce McIntoshallocated, if necessary, by 110*877332c4SJoyce McIntosh.Fn sema_init . 111*877332c4SJoyce McIntoshFor this reason, 112*877332c4SJoyce McIntosh.Fn sema_destroy 113*877332c4SJoyce McIntoshshould be called before deallocating the storage containing the semaphore. 114*877332c4SJoyce McIntosh.Pp 115*877332c4SJoyce McIntoshThe 116*877332c4SJoyce McIntosh.Fn sema_p_sig 117*877332c4SJoyce McIntoshfunction decrements the semaphore, as does 118*877332c4SJoyce McIntosh.Fn sema_p . 119*877332c4SJoyce McIntoshHowever, if the semaphore value is zero, 120*877332c4SJoyce McIntosh.Fn sema_p_sig 121*877332c4SJoyce McIntoshwill return without decrementing the value if a signal 122*877332c4SJoyce McIntosh.Po that is, from 123*877332c4SJoyce McIntosh.Xr kill 2 124*877332c4SJoyce McIntosh.Pc 125*877332c4SJoyce McIntoshis pending for the thread. 126*877332c4SJoyce McIntosh.Pp 127*877332c4SJoyce McIntoshThe 128*877332c4SJoyce McIntosh.Fn sema_tryp 129*877332c4SJoyce McIntoshfunction will decrement the semaphore value only if it is greater than zero, and 130*877332c4SJoyce McIntoshwill not block. 131*877332c4SJoyce McIntosh.Sh CONTEXT 132c10c16deSRichard LoweThese functions can be called from user, interrupt, or kernel context, except 133*877332c4SJoyce McIntoshfor 134*877332c4SJoyce McIntosh.Fn sema_init 135*877332c4SJoyce McIntoshand 136*877332c4SJoyce McIntosh.Fn sema_destroy , 137*877332c4SJoyce McIntoshwhich can be called from user or kernel context only. 138*877332c4SJoyce McIntoshNone of these functions can be called from a high-level interrupt context. 139*877332c4SJoyce McIntoshIn most cases, 140*877332c4SJoyce McIntosh.Fn sema_v 141*877332c4SJoyce McIntoshand 142*877332c4SJoyce McIntosh.Fn sema_p 143*877332c4SJoyce McIntoshshould not be called from any interrupt context. 144*877332c4SJoyce McIntosh.Pp 145*877332c4SJoyce McIntoshIf 146*877332c4SJoyce McIntosh.Fn sema_p 147*877332c4SJoyce McIntoshis used from interrupt context, lower-priority interrupts will not be serviced 148*877332c4SJoyce McIntoshduring the wait. 149*877332c4SJoyce McIntoshThis means that if the thread that will eventually perform the 150*877332c4SJoyce McIntosh.Fn sema_v 151*877332c4SJoyce McIntoshbecomes blocked on anything that requires the lower-priority interrupt, the 152*877332c4SJoyce McIntoshsystem will hang. 153*877332c4SJoyce McIntosh.Pp 154*877332c4SJoyce McIntoshFor example, the thread that will perform the 155*877332c4SJoyce McIntosh.Fn sema_v 156*877332c4SJoyce McIntoshmay need to first allocate memory. 157*877332c4SJoyce McIntoshThis memory allocation may require waiting for paging I/O to complete, which may 158*877332c4SJoyce McIntoshrequire a lower-priority disk or network interrupt to be serviced. 159*877332c4SJoyce McIntoshIn general, situations like this are hard to predict, so it is advisable to 160*877332c4SJoyce McIntoshavoid waiting on semaphores or condition variables in an interrupt context. 161*877332c4SJoyce McIntosh.Pp 162*877332c4SJoyce McIntoshSimilar to many other synchronization mechanisms, semaphores should not be used 163*877332c4SJoyce McIntoshin any code path that requires synchronization while handling system panic, at 164*877332c4SJoyce McIntoshwhich time many of the semaphore operations become no-ops. 165*877332c4SJoyce McIntosh.Sh RETURN VALUES 166*877332c4SJoyce McIntosh.Bl -tag -width Ds 167*877332c4SJoyce McIntosh.It Li 0 168*877332c4SJoyce McIntosh.Fn sema_tryp 169*877332c4SJoyce McIntoshcould not decrement the semaphore value because it was zero. 170*877332c4SJoyce McIntosh.It Li 1 171*877332c4SJoyce McIntosh.Fn sema_p_sig 172*877332c4SJoyce McIntoshwas not able to decrement the semaphore value and detected a pending signal. 173*877332c4SJoyce McIntosh.El 174*877332c4SJoyce McIntosh.Sh SEE ALSO 175*877332c4SJoyce McIntosh.Xr kill 2 , 176*877332c4SJoyce McIntosh.Xr condvar 9F , 177*877332c4SJoyce McIntosh.Xr mutex 9F 178*877332c4SJoyce McIntosh.Pp 179*877332c4SJoyce McIntosh.Em Writing Device Drivers 180