commit a4bba7dbc11a6083b919f9ade88159468e339c66 Author: Kamil Rytarowski Date: Thu Apr 18 14:17:51 2019 +0200 Add C11 threads in -lpthread diff --git a/distrib/sets/lists/comp/mi b/distrib/sets/lists/comp/mi index 4ee38f6a0518..09d31bb3b0a2 100644 --- a/distrib/sets/lists/comp/mi +++ b/distrib/sets/lists/comp/mi @@ -3172,6 +3172,7 @@ ./usr/include/termcap.h comp-c-include ./usr/include/termios.h comp-c-include ./usr/include/threadlib.h comp-obsolete obsolete +./usr/include/threads.h comp-c-include ./usr/include/time.h comp-c-include ./usr/include/trousers/trousers.h comp-c-include tpm ./usr/include/trousers/tss.h comp-c-include tpm @@ -4850,6 +4851,7 @@ ./usr/share/man/cat3/MDC2_Init.0 comp-c-catman .cat,openssl=11 ./usr/share/man/cat3/MIN.0 comp-c-catman .cat ./usr/share/man/cat3/OBJ_nid2obj.0 comp-c-catman .cat +./usr/share/man/cat3/ONCE_FLAG_INIT.0 comp-c-catman .cat ./usr/share/man/cat3/OCSP_REQUEST_new.0 comp-c-catman .cat,openssl=11 ./usr/share/man/cat3/OCSP_cert_to_id.0 comp-c-catman .cat,openssl=11 ./usr/share/man/cat3/OCSP_request_add1_nonce.0 comp-c-catman .cat,openssl=11 @@ -5276,6 +5278,7 @@ ./usr/share/man/cat3/TAILQ_REPLACE.0 comp-c-catman .cat ./usr/share/man/cat3/TIMESPEC_TO_TIMEVAL.0 comp-c-catman .cat ./usr/share/man/cat3/TIMEVAL_TO_TIMESPEC.0 comp-c-catman .cat +./usr/share/man/cat3/TSS_DTOR_ITERATIONS.0 comp-c-catman .cat ./usr/share/man/cat3/Tspi_ChangeAuth.0 comp-c-catman tpm,.cat ./usr/share/man/cat3/Tspi_ChangeAuthAsym.0 comp-c-catman tpm,.cat ./usr/share/man/cat3/Tspi_Context_Close.0 comp-c-catman tpm,.cat @@ -6049,6 +6052,7 @@ ./usr/share/man/cat3/cabsf.0 comp-c-catman complex,.cat ./usr/share/man/cat3/cabsl.0 comp-c-catman complex,.cat ./usr/share/man/cat3/cacos.0 comp-c-catman complex,.cat +./usr/share/man/cat3/call_once.0 comp-c-catman .cat ./usr/share/man/cat3/cacosf.0 comp-c-catman complex,.cat ./usr/share/man/cat3/cacosh.0 comp-c-catman complex,.cat ./usr/share/man/cat3/cacoshf.0 comp-c-catman complex,.cat @@ -6206,6 +6210,13 @@ ./usr/share/man/cat3/clrtobot.0 comp-c-catman .cat ./usr/share/man/cat3/clrtoeol.0 comp-c-catman .cat ./usr/share/man/cat3/cmsg.0 comp-c-catman .cat +./usr/share/man/cat3/cnd.0 comp-c-catman .cat +./usr/share/man/cat3/cnd_broadcast.0 comp-c-catman .cat +./usr/share/man/cat3/cnd_destroy.0 comp-c-catman .cat +./usr/share/man/cat3/cnd_init.0 comp-c-catman .cat +./usr/share/man/cat3/cnd_signal.0 comp-c-catman .cat +./usr/share/man/cat3/cnd_timedwait.0 comp-c-catman .cat +./usr/share/man/cat3/cnd_wait.0 comp-c-catman .cat ./usr/share/man/cat3/color_content.0 comp-c-catman .cat ./usr/share/man/cat3/color_set.0 comp-c-catman .cat ./usr/share/man/cat3/com_err.0 comp-krb5-catman kerberos,.cat @@ -8490,6 +8501,13 @@ ./usr/share/man/cat3/mvwinstr.0 comp-c-catman .cat ./usr/share/man/cat3/mvwprintw.0 comp-c-catman .cat ./usr/share/man/cat3/mvwvline.0 comp-c-catman .cat +./usr/share/man/cat3/mtx.0 comp-c-catman .cat +./usr/share/man/cat3/mtx_destroy.0 comp-c-catman .cat +./usr/share/man/cat3/mtx_init.0 comp-c-catman .cat +./usr/share/man/cat3/mtx_lock.0 comp-c-catman .cat +./usr/share/man/cat3/mtx_timedlock.0 comp-c-catman .cat +./usr/share/man/cat3/mtx_trylock.0 comp-c-catman .cat +./usr/share/man/cat3/mtx_unlock.0 comp-c-catman .cat ./usr/share/man/cat3/nan.0 comp-c-catman .cat ./usr/share/man/cat3/nanf.0 comp-c-catman .cat ./usr/share/man/cat3/nanl.0 comp-c-catman .cat @@ -8543,6 +8561,7 @@ ./usr/share/man/cat3/offsetof.0 comp-c-catman .cat ./usr/share/man/cat3/offtime.0 comp-c-catman .cat ./usr/share/man/cat3/offtime_r.0 comp-c-catman .cat +./usr/share/man/cat3/once_flag.0 comp-c-catman .cat ./usr/share/man/cat3/open_memstream.0 comp-c-catman .cat ./usr/share/man/cat3/open_wmemstream.0 comp-c-catman .cat ./usr/share/man/cat3/opendir.0 comp-c-catman .cat @@ -10048,7 +10067,16 @@ ./usr/share/man/cat3/tgetstr.0 comp-c-catman .cat ./usr/share/man/cat3/tgmath.0 comp-c-catman .cat ./usr/share/man/cat3/tgoto.0 comp-c-catman .cat -./usr/share/man/cat3/threads.0 comp-obsolete obsolete +./usr/share/man/cat3/threads.0 comp-c-catman .cat +./usr/share/man/cat3/thrd.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_create.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_current.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_detach.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_equal.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_exit.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_join.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_sleep.0 comp-c-catman .cat +./usr/share/man/cat3/thrd_yield.0 comp-c-catman .cat ./usr/share/man/cat3/ti_getflag.0 comp-c-catman .cat ./usr/share/man/cat3/ti_getnum.0 comp-c-catman .cat ./usr/share/man/cat3/ti_getstr.0 comp-c-catman .cat @@ -10113,6 +10141,11 @@ ./usr/share/man/cat3/truncf.0 comp-c-catman .cat ./usr/share/man/cat3/truncl.0 comp-c-catman .cat ./usr/share/man/cat3/tsearch.0 comp-c-catman .cat +./usr/share/man/cat3/tss.0 comp-c-catman .cat +./usr/share/man/cat3/tss_create.0 comp-c-catman .cat +./usr/share/man/cat3/tss_delete.0 comp-c-catman .cat +./usr/share/man/cat3/tss_get.0 comp-c-catman .cat +./usr/share/man/cat3/tss_set.0 comp-c-catman .cat ./usr/share/man/cat3/ttyaction.0 comp-c-catman .cat ./usr/share/man/cat3/ttylock.0 comp-c-catman .cat ./usr/share/man/cat3/ttymsg.0 comp-c-catman .cat @@ -12864,6 +12897,7 @@ ./usr/share/man/html3/MDC2_Init.html comp-c-htmlman html,openssl=11 ./usr/share/man/html3/MIN.html comp-c-htmlman html ./usr/share/man/html3/OBJ_nid2obj.html comp-c-htmlman html +./usr/share/man/html3/ONCE_FLAG_INIT.html comp-c-htmlman html ./usr/share/man/html3/OCSP_REQUEST_new.html comp-c-htmlman html,openssl=11 ./usr/share/man/html3/OCSP_cert_to_id.html comp-c-htmlman html,openssl=11 ./usr/share/man/html3/OCSP_request_add1_nonce.html comp-c-htmlman html,openssl=11 @@ -13296,6 +13330,7 @@ ./usr/share/man/html3/TAILQ_REPLACE.html comp-c-htmlman html ./usr/share/man/html3/TIMESPEC_TO_TIMEVAL.html comp-c-htmlman html ./usr/share/man/html3/TIMEVAL_TO_TIMESPEC.html comp-c-htmlman html +./usr/share/man/html3/TSS_DTOR_ITERATIONS.html comp-c-htmlman html ./usr/share/man/html3/Tspi_ChangeAuth.html comp-c-htmlman tpm,html ./usr/share/man/html3/Tspi_ChangeAuthAsym.html comp-c-htmlman tpm,html ./usr/share/man/html3/Tspi_Context_Close.html comp-c-htmlman tpm,html @@ -14065,6 +14100,7 @@ ./usr/share/man/html3/cacoshf.html comp-c-htmlman complex,html ./usr/share/man/html3/cacoshl.html comp-c-htmlman complex,html ./usr/share/man/html3/cacosl.html comp-c-htmlman complex,html +./usr/share/man/html3/call_once.html comp-c-htmlman html ./usr/share/man/html3/calloc.html comp-c-htmlman html ./usr/share/man/html3/callrpc.html comp-c-htmlman html ./usr/share/man/html3/can_change_color.html comp-c-htmlman html @@ -14174,6 +14210,13 @@ ./usr/share/man/html3/clrtobot.html comp-c-htmlman html ./usr/share/man/html3/clrtoeol.html comp-c-htmlman html ./usr/share/man/html3/cmsg.html comp-c-htmlman html +./usr/share/man/html3/cnd.html comp-c-htmlman html +./usr/share/man/html3/cnd_broadcast.html comp-c-htmlman html +./usr/share/man/html3/cnd_destroy.html comp-c-htmlman html +./usr/share/man/html3/cnd_init.html comp-c-htmlman html +./usr/share/man/html3/cnd_signal.html comp-c-htmlman html +./usr/share/man/html3/cnd_timedwait.html comp-c-htmlman html +./usr/share/man/html3/cnd_wait.html comp-c-htmlman html ./usr/share/man/html3/color_content.html comp-c-htmlman html ./usr/share/man/html3/color_set.html comp-c-htmlman html ./usr/share/man/html3/com_err.html comp-krb5-htmlman kerberos,html @@ -16432,6 +16475,13 @@ ./usr/share/man/html3/mvwinstr.html comp-c-htmlman html ./usr/share/man/html3/mvwprintw.html comp-c-htmlman html ./usr/share/man/html3/mvwvline.html comp-c-htmlman html +./usr/share/man/html3/mtx.html comp-c-htmlman html +./usr/share/man/html3/mtx_destroy.html comp-c-htmlman html +./usr/share/man/html3/mtx_init.html comp-c-htmlman html +./usr/share/man/html3/mtx_lock.html comp-c-htmlman html +./usr/share/man/html3/mtx_timedlock.html comp-c-htmlman html +./usr/share/man/html3/mtx_trylock.html comp-c-htmlman html +./usr/share/man/html3/mtx_unlock.html comp-c-htmlman html ./usr/share/man/html3/nan.html comp-c-htmlman html ./usr/share/man/html3/nanf.html comp-c-htmlman html ./usr/share/man/html3/nanl.html comp-c-htmlman html @@ -16482,6 +16532,7 @@ ./usr/share/man/html3/offsetof.html comp-c-htmlman html ./usr/share/man/html3/offtime.html comp-c-htmlman html ./usr/share/man/html3/offtime_r.html comp-c-htmlman html +./usr/share/man/html3/once_flag.html comp-c-htmlman html ./usr/share/man/html3/open_memstream.html comp-c-htmlman html ./usr/share/man/html3/open_wmemstream.html comp-c-htmlman html ./usr/share/man/html3/opendir.html comp-c-htmlman html @@ -17972,6 +18023,16 @@ ./usr/share/man/html3/tgetstr.html comp-c-htmlman html ./usr/share/man/html3/tgmath.html comp-c-htmlman html ./usr/share/man/html3/tgoto.html comp-c-htmlman html +./usr/share/man/html3/threads.html comp-c-htmlman html +./usr/share/man/html3/thrd.html comp-c-htmlman html +./usr/share/man/html3/thrd_create.html comp-c-htmlman html +./usr/share/man/html3/thrd_current.html comp-c-htmlman html +./usr/share/man/html3/thrd_detach.html comp-c-htmlman html +./usr/share/man/html3/thrd_equal.html comp-c-htmlman html +./usr/share/man/html3/thrd_exit.html comp-c-htmlman html +./usr/share/man/html3/thrd_join.html comp-c-htmlman html +./usr/share/man/html3/thrd_sleep.html comp-c-htmlman html +./usr/share/man/html3/thrd_yield.html comp-c-htmlman html ./usr/share/man/html3/ti_getflag.html comp-c-htmlman html ./usr/share/man/html3/ti_getnum.html comp-c-htmlman html ./usr/share/man/html3/ti_getstr.html comp-c-htmlman html @@ -18036,6 +18097,11 @@ ./usr/share/man/html3/truncf.html comp-c-htmlman html ./usr/share/man/html3/truncl.html comp-c-htmlman html ./usr/share/man/html3/tsearch.html comp-c-htmlman html +./usr/share/man/html3/tss.html comp-c-htmlman html +./usr/share/man/html3/tss_create.html comp-c-htmlman html +./usr/share/man/html3/tss_delete.html comp-c-htmlman html +./usr/share/man/html3/tss_get.html comp-c-htmlman html +./usr/share/man/html3/tss_set.html comp-c-htmlman html ./usr/share/man/html3/ttyaction.html comp-c-htmlman html ./usr/share/man/html3/ttylock.html comp-c-htmlman html ./usr/share/man/html3/ttymsg.html comp-c-htmlman html @@ -20720,6 +20786,7 @@ ./usr/share/man/man3/MDC2_Init.3 comp-c-man .man,openssl=11 ./usr/share/man/man3/MIN.3 comp-c-man .man ./usr/share/man/man3/OBJ_nid2obj.3 comp-c-man .man +./usr/share/man/man3/ONCE_FLAG_INIT.3 comp-c-man .man ./usr/share/man/man3/OCSP_REQUEST_new.3 comp-c-man .man,openssl=11 ./usr/share/man/man3/OCSP_cert_to_id.3 comp-c-man .man,openssl=11 ./usr/share/man/man3/OCSP_request_add1_nonce.3 comp-c-man .man,openssl=11 @@ -21153,6 +21220,7 @@ ./usr/share/man/man3/TAILQ_REPLACE.3 comp-c-man .man ./usr/share/man/man3/TIMESPEC_TO_TIMEVAL.3 comp-c-man .man ./usr/share/man/man3/TIMEVAL_TO_TIMESPEC.3 comp-c-man .man +./usr/share/man/man3/TSS_DTOR_ITERATIONS.3 comp-c-man .man ./usr/share/man/man3/Tspi_ChangeAuth.3 comp-c-man tpm,.man ./usr/share/man/man3/Tspi_ChangeAuthAsym.3 comp-c-man tpm,.man ./usr/share/man/man3/Tspi_Context_Close.3 comp-c-man tpm,.man @@ -21927,6 +21995,7 @@ ./usr/share/man/man3/cabsf.3 comp-c-man complex,.man ./usr/share/man/man3/cabsl.3 comp-c-man complex,.man ./usr/share/man/man3/cacos.3 comp-c-man complex,.man +./usr/share/man/man3/call_once.3 comp-c-man .man ./usr/share/man/man3/cacosf.3 comp-c-man complex,.man ./usr/share/man/man3/cacosh.3 comp-c-man complex,.man ./usr/share/man/man3/cacoshf.3 comp-c-man complex,.man @@ -22084,6 +22153,13 @@ ./usr/share/man/man3/clrtobot.3 comp-c-man .man ./usr/share/man/man3/clrtoeol.3 comp-c-man .man ./usr/share/man/man3/cmsg.3 comp-c-man .man +./usr/share/man/man3/cnd.3 comp-c-man .man +./usr/share/man/man3/cnd_broadcast.3 comp-c-man .man +./usr/share/man/man3/cnd_destroy.3 comp-c-man .man +./usr/share/man/man3/cnd_init.3 comp-c-man .man +./usr/share/man/man3/cnd_signal.3 comp-c-man .man +./usr/share/man/man3/cnd_timedwait.3 comp-c-man .man +./usr/share/man/man3/cnd_wait.3 comp-c-man .man ./usr/share/man/man3/color_content.3 comp-c-man .man ./usr/share/man/man3/color_set.3 comp-c-man .man ./usr/share/man/man3/com_err.3 comp-krb5-man kerberos,.man @@ -24380,6 +24456,13 @@ ./usr/share/man/man3/mvwinstr.3 comp-c-man .man ./usr/share/man/man3/mvwprintw.3 comp-c-man .man ./usr/share/man/man3/mvwvline.3 comp-c-man .man +./usr/share/man/man3/mtx.3 comp-c-man .man +./usr/share/man/man3/mtx_destroy.3 comp-c-man .man +./usr/share/man/man3/mtx_init.3 comp-c-man .man +./usr/share/man/man3/mtx_lock.3 comp-c-man .man +./usr/share/man/man3/mtx_timedlock.3 comp-c-man .man +./usr/share/man/man3/mtx_trylock.3 comp-c-man .man +./usr/share/man/man3/mtx_unlock.3 comp-c-man .man ./usr/share/man/man3/nan.3 comp-c-man .man ./usr/share/man/man3/nanf.3 comp-c-man .man ./usr/share/man/man3/nanl.3 comp-c-man .man @@ -24432,6 +24515,7 @@ ./usr/share/man/man3/o2i_SCT_LIST.3 comp-c-man .man,openssl=11 ./usr/share/man/man3/offsetof.3 comp-c-man .man ./usr/share/man/man3/offtime.3 comp-c-man .man +./usr/share/man/man3/once_flag.3 comp-c-man .man ./usr/share/man/man3/offtime_r.3 comp-c-man .man ./usr/share/man/man3/open_memstream.3 comp-c-man .man ./usr/share/man/man3/open_wmemstream.3 comp-c-man .man @@ -25957,7 +26041,16 @@ ./usr/share/man/man3/tgetstr.3 comp-c-man .man ./usr/share/man/man3/tgmath.3 comp-c-man .man ./usr/share/man/man3/tgoto.3 comp-c-man .man -./usr/share/man/man3/threads.3 comp-obsolete obsolete +./usr/share/man/man3/threads.3 comp-c-man .man +./usr/share/man/man3/thrd.3 comp-c-man .man +./usr/share/man/man3/thrd_create.3 comp-c-man .man +./usr/share/man/man3/thrd_current.3 comp-c-man .man +./usr/share/man/man3/thrd_detach.3 comp-c-man .man +./usr/share/man/man3/thrd_equal.3 comp-c-man .man +./usr/share/man/man3/thrd_exit.3 comp-c-man .man +./usr/share/man/man3/thrd_join.3 comp-c-man .man +./usr/share/man/man3/thrd_sleep.3 comp-c-man .man +./usr/share/man/man3/thrd_yield.3 comp-c-man .man ./usr/share/man/man3/ti_getflag.3 comp-c-man .man ./usr/share/man/man3/ti_getnum.3 comp-c-man .man ./usr/share/man/man3/ti_getstr.3 comp-c-man .man @@ -26022,6 +26115,11 @@ ./usr/share/man/man3/truncf.3 comp-c-man .man ./usr/share/man/man3/truncl.3 comp-c-man .man ./usr/share/man/man3/tsearch.3 comp-c-man .man +./usr/share/man/man3/tss.3 comp-c-man .man +./usr/share/man/man3/tss_create.3 comp-c-man .man +./usr/share/man/man3/tss_delete.3 comp-c-man .man +./usr/share/man/man3/tss_get.3 comp-c-man .man +./usr/share/man/man3/tss_set.3 comp-c-man .man ./usr/share/man/man3/ttyaction.3 comp-c-man .man ./usr/share/man/man3/ttylock.3 comp-c-man .man ./usr/share/man/man3/ttymsg.3 comp-c-man .man diff --git a/lib/libpthread/Makefile b/lib/libpthread/Makefile index 572cb1444a48..0f293a25ca49 100644 --- a/lib/libpthread/Makefile +++ b/lib/libpthread/Makefile @@ -230,6 +230,45 @@ MLINKS+= pthread_suspend_np.3 pthread_resume_np.3 MLINKS+= pthread_testcancel.3 pthread_setcancelstate.3 MLINKS+= pthread_testcancel.3 pthread_setcanceltype.3 +# ISO C threads (ISO/IEC 9899:2011) + +SRCS+= call_once.c cnd.c mtx.c thrd.c tss.c +MAN+= threads.3 call_once.3 cnd.3 mtx.3 thrd.3 tss.3 + +MLINKS+= call_once.3 ONCE_FLAG_INIT.3 +MLINKS+= call_once.3 once_flag.3 + +MLINKS+= cnd.3 cnd_broadcast.3 +MLINKS+= cnd.3 cnd_destroy.3 +MLINKS+= cnd.3 cnd_init.3 +MLINKS+= cnd.3 cnd_signal.3 +MLINKS+= cnd.3 cnd_timedwait.3 +MLINKS+= cnd.3 cnd_wait.3 + +MLINKS+= mtx.3 mtx_destroy.3 +MLINKS+= mtx.3 mtx_init.3 +MLINKS+= mtx.3 mtx_lock.3 +MLINKS+= mtx.3 mtx_timedlock.3 +MLINKS+= mtx.3 mtx_trylock.3 +MLINKS+= mtx.3 mtx_unlock.3 + +MLINKS+= thrd.3 thrd_create.3 +MLINKS+= thrd.3 thrd_current.3 +MLINKS+= thrd.3 thrd_detach.3 +MLINKS+= thrd.3 thrd_equal.3 +MLINKS+= thrd.3 thrd_exit.3 +MLINKS+= thrd.3 thrd_join.3 +MLINKS+= thrd.3 thrd_sleep.3 +MLINKS+= thrd.3 thrd_yield.3 + +MLINKS+= tss.3 TSS_DTOR_ITERATIONS.3 +MLINKS+= tss.3 tss_create.3 +MLINKS+= tss.3 tss_delete.3 +MLINKS+= tss.3 tss_get.3 +MLINKS+= tss.3 tss_set.3 + +INCS+= threads.h + .include .else diff --git a/lib/libpthread/call_once.3 b/lib/libpthread/call_once.3 new file mode 100644 index 000000000000..5f43b3299fb9 --- /dev/null +++ b/lib/libpthread/call_once.3 @@ -0,0 +1,113 @@ +.\" $NetBSD$ +.\" +.\" 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 CALL_ONCE 3 +.Os +.Sh NAME +.Nm call_once +.Nd calls function exactly once +.Sh LIBRARY +.Lb libpthread +.Sh SYNOPSIS +.In threads.h +.Ft void +.Fn call_once "once_flag *flag" "void (*func)(void))" +.Vt once_flag flag No = Dv ONCE_FLAG_INIT ; +.Sh DESCRIPTION +The +.Nm +function uses the +.Fa flag +parameter to ensure that +.Fa func +is called exactly once, +even if called from several threads. +.Pp +The +.Dv ONCE_FLAG_INIT +definition expands to a value that can be used to initialize an object of type +.Dv once_flag . +.Pp +This portable interface is implemented on top of the +.Xr pthread_once 3 +functionality. +.Sh RETURN VALUES +None. +.Sh EXAMPLES +The following calls +.Nm +from two threads using the portable +.Xr thrd 3 +interface. +.Bd -literal -offset indent +#include +#include + +static once_flag oflag = ONCE_FLAG_INIT; + +void +called_once(void) +{ + printf("called once\n"); +} + +inf +tfun(void *ptr) +{ + call_once(&oflag, called_once); +} + +int +main(int argc, char **argv) +{ + thrd_t th1, th2; + + thrd_create(&th1, tfun, NULL); + thrd_create(&th2, tfun, NULL); + + thrd_join(th1, NULL); + thrd_join(th2, NULL); + + return 0; +} +.Ed +.Sh SEE ALSO +.Xr pthread_once 3 , +.Xr threads 3 +.Sh STANDARDS +The +.Nm +function conforms to +.St -isoC-2011 . +.Sh HISTORY +This interface first appeared in +.Nx 9 . +.Sh AUTHORS +.An Kamil Rytarowski Aq Mt kamil@NetBSD.org diff --git a/lib/libpthread/call_once.c b/lib/libpthread/call_once.c new file mode 100644 index 000000000000..9ac5b51f7a21 --- /dev/null +++ b/lib/libpthread/call_once.c @@ -0,0 +1,52 @@ +/* $NetBSD$ */ + +/*- + * 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. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include +#include +#include + +void +call_once(once_flag *flag, void (*func)(void)) +{ + + _DIAGASSERT(flag != NULL); + _DIAGASSERT(func != NULL); + + /* The call_once(3) function returns no value, this forces this code to + * break as there is nothing better available. */ + if (pthread_once(flag, func) != 0) + errx(EXIT_FAILURE, "pthread_once failed"); +} diff --git a/lib/libpthread/cnd.3 b/lib/libpthread/cnd.3 new file mode 100644 index 000000000000..a6abda3ea4e6 --- /dev/null +++ b/lib/libpthread/cnd.3 @@ -0,0 +1,192 @@ +.\" $NetBSD$ +.\" +.\" 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 CND 3 +.Os +.Sh NAME +.Nm cnd +.Nd condition variable functions +.Sh LIBRARY +.Lb libpthread +.Sh SYNOPSIS +.In threads.h +.Ft int +.Fn cnd_broadcast "cnd_t *cond" +.Ft void +.Fn cnd_destroy "cnd_t *cond" +.Ft int +.Fn cnd_init "cnd_t *cond" +.Ft int +.Fn cnd_signal "cnd_t *cond" +.Ft int +.Fo cnd_timedwait +.Fa "cnd_t * __restrict cond" +.Fa "mtx_t * __restrict mtx" +.Fa "const struct timespec * __restrict ts" +.Fc +.Ft int +.Fn cnd_wait "cnd_t *cond" "mtx_t *mtx" +.Sh DESCRIPTION +The +.Fn cnd_broadcast +function unblocks all threads that are blocked on a condition variable +.Fa cond +at the time of the call. +If no thread is blocked on the +.Fa cond +condition variable at the time of the call, +the function does nothing and returns success. +.Pp +The +.Fn cnd_destroy +function destroys the +.Fa cond +condition variable. +.Pp +The +.Fn cnd_init +function initializes new +.Fa cond +variable. +.Pp +The +.Fn cnd_signal +function unblock one thread that currently waits on the +.Fa cond +variable. +If there are no threads blocked +.Fn cnd_signal +does nothing and returns success. +.Pp +The +.Fn cnd_timedwait +function atomicalyl unlocks the mutex +.Fa mtx +and blocks on the condition variable +.Fa cond +until a thread is signalled by a call to +.Fn cnd_signal +or +.Fn cnd_broadcast +or timeout +.Fa ts +has been reached. +The +.Fa ts +parameter is specified as +.Dv TIME_UTC +based calendar time. +If the mutex is not locked by the calling thread then behavior is undefined. +.Pp +The +.Fn cnd_wait +function atomically unlocks the mutex +.Fa mtx +and tries to blocks on on the conditional variable +.Fa cond +until a thread is signalled by a call to +.Fn cnd_signal +or +.Fn cnd_broadcast . +The +.Fa mtx +mutex is locked again before the function returns. +If the mutex is not locked by the calling thread then behavior is undefined. +.Sh RETURN VALUES +The +.Fn cnd_broadcast , +function returns +.Dv thrd_success +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn cnd_destroy +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_busy +if the value +.Dv cond +is locked by another thread or +.Dv thrd_error +if the value +.Dv cond +is invalid. +.Pp +The +.Fn cnd_init +function returns +.Dv thrd_success +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn cnd_signal +function returns +.Dv thrd_success +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn cnd_timedwait +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 cnd_wait +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_error +on failure. +.Sh SEE ALSO +.Xr pthread_cond 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 diff --git a/lib/libpthread/cnd.c b/lib/libpthread/cnd.c new file mode 100644 index 000000000000..c0c9f283716d --- /dev/null +++ b/lib/libpthread/cnd.c @@ -0,0 +1,118 @@ +/* $NetBSD$ */ + +/*- + * 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. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include +#include + +int +cnd_broadcast(cnd_t *cond) +{ + + _DIAGASSERT(cond != NULL); + + if (pthread_cond_broadcast(cond) == 0) + return thrd_success; + + return thrd_error; +} + +void +cnd_destroy(cnd_t *cond) +{ + + _DIAGASSERT(cond != NULL); + + /* + * The cnd_destroy(3) function that conforms to C11 returns no value. + */ + (void)pthread_cond_destroy(cond); +} + +int +cnd_init(cnd_t *cond) +{ + + _DIAGASSERT(cond != NULL); + + if (pthread_cond_init(cond, NULL) == 0) + return thrd_success; + + return thrd_error; +} + +int +cnd_signal(cnd_t *cond) +{ + + _DIAGASSERT(cond != NULL); + + if (pthread_cond_signal(cond) == 0) + return thrd_success; + + return thrd_error; +} + +int +cnd_timedwait(cnd_t * __restrict cond, mtx_t * __restrict mtx, + const struct timespec * __restrict ts) +{ + + _DIAGASSERT(cond != NULL); + _DIAGASSERT(mtx != NULL); + _DIAGASSERT(ts != NULL); + + switch (pthread_cond_timedwait(cond, mtx, ts)) { + case 0: + return thrd_success; + case ETIMEDOUT: + return thrd_timedout; + default: + return thrd_error; + } +} + +int +cnd_wait(cnd_t *cond, mtx_t *mtx) +{ + + _DIAGASSERT(cond != NULL); + _DIAGASSERT(mtx != NULL); + + if (pthread_cond_wait(cond, mtx) == 0) + return thrd_success; + + return thrd_error; +} diff --git a/lib/libpthread/mtx.3 b/lib/libpthread/mtx.3 new file mode 100644 index 000000000000..35ec6e6abe9c --- /dev/null +++ b/lib/libpthread/mtx.3 @@ -0,0 +1,227 @@ +.\" $NetBSD$ +.\" +.\" 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 Xr mtx_plain Ta basic mutex +.It Xr mtx_timed Ta mutex with timeout support +.It Xr mtx_plain | mtx_recursive Ta basic recursive mutex +.It Xr mtx_timed | mtx_recursive Ta recursive mutex with timeout support +.El +.Pp +The underlaying +.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 the never lock multiple times the same +.Fa mtx +object without the +.Dv mtx_recursive +property. +If the +.Fa mtx +object is already locked by another thread, +caller of +.Fa mtx_lock +blocks until 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 with specified timeout in the +.Fa ts +argument. +The timeout argument is +.Dv TIME_UTC +based time of +.Dv timespec +type. +It is required the never lock multiple times the same +.Fa mtx +object without the +.Dv mtx_recursive +property. +In portable code there must be used +.Fa mtx +object with the +.Dv mtx_recursive +property. +.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 proceded with lock by the calling thread. +.Sh RETURN VALUES +The +.Fn mtx_destroy +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_busy +if the value +.Dv mtx +is locked by another thread or +.Dv thrd_error +if the value +.Dv mtx +is invalid. +.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 diff --git a/lib/libpthread/mtx.c b/lib/libpthread/mtx.c new file mode 100644 index 000000000000..3ff5a9bf66ed --- /dev/null +++ b/lib/libpthread/mtx.c @@ -0,0 +1,161 @@ +/* $NetBSD$ */ + +/*- + * 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. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include +#include + +void +mtx_destroy(mtx_t *mtx) +{ + + _DIAGASSERT(mtx != NULL); + + /* + * The cnd_destroy(3) function that conforms to C11 returns no value. + */ + (void)pthread_mutex_destroy(mtx); +} + +static inline int +mtx_init_default(mtx_t *mtx) +{ + + _DIAGASSERT(mtx != NULL); + + if (pthread_mutex_init(mtx, NULL) != 0) + return thrd_error; + + return thrd_success; +} + +static inline int +mtx_init_recursive(mtx_t *mtx) +{ + pthread_mutexattr_t attr; + + _DIAGASSERT(mtx != NULL); + + if (pthread_mutexattr_init(&attr) != 0) + return thrd_error; + + if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0) { + pthread_mutexattr_destroy(&attr); + + return thrd_error; + } + + if (pthread_mutex_init(mtx, &attr) == 0) + return thrd_success; + + pthread_mutexattr_destroy(&attr); + + return thrd_error; +} + +int +mtx_init(mtx_t *mtx, int type) +{ + + _DIAGASSERT(mtx != NULL); + + switch (type) { + case mtx_plain: + case mtx_timed: + return mtx_init_default(mtx); + case mtx_plain | mtx_recursive: + case mtx_timed | mtx_recursive: + return mtx_init_recursive(mtx); + default: + return thrd_error; + } +} + +int +mtx_lock(mtx_t *mtx) +{ + + _DIAGASSERT(mtx != NULL); + + if (pthread_mutex_lock(mtx) == 0) + return thrd_success; + + return thrd_error; +} + +int +mtx_timedlock(mtx_t *__restrict mtx, const struct timespec *__restrict ts) +{ + + _DIAGASSERT(mtx != NULL); + _DIAGASSERT(ts != NULL); + + switch(pthread_mutex_timedlock(mtx, ts)) { + case 0: + return thrd_success; + case ETIMEDOUT: + return thrd_timedout; + default: + return thrd_error; + } +} + +int +mtx_trylock(mtx_t *mtx) +{ + + _DIAGASSERT(mtx != NULL); + + switch(pthread_mutex_trylock(mtx)) { + case 0: + return thrd_success; + case EBUSY: + return thrd_busy; + default: + return thrd_error; + } +} + +int +mtx_unlock(mtx_t *mtx) +{ + + _DIAGASSERT(mtx != NULL); + + if (pthread_mutex_unlock(mtx) == 0) + return thrd_success; + + return thrd_error; +} diff --git a/lib/libpthread/thrd.3 b/lib/libpthread/thrd.3 new file mode 100644 index 000000000000..f35190ca78e0 --- /dev/null +++ b/lib/libpthread/thrd.3 @@ -0,0 +1,246 @@ +.\" $NetBSD$ +.\" +.\" 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 THRD 3 +.Os +.Sh NAME +.Nm thrd +.Nd thread functions +.Sh LIBRARY +.Lb libpthread +.Sh SYNOPSIS +.In threads.h +.Ft int +.Fn thrd_create "thrd_t *thr" "thrd_start_t func" "void *arg" +.Ft thrd_t +.Fn thrd_current "void" +.Ft int +.Fn thrd_detach "thrd_t thr" +.Ft int +.Fn thrd_equal "thrd_t t1" "thrd_t t2" +.Ft __dead void +.Fn thrd_exit "int res" +.Ft int +.Fn thrd_join "thrd_t thr" "int *res" +.Ft int +.Fn thrd_sleep "const struct timespec *duration" "struct timespec *remaining" +.Ft void +.Fn thrd_yield "void" +.Sh DESCRIPTION +The +.Nm +interface operates over opaque object of the +.Dv thrd_t +type. +.Pp +The +.Fn thrd_create +function is used to create a new thread, calling +.Fa func +with +.Fa arg +parameter. +This function initializes the +.Fa thr +object with the identifier of newly created thread. +The completion of +.Fn thrd_create +is synchronized with the start of the newly produced thread. +It is possible to reuse the +.Fa thr +object once the thread has terminated either by joining another thread +operation or been detached. +.Pp +The +.Fn thrd_current +function returns thread identifier. +.Pp +The +.Fn thrd_detach +function is used to indicate that storage for the +.Fa thr +object can be reclaimed on thread's termination. +The +.Fa thr +object cannot be already detached or joined. +.Pp +The +.Fn thrd_equal +function is used to check whether two +.Fa t1 +and +.Fa t2 +objects refer to the same thread. +.Pp +The +.Fn thrd_exit +function terminates calling thread and passes the +.Fa res +value, that might be read by the +.Fn thrd_join +function. +The program terminates once all threads has been terminated with +result equivalent to calling the +.Xr exit 3 +function with the +.Dv EXIT_SUCCESS +status. +The +.Fn thrd_join +function joins the +.Fa thr +thread, waiting and blocking until it has terminated. +The +.Fa res +parameter points to a variable that will store the status passed from the +joined function. +If +.Fa res +is +.Dv NULL +then the status from the +.Fn thrd_exit +function is ignored. +.Pp +The +.Fn thrd_sleep +function suspends execution of the calling thread until either +a signal is received or the interval specified in the +.Fa duration +argument has been passed. +The +.Fa remaining +parameter stores requested timeout reduced with the time actually suspended. +This argument is used when the +.Fn thrd_sleep +has been interrupted. +It is valid code to point both arguments +.Fa duration +and +.Fa remaining +to the same object. +It is not guaranteed that sleep will not take longer than specified +.Fa duration , +however unless interrupted with a signal it will not take shorter +than specified interval. +.Pp +The +.Fn thrd_yield +function yields a process voluntarily and gives other threads a chance to run +without waiting for any involuntary preemptive switch. +.Sh RETURN VALUES +The +.Fn thrd_create +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_nomem +if not sufficient memory could be allocated or +.Dv thrd_error +on other error. +.Pp +The +.Fn thrd_current +function returns identifier of the calling thread. +.Pp +The +.Fn thrd_detach +function returns +.Dv thrd_current +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn thrd_equal +function returns zero if +.Fa t0 +and +.Fa t1 +refer to the same thread, +otherwise it will return non-zero. +.Pp +The +.Fn thrd_exit +function does not return. +The standard attributes this function with the +.Dv _Noreturn +keyword, +however in the +.Nx +version it is attributed with the traditional and functionally equivalent +.Dv __dead +keyword. +.Pp +The +.Fn thrd_join +function returns +.Dv thrd_success +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn thrd_sleep +function returns +.Dv 0 +on success (as the requested time has elapsed), +.Dv -1 +once the function was interrupted by a signal +or a negative value to indicate error. +The +.Nx +implementation returns +.Dv -2 +on error. +.Pp +The +.Fn thrd_yield +returns no value. +.Sh SEE ALSO +.Xr nanosleep 2 , +.Xr pthread_create 3 , +.Xr pthread_detach 3 , +.Xr pthread_equal 3 , +.Xr pthread_exit 3 , +.Xr pthread_join 3 , +.Xr pthread_self 3 , +.Xr sched 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 diff --git a/lib/libpthread/thrd.c b/lib/libpthread/thrd.c new file mode 100644 index 000000000000..ef75ed9f1daf --- /dev/null +++ b/lib/libpthread/thrd.c @@ -0,0 +1,137 @@ +/* $NetBSD$ */ + +/*- + * 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. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include +#include +#include +#include + +int +thrd_create(thrd_t *thr, thrd_start_t func, void *arg) +{ + + _DIAGASSERT(thr != NULL); + _DIAGASSERT(func != NULL); + + switch(pthread_create(thr, NULL, (void *(*)(void *))func, arg)) { + case 0: + return thrd_success; + case EAGAIN: + return thrd_nomem; + default: + return thrd_error; + } +} + +thrd_t +thrd_current(void) +{ + + return pthread_self(); +} + +int +thrd_detach(thrd_t thr) +{ + + _DIAGASSERT(thr != NULL); + + if (pthread_detach(thr) == 0) + return thrd_success; + + return thrd_error; +} + +int +thrd_equal(thrd_t t1, thrd_t t2) +{ + + _DIAGASSERT(t1 != NULL); + _DIAGASSERT(t2 != NULL); + + return !pthread_equal(t1, t2); +} + +__dead void +thrd_exit(int res) +{ + + pthread_exit((void *)res); +} + +int +thrd_join(thrd_t thrd, int *res) +{ + void *ptr; + + _DIAGASSERT(thrd != NULL); + + if (pthread_join(thrd, &ptr) == 0) { + if (res) + *res = (int)ptr; + + return thrd_success; + } + + return thrd_error; +} + +int +thrd_sleep(const struct timespec *duration, struct timespec *remaining) +{ + + _DIAGASSERT(duration != NULL); + + /* Use clock_nanosleep(3) to skip handling errno */ + + switch (clock_nanosleep(CLOCK_MONOTONIC, TIMER_RELTIME, duration, + remaining)) { + case 0: + return 0; + case EINTR: + return -1; + default: + /* Negative value different than -1 */ + return -2; + } +} + +void +thrd_yield(void) +{ + + sched_yield(); +} diff --git a/lib/libpthread/threads.3 b/lib/libpthread/threads.3 new file mode 100644 index 000000000000..3214b6689704 --- /dev/null +++ b/lib/libpthread/threads.3 @@ -0,0 +1,98 @@ +.\" $NetBSD$ +.\" +.\" 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 THREDS 3 +.Os +.Sh NAME +.Nm threads +.Nd C11 thread support library +.Sh LIBRARY +.Lb libpthread +.Sh SYNOPSIS +.In threads.h +.Sh DESCRIPTION +Portable Threads library provides an implementation of portable threading +interface, built on top of the +.Xr pthread 3 +native interfaces. +This library is composed of: +.Bl -dash +.It +Initialization functions. +.It +Condition variable functions. +.It +Mutex functions. +.It +Thread functions. +.It +Thread-specifig storage functions. +.El +.Bl -column "mtx_timedwait" +.It Sy "Function" Ta Sy "Summary" +.It Xr call_once 3 Ta calls function exactly once +.It Xr cnd_broadcast 3 Ta unblocks all threads blocked on a condition +.It Xr cnd_destroy 3 Ta releases all resources used by a condition +.It Xr cnd_init 3 Ta creates a condition variable +.It Xr cnd_signal 3 Ta unblocks one of threads blocked on a condition +.It Xr cnd_timedwait 3 Ta unlocks a mutex and blocks until a signal or timeout +.It Xr cnd_wait 3 Ta unlocks a mutex and blocks until a signal +.It Xr mtx_destroy 3 Ta releases resources used by a mutex +.It Xr mtx_init 3 Ta creates a mutex object with requested properties +.It Xr mtx_lock 3 Ta blocks on a mutex optionally with a recursive type +.It Xr mtx_timedwait 3 Ta tries to block until it locks a mutex or timeout +.It Xr mtx_trylock 3 Ta tries to lock a mutex +.It Xr mtx_unlock 3 Ta unlocks a mutex +.It Xr thrd_create 3 Ta creates a thread executing a function with a parameter +.It Xr thrd_current 3 Ta identifies the thread that called this function +.It Xr thrd_detach 3 Ta dispose of resources allocatd to a thread on exit +.It Xr thrd_equal 3 Ta determines whether two threads reffer the same thread +.It Xr thrd_exit 3 Ta terminates a calling thread and sets its result code +.It Xr thrd_join 3 Ta joins a thread with the current one and blocks +.It Xr thrd_sleep 3 Ta suspends a calling thread until a signal or timeout +.It Xr tss_create 3 Ta crates a thread-specific storage pointer with a dtor +.It Xr tss_delete 3 Ta releases resources used by a thread-specific storage +.It Xr tss_get 3 Ta gets a value of thread-specific storage from a key +.It Xr tss_set 3 Ta sets a value of thread-specific storage to a key +.El +.Sh SEE ALSO +.Xr c11 1 , +.Xr pthread 3 , +.Xr c 7 +.Sh STANDARDS +The +.Nm +library interfaces conform to +.St -isoC-2011 . +.Sh HISTORY +This interface first appeared in +.Nx 9 . +.Sh AUTHORS +.An Kamil Rytarowski Aq Mt kamil@NetBSD.org diff --git a/lib/libpthread/threads.h b/lib/libpthread/threads.h new file mode 100644 index 000000000000..41b5e2304400 --- /dev/null +++ b/lib/libpthread/threads.h @@ -0,0 +1,121 @@ +/* $NetBSD$ */ + +/*- + * 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. + */ + +#ifndef _THREADS_H_ +#define _THREADS_H_ + +#include +#include +#include +#include +#include + +/* ISO/IEC 9899:201x 7.26.1/3 */ +#ifndef __thread_local_is_defined +#if ((__cplusplus - 0) < 201103L) +#define thread_local _Thread_local +#endif +#define __thread_local_is_defined +#endif /* __thread_local_is_defined */ + +#ifndef ONCE_FLAG_INIT +#define ONCE_FLAG_INIT _PTHREAD_ONCE_INIT +#endif /* ONCE_FLAG_INIT */ + +#ifndef TSS_DTOR_ITERATIONS +#define TSS_DTOR_ITERATIONS PTHREAD_DESTRUCTOR_ITERATIONS +#endif /* TSS_DTOR_ITERATIONS */ + +/* ISO/IEC 9899:201x 7.26.1/4 */ +typedef pthread_cond_t cnd_t; +typedef pthread_t thrd_t; +typedef int tss_t; +typedef pthread_mutex_t mtx_t; +typedef void (*tss_dtor_t) (void *); +typedef int (*thrd_start_t) (void *); +typedef pthread_once_t once_flag; + +/* ISO/IEC 9899:201x 7.26.1/5 */ +enum { + mtx_plain = 1, + mtx_recursive = 2, + mtx_timed = 4, + _MTX_MAXTYPE = 0x7fffffff +}; + +enum { + thrd_timedout = -1, + thrd_success = 0, + thrd_busy = 1, + thrd_error = 2, + thrd_nomem = 3, + _THRD_MAXTYPE = 0x7fffffff +}; + +__BEGIN_DECLS +/* ISO/IEC 9899:201x 7.26.2 Initialization functions */ +void call_once(once_flag *, void (*)(void)); + +/* ISO/IEC 9899:201x 7.26.3 Condition variable functions */ +int cnd_broadcast(cnd_t *); +void cnd_destroy(cnd_t *); +int cnd_init(cnd_t *); +int cnd_signal(cnd_t *); +int cnd_timedwait(cnd_t * __restrict, mtx_t * __restrict, + const struct timespec * __restrict); +int cnd_wait(cnd_t *, mtx_t *); + +/* ISO/IEC 9899:201x 7.26.4 Mutex functions */ +void mtx_destroy(mtx_t *); +int mtx_init(mtx_t *, int); +int mtx_lock(mtx_t *); +int mtx_timedlock(mtx_t *__restrict, const struct timespec *__restrict); +int mtx_trylock(mtx_t *); +int mtx_unlock(mtx_t *); + +/* ISO/IEC 9899:201x 7.26.5 Thread functions */ +int thrd_create(thrd_t *, thrd_start_t, void *); +thrd_t thrd_current(void); +int thrd_detach(thrd_t); +int thrd_equal(thrd_t, thrd_t); +void thrd_exit(int) __dead; +int thrd_join(thrd_t, int *); +int thrd_sleep(const struct timespec *, struct timespec *); +void thrd_yield(void); + +/* ISO/IEC 9899:201x 7.26.6 Thread-specific storage functions */ +int tss_create(tss_t *, tss_dtor_t); +void tss_delete(tss_t); +void *tss_get(tss_t); +int tss_set(tss_t, void *); +__END_DECLS + +#endif diff --git a/lib/libpthread/tss.3 b/lib/libpthread/tss.3 new file mode 100644 index 000000000000..826703afe25e --- /dev/null +++ b/lib/libpthread/tss.3 @@ -0,0 +1,164 @@ +.\" $NetBSD$ +.\" +.\" 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 TSS 3 +.Os +.Sh NAME +.Nm tss +.Nd thread-specific storage functions +.Sh LIBRARY +.Lb libpthread +.Sh SYNOPSIS +.In threads.h +.Ft int +.Fn tss_create "tss_t *key" "tss_dtor_t dtor" +.Ft void +.Fn tss_delete "tss_t key" +.Ft void * +.Fn tss_get "tss_t key" +.Ft int +.Fn tss_set "tss_t key" "void *val" +.Vt #define TSS_DTOR_ITERATIONS /* implementation specified */ +.Sh DESCRIPTION +There are two groups of storage in C programs: +.Bl -dash +.It +local storage, +.It +global storage. +.El +Multithreaded programs in C add the third group +.Sy thread-specific storage . +This data is private to thread and every entry of this type has associated +.Dv tss_t +opaque key that is global to all threads in the process. +A thread using the +.Dv tss_t * +pointer accesses private data. +.Pp +The +.Fn tss_create +function creates a thread-specific storage with the +.Fa key +handler with optional destructor +.Fa dtor . +If the +.Fa dtor +parameter is not +.Dv NULL +then specified appropriate destructor will be called on thread termination. +Destructor is not called if a thread called the +.Fn tss_delete +function for the specified +.Fa key . +If, +after all the destructors have been called for all +.Dv non-NULL +values with associated destructors, +ther are still some +.Dv non-NULL values with +associated destructors, +then the process is repeated. +If, +after at least +.Dv TSS_DTOR_ITERATIONS +iterations of destructor calls for outstanding +.Dv non-NULL +values, +there are still some +.Dv non-NULL +values with associated destructors, +the +.Nx +implementation stops further calling destructors. +The +.Xr thrd_exit +function must not be called from a destructor. +.Pp +The +.Fn tss_delete +function frees resources used by the thread-specific storage identified by the +.Fa key +object. +This function can be called inside the +.Fa dtor +destructor, +however the destructor is not called by +.Fn tss_delete . +.Pp +The +.Fn tss_get +and +.Fn tss_set +functions are used to get and set thread-specific storage. +.Sh RETURN VALUES +The +.Fn tss_create +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_error +on failure. +.Pp +The +.Fn tss_delete +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_error +on failure. +.Pp +The +.Fn tss_get +returns pointer to thread-specific storage on success or +.Dv NULL +on failure. +.Pp +The +.Fn tss_delete +function returns +.Dv thrd_success +on success, otherwise +.Dv thrd_error +on failure. +.Sh SEE ALSO +.Xr pthread_getspecific 3 , +.Xr pthread_key_create 3 , +.Xr threads 3 +.Sh STANDARDS +The +.Nm +interface conforms to +.St -isoC-2011 . +.Sh HISTORY +This interface first appeared in +.Nx 8 . +.Sh AUTHORS +.An Kamil Rytarowski Aq Mt kamil@NetBSD.org diff --git a/lib/libpthread/tss.c b/lib/libpthread/tss.c new file mode 100644 index 000000000000..b811119c4f71 --- /dev/null +++ b/lib/libpthread/tss.c @@ -0,0 +1,83 @@ +/* $NetBSD$ */ + +/*- + * 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. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include +#include + +int +tss_create(tss_t *key, tss_dtor_t dtor) +{ + + _DIAGASSERT(key != NULL); + + if (pthread_key_create(key, dtor) == 0) + return thrd_success; + + return thrd_error; +} + +void +tss_delete(tss_t key) +{ + + _DIAGASSERT(key != NULL); + + /* + * The tss_delete(3) function that conforms to C11 returns no value. + */ + (void)pthread_key_delete(key); +} + +void * +tss_get(tss_t key) +{ + + _DIAGASSERT(key != NULL); + + return pthread_getspecific(key); +} + +int +tss_set(tss_t key, void *val) +{ + + _DIAGASSERT(key != NULL); + + if (pthread_setspecific(key, val) == 0) + return thrd_success; + + return thrd_error; +}