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