summaryrefslogtreecommitdiff
path: root/static/netbsd/man3/mtx.3
blob: 907c526cabcdd4b75b69eada1410b118621b2159 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
.\"	$NetBSD: mtx.3,v 1.3 2025/02/10 20:40:55 riastradh Exp $
.\"
.\" Copyright (c) 2016 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" This code is derived from software contributed to The NetBSD Foundation
.\" by Kamil Rytarowski.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd October 16, 2016
.Dt MTX 3
.Os
.Sh NAME
.Nm mtx
.Nd mutex functions
.Sh LIBRARY
.Lb libpthread
.Sh SYNOPSIS
.In threads.h
.Ft void
.Fn mtx_destroy "mtx_t *mtx"
.Ft int
.Fn mtx_init "mtx_t *mtx" "int type"
.Ft int
.Fn mtx_lock "mtx_t *mtx"
.Ft int
.Fn mtx_timedlock "mtx_t * restrict mtx" "const struct timespec * restrict ts"
.Ft int
.Fn mtx_trylock "mtx_t *mtx"
.Ft int
.Fn mtx_unlock "mtx_t *mtx"
.Sh DESCRIPTION
The
.Fn mtx_destroy
function releases the resources of
.Fa mtx .
It is not allowed to block the same
.Fa mtx
during the
.Fn mtx_destroy
call.
.Pp
The
.Fn mtx_init
function initialized the
.Fa mtx
object uniquely identificable with the
.Fa type
properties.
The allowed values of
.Fa type
are as follows:
.Bl -column "mtx_plain | mtx_recursive"
.It Sy "Type"                    Ta Sy "Description"
.It Dv mtx_plain                 Ta basic mutex
.It Dv mtx_timed                 Ta mutex with timeout support
.It Dv mtx_plain | Dv mtx_recursive Ta basic recursive mutex
.It Dv mtx_timed | Dv mtx_recursive Ta recursive mutex with timeout support
.El
.Pp
The underlying
.Nx
implementation of mutex types does not distinguish between
.Dv mtx_plain
and
.Dv mtx_timed ,
however portable code must keep the distinction.
.Pp
The
.Fn mtx_lock
function locks the
.Fa mtx
object.
It is required to never lock the same
.Fa mtx
object without the
.Dv mtx_recursive
property multiple times.
If the
.Fa mtx
object is already locked by another thread,
the caller of
.Fa mtx_lock
blocks until the lock becomes available.
.Pp
The
.Fn mtx_timedlock
function tries to lock the
.Fa mtx
object.
In case of blocked resource by another thread,
this call blocks for the specified timeout in the
.Fa ts
argument.
The timeout argument is
.Dv TIME_UTC
based time of
.Dv timespec
type.
It is required to never lock the same
.Fa mtx
object without the
.Dv mtx_recursive
property multiple times.
In portable code, a
.Fa mtx
object with the
.Dv mtx_recursive
property must be used in such a case.
.Pp
The
.Fn mtx_trylock
function call attempts to lock the
.Fa mtx
object.
This function does not block if another thread already locked the
.Fa mtx
object, but immediately returns indicating proper status.
.Pp
The
.Fn mtx_unlock
function unlocks the
.Fa mtx
object.
This call must be preceded with a matching
.Fn mtx_lock
call in the same thread.
.Sh RETURN VALUES
The
.Fn mtx_destroy
function returns no value.
.Pp
The
.Fn mtx_init
function returns
.Dv thrd_success
on success or
.Dv thrd_error
on failure.
.Pp
The
.Fn mtx_lock
function returns
.Dv thrd_success
on success or
.Dv thrd_error
on failure.
.Pp
The
.Fn mtx_lock
function returns
.Dv thrd_success
on success,
otherwise
.Dv thrd_timedout
to indicate that system time has reached or exceeded the time specified in
.Dv ts ,
or
.Dv thrd_error
on failure.
.Pp
The
.Fn mtx_trylock
function returns
.Dv thrd_success
on success,
otherwise
.Dv thrd_timedout
to indicate that
.Fa mtx
object is already locked, or
.Dv thrd_error
on failure.
.Pp
The
.Fn mtx_unlock
function returns
.Dv thrd_success
on success,
otherwise
.Dv thrd_timedout
to indicate that
.Fa mtx
object is already locked, or
.Dv thrd_error
on failure.
.Sh SEE ALSO
.Xr pthread_mutex 3 ,
.Xr threads 3
.Sh STANDARDS
The
.Nm
interface conforms to
.St -isoC-2011 .
.Sh HISTORY
This interface first appeared in
.Nx 9 .
.Sh AUTHORS
.An Kamil Rytarowski Aq Mt kamil@NetBSD.org