1fc6f0665SEd Schouten.\" Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org> 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.Dd December 26, 2011 26fc6f0665SEd Schouten.Dt THRD_CREATE 3 27fc6f0665SEd Schouten.Os 28fc6f0665SEd Schouten.Sh NAME 29fc6f0665SEd Schouten.Nm call_once , 30fc6f0665SEd Schouten.Nm cnd_broadcast , 31fc6f0665SEd Schouten.Nm cnd_destroy , 32fc6f0665SEd Schouten.Nm cnd_init , 33fc6f0665SEd Schouten.Nm cnd_signal , 34fc6f0665SEd Schouten.Nm cnd_timedwait , 35fc6f0665SEd Schouten.Nm cnd_wait , 36fc6f0665SEd Schouten.Nm mtx_destroy , 37fc6f0665SEd Schouten.Nm mtx_init , 38fc6f0665SEd Schouten.Nm mtx_lock , 39fc6f0665SEd Schouten.Nm mtx_timedlock , 40fc6f0665SEd Schouten.Nm mtx_trylock , 41fc6f0665SEd Schouten.Nm mtx_unlock , 42fc6f0665SEd Schouten.Nm thrd_create , 43fc6f0665SEd Schouten.Nm thrd_current , 44fc6f0665SEd Schouten.Nm thrd_detach , 45fc6f0665SEd Schouten.Nm thrd_equal , 46fc6f0665SEd Schouten.Nm thrd_exit , 47fc6f0665SEd Schouten.Nm thrd_join , 48fc6f0665SEd Schouten.Nm thrd_sleep , 49fc6f0665SEd Schouten.Nm thrd_yield , 50fc6f0665SEd Schouten.Nm tss_create , 51fc6f0665SEd Schouten.Nm tss_delete , 52fc6f0665SEd Schouten.Nm tss_get , 53fc6f0665SEd Schouten.Nm tss_set 54fc6f0665SEd Schouten.Nd C11 threads interface 55fc6f0665SEd Schouten.Sh LIBRARY 56fc6f0665SEd Schouten.Lb libstdthreads 57fc6f0665SEd Schouten.Sh SYNOPSIS 58fc6f0665SEd Schouten.In threads.h 59fc6f0665SEd Schouten.Ft void 60fc6f0665SEd Schouten.Fn call_once "once_flag *flag" "void (*func)(void)" 61fc6f0665SEd Schouten.Ft int 62fc6f0665SEd Schouten.Fn cnd_broadcast "cnd_t *cond" 63fc6f0665SEd Schouten.Ft void 64fc6f0665SEd Schouten.Fn cnd_destroy "cnd_t *cond" 65fc6f0665SEd Schouten.Ft int 66fc6f0665SEd Schouten.Fn cnd_init "cnd_t *cond" 67fc6f0665SEd Schouten.Ft int 68fc6f0665SEd Schouten.Fn cnd_signal "cnd_t *cond" 69fc6f0665SEd Schouten.Ft int 70fc6f0665SEd Schouten.Fn cnd_timedwait "cnd_t * restrict cond" "mtx_t * restrict mtx" "const struct timespec * restrict ts" 71fc6f0665SEd Schouten.Ft int 72fc6f0665SEd Schouten.Fn cnd_wait "cnd_t *cond" "mtx_t *mtx" 73fc6f0665SEd Schouten.Ft void 74fc6f0665SEd Schouten.Fn mtx_destroy "mtx_t *mtx" 75fc6f0665SEd Schouten.Ft int 76fc6f0665SEd Schouten.Fn mtx_init "mtx_t *mtx" "int type" 77fc6f0665SEd Schouten.Ft int 78fc6f0665SEd Schouten.Fn mtx_lock "mtx_t *mtx" 79fc6f0665SEd Schouten.Ft int 80fc6f0665SEd Schouten.Fn mtx_timedlock "mtx_t * restrict mtx" "const struct timespec * restrict ts" 81fc6f0665SEd Schouten.Ft int 82fc6f0665SEd Schouten.Fn mtx_trylock "mtx_t *mtx" 83fc6f0665SEd Schouten.Ft int 84fc6f0665SEd Schouten.Fn mtx_unlock "mtx_t *mtx" 85fc6f0665SEd Schouten.Ft int 86fc6f0665SEd Schouten.Fn thrd_create "thrd_t *thr" "int (*func)(void *)" "void *arg" 87fc6f0665SEd Schouten.Ft thrd_t 88fc6f0665SEd Schouten.Fn thrd_current "void" 89fc6f0665SEd Schouten.Ft int 90fc6f0665SEd Schouten.Fn thrd_detach "thrd_t thr" 91fc6f0665SEd Schouten.Ft int 92fc6f0665SEd Schouten.Fn thrd_equal "thrd_t thr0" "thrd_t thr1" 93fc6f0665SEd Schouten.Ft _Noreturn void 94fc6f0665SEd Schouten.Fn thrd_exit "int res" 95fc6f0665SEd Schouten.Ft int 96fc6f0665SEd Schouten.Fn thrd_join "thrd_t thr" "int *res" 97fc6f0665SEd Schouten.Ft int 98fc6f0665SEd Schouten.Fn thrd_sleep "const struct timespec *duration" "struct timespec *remaining" 99fc6f0665SEd Schouten.Ft void 100fc6f0665SEd Schouten.Fn thrd_yield "void" 101fc6f0665SEd Schouten.Ft int 102fc6f0665SEd Schouten.Fn tss_create "tss_t *key" "void (*dtor)(void *)" 103fc6f0665SEd Schouten.Ft void 104fc6f0665SEd Schouten.Fn tss_delete "tss_t key" 105fc6f0665SEd Schouten.Ft void * 106fc6f0665SEd Schouten.Fn tss_get "tss_t key" 107fc6f0665SEd Schouten.Ft int 108fc6f0665SEd Schouten.Fn tss_set "tss_t key" "void *val" 109fc6f0665SEd Schouten.Sh DESCRIPTION 110fc6f0665SEd SchoutenAs of 111cabdddaeSUlrich Spörlein.St -isoC-2011 , 112fc6f0665SEd Schoutenthe C standard includes an API for writing multithreaded applications. 113fc6f0665SEd SchoutenSince POSIX.1 already includes a threading API that is used by virtually 114fc6f0665SEd Schoutenany multithreaded application, the interface provided by the C standard 115fc6f0665SEd Schoutencan be considered superfluous. 116fc6f0665SEd Schouten.Pp 117fc6f0665SEd SchoutenIn this implementation, the threading interface is therefore implemented 118fc6f0665SEd Schoutenas a light-weight layer on top of existing interfaces. 119fc6f0665SEd SchoutenThe functions to which these routines are mapped, are listed in the 120fc6f0665SEd Schoutenfollowing table. 121fc6f0665SEd SchoutenPlease refer to the documentation of the POSIX equivalent functions for 122fc6f0665SEd Schoutenmore information. 123fc6f0665SEd Schouten.Bl -column ".Fn mtx_timedlock" ".Xr pthread_mutex_timedlock 3" -offset indent 124fc6f0665SEd Schouten.It Em Function Ta Em POSIX equivalent 125fc6f0665SEd Schouten.It Fn call_once Ta Xr pthread_once 3 126fc6f0665SEd Schouten.It Fn cnd_broadcast Ta Xr pthread_cond_broadcast 3 127fc6f0665SEd Schouten.It Fn cnd_destroy Ta Xr pthread_cond_destroy 3 128fc6f0665SEd Schouten.It Fn cnd_init Ta Xr pthread_cond_init 3 129fc6f0665SEd Schouten.It Fn cnd_signal Ta Xr pthread_cond_signal 3 130fc6f0665SEd Schouten.It Fn cnd_timedwait Ta Xr pthread_cond_timedwait 3 131fc6f0665SEd Schouten.It Fn cnd_wait Ta Xr pthread_cond_wait 3 132fc6f0665SEd Schouten.It Fn mtx_destroy Ta Xr pthread_mutex_destroy 3 133fc6f0665SEd Schouten.It Fn mtx_init Ta Xr pthread_mutex_init 3 134fc6f0665SEd Schouten.It Fn mtx_lock Ta Xr pthread_mutex_lock 3 135fc6f0665SEd Schouten.It Fn mtx_timedlock Ta Xr pthread_mutex_timedlock 3 136fc6f0665SEd Schouten.It Fn mtx_trylock Ta Xr pthread_mutex_trylock 3 137fc6f0665SEd Schouten.It Fn mtx_unlock Ta Xr pthread_mutex_unlock 3 138fc6f0665SEd Schouten.It Fn thrd_create Ta Xr pthread_create 3 139fc6f0665SEd Schouten.It Fn thrd_current Ta Xr pthread_self 3 140fc6f0665SEd Schouten.It Fn thrd_detach Ta Xr pthread_detach 3 141fc6f0665SEd Schouten.It Fn thrd_equal Ta Xr pthread_equal 3 142fc6f0665SEd Schouten.It Fn thrd_exit Ta Xr pthread_exit 3 143fc6f0665SEd Schouten.It Fn thrd_join Ta Xr pthread_join 3 144fc6f0665SEd Schouten.It Fn thrd_sleep Ta Xr nanosleep 2 145fc6f0665SEd Schouten.It Fn thrd_yield Ta Xr pthread_yield 3 146fc6f0665SEd Schouten.It Fn tss_create Ta Xr pthread_key_create 3 147fc6f0665SEd Schouten.It Fn tss_delete Ta Xr pthread_key_delete 3 148fc6f0665SEd Schouten.It Fn tss_get Ta Xr pthread_getspecific 3 149fc6f0665SEd Schouten.It Fn tss_set Ta Xr pthread_setspecific 3 150fc6f0665SEd Schouten.El 151fc6f0665SEd Schouten.Sh DIFFERENCES WITH POSIX EQUIVALENTS 152fc6f0665SEd SchoutenThe 153fc6f0665SEd Schouten.Fn thrd_exit 154fc6f0665SEd Schoutenfunction returns an integer value to the thread calling 155fc6f0665SEd Schouten.Fn thrd_join , 156fc6f0665SEd Schoutenwhereas the 157fc6f0665SEd Schouten.Fn pthread_exit 158fc6f0665SEd Schoutenfunction uses a pointer. 159fc6f0665SEd Schouten.Pp 160fc6f0665SEd SchoutenThe mutex created by 161fc6f0665SEd Schouten.Fn mtx_init 162fc6f0665SEd Schoutencan be of 163fc6f0665SEd Schouten.Fa type 164fc6f0665SEd Schouten.Dv mtx_plain 165fc6f0665SEd Schoutenor 166fc6f0665SEd Schouten.Dv mtx_timed 167fc6f0665SEd Schoutento distinguish between a mutex that supports 168fc6f0665SEd Schouten.Fn mtx_timedlock . 169fc6f0665SEd SchoutenThis type can be 170fc6f0665SEd Schouten.Em or'd 171fc6f0665SEd Schoutenwith 172fc6f0665SEd Schouten.Dv mtx_recursive 173fc6f0665SEd Schoutento create a mutex that allows recursive acquisition. 174fc6f0665SEd SchoutenThese properties are normally set using 175fc6f0665SEd Schouten.Fn pthread_mutex_init Ns 's 176fc6f0665SEd Schouten.Fa attr 177fc6f0665SEd Schoutenparameter. 178fc6f0665SEd Schouten.Sh RETURN VALUES 179fc6f0665SEd SchoutenIf successful, the 180fc6f0665SEd Schouten.Fn cnd_broadcast , 181fc6f0665SEd Schouten.Fn cnd_init , 182fc6f0665SEd Schouten.Fn cnd_signal , 183fc6f0665SEd Schouten.Fn cnd_timedwait , 184fc6f0665SEd Schouten.Fn cnd_wait , 185fc6f0665SEd Schouten.Fn mtx_init , 186fc6f0665SEd Schouten.Fn mtx_lock , 187fc6f0665SEd Schouten.Fn mtx_timedlock , 188fc6f0665SEd Schouten.Fn mtx_trylock , 189fc6f0665SEd Schouten.Fn mtx_unlock , 190fc6f0665SEd Schouten.Fn thrd_create , 191fc6f0665SEd Schouten.Fn thrd_detach , 192fc6f0665SEd Schouten.Fn thrd_equal , 193fc6f0665SEd Schouten.Fn thrd_join , 194fc6f0665SEd Schouten.Fn thrd_sleep , 195fc6f0665SEd Schouten.Fn tss_create 196fc6f0665SEd Schoutenand 197fc6f0665SEd Schouten.Fn tss_set 198fc6f0665SEd Schoutenfunctions return 199fc6f0665SEd Schouten.Dv thrd_success . 200fc6f0665SEd SchoutenOtherwise an error code will be returned to indicate the error. 201fc6f0665SEd Schouten.Pp 202fc6f0665SEd SchoutenThe 203fc6f0665SEd Schouten.Fn thrd_current 204fc6f0665SEd Schoutenfunction returns the thread ID of the calling thread. 205fc6f0665SEd Schouten.Pp 206fc6f0665SEd SchoutenThe 207fc6f0665SEd Schouten.Fn tss_get 208fc6f0665SEd Schoutenfunction returns the thread-specific data value associated with the 209fc6f0665SEd Schoutengiven 210fc6f0665SEd Schouten.Fa key . 211fc6f0665SEd SchoutenIf no thread-specific data value is associated with 212fc6f0665SEd Schouten.Fa key , 213fc6f0665SEd Schoutenthen the value NULL is returned. 214fc6f0665SEd Schouten.Sh ERRORS 215fc6f0665SEd SchoutenThe 216fc6f0665SEd Schouten.Fn cnd_init 217fc6f0665SEd Schoutenand 218fc6f0665SEd Schouten.Fn thrd_create 219fc6f0665SEd Schoutenfunctions will fail if: 220fc6f0665SEd Schouten.Bl -tag -width thrd_timedout 221fc6f0665SEd Schouten.It Dv thrd_nomem 222fc6f0665SEd SchoutenThe system has insufficient memory. 223fc6f0665SEd Schouten.El 224fc6f0665SEd Schouten.Pp 225fc6f0665SEd SchoutenThe 226fc6f0665SEd Schouten.Fn cnd_timedwait 227fc6f0665SEd Schoutenand 228fc6f0665SEd Schouten.Fn mtx_timedlock 229fc6f0665SEd Schoutenfunctions will fail if: 230fc6f0665SEd Schouten.Bl -tag -width thrd_timedout 231fc6f0665SEd Schouten.It Dv thrd_timedout 232fc6f0665SEd SchoutenThe system time has reached or exceeded the time specified in 233fc6f0665SEd Schouten.Fa ts 234fc6f0665SEd Schoutenbefore the operation could be completed. 235fc6f0665SEd Schouten.El 236fc6f0665SEd Schouten.Pp 237fc6f0665SEd SchoutenThe 238fc6f0665SEd Schouten.Fn mtx_trylock 239fc6f0665SEd Schoutenfunction will fail if: 240fc6f0665SEd Schouten.Bl -tag -width thrd_timedout 241fc6f0665SEd Schouten.It Dv thrd_busy 242fc6f0665SEd SchoutenThe mutex is already locked. 243fc6f0665SEd Schouten.El 244fc6f0665SEd Schouten.Pp 245fc6f0665SEd SchoutenIn all other cases, these functions may fail by returning general error 246fc6f0665SEd Schoutencode 247fc6f0665SEd Schouten.Dv thrd_error . 248fc6f0665SEd Schouten.Sh SEE ALSO 249fc6f0665SEd Schouten.Xr nanosleep 2 , 250fc6f0665SEd Schouten.Xr pthread 3 251fc6f0665SEd Schouten.Sh STANDARDS 252fc6f0665SEd SchoutenThese functions are expected to conform to 253cabdddaeSUlrich Spörlein.St -isoC-2011 . 254fc6f0665SEd Schouten.Sh HISTORY 255fc6f0665SEd SchoutenThese functions appeared in 256fc6f0665SEd Schouten.Fx 10.0 . 257fc6f0665SEd Schouten.Sh AUTHORS 258*2b7af31cSBaptiste Daroussin.An Ed Schouten Aq Mt ed@FreeBSD.org 259