xref: /freebsd/lib/libstdthreads/thrd_create.3 (revision fc6f0665bae406f564e6eb6e1deea849d368a708)
1*fc6f0665SEd Schouten.\" Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
2*fc6f0665SEd Schouten.\" All rights reserved.
3*fc6f0665SEd Schouten.\"
4*fc6f0665SEd Schouten.\" Redistribution and use in source and binary forms, with or without
5*fc6f0665SEd Schouten.\" modification, are permitted provided that the following conditions
6*fc6f0665SEd Schouten.\" are met:
7*fc6f0665SEd Schouten.\" 1. Redistributions of source code must retain the above copyright
8*fc6f0665SEd Schouten.\"    notice, this list of conditions and the following disclaimer.
9*fc6f0665SEd Schouten.\" 2. Redistributions in binary form must reproduce the above copyright
10*fc6f0665SEd Schouten.\"    notice, this list of conditions and the following disclaimer in the
11*fc6f0665SEd Schouten.\"    documentation and/or other materials provided with the distribution.
12*fc6f0665SEd Schouten.\"
13*fc6f0665SEd Schouten.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*fc6f0665SEd Schouten.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*fc6f0665SEd Schouten.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*fc6f0665SEd Schouten.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*fc6f0665SEd Schouten.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*fc6f0665SEd Schouten.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*fc6f0665SEd Schouten.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*fc6f0665SEd Schouten.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*fc6f0665SEd Schouten.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*fc6f0665SEd Schouten.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*fc6f0665SEd Schouten.\" SUCH DAMAGE.
24*fc6f0665SEd Schouten.\"
25*fc6f0665SEd Schouten.\" $FreeBSD$
26*fc6f0665SEd Schouten.\"
27*fc6f0665SEd Schouten.Dd December 26, 2011
28*fc6f0665SEd Schouten.Dt THRD_CREATE 3
29*fc6f0665SEd Schouten.Os
30*fc6f0665SEd Schouten.Sh NAME
31*fc6f0665SEd Schouten.Nm call_once ,
32*fc6f0665SEd Schouten.Nm cnd_broadcast ,
33*fc6f0665SEd Schouten.Nm cnd_destroy ,
34*fc6f0665SEd Schouten.Nm cnd_init ,
35*fc6f0665SEd Schouten.Nm cnd_signal ,
36*fc6f0665SEd Schouten.Nm cnd_timedwait ,
37*fc6f0665SEd Schouten.Nm cnd_wait ,
38*fc6f0665SEd Schouten.Nm mtx_destroy ,
39*fc6f0665SEd Schouten.Nm mtx_init ,
40*fc6f0665SEd Schouten.Nm mtx_lock ,
41*fc6f0665SEd Schouten.Nm mtx_timedlock ,
42*fc6f0665SEd Schouten.Nm mtx_trylock ,
43*fc6f0665SEd Schouten.Nm mtx_unlock ,
44*fc6f0665SEd Schouten.Nm thrd_create ,
45*fc6f0665SEd Schouten.Nm thrd_current ,
46*fc6f0665SEd Schouten.Nm thrd_detach ,
47*fc6f0665SEd Schouten.Nm thrd_equal ,
48*fc6f0665SEd Schouten.Nm thrd_exit ,
49*fc6f0665SEd Schouten.Nm thrd_join ,
50*fc6f0665SEd Schouten.Nm thrd_sleep ,
51*fc6f0665SEd Schouten.Nm thrd_yield ,
52*fc6f0665SEd Schouten.Nm tss_create ,
53*fc6f0665SEd Schouten.Nm tss_delete ,
54*fc6f0665SEd Schouten.Nm tss_get ,
55*fc6f0665SEd Schouten.Nm tss_set
56*fc6f0665SEd Schouten.Nd C11 threads interface
57*fc6f0665SEd Schouten.Sh LIBRARY
58*fc6f0665SEd Schouten.Lb libstdthreads
59*fc6f0665SEd Schouten.Sh SYNOPSIS
60*fc6f0665SEd Schouten.In threads.h
61*fc6f0665SEd Schouten.Ft void
62*fc6f0665SEd Schouten.Fn call_once "once_flag *flag" "void (*func)(void)"
63*fc6f0665SEd Schouten.Ft int
64*fc6f0665SEd Schouten.Fn cnd_broadcast "cnd_t *cond"
65*fc6f0665SEd Schouten.Ft void
66*fc6f0665SEd Schouten.Fn cnd_destroy "cnd_t *cond"
67*fc6f0665SEd Schouten.Ft int
68*fc6f0665SEd Schouten.Fn cnd_init "cnd_t *cond"
69*fc6f0665SEd Schouten.Ft int
70*fc6f0665SEd Schouten.Fn cnd_signal "cnd_t *cond"
71*fc6f0665SEd Schouten.Ft int
72*fc6f0665SEd Schouten.Fn cnd_timedwait "cnd_t * restrict cond" "mtx_t * restrict mtx" "const struct timespec * restrict ts"
73*fc6f0665SEd Schouten.Ft int
74*fc6f0665SEd Schouten.Fn cnd_wait "cnd_t *cond" "mtx_t *mtx"
75*fc6f0665SEd Schouten.Ft void
76*fc6f0665SEd Schouten.Fn mtx_destroy "mtx_t *mtx"
77*fc6f0665SEd Schouten.Ft int
78*fc6f0665SEd Schouten.Fn mtx_init "mtx_t *mtx" "int type"
79*fc6f0665SEd Schouten.Ft int
80*fc6f0665SEd Schouten.Fn mtx_lock "mtx_t *mtx"
81*fc6f0665SEd Schouten.Ft int
82*fc6f0665SEd Schouten.Fn mtx_timedlock "mtx_t * restrict mtx" "const struct timespec * restrict ts"
83*fc6f0665SEd Schouten.Ft int
84*fc6f0665SEd Schouten.Fn mtx_trylock "mtx_t *mtx"
85*fc6f0665SEd Schouten.Ft int
86*fc6f0665SEd Schouten.Fn mtx_unlock "mtx_t *mtx"
87*fc6f0665SEd Schouten.Ft int
88*fc6f0665SEd Schouten.Fn thrd_create "thrd_t *thr" "int (*func)(void *)" "void *arg"
89*fc6f0665SEd Schouten.Ft thrd_t
90*fc6f0665SEd Schouten.Fn thrd_current "void"
91*fc6f0665SEd Schouten.Ft int
92*fc6f0665SEd Schouten.Fn thrd_detach "thrd_t thr"
93*fc6f0665SEd Schouten.Ft int
94*fc6f0665SEd Schouten.Fn thrd_equal "thrd_t thr0" "thrd_t thr1"
95*fc6f0665SEd Schouten.Ft _Noreturn void
96*fc6f0665SEd Schouten.Fn thrd_exit "int res"
97*fc6f0665SEd Schouten.Ft int
98*fc6f0665SEd Schouten.Fn thrd_join "thrd_t thr" "int *res"
99*fc6f0665SEd Schouten.Ft int
100*fc6f0665SEd Schouten.Fn thrd_sleep "const struct timespec *duration" "struct timespec *remaining"
101*fc6f0665SEd Schouten.Ft void
102*fc6f0665SEd Schouten.Fn thrd_yield "void"
103*fc6f0665SEd Schouten.Ft int
104*fc6f0665SEd Schouten.Fn tss_create "tss_t *key" "void (*dtor)(void *)"
105*fc6f0665SEd Schouten.Ft void
106*fc6f0665SEd Schouten.Fn tss_delete "tss_t key"
107*fc6f0665SEd Schouten.Ft void *
108*fc6f0665SEd Schouten.Fn tss_get "tss_t key"
109*fc6f0665SEd Schouten.Ft int
110*fc6f0665SEd Schouten.Fn tss_set "tss_t key" "void *val"
111*fc6f0665SEd Schouten.Sh DESCRIPTION
112*fc6f0665SEd SchoutenAs of
113*fc6f0665SEd Schouten.St -isoC-11 ,
114*fc6f0665SEd Schoutenthe C standard includes an API for writing multithreaded applications.
115*fc6f0665SEd SchoutenSince POSIX.1 already includes a threading API that is used by virtually
116*fc6f0665SEd Schoutenany multithreaded application, the interface provided by the C standard
117*fc6f0665SEd Schoutencan be considered superfluous.
118*fc6f0665SEd Schouten.Pp
119*fc6f0665SEd SchoutenIn this implementation, the threading interface is therefore implemented
120*fc6f0665SEd Schoutenas a light-weight layer on top of existing interfaces.
121*fc6f0665SEd SchoutenThe functions to which these routines are mapped, are listed in the
122*fc6f0665SEd Schoutenfollowing table.
123*fc6f0665SEd SchoutenPlease refer to the documentation of the POSIX equivalent functions for
124*fc6f0665SEd Schoutenmore information.
125*fc6f0665SEd Schouten.Bl -column ".Fn mtx_timedlock" ".Xr pthread_mutex_timedlock 3" -offset indent
126*fc6f0665SEd Schouten.It Em Function Ta Em POSIX equivalent
127*fc6f0665SEd Schouten.It Fn call_once Ta Xr pthread_once 3
128*fc6f0665SEd Schouten.It Fn cnd_broadcast Ta Xr pthread_cond_broadcast 3
129*fc6f0665SEd Schouten.It Fn cnd_destroy Ta Xr pthread_cond_destroy 3
130*fc6f0665SEd Schouten.It Fn cnd_init Ta Xr pthread_cond_init 3
131*fc6f0665SEd Schouten.It Fn cnd_signal Ta Xr pthread_cond_signal 3
132*fc6f0665SEd Schouten.It Fn cnd_timedwait Ta Xr pthread_cond_timedwait 3
133*fc6f0665SEd Schouten.It Fn cnd_wait Ta Xr pthread_cond_wait 3
134*fc6f0665SEd Schouten.It Fn mtx_destroy Ta Xr pthread_mutex_destroy 3
135*fc6f0665SEd Schouten.It Fn mtx_init Ta Xr pthread_mutex_init 3
136*fc6f0665SEd Schouten.It Fn mtx_lock Ta Xr pthread_mutex_lock 3
137*fc6f0665SEd Schouten.It Fn mtx_timedlock Ta Xr pthread_mutex_timedlock 3
138*fc6f0665SEd Schouten.It Fn mtx_trylock Ta Xr pthread_mutex_trylock 3
139*fc6f0665SEd Schouten.It Fn mtx_unlock Ta Xr pthread_mutex_unlock 3
140*fc6f0665SEd Schouten.It Fn thrd_create Ta Xr pthread_create 3
141*fc6f0665SEd Schouten.It Fn thrd_current Ta Xr pthread_self 3
142*fc6f0665SEd Schouten.It Fn thrd_detach Ta Xr pthread_detach 3
143*fc6f0665SEd Schouten.It Fn thrd_equal Ta Xr pthread_equal 3
144*fc6f0665SEd Schouten.It Fn thrd_exit Ta Xr pthread_exit 3
145*fc6f0665SEd Schouten.It Fn thrd_join Ta Xr pthread_join 3
146*fc6f0665SEd Schouten.It Fn thrd_sleep Ta Xr nanosleep 2
147*fc6f0665SEd Schouten.It Fn thrd_yield Ta Xr pthread_yield 3
148*fc6f0665SEd Schouten.It Fn tss_create Ta Xr pthread_key_create 3
149*fc6f0665SEd Schouten.It Fn tss_delete Ta Xr pthread_key_delete 3
150*fc6f0665SEd Schouten.It Fn tss_get Ta Xr pthread_getspecific 3
151*fc6f0665SEd Schouten.It Fn tss_set Ta Xr pthread_setspecific 3
152*fc6f0665SEd Schouten.El
153*fc6f0665SEd Schouten.Sh DIFFERENCES WITH POSIX EQUIVALENTS
154*fc6f0665SEd SchoutenThe
155*fc6f0665SEd Schouten.Fn thrd_exit
156*fc6f0665SEd Schoutenfunction returns an integer value to the thread calling
157*fc6f0665SEd Schouten.Fn thrd_join ,
158*fc6f0665SEd Schoutenwhereas the
159*fc6f0665SEd Schouten.Fn pthread_exit
160*fc6f0665SEd Schoutenfunction uses a pointer.
161*fc6f0665SEd Schouten.Pp
162*fc6f0665SEd SchoutenThe mutex created by
163*fc6f0665SEd Schouten.Fn mtx_init
164*fc6f0665SEd Schoutencan be of
165*fc6f0665SEd Schouten.Fa type
166*fc6f0665SEd Schouten.Dv mtx_plain
167*fc6f0665SEd Schoutenor
168*fc6f0665SEd Schouten.Dv mtx_timed
169*fc6f0665SEd Schoutento distinguish between a mutex that supports
170*fc6f0665SEd Schouten.Fn mtx_timedlock .
171*fc6f0665SEd SchoutenThis type can be
172*fc6f0665SEd Schouten.Em or'd
173*fc6f0665SEd Schoutenwith
174*fc6f0665SEd Schouten.Dv mtx_recursive
175*fc6f0665SEd Schoutento create a mutex that allows recursive acquisition.
176*fc6f0665SEd SchoutenThese properties are normally set using
177*fc6f0665SEd Schouten.Fn pthread_mutex_init Ns 's
178*fc6f0665SEd Schouten.Fa attr
179*fc6f0665SEd Schoutenparameter.
180*fc6f0665SEd Schouten.Sh RETURN VALUES
181*fc6f0665SEd SchoutenIf successful, the
182*fc6f0665SEd Schouten.Fn cnd_broadcast ,
183*fc6f0665SEd Schouten.Fn cnd_init ,
184*fc6f0665SEd Schouten.Fn cnd_signal ,
185*fc6f0665SEd Schouten.Fn cnd_timedwait ,
186*fc6f0665SEd Schouten.Fn cnd_wait ,
187*fc6f0665SEd Schouten.Fn mtx_init ,
188*fc6f0665SEd Schouten.Fn mtx_lock ,
189*fc6f0665SEd Schouten.Fn mtx_timedlock ,
190*fc6f0665SEd Schouten.Fn mtx_trylock ,
191*fc6f0665SEd Schouten.Fn mtx_unlock ,
192*fc6f0665SEd Schouten.Fn thrd_create ,
193*fc6f0665SEd Schouten.Fn thrd_detach ,
194*fc6f0665SEd Schouten.Fn thrd_equal ,
195*fc6f0665SEd Schouten.Fn thrd_join ,
196*fc6f0665SEd Schouten.Fn thrd_sleep ,
197*fc6f0665SEd Schouten.Fn tss_create
198*fc6f0665SEd Schoutenand
199*fc6f0665SEd Schouten.Fn tss_set
200*fc6f0665SEd Schoutenfunctions return
201*fc6f0665SEd Schouten.Dv thrd_success .
202*fc6f0665SEd SchoutenOtherwise an error code will be returned to indicate the error.
203*fc6f0665SEd Schouten.Pp
204*fc6f0665SEd SchoutenThe
205*fc6f0665SEd Schouten.Fn thrd_current
206*fc6f0665SEd Schoutenfunction returns the thread ID of the calling thread.
207*fc6f0665SEd Schouten.Pp
208*fc6f0665SEd SchoutenThe
209*fc6f0665SEd Schouten.Fn tss_get
210*fc6f0665SEd Schoutenfunction returns the thread-specific data value associated with the
211*fc6f0665SEd Schoutengiven
212*fc6f0665SEd Schouten.Fa key .
213*fc6f0665SEd SchoutenIf no thread-specific data value is associated with
214*fc6f0665SEd Schouten.Fa key ,
215*fc6f0665SEd Schoutenthen the value NULL is returned.
216*fc6f0665SEd Schouten.Sh ERRORS
217*fc6f0665SEd SchoutenThe
218*fc6f0665SEd Schouten.Fn cnd_init
219*fc6f0665SEd Schoutenand
220*fc6f0665SEd Schouten.Fn thrd_create
221*fc6f0665SEd Schoutenfunctions will fail if:
222*fc6f0665SEd Schouten.Bl -tag -width thrd_timedout
223*fc6f0665SEd Schouten.It Dv thrd_nomem
224*fc6f0665SEd SchoutenThe system has insufficient memory.
225*fc6f0665SEd Schouten.El
226*fc6f0665SEd Schouten.Pp
227*fc6f0665SEd SchoutenThe
228*fc6f0665SEd Schouten.Fn cnd_timedwait
229*fc6f0665SEd Schoutenand
230*fc6f0665SEd Schouten.Fn mtx_timedlock
231*fc6f0665SEd Schoutenfunctions will fail if:
232*fc6f0665SEd Schouten.Bl -tag -width thrd_timedout
233*fc6f0665SEd Schouten.It Dv thrd_timedout
234*fc6f0665SEd SchoutenThe system time has reached or exceeded the time specified in
235*fc6f0665SEd Schouten.Fa ts
236*fc6f0665SEd Schoutenbefore the operation could be completed.
237*fc6f0665SEd Schouten.El
238*fc6f0665SEd Schouten.Pp
239*fc6f0665SEd SchoutenThe
240*fc6f0665SEd Schouten.Fn mtx_trylock
241*fc6f0665SEd Schoutenfunction will fail if:
242*fc6f0665SEd Schouten.Bl -tag -width thrd_timedout
243*fc6f0665SEd Schouten.It Dv thrd_busy
244*fc6f0665SEd SchoutenThe mutex is already locked.
245*fc6f0665SEd Schouten.El
246*fc6f0665SEd Schouten.Pp
247*fc6f0665SEd SchoutenIn all other cases, these functions may fail by returning general error
248*fc6f0665SEd Schoutencode
249*fc6f0665SEd Schouten.Dv thrd_error .
250*fc6f0665SEd Schouten.Sh SEE ALSO
251*fc6f0665SEd Schouten.Xr nanosleep 2 ,
252*fc6f0665SEd Schouten.Xr pthread 3
253*fc6f0665SEd Schouten.Sh STANDARDS
254*fc6f0665SEd SchoutenThese functions are expected to conform to
255*fc6f0665SEd Schouten.St -isoC-11 .
256*fc6f0665SEd Schouten.Sh HISTORY
257*fc6f0665SEd SchoutenThese functions appeared in
258*fc6f0665SEd Schouten.Fx 10.0 .
259*fc6f0665SEd Schouten.Sh AUTHORS
260*fc6f0665SEd Schouten.An Ed Schouten Aq ed@FreeBSD.org
261