Index: Makefile =================================================================== RCS file: /cvsroot/src/lib/Makefile,v retrieving revision 1.248 diff -u -r1.248 Makefile --- Makefile 14 Oct 2016 17:29:29 -0000 1.248 +++ Makefile 16 Oct 2016 04:23:01 -0000 @@ -172,6 +172,8 @@ SUBDIR+= ../external/gpl3/${EXTERNAL_GCC_SUBDIR}/lib/libsupc++ .endif +SUBDIR+= libstdthreads # depends on libpthread + #==================== 2nd library dependency barrier ==================== SUBDIR+= .WAIT Index: libstdthreads/Makefile =================================================================== RCS file: libstdthreads/Makefile diff -N libstdthreads/Makefile --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/Makefile 16 Oct 2016 04:23:02 -0000 @@ -0,0 +1,61 @@ +# $NetBSD$ + +# WARNS= 6 + +CPPFLAGS+= -I${.CURDIR} + +LIB= stdthreads + +INCS= threads.h +INCSDIR= /usr/include + +SRCS= # +SRCS+= call_once.c # ISO/IEC 9899:201x 7.26.2 Initialization functions +SRCS+= cnd.c # ISO/IEC 9899:201x 7.26.3 Condition variable functions +SRCS+= mtx.c # ISO/IEC 9899:201x 7.26.4 Mutex functions +SRCS+= thrd.c # ISO/IEC 9899:201x 7.26.5 Thread functions +SRCS+= tss.c # ISO/IEC 9899:201x 7.26.6 Thread-specific storage functions + +LIBDPLIBS+= pthread ${.CURDIR}/../libpthread + +MAN= # +MAN+= stdthread.3 # The libstdthreads library overview +MAN+= call_once.3 +MAN+= cnd.3 +MAN+= mtx.3 +MAN+= thrd.3 +MAN+= 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 + +.include Index: libstdthreads/call_once.c =================================================================== RCS file: libstdthreads/call_once.c diff -N libstdthreads/call_once.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/call_once.c 16 Oct 2016 04:23:02 -0000 @@ -0,0 +1,51 @@ +/* $NetBSD$ */ + +/*- + * Copyright (c) 2016 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by + * + * 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 +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"); +} Index: libstdthreads/cnd.c =================================================================== RCS file: libstdthreads/cnd.c diff -N libstdthreads/cnd.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/cnd.c 16 Oct 2016 04:23:02 -0000 @@ -0,0 +1,119 @@ +/* $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) +{ + int ret; + + _DIAGASSERT(cond != NULL); + + /* The cnd_destroy(3) function returns no value, this forces this code + * to break as there is nothing better available. */ + if ((ret = pthread_cond_destroy(cond)) != 0) + errc(EXIT_FAILURE, ret, "pthread_cond_destroy failed"); +} + +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; +} Index: libstdthreads/mtx.c =================================================================== RCS file: libstdthreads/mtx.c diff -N libstdthreads/mtx.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/mtx.c 16 Oct 2016 04:23:02 -0000 @@ -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. + */ + +#include +__RCSID("$NetBSD$"); + +#include +#include +#include +#include + +void +mtx_destroy(mtx_t *mtx) +{ + int ret; + + _DIAGASSERT(mtx != NULL); + + /* The mtx_destroy(3) function returns no value, this forces this code + * to break as there is nothing better available. */ + if ((ret = pthread_mutex_destroy(mtx)) != 0) + errc(EXIT_FAILURE, ret, "pthread_mutex_destroy failed"); +} + +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; +} Index: libstdthreads/shlib_version =================================================================== RCS file: libstdthreads/shlib_version diff -N libstdthreads/shlib_version --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/shlib_version 16 Oct 2016 04:23:02 -0000 @@ -0,0 +1,5 @@ +# $NetBSD$ +# Remember to update distrib/sets/lists/base/shl.* when changing +# +major=0 +minor=0 Index: libstdthreads/thrd.c =================================================================== RCS file: libstdthreads/thrd.c diff -N libstdthreads/thrd.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/thrd.c 16 Oct 2016 04:23:02 -0000 @@ -0,0 +1,135 @@ +/* $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 +thrd_create(thrd_t *thr, thrd_start_t func, void *arg) +{ + + _DIAGASSERT(thr != NULL); + _DIAGASSERT(func != NULL); + + switch(pthread_create(thr, NULL, 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, thrd_t) +{ + + _DIAGASSERT(t1 != NULL); + _DIAGASSERT(t2 != NULL); + + return !pthread_equal(t1, t2); +} + +__dead void +thrd_exit(int res) +{ + + pthread_exit((void *)(uintptr_t)res); +} + +int +thrd_join(thrd_t thrd, int *res) +{ + void *ptr; + + _DIAGASSERT(thrd != NULL); + + if (pthread_join(thrd, &ptr) == 0) { + if (res != NULL) + *res = 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(); +} Index: libstdthreads/threads.h =================================================================== RCS file: libstdthreads/threads.h diff -N libstdthreads/threads.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/threads.h 16 Oct 2016 04:23:02 -0000 @@ -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. + */ + +#ifndef _THREADS_H_ +#define _THREADS_H_ + +#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 struct pthread_cond *cnd_t; +typedef struct pthread *thrd_t; +typedef int tss_t; +typedef struct pthread_mutex *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 +}; + +/* 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 *); + +#endif Index: libstdthreads/tss.c =================================================================== RCS file: libstdthreads/tss.c diff -N libstdthreads/tss.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ libstdthreads/tss.c 16 Oct 2016 04:23:02 -0000 @@ -0,0 +1,87 @@ +/* $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 +tss_create(tss_t *key, tss_dtor_t dtor) +{ + + _DIAGASSERT(key != NULL); + + if (pthread_key_create(key, dtor) == 0) + return thrd_success; + + return thrd_errno; +} + +void +tss_delete(tss_t key) +{ + int ret; + + _DIAGASSERT(key != NULL); + + /* The mtx_destroy(3) function returns no value, this forces this code + * to break as there is nothing better available. */ + if ((ret = pthread_key_delete(key)) != 0) + errc(EXIT_FAILURE, ret, "pthread_key_delete failed"); + +} + +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; +}