commit 465e64315deaf5b5550d89338404f70575a7782b 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/distrib/sets/lists/debug/mi b/distrib/sets/lists/debug/mi index c4f88a08a3e3..c2e752f2825a 100644 --- a/distrib/sets/lists/debug/mi +++ b/distrib/sets/lists/debug/mi @@ -2243,11 +2243,13 @@ ./usr/libdata/debug/usr/tests/lib/libpthread/dlopen/t_dso_pthread_create.debug tests-lib-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/dlopen/t_main_pthread_create.debug tests-lib-debug debug,atf,pic,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/h_atexit.debug tests-lib-tests debug,atf,compattestfile -./usr/libdata/debug/usr/tests/lib/libpthread/h_cancel.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/h_exit.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/h_resolv.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/h_thread_local_dtor.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_barrier.debug tests-lib-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/lib/libpthread/t_call_once.debug tests-lib-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/lib/libpthread/t_cnd.debug tests-lib-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/lib/libpthread/h_cancel.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_cond.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_condwait.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_detach.debug tests-lib-tests debug,atf,compattestfile @@ -2256,6 +2258,7 @@ ./usr/libdata/debug/usr/tests/lib/libpthread/t_fpu.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_join.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_kill.debug tests-lib-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/lib/libpthread/t_mtx.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_mutex.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_name.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_once.debug tests-lib-tests debug,atf,compattestfile @@ -2269,6 +2272,8 @@ ./usr/libdata/debug/usr/tests/lib/libpthread/t_status.debug tests-obsolete obsolete,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_swapcontext.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread/t_timedmutex.debug tests-lib-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/lib/libpthread/t_tss.debug tests-lib-tests debug,atf,compattestfile +./usr/libdata/debug/usr/tests/lib/libpthread/t_thrd.debug tests-lib-tests debug,atf,compattestfile ./usr/libdata/debug/usr/tests/lib/libpthread_dbg/t_dummy.debug tests-obsolete obsolete ./usr/libdata/debug/usr/tests/lib/libpthread_dbg/t_threads.debug tests-obsolete obsolete ./usr/libdata/debug/usr/tests/lib/librefuse/t_refuse_opt.debug tests-lib-debug debug,atf,compattestfile diff --git a/distrib/sets/lists/tests/mi b/distrib/sets/lists/tests/mi index 9ec34d1cc819..33aac5aeade1 100644 --- a/distrib/sets/lists/tests/mi +++ b/distrib/sets/lists/tests/mi @@ -3117,7 +3117,9 @@ ./usr/tests/lib/libpthread/h_thread_local_dtor tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_atexit tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_barrier tests-lib-tests compattestfile,atf +./usr/tests/lib/libpthread/t_call_once tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_cancel tests-lib-tests compattestfile,atf +./usr/tests/lib/libpthread/t_cnd tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_cond tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_condwait tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_detach tests-lib-tests compattestfile,atf @@ -3127,6 +3129,7 @@ ./usr/tests/lib/libpthread/t_fpu tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_join tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_kill tests-lib-tests compattestfile,atf +./usr/tests/lib/libpthread/t_mtx tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_mutex tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_name tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_once tests-lib-tests compattestfile,atf @@ -3140,7 +3143,9 @@ ./usr/tests/lib/libpthread/t_sleep tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_status tests-obsolete obsolete ./usr/tests/lib/libpthread/t_swapcontext tests-lib-tests compattestfile,atf +./usr/tests/lib/libpthread/t_tss tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_thread_local_dtor tests-lib-tests compattestfile,atf +./usr/tests/lib/libpthread/t_thrd tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread/t_timedmutex tests-lib-tests compattestfile,atf ./usr/tests/lib/libpthread_dbg tests-obsolete obsolete ./usr/tests/lib/libpthread_dbg/Atffile tests-obsolete obsolete 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..8206a52b0ec4 --- /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 #define ONCE_FLAG_INIT /* implementation specified */ +.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"); +} + +int +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..3e5238983e60 --- /dev/null +++ b/lib/libpthread/cnd.3 @@ -0,0 +1,180 @@ +.\" $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 no value. +.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..214fd4a279dc --- /dev/null +++ b/lib/libpthread/mtx.3 @@ -0,0 +1,217 @@ +.\" $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 no value. +.Pp +The +.Fn mtx_init +function returns +.Dv thrd_success +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn mtx_lock +function returns +.Dv thrd_success +on success or +.Dv thrd_error +on failure. +.Pp +The +.Fn mtx_lock +function returns +.Dv thrd_success +on success, +otherwise +.Dv thrd_timedout +to indicate that system time has reached or exceeded the time specified in +.Dv ts +or +.Dv thrd_error +on failure. +.Pp +The +.Fn mtx_trylock +function returns +.Dv thrd_success +on success, +otherwise +.Dv thrd_timedout +to indicate that +.Fa mtx +object is already locked or +.Dv thrd_error +on failure. +.Pp +The +.Fn mtx_unlock +function returns +.Dv thrd_success +on success, +otherwise +.Dv thrd_timedout +to indicate that +.Fa mtx +object is already locked or +.Dv thrd_error +on failure. +.Sh SEE ALSO +.Xr pthread_mutex 3 , +.Xr threads 3 +.Sh STANDARDS +The +.Nm +interface conforms to +.St -isoC-2011 . +.Sh HISTORY +This interface first appeared in +.Nx 9 . +.Sh AUTHORS +.An Kamil Rytarowski Aq Mt kamil@NetBSD.org diff --git a/lib/libpthread/mtx.c b/lib/libpthread/mtx.c new file mode 100644 index 000000000000..52bea7f53317 --- /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..328cdb6feb9b --- /dev/null +++ b/lib/libpthread/thrd.3 @@ -0,0 +1,247 @@ +.\" $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 +.Vt typedef "int" "(*thrd_start_t)" "(void *)" +.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 different threads, +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..984c939dd8ca --- /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 *)(intptr_t)res); +} + +int +thrd_join(thrd_t thrd, int *res) +{ + void *ptr; + + _DIAGASSERT(thrd != NULL); + + if (pthread_join(thrd, &ptr) == 0) { + if (res) + *res = (int)(intptr_t)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..3994e3716f9e --- /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..7bbb2a602a2f --- /dev/null +++ b/lib/libpthread/tss.3 @@ -0,0 +1,162 @@ +.\" $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 +.Vt "typedef" "void" "(*tss_dtor_t)" "(void *)" +.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 +.Pp +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 no value. +.Pp +The +.Fn tss_get +returns pointer to thread-specific storage on success or +.Dv NULL +on failure. +.Pp +The +.Fn tss_set +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 9 . +.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; +} diff --git a/tests/lib/libpthread/Makefile b/tests/lib/libpthread/Makefile index b25284e5db4c..20ddfbe960af 100644 --- a/tests/lib/libpthread/Makefile +++ b/tests/lib/libpthread/Makefile @@ -50,6 +50,8 @@ PROGS+= h_exit PROGS+= h_resolv PROGS_CXX+= h_thread_local_dtor +TESTS_C+= t_call_once t_cnd t_mtx t_thrd t_tss # C11 threads(3) + COPTS.h_thread_local_dtor.cpp+= -std=c++11 # Deal with questionable warning and header quality in libstdc++. COPTS.h_thread_local_dtor.cpp+= ${${ACTIVE_CC} == "gcc" :? -Wno-ctor-dtor-privacy -Wno-sign-compare -Wno-shadow :} diff --git a/tests/lib/libpthread/t_call_once.c b/tests/lib/libpthread/t_call_once.c new file mode 100644 index 000000000000..2f5c32617948 --- /dev/null +++ b/tests/lib/libpthread/t_call_once.c @@ -0,0 +1,89 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2019 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 +__COPYRIGHT("@(#) Copyright (c) 2019\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD$"); + +#include + +#include + +ATF_TC(call_once); +ATF_TC_HEAD(call_once, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 call_once(3)"); +} + +#define CO_THREADS 8 + +static int co_val; +static once_flag oflag = ONCE_FLAG_INIT; + +static void +called_once(void) +{ + + ++co_val; +} + +static int +co_func(void *arg __unused) +{ + + call_once(&oflag, called_once); + + return 0; +} + +ATF_TC_BODY(call_once, tc) +{ + thrd_t t[CO_THREADS]; + size_t i; + + for (i = 0; i < CO_THREADS; i++) { + ATF_REQUIRE_EQ(thrd_create(&t[i], co_func, NULL), thrd_success); + } + + for (i = 0; i < CO_THREADS; i++) { + ATF_REQUIRE_EQ(thrd_join(t[i], NULL), thrd_success); + } + + ATF_REQUIRE_EQ(co_val, 1); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, call_once); + + return atf_no_error(); +} diff --git a/tests/lib/libpthread/t_cnd.c b/tests/lib/libpthread/t_cnd.c new file mode 100644 index 000000000000..90ca47351cd3 --- /dev/null +++ b/tests/lib/libpthread/t_cnd.c @@ -0,0 +1,175 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2019 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 +__COPYRIGHT("@(#) Copyright (c) 2019\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD$"); + +#include +#include +#include + +#include + +ATF_TC(cnd_init); +ATF_TC_HEAD(cnd_init, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 cnd_init(3)"); +} + +ATF_TC_BODY(cnd_init, tc) +{ + cnd_t c; + + ATF_REQUIRE_EQ(cnd_init(&c), thrd_success); + cnd_destroy(&c); +} + +#define B_THREADS 8 + +static mtx_t b_m; +static cnd_t b_c; +static bool b_finished; + +static int +b_func(void *arg) +{ + bool signal; + + signal = (bool)(intptr_t)arg; + + ATF_REQUIRE_EQ(mtx_lock(&b_m), thrd_success); + while (!b_finished) { + ATF_REQUIRE_EQ(cnd_wait(&b_c, &b_m), thrd_success); + } + ATF_REQUIRE_EQ(mtx_unlock(&b_m), thrd_success); + + if (signal) { + ATF_REQUIRE_EQ(cnd_signal(&b_c), thrd_success); + } + + return 0; +} + +static void +cnd_notify(bool signal) +{ + size_t i; + thrd_t t[B_THREADS]; + void *n; + + n = (void *)(intptr_t)signal; + + ATF_REQUIRE_EQ(mtx_init(&b_m, mtx_plain), thrd_success); + ATF_REQUIRE_EQ(cnd_init(&b_c), thrd_success); + + for (i = 0; i < B_THREADS; i++) { + ATF_REQUIRE_EQ(thrd_create(&t[i], b_func, n), thrd_success); + } + + ATF_REQUIRE_EQ(mtx_lock(&b_m), thrd_success); + b_finished = true; + ATF_REQUIRE_EQ(mtx_unlock(&b_m), thrd_success); + + if (signal) { + ATF_REQUIRE_EQ(cnd_signal(&b_c), thrd_success); + } else { + ATF_REQUIRE_EQ(cnd_broadcast(&b_c), thrd_success); + } + + for (i = 0; i < B_THREADS; i++) { + ATF_REQUIRE_EQ(thrd_join(t[i], NULL), thrd_success); + } + + mtx_destroy(&b_m); + cnd_destroy(&b_c); +} + +ATF_TC(cnd_broadcast); +ATF_TC_HEAD(cnd_broadcast, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 cnd_broadcast(3)"); +} + +ATF_TC_BODY(cnd_broadcast, tc) +{ + + cnd_notify(false); +} + +ATF_TC(cnd_signal); +ATF_TC_HEAD(cnd_signal, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 cnd_signal(3)"); +} + +ATF_TC_BODY(cnd_signal, tc) +{ + + cnd_notify(true); +} + +ATF_TC(cnd_timedwait); +ATF_TC_HEAD(cnd_timedwait, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 cnd_timedwait(3)"); +} + +ATF_TC_BODY(cnd_timedwait, tc) +{ + mtx_t m; + cnd_t c; + struct timespec ts; + + ts.tv_sec = 0; + ts.tv_nsec = 1; + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success); + ATF_REQUIRE_EQ(cnd_init(&c), thrd_success); + + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(cnd_timedwait(&c, &m, &ts), thrd_timedout); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + + mtx_destroy(&m); + cnd_destroy(&c); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, cnd_init); + ATF_TP_ADD_TC(tp, cnd_broadcast); + ATF_TP_ADD_TC(tp, cnd_signal); + ATF_TP_ADD_TC(tp, cnd_timedwait); + + return atf_no_error(); +} diff --git a/tests/lib/libpthread/t_mtx.c b/tests/lib/libpthread/t_mtx.c new file mode 100644 index 000000000000..06940fb83c87 --- /dev/null +++ b/tests/lib/libpthread/t_mtx.c @@ -0,0 +1,215 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2019 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 +__COPYRIGHT("@(#) Copyright (c) 2019\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD$"); + +#include +#include + +#include + +ATF_TC(mtx_init); +ATF_TC_HEAD(mtx_init, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 mtx_init(3)"); +} + +ATF_TC_BODY(mtx_init, tc) +{ + mtx_t m; + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_recursive), thrd_error); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_timed), thrd_error); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, -1), thrd_error); + mtx_destroy(&m); +} + +ATF_TC(mtx_lock); +ATF_TC_HEAD(mtx_lock, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 mtx_lock(3)"); +} + +ATF_TC_BODY(mtx_lock, tc) +{ + mtx_t m; + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); +} + +ATF_TC(mtx_timedlock); +ATF_TC_HEAD(mtx_timedlock, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 mtx_timedlock(3)"); +} + +ATF_TC_BODY(mtx_timedlock, tc) +{ + mtx_t m; + struct timespec ts; + + ts.tv_sec = 0; + ts.tv_nsec = 1; + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success); + ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_timedout); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success); + ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_timedlock(&m, &ts), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); +} + +ATF_TC(mtx_trylock); +ATF_TC_HEAD(mtx_trylock, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 mtx_trylock(3)"); +} + +ATF_TC_BODY(mtx_trylock, tc) +{ + mtx_t m; + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success); + ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_busy); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_plain | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success); + ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_busy); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); + + ATF_REQUIRE_EQ(mtx_init(&m, mtx_timed | mtx_recursive), thrd_success); + ATF_REQUIRE_EQ(mtx_lock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_trylock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + ATF_REQUIRE_EQ(mtx_unlock(&m), thrd_success); + mtx_destroy(&m); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, mtx_init); + ATF_TP_ADD_TC(tp, mtx_lock); + ATF_TP_ADD_TC(tp, mtx_timedlock); + ATF_TP_ADD_TC(tp, mtx_trylock); + + return atf_no_error(); +} diff --git a/tests/lib/libpthread/t_thrd.c b/tests/lib/libpthread/t_thrd.c new file mode 100644 index 000000000000..93e90563fd18 --- /dev/null +++ b/tests/lib/libpthread/t_thrd.c @@ -0,0 +1,237 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2019 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 +__COPYRIGHT("@(#) Copyright (c) 2019\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD$"); + +#include +#include +#include +#include + +#include + +ATF_TC(thrd_create); +ATF_TC_HEAD(thrd_create, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 thrd_create(3)"); +} + +#define TC_ADDON 5 + +static int +tcr_func(void *arg) +{ + int a; + + a = (int)(intptr_t)arg; + + return a + TC_ADDON; +} + +ATF_TC_BODY(thrd_create, tc) +{ + thrd_t t; + const int a = 5; + int b; + void *v; + + v = (void *)(intptr_t)a; + + ATF_REQUIRE_EQ(thrd_create(&t, tcr_func, v), thrd_success); + ATF_REQUIRE_EQ(thrd_join(t, &b), thrd_success); + ATF_REQUIRE_EQ(a + TC_ADDON, b); +} + +ATF_TC(thrd_current); +ATF_TC_HEAD(thrd_current, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 thrd_current(3)"); +} + +static int +tcur_func(void *arg __unused) +{ + + return 0; +} + +ATF_TC_BODY(thrd_current, tc) +{ + thrd_t s, t; + + s = thrd_current(); + + ATF_REQUIRE(thrd_equal(s, s) != 0); + ATF_REQUIRE_EQ(thrd_create(&t, tcur_func, NULL), thrd_success); + ATF_REQUIRE(thrd_equal(t, s) == 0); + ATF_REQUIRE(thrd_equal(s, t) == 0); + ATF_REQUIRE(thrd_equal(t, t) != 0); + + ATF_REQUIRE_EQ(thrd_join(t, NULL), thrd_success); +} + +ATF_TC(thrd_detach); +ATF_TC_HEAD(thrd_detach, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 thrd_detach(3)"); +} + +static int +tdet_func(void *arg __unused) +{ + + return 0; +} + +ATF_TC_BODY(thrd_detach, tc) +{ + thrd_t t; + + ATF_REQUIRE_EQ(thrd_create(&t, tdet_func, NULL), thrd_success); + ATF_REQUIRE_EQ(thrd_detach(t), thrd_success); +} + +ATF_TC(thrd_exit); +ATF_TC_HEAD(thrd_exit, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 thrd_exit(3)"); +} + +static void +tex_func2(void) +{ + + thrd_exit(1); +} + +static int +tex_func(void *arg __unused) +{ + + tex_func2(); + + return -1; +} + +ATF_TC_BODY(thrd_exit, tc) +{ + thrd_t t; + int b = 0; + + ATF_REQUIRE_EQ(thrd_create(&t, tex_func, NULL), thrd_success); + ATF_REQUIRE_EQ(thrd_join(t, &b), thrd_success); + ATF_REQUIRE_EQ(b, 1); +} + +ATF_TC(thrd_sleep); +ATF_TC_HEAD(thrd_sleep, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 thrd_sleep(3)"); +} + +static int alarmed; + +static void +alarm_handler(int signum) +{ + + ATF_REQUIRE_EQ(signum, SIGALRM); + ++alarmed; +} + +ATF_TC_BODY(thrd_sleep, tc) +{ + struct timespec start, stop, diff; + struct timespec ts, rem; + struct timespec zero; + struct sigaction sa; + struct itimerval timer; + + zero.tv_sec = 0; + zero.tv_nsec = 0; + + ts.tv_sec = 1; + ts.tv_nsec = -1; + ATF_REQUIRE_EQ(!thrd_sleep(&ts, NULL), 0); + + ts.tv_sec = 0; + ts.tv_nsec = 1000000000/100; /* 1/100 sec */ + ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &start), 0); + ATF_REQUIRE_EQ(thrd_sleep(&ts, &rem), 0); + ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &stop), 0); + timespecsub(&stop, &start, &diff); + ATF_REQUIRE(timespeccmp(&diff, &ts, >=)); + ATF_REQUIRE(timespeccmp(&zero, &rem, ==)); + + ts.tv_sec = 1; + ts.tv_nsec = 0; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = alarm_handler; + sigaction(SIGALRM, &sa, NULL); + memset(&timer, 0, sizeof(timer)); + timer.it_value.tv_sec = 0; + timer.it_value.tv_usec = 100000; /* 100 msec */ + ATF_REQUIRE_EQ(setitimer(ITIMER_MONOTONIC, &timer, NULL), 0); + ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &start), 0); + ATF_REQUIRE_EQ(!thrd_sleep(&ts, &rem), 0); + ATF_REQUIRE_EQ(clock_gettime(CLOCK_MONOTONIC, &stop), 0); + timespecsub(&stop, &start, &diff); + ATF_REQUIRE(timespeccmp(&diff, &ts, <)); + ATF_REQUIRE(timespeccmp(&zero, &rem, !=)); + ATF_REQUIRE_EQ(alarmed, 1); +} + +ATF_TC(thrd_yield); +ATF_TC_HEAD(thrd_yield, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 thrd_yield(3)"); +} + +ATF_TC_BODY(thrd_yield, tc) +{ + + thrd_yield(); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, thrd_create); + ATF_TP_ADD_TC(tp, thrd_current); + ATF_TP_ADD_TC(tp, thrd_detach); + ATF_TP_ADD_TC(tp, thrd_exit); + ATF_TP_ADD_TC(tp, thrd_sleep); + ATF_TP_ADD_TC(tp, thrd_yield);; + + return atf_no_error(); +} diff --git a/tests/lib/libpthread/t_tss.c b/tests/lib/libpthread/t_tss.c new file mode 100644 index 000000000000..c72aea1f726b --- /dev/null +++ b/tests/lib/libpthread/t_tss.c @@ -0,0 +1,174 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2019 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 +__COPYRIGHT("@(#) Copyright (c) 2019\ + The NetBSD Foundation, inc. All rights reserved."); +__RCSID("$NetBSD$"); + +#include +#include + +#include + +ATF_TC(tss_create); +ATF_TC_HEAD(tss_create, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 tss_create(3)"); +} + +ATF_TC_BODY(tss_create, tc) +{ + tss_t s; + + ATF_REQUIRE_EQ(tss_create(&s, NULL), thrd_success); + tss_delete(s); +} + +ATF_TC(tss_set); +ATF_TC_HEAD(tss_set, tc) +{ + atf_tc_set_md_var(tc, "descr", "Test C11 tss_set(3)"); +} + +ATF_TC_BODY(tss_set, tc) +{ + tss_t s; + int b = 5; + void *v; + + v = (void *)(intptr_t)b; + + ATF_REQUIRE_EQ(tss_create(&s, NULL), thrd_success); + ATF_REQUIRE_EQ(tss_get(s), NULL); + ATF_REQUIRE_EQ(tss_set(s, v), thrd_success); + ATF_REQUIRE_EQ(tss_get(s), v); + + tss_delete(s); +} + +ATF_TC(tss_destructor_main_thread); +ATF_TC_HEAD(tss_destructor_main_thread, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test C11 tss(3) destructor in main thread"); +} + +static void +c_destructor_main(void *arg) +{ + + abort(); +} + +ATF_TC_BODY(tss_destructor_main_thread, tc) +{ + tss_t s; + int b = 5; + void *v; + + v = (void *)(intptr_t)b; + + ATF_REQUIRE_EQ(tss_create(&s, c_destructor_main), thrd_success); + ATF_REQUIRE_EQ(tss_set(s, v), thrd_success); + + tss_delete(s); + + /* Destructor must NOT be called for tss_delete(3) */ +} + +ATF_TC(tss_destructor_thread_exit); +ATF_TC_HEAD(tss_destructor_thread_exit, tc) +{ + atf_tc_set_md_var(tc, "descr", + "Test C11 tss(3) destructor on thread exit"); +} + +static tss_t s_empty, s_nonempty; +static int c_iter_empty, c_iter_nonempty; + +static void +c_destructor_thread_empty(void *arg) +{ + + tss_set(s_empty, arg); + ++c_iter_empty; +} + +static void +c_destructor_thread_nonempty(void *arg) +{ + + tss_set(s_nonempty, arg); + ++c_iter_nonempty; +} + +static int +t_func(void *arg __unused) +{ + int b = 5; + void *v; + + v = (void *)(intptr_t)b; + + ATF_REQUIRE_EQ(tss_set(s_nonempty, v), thrd_success); + + return 0; +} + +ATF_TC_BODY(tss_destructor_thread_exit, tc) +{ + thrd_t t; + + ATF_REQUIRE_EQ(tss_create(&s_empty, c_destructor_thread_empty), + thrd_success); + ATF_REQUIRE_EQ(tss_create(&s_nonempty, c_destructor_thread_nonempty), + thrd_success); + + ATF_REQUIRE_EQ(thrd_create(&t, t_func, NULL), thrd_success); + ATF_REQUIRE_EQ(thrd_join(t, NULL), thrd_success); + + ATF_REQUIRE_EQ(c_iter_empty, 0); + ATF_REQUIRE_EQ(c_iter_nonempty, TSS_DTOR_ITERATIONS); + + tss_delete(s_empty); + tss_delete(s_nonempty); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, tss_create); + ATF_TP_ADD_TC(tp, tss_set); + ATF_TP_ADD_TC(tp, tss_destructor_main_thread); + ATF_TP_ADD_TC(tp, tss_destructor_thread_exit); + + return atf_no_error(); +}