1877332c4SJoyce McIntosh.\"
2877332c4SJoyce McIntosh.\" The contents of this file are subject to the terms of the
3877332c4SJoyce McIntosh.\" Common Development and Distribution License (the "License").
4877332c4SJoyce McIntosh.\" You may not use this file except in compliance with the License.
5877332c4SJoyce McIntosh.\"
6877332c4SJoyce McIntosh.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7877332c4SJoyce McIntosh.\" or http://www.opensolaris.org/os/licensing.
8877332c4SJoyce McIntosh.\" See the License for the specific language governing permissions
9877332c4SJoyce McIntosh.\" and limitations under the License.
10877332c4SJoyce McIntosh.\"
11877332c4SJoyce McIntosh.\" When distributing Covered Code, include this CDDL HEADER in each
12877332c4SJoyce McIntosh.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
13877332c4SJoyce McIntosh.\" If applicable, add the following below this CDDL HEADER, with the
14877332c4SJoyce McIntosh.\" fields enclosed by brackets "[]" replaced with your own identifying
15877332c4SJoyce McIntosh.\" information: Portions Copyright [yyyy] [name of copyright owner]
16877332c4SJoyce McIntosh.\"
17877332c4SJoyce McIntosh.\"
18c10c16deSRichard Lowe.\" Copyright (c) 2006, Sun Microsystems, Inc.  All Rights Reserved
19877332c4SJoyce McIntosh.\" Copyright 2018 Nexenta Systems, Inc.
20877332c4SJoyce McIntosh.\"
21877332c4SJoyce McIntosh.Dd July 30, 2018
22877332c4SJoyce McIntosh.Dt SEMAPHORE 9F
23877332c4SJoyce McIntosh.Os
24877332c4SJoyce McIntosh.Sh NAME
25877332c4SJoyce McIntosh.Nm semaphore ,
26877332c4SJoyce McIntosh.Nm sema_init ,
27877332c4SJoyce McIntosh.Nm sema_destroy ,
28877332c4SJoyce McIntosh.Nm sema_p ,
29877332c4SJoyce McIntosh.Nm sema_p_sig ,
30877332c4SJoyce McIntosh.Nm sema_v ,
31877332c4SJoyce McIntosh.Nm sema_tryp
32877332c4SJoyce McIntosh.Nd semaphore functions
33877332c4SJoyce McIntosh.Sh SYNOPSIS
34877332c4SJoyce McIntosh.In sys/ksynch.h
35877332c4SJoyce McIntosh.Ft void
36877332c4SJoyce McIntosh.Fo sema_init
37877332c4SJoyce McIntosh.Fa "ksema_t *sp"
38877332c4SJoyce McIntosh.Fa "uint_t val"
39877332c4SJoyce McIntosh.Fa "char *name"
40877332c4SJoyce McIntosh.Fa "ksema_type_t type"
41877332c4SJoyce McIntosh.Fa "void *arg"
42877332c4SJoyce McIntosh.Fc
43877332c4SJoyce McIntosh.Ft void
44877332c4SJoyce McIntosh.Fo sema_destroy
45877332c4SJoyce McIntosh.Fa "ksema_t *sp"
46877332c4SJoyce McIntosh.Fc
47877332c4SJoyce McIntosh.Ft void
48877332c4SJoyce McIntosh.Fo sema_p
49877332c4SJoyce McIntosh.Fa "ksema_t *sp"
50877332c4SJoyce McIntosh.Fc
51877332c4SJoyce McIntosh.Ft void
52877332c4SJoyce McIntosh.Fo sema_v
53877332c4SJoyce McIntosh.Fa "ksema_t *sp"
54877332c4SJoyce McIntosh.Fc
55877332c4SJoyce McIntosh.Ft int
56877332c4SJoyce McIntosh.Fo sema_p_sig
57877332c4SJoyce McIntosh.Fa "ksema_t *sp"
58877332c4SJoyce McIntosh.Fc
59877332c4SJoyce McIntosh.Ft int
60877332c4SJoyce McIntosh.Fo sema_tryp
61877332c4SJoyce McIntosh.Fa "ksema_t *sp"
62877332c4SJoyce McIntosh.Fc
63877332c4SJoyce McIntosh.Sh INTERFACE LEVEL
64877332c4SJoyce McIntoshillumos DDI specific (illumos DDI).
65877332c4SJoyce McIntosh.Sh PARAMETERS
66877332c4SJoyce McIntosh.Bl -tag -width Ds
67877332c4SJoyce McIntosh.It Fa sp
68877332c4SJoyce McIntoshA pointer to a semaphore, type
69877332c4SJoyce McIntosh.Vt ksema_t .
70877332c4SJoyce McIntosh.It Fa val
71c10c16deSRichard LoweInitial value for semaphore.
72877332c4SJoyce McIntosh.It Fa name
73877332c4SJoyce McIntoshDescriptive string.
74877332c4SJoyce McIntoshThis is obsolete and should be
75877332c4SJoyce McIntosh.Dv NULL .
76877332c4SJoyce McIntosh.Po Non- Ns
77877332c4SJoyce McIntosh.Dv NULL
78877332c4SJoyce McIntoshstrings are legal, but they are a waste of kernel memory.
79877332c4SJoyce McIntosh.Pc
80877332c4SJoyce McIntosh.It Fa type
81877332c4SJoyce McIntoshVariant type of the semaphore.
82877332c4SJoyce McIntoshCurrently, only
83877332c4SJoyce McIntosh.Dv SEMA_DRIVER
84877332c4SJoyce McIntoshis supported.
85877332c4SJoyce McIntosh.It Fa arg
86877332c4SJoyce McIntoshType-specific argument; should be
87877332c4SJoyce McIntosh.Dv NULL .
88877332c4SJoyce McIntosh.El
89877332c4SJoyce McIntosh.Sh DESCRIPTION
90877332c4SJoyce McIntoshThese functions implement counting semaphores as described by Dijkstra.
91877332c4SJoyce McIntoshA semaphore has a value which is atomically decremented by
92877332c4SJoyce McIntosh.Fn sema_p
93877332c4SJoyce McIntoshand atomically incremented by
94877332c4SJoyce McIntosh.Fn sema_v .
95877332c4SJoyce McIntoshThe value must always be greater than or equal to zero.
96877332c4SJoyce McIntoshIf
97877332c4SJoyce McIntosh.Fn sema_p
98877332c4SJoyce McIntoshis called and the value is zero, the calling thread is blocked until another
99877332c4SJoyce McIntoshthread performs a
100877332c4SJoyce McIntosh.Fn sema_v
101c10c16deSRichard Loweoperation on the semaphore.
102877332c4SJoyce McIntosh.Pp
103877332c4SJoyce McIntoshSemaphores are initialized by calling
104877332c4SJoyce McIntosh.Fn sema_init .
105877332c4SJoyce McIntoshThe argument,
106877332c4SJoyce McIntosh.Fa val ,
107877332c4SJoyce McIntoshgives the initial value for the semaphore.
108877332c4SJoyce McIntoshThe semaphore storage is provided by the caller but more may be dynamically
109877332c4SJoyce McIntoshallocated, if necessary, by
110877332c4SJoyce McIntosh.Fn sema_init .
111877332c4SJoyce McIntoshFor this reason,
112877332c4SJoyce McIntosh.Fn sema_destroy
113877332c4SJoyce McIntoshshould be called before deallocating the storage containing the semaphore.
114877332c4SJoyce McIntosh.Pp
115877332c4SJoyce McIntoshThe
116877332c4SJoyce McIntosh.Fn sema_p_sig
117877332c4SJoyce McIntoshfunction decrements the semaphore, as does
118877332c4SJoyce McIntosh.Fn sema_p .
119877332c4SJoyce McIntoshHowever, if the semaphore value is zero,
120877332c4SJoyce McIntosh.Fn sema_p_sig
121877332c4SJoyce McIntoshwill return without decrementing the value if a signal
122877332c4SJoyce McIntosh.Po that is, from
123877332c4SJoyce McIntosh.Xr kill 2
124877332c4SJoyce McIntosh.Pc
125877332c4SJoyce McIntoshis pending for the thread.
126877332c4SJoyce McIntosh.Pp
127877332c4SJoyce McIntoshThe
128877332c4SJoyce McIntosh.Fn sema_tryp
129877332c4SJoyce McIntoshfunction will decrement the semaphore value only if it is greater than zero, and
130877332c4SJoyce McIntoshwill not block.
131877332c4SJoyce McIntosh.Sh CONTEXT
132c10c16deSRichard LoweThese functions can be called from user, interrupt, or kernel context, except
133877332c4SJoyce McIntoshfor
134877332c4SJoyce McIntosh.Fn sema_init
135877332c4SJoyce McIntoshand
136877332c4SJoyce McIntosh.Fn sema_destroy ,
137877332c4SJoyce McIntoshwhich can be called from user or kernel context only.
138877332c4SJoyce McIntoshNone of these functions can be called from a high-level interrupt context.
139877332c4SJoyce McIntoshIn most cases,
140877332c4SJoyce McIntosh.Fn sema_v
141877332c4SJoyce McIntoshand
142877332c4SJoyce McIntosh.Fn sema_p
143877332c4SJoyce McIntoshshould not be called from any interrupt context.
144877332c4SJoyce McIntosh.Pp
145877332c4SJoyce McIntoshIf
146877332c4SJoyce McIntosh.Fn sema_p
147877332c4SJoyce McIntoshis used from interrupt context, lower-priority interrupts will not be serviced
148877332c4SJoyce McIntoshduring the wait.
149877332c4SJoyce McIntoshThis means that if the thread that will eventually perform the
150877332c4SJoyce McIntosh.Fn sema_v
151877332c4SJoyce McIntoshbecomes blocked on anything that requires the lower-priority interrupt, the
152877332c4SJoyce McIntoshsystem will hang.
153877332c4SJoyce McIntosh.Pp
154877332c4SJoyce McIntoshFor example, the thread that will perform the
155877332c4SJoyce McIntosh.Fn sema_v
156877332c4SJoyce McIntoshmay need to first allocate memory.
157877332c4SJoyce McIntoshThis memory allocation may require waiting for paging I/O to complete, which may
158877332c4SJoyce McIntoshrequire a lower-priority disk or network interrupt to be serviced.
159877332c4SJoyce McIntoshIn general, situations like this are hard to predict, so it is advisable to
160877332c4SJoyce McIntoshavoid waiting on semaphores or condition variables in an interrupt context.
161877332c4SJoyce McIntosh.Pp
162877332c4SJoyce McIntoshSimilar to many other synchronization mechanisms, semaphores should not be used
163877332c4SJoyce McIntoshin any code path that requires synchronization while handling system panic, at
164877332c4SJoyce McIntoshwhich time many of the semaphore operations become no-ops.
165877332c4SJoyce McIntosh.Sh RETURN VALUES
166877332c4SJoyce McIntosh.Bl -tag -width Ds
167877332c4SJoyce McIntosh.It Li 0
168877332c4SJoyce McIntosh.Fn sema_tryp
169877332c4SJoyce McIntoshcould not decrement the semaphore value because it was zero.
170877332c4SJoyce McIntosh.It Li 1
171877332c4SJoyce McIntosh.Fn sema_p_sig
172877332c4SJoyce McIntoshwas not able to decrement the semaphore value and detected a pending signal.
173877332c4SJoyce McIntosh.El
174877332c4SJoyce McIntosh.Sh SEE ALSO
175877332c4SJoyce McIntosh.Xr kill 2 ,
176877332c4SJoyce McIntosh.Xr condvar 9F ,
177877332c4SJoyce McIntosh.Xr mutex 9F
178877332c4SJoyce McIntosh.Pp
179877332c4SJoyce McIntosh.Em Writing Device Drivers
180