Index: head/lib/libthr/thread/thr_attr.c =================================================================== --- head/lib/libthr/thread/thr_attr.c (revision 318538) +++ head/lib/libthr/thread/thr_attr.c (revision 318539) @@ -1,651 +1,651 @@ /* * Copyright (c) 2003 Craig Rodrigues . * All rights reserved. * * 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. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Craig Rodrigues. * 4. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY CRAIG RODRIGUES 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 AUTHOR 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. * */ /* * Copyright (c) 1998 Daniel Eischen . * Copyright (C) 2001 Jason Evans . * Copyright (c) 2002,2003 Alexey Zelkin * All rights reserved. * * 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(s), this list of conditions and the following disclaimer * unmodified other than the allowable addition of one or more * copyright notices. * 2. Redistributions in binary form must reproduce the above copyright * notice(s), 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 COPYRIGHT HOLDER(S) ``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 COPYRIGHT HOLDER(S) 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. */ /* * Copyright (c) 1996 John Birrell . * All rights reserved. * * 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. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include "un-namespace.h" #include "thr_private.h" static size_t _get_kern_cpuset_size(void); __weak_reference(_pthread_attr_destroy, pthread_attr_destroy); int _pthread_attr_destroy(pthread_attr_t *attr) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL) /* Invalid argument: */ ret = EINVAL; else { if ((*attr)->cpuset != NULL) free((*attr)->cpuset); /* Free the memory allocated to the attribute object: */ free(*attr); /* * Leave the attribute pointer NULL now that the memory * has been freed: */ *attr = NULL; ret = 0; } return(ret); } __weak_reference(_pthread_attr_get_np, pthread_attr_get_np); int _pthread_attr_get_np(pthread_t pthread, pthread_attr_t *dstattr) { struct pthread *curthread; struct pthread_attr attr, *dst; int ret; size_t kern_size; if (pthread == NULL || dstattr == NULL || (dst = *dstattr) == NULL) return (EINVAL); kern_size = _get_kern_cpuset_size(); if (dst->cpuset == NULL) { dst->cpuset = calloc(1, kern_size); dst->cpusetsize = kern_size; } curthread = _get_curthread(); if ((ret = _thr_find_thread(curthread, pthread, /*include dead*/0)) != 0) return (ret); attr = pthread->attr; if (pthread->flags & THR_FLAGS_DETACHED) attr.flags |= PTHREAD_DETACHED; ret = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, TID(pthread), dst->cpusetsize, dst->cpuset); if (ret == -1) ret = errno; THR_THREAD_UNLOCK(curthread, pthread); if (ret == 0) { memcpy(&dst->pthread_attr_start_copy, &attr.pthread_attr_start_copy, offsetof(struct pthread_attr, pthread_attr_end_copy) - offsetof(struct pthread_attr, pthread_attr_start_copy)); } return (ret); } __weak_reference(_pthread_attr_getdetachstate, pthread_attr_getdetachstate); int _pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || detachstate == NULL) ret = EINVAL; else { /* Check if the detached flag is set: */ if ((*attr)->flags & PTHREAD_DETACHED) /* Return detached: */ *detachstate = PTHREAD_CREATE_DETACHED; else /* Return joinable: */ *detachstate = PTHREAD_CREATE_JOINABLE; ret = 0; } return(ret); } __weak_reference(_pthread_attr_getguardsize, pthread_attr_getguardsize); int _pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || guardsize == NULL) ret = EINVAL; else { /* Return the guard size: */ *guardsize = (*attr)->guardsize_attr; ret = 0; } return(ret); } __weak_reference(_pthread_attr_getinheritsched, pthread_attr_getinheritsched); int _pthread_attr_getinheritsched(const pthread_attr_t *attr, int *sched_inherit) { int ret = 0; if ((attr == NULL) || (*attr == NULL)) ret = EINVAL; else *sched_inherit = (*attr)->sched_inherit; return(ret); } __weak_reference(_pthread_attr_getschedparam, pthread_attr_getschedparam); int _pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param) { int ret = 0; if ((attr == NULL) || (*attr == NULL) || (param == NULL)) ret = EINVAL; else param->sched_priority = (*attr)->prio; return(ret); } __weak_reference(_pthread_attr_getschedpolicy, pthread_attr_getschedpolicy); int _pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) { int ret = 0; if ((attr == NULL) || (*attr == NULL) || (policy == NULL)) ret = EINVAL; else *policy = (*attr)->sched_policy; return(ret); } __weak_reference(_pthread_attr_getscope, pthread_attr_getscope); int _pthread_attr_getscope(const pthread_attr_t *attr, int *contentionscope) { int ret = 0; if ((attr == NULL) || (*attr == NULL) || (contentionscope == NULL)) /* Return an invalid argument: */ ret = EINVAL; else *contentionscope = (*attr)->flags & PTHREAD_SCOPE_SYSTEM ? PTHREAD_SCOPE_SYSTEM : PTHREAD_SCOPE_PROCESS; return(ret); } __weak_reference(_pthread_attr_getstack, pthread_attr_getstack); int _pthread_attr_getstack(const pthread_attr_t * __restrict attr, void ** __restrict stackaddr, size_t * __restrict stacksize) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || stackaddr == NULL || stacksize == NULL ) ret = EINVAL; else { /* Return the stack address and size */ *stackaddr = (*attr)->stackaddr_attr; *stacksize = (*attr)->stacksize_attr; ret = 0; } return(ret); } __weak_reference(_pthread_attr_getstackaddr, pthread_attr_getstackaddr); int _pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || stackaddr == NULL) ret = EINVAL; else { /* Return the stack address: */ *stackaddr = (*attr)->stackaddr_attr; ret = 0; } return(ret); } __weak_reference(_pthread_attr_getstacksize, pthread_attr_getstacksize); int _pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || stacksize == NULL) ret = EINVAL; else { /* Return the stack size: */ *stacksize = (*attr)->stacksize_attr; ret = 0; } return(ret); } __weak_reference(_pthread_attr_init, pthread_attr_init); int _pthread_attr_init(pthread_attr_t *attr) { int ret; pthread_attr_t pattr; _thr_check_init(); /* Allocate memory for the attribute object: */ if ((pattr = (pthread_attr_t) malloc(sizeof(struct pthread_attr))) == NULL) /* Insufficient memory: */ ret = ENOMEM; else { /* Initialise the attribute object with the defaults: */ memcpy(pattr, &_pthread_attr_default, sizeof(struct pthread_attr)); /* Return a pointer to the attribute object: */ *attr = pattr; ret = 0; } return(ret); } __weak_reference(_pthread_attr_setcreatesuspend_np, pthread_attr_setcreatesuspend_np); int _pthread_attr_setcreatesuspend_np(pthread_attr_t *attr) { int ret; if (attr == NULL || *attr == NULL) { ret = EINVAL; } else { (*attr)->suspend = THR_CREATE_SUSPENDED; ret = 0; } return(ret); } __weak_reference(_pthread_attr_setdetachstate, pthread_attr_setdetachstate); int _pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || (detachstate != PTHREAD_CREATE_DETACHED && detachstate != PTHREAD_CREATE_JOINABLE)) ret = EINVAL; else { /* Check if detached state: */ if (detachstate == PTHREAD_CREATE_DETACHED) /* Set the detached flag: */ (*attr)->flags |= PTHREAD_DETACHED; else /* Reset the detached flag: */ (*attr)->flags &= ~PTHREAD_DETACHED; ret = 0; } return(ret); } __weak_reference(_pthread_attr_setguardsize, pthread_attr_setguardsize); int _pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) { int ret; /* Check for invalid arguments. */ if (attr == NULL || *attr == NULL) ret = EINVAL; else { /* Save the stack size. */ (*attr)->guardsize_attr = guardsize; ret = 0; } return(ret); } __weak_reference(_pthread_attr_setinheritsched, pthread_attr_setinheritsched); int _pthread_attr_setinheritsched(pthread_attr_t *attr, int sched_inherit) { int ret = 0; if ((attr == NULL) || (*attr == NULL)) ret = EINVAL; else if (sched_inherit != PTHREAD_INHERIT_SCHED && sched_inherit != PTHREAD_EXPLICIT_SCHED) ret = ENOTSUP; else (*attr)->sched_inherit = sched_inherit; return(ret); } __weak_reference(_pthread_attr_setschedparam, pthread_attr_setschedparam); int _pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param) { int policy; if ((attr == NULL) || (*attr == NULL)) return (EINVAL); if (param == NULL) return (ENOTSUP); policy = (*attr)->sched_policy; if (policy == SCHED_FIFO || policy == SCHED_RR) { if (param->sched_priority < _thr_priorities[policy-1].pri_min || param->sched_priority > _thr_priorities[policy-1].pri_max) return (ENOTSUP); } else { /* * Ignore it for SCHED_OTHER now, patches for glib ports * are wrongly using M:N thread library's internal macro * THR_MIN_PRIORITY and THR_MAX_PRIORITY. */ } (*attr)->prio = param->sched_priority; return (0); } __weak_reference(_pthread_attr_setschedpolicy, pthread_attr_setschedpolicy); int _pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) { int ret = 0; if ((attr == NULL) || (*attr == NULL)) ret = EINVAL; else if ((policy < SCHED_FIFO) || (policy > SCHED_RR)) { ret = ENOTSUP; } else { (*attr)->sched_policy = policy; (*attr)->prio = _thr_priorities[policy-1].pri_default; } return(ret); } __weak_reference(_pthread_attr_setscope, pthread_attr_setscope); int _pthread_attr_setscope(pthread_attr_t *attr, int contentionscope) { int ret = 0; if ((attr == NULL) || (*attr == NULL)) { /* Return an invalid argument: */ ret = EINVAL; } else if ((contentionscope != PTHREAD_SCOPE_PROCESS) && (contentionscope != PTHREAD_SCOPE_SYSTEM)) { ret = EINVAL; } else if (contentionscope == PTHREAD_SCOPE_SYSTEM) { (*attr)->flags |= contentionscope; } else { (*attr)->flags &= ~PTHREAD_SCOPE_SYSTEM; } return (ret); } __weak_reference(_pthread_attr_setstack, pthread_attr_setstack); int _pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || stackaddr == NULL || stacksize < PTHREAD_STACK_MIN) ret = EINVAL; else { /* Save the stack address and stack size */ (*attr)->stackaddr_attr = stackaddr; (*attr)->stacksize_attr = stacksize; ret = 0; } return(ret); } __weak_reference(_pthread_attr_setstackaddr, pthread_attr_setstackaddr); int _pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || stackaddr == NULL) ret = EINVAL; else { /* Save the stack address: */ (*attr)->stackaddr_attr = stackaddr; ret = 0; } return(ret); } __weak_reference(_pthread_attr_setstacksize, pthread_attr_setstacksize); int _pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize) { int ret; /* Check for invalid arguments: */ if (attr == NULL || *attr == NULL || stacksize < PTHREAD_STACK_MIN) ret = EINVAL; else { /* Save the stack size: */ (*attr)->stacksize_attr = stacksize; ret = 0; } return(ret); } static size_t _get_kern_cpuset_size(void) { static int kern_cpuset_size = 0; if (kern_cpuset_size == 0) { size_t len; len = sizeof(kern_cpuset_size); if (sysctlbyname("kern.sched.cpusetsize", &kern_cpuset_size, &len, NULL, 0)) PANIC("failed to get sysctl kern.sched.cpusetsize"); } return (kern_cpuset_size); } __weak_reference(_pthread_attr_setaffinity_np, pthread_attr_setaffinity_np); int _pthread_attr_setaffinity_np(pthread_attr_t *pattr, size_t cpusetsize, const cpuset_t *cpusetp) { pthread_attr_t attr; int ret; if (pattr == NULL || (attr = (*pattr)) == NULL) ret = EINVAL; else { if (cpusetsize == 0 || cpusetp == NULL) { if (attr->cpuset != NULL) { free(attr->cpuset); attr->cpuset = NULL; attr->cpusetsize = 0; } return (0); } size_t kern_size = _get_kern_cpuset_size(); /* Kernel rejects small set, we check it here too. */ if (cpusetsize < kern_size) return (ERANGE); if (cpusetsize > kern_size) { /* Kernel checks invalid bits, we check it here too. */ size_t i; for (i = kern_size; i < cpusetsize; ++i) { - if (((char *)cpusetp)[i]) + if (((const char *)cpusetp)[i]) return (EINVAL); } } if (attr->cpuset == NULL) { attr->cpuset = calloc(1, kern_size); if (attr->cpuset == NULL) return (errno); attr->cpusetsize = kern_size; } memcpy(attr->cpuset, cpusetp, kern_size); ret = 0; } return (ret); } __weak_reference(_pthread_attr_getaffinity_np, pthread_attr_getaffinity_np); int _pthread_attr_getaffinity_np(const pthread_attr_t *pattr, size_t cpusetsize, cpuset_t *cpusetp) { pthread_attr_t attr; int ret = 0; if (pattr == NULL || (attr = (*pattr)) == NULL) ret = EINVAL; else { /* Kernel rejects small set, we check it here too. */ size_t kern_size = _get_kern_cpuset_size(); if (cpusetsize < kern_size) return (ERANGE); if (attr->cpuset != NULL) memcpy(cpusetp, attr->cpuset, MIN(cpusetsize, attr->cpusetsize)); else memset(cpusetp, -1, kern_size); if (cpusetsize > kern_size) memset(((char *)cpusetp) + kern_size, 0, cpusetsize - kern_size); } return (ret); } Index: head/lib/libthr/thread/thr_exit.c =================================================================== --- head/lib/libthr/thread/thr_exit.c (revision 318538) +++ head/lib/libthr/thread/thr_exit.c (revision 318539) @@ -1,325 +1,326 @@ /* * Copyright (c) 1995-1998 John Birrell * All rights reserved. * * 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. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include "namespace.h" #include #ifdef _PTHREAD_FORCED_UNWIND #include #endif #include #include #include #include #include #include #include "un-namespace.h" #include "libc_private.h" #include "thr_private.h" void _pthread_exit(void *status); static void exit_thread(void) __dead2; __weak_reference(_pthread_exit, pthread_exit); #ifdef _PTHREAD_FORCED_UNWIND static int message_printed; static void thread_unwind(void) __dead2; #ifdef PIC static void thread_uw_init(void); static _Unwind_Reason_Code thread_unwind_stop(int version, _Unwind_Action actions, int64_t exc_class, struct _Unwind_Exception *exc_obj, struct _Unwind_Context *context, void *stop_parameter); /* unwind library pointers */ static _Unwind_Reason_Code (*uwl_forcedunwind)(struct _Unwind_Exception *, _Unwind_Stop_Fn, void *); static unsigned long (*uwl_getcfa)(struct _Unwind_Context *); static void thread_uw_init(void) { static int inited = 0; Dl_info dlinfo; void *handle; void *forcedunwind, *getcfa; if (inited) return; handle = RTLD_DEFAULT; if ((forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind")) != NULL) { if (dladdr(forcedunwind, &dlinfo)) { /* * Make sure the address is always valid by holding the library, * also assume functions are in same library. */ if ((handle = dlopen(dlinfo.dli_fname, RTLD_LAZY)) != NULL) { forcedunwind = dlsym(handle, "_Unwind_ForcedUnwind"); getcfa = dlsym(handle, "_Unwind_GetCFA"); if (forcedunwind != NULL && getcfa != NULL) { uwl_getcfa = getcfa; atomic_store_rel_ptr((volatile void *)&uwl_forcedunwind, (uintptr_t)forcedunwind); } else { dlclose(handle); } } } } inited = 1; } _Unwind_Reason_Code _Unwind_ForcedUnwind(struct _Unwind_Exception *ex, _Unwind_Stop_Fn stop_func, void *stop_arg) { return (*uwl_forcedunwind)(ex, stop_func, stop_arg); } unsigned long _Unwind_GetCFA(struct _Unwind_Context *context) { return (*uwl_getcfa)(context); } #else #pragma weak _Unwind_GetCFA #pragma weak _Unwind_ForcedUnwind #endif /* PIC */ static void -thread_unwind_cleanup(_Unwind_Reason_Code code, struct _Unwind_Exception *e) +thread_unwind_cleanup(_Unwind_Reason_Code code __unused, + struct _Unwind_Exception *e __unused) { /* * Specification said that _Unwind_Resume should not be used here, * instead, user should rethrow the exception. For C++ user, they * should put "throw" sentence in catch(...) block. */ PANIC("exception should be rethrown"); } static _Unwind_Reason_Code -thread_unwind_stop(int version, _Unwind_Action actions, - int64_t exc_class, - struct _Unwind_Exception *exc_obj, - struct _Unwind_Context *context, void *stop_parameter) +thread_unwind_stop(int version __unused, _Unwind_Action actions, + int64_t exc_class __unused, + struct _Unwind_Exception *exc_obj __unused, + struct _Unwind_Context *context, void *stop_parameter __unused) { struct pthread *curthread = _get_curthread(); struct pthread_cleanup *cur; uintptr_t cfa; int done = 0; /* XXX assume stack grows down to lower address */ cfa = _Unwind_GetCFA(context); if (actions & _UA_END_OF_STACK || cfa >= (uintptr_t)curthread->unwind_stackend) { done = 1; } while ((cur = curthread->cleanup) != NULL && (done || (uintptr_t)cur <= cfa)) { __pthread_cleanup_pop_imp(1); } if (done) { /* Tell libc that it should call non-trivial TLS dtors. */ __cxa_thread_call_dtors(); exit_thread(); /* Never return! */ } return (_URC_NO_REASON); } static void thread_unwind(void) { struct pthread *curthread = _get_curthread(); curthread->ex.exception_class = 0; curthread->ex.exception_cleanup = thread_unwind_cleanup; _Unwind_ForcedUnwind(&curthread->ex, thread_unwind_stop, NULL); PANIC("_Unwind_ForcedUnwind returned"); } #endif void _thread_exitf(const char *fname, int lineno, const char *fmt, ...) { va_list ap; /* Write an error message to the standard error file descriptor: */ _thread_printf(STDERR_FILENO, "Fatal error '"); va_start(ap, fmt); _thread_vprintf(STDERR_FILENO, fmt, ap); va_end(ap); _thread_printf(STDERR_FILENO, "' at line %d in file %s (errno = %d)\n", lineno, fname, errno); abort(); } void _thread_exit(const char *fname, int lineno, const char *msg) { _thread_exitf(fname, lineno, "%s", msg); } void _pthread_exit(void *status) { _pthread_exit_mask(status, NULL); } void _pthread_exit_mask(void *status, sigset_t *mask) { struct pthread *curthread = _get_curthread(); /* Check if this thread is already in the process of exiting: */ if (curthread->cancelling) PANIC("Thread %p has called " "pthread_exit() from a destructor. POSIX 1003.1 " "1996 s16.2.5.2 does not allow this!", curthread); /* Flag this thread as exiting. */ curthread->cancelling = 1; curthread->no_cancel = 1; curthread->cancel_async = 0; curthread->cancel_point = 0; if (mask != NULL) __sys_sigprocmask(SIG_SETMASK, mask, NULL); if (curthread->unblock_sigcancel) { sigset_t set; curthread->unblock_sigcancel = 0; SIGEMPTYSET(set); SIGADDSET(set, SIGCANCEL); __sys_sigprocmask(SIG_UNBLOCK, mask, NULL); } /* Save the return value: */ curthread->ret = status; #ifdef _PTHREAD_FORCED_UNWIND #ifdef PIC thread_uw_init(); if (uwl_forcedunwind != NULL) { #else if (_Unwind_ForcedUnwind != NULL) { #endif if (curthread->unwind_disabled) { if (message_printed == 0) { message_printed = 1; _thread_printf(2, "Warning: old _pthread_cleanup_push was called, " "stack unwinding is disabled.\n"); } goto cleanup; } thread_unwind(); } else { cleanup: while (curthread->cleanup != NULL) { __pthread_cleanup_pop_imp(1); } __cxa_thread_call_dtors(); exit_thread(); } #else while (curthread->cleanup != NULL) { __pthread_cleanup_pop_imp(1); } __cxa_thread_call_dtors(); exit_thread(); #endif /* _PTHREAD_FORCED_UNWIND */ } static void exit_thread(void) { struct pthread *curthread = _get_curthread(); /* Check if there is thread specific data: */ if (curthread->specific != NULL) { /* Run the thread-specific data destructors: */ _thread_cleanupspecific(); } if (!_thr_isthreaded()) exit(0); if (atomic_fetchadd_int(&_thread_active_threads, -1) == 1) { exit(0); /* Never reach! */ } /* Tell malloc that the thread is exiting. */ _malloc_thread_cleanup(); THR_LOCK(curthread); curthread->state = PS_DEAD; if (curthread->flags & THR_FLAGS_NEED_SUSPEND) { curthread->cycle++; _thr_umtx_wake(&curthread->cycle, INT_MAX, 0); } if (!curthread->force_exit && SHOULD_REPORT_EVENT(curthread, TD_DEATH)) _thr_report_death(curthread); /* * Thread was created with initial refcount 1, we drop the * reference count to allow it to be garbage collected. */ curthread->refcount--; _thr_try_gc(curthread, curthread); /* thread lock released */ #if defined(_PTHREADS_INVARIANTS) if (THR_IN_CRITICAL(curthread)) PANIC("thread %p exits with resources held!", curthread); #endif /* * Kernel will do wakeup at the address, so joiner thread * will be resumed if it is sleeping at the address. */ thr_exit(&curthread->tid); PANIC("thr_exit() returned"); /* Never reach! */ } Index: head/lib/libthr/thread/thr_sig.c =================================================================== --- head/lib/libthr/thread/thr_sig.c (revision 318538) +++ head/lib/libthr/thread/thr_sig.c (revision 318539) @@ -1,761 +1,761 @@ /* * Copyright (c) 2005, David Xu * All rights reserved. * * 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 unmodified, 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 AUTHOR ``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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include #include #include #include "un-namespace.h" #include "libc_private.h" #include "libc_private.h" #include "thr_private.h" /* #define DEBUG_SIGNAL */ #ifdef DEBUG_SIGNAL #define DBG_MSG stdout_debug #else #define DBG_MSG(x...) #endif struct usigaction { struct sigaction sigact; struct urwlock lock; }; static struct usigaction _thr_sigact[_SIG_MAXSIG]; static inline struct usigaction * __libc_sigaction_slot(int signo) { return (&_thr_sigact[signo - 1]); } static void thr_sighandler(int, siginfo_t *, void *); static void handle_signal(struct sigaction *, int, siginfo_t *, ucontext_t *); static void check_deferred_signal(struct pthread *); static void check_suspend(struct pthread *); static void check_cancel(struct pthread *curthread, ucontext_t *ucp); int _sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec * timeout); int _sigwaitinfo(const sigset_t *set, siginfo_t *info); int _sigwait(const sigset_t *set, int *sig); int _setcontext(const ucontext_t *); int _swapcontext(ucontext_t *, const ucontext_t *); static const sigset_t _thr_deferset={{ 0xffffffff & ~(_SIG_BIT(SIGBUS)|_SIG_BIT(SIGILL)|_SIG_BIT(SIGFPE)| _SIG_BIT(SIGSEGV)|_SIG_BIT(SIGTRAP)|_SIG_BIT(SIGSYS)), 0xffffffff, 0xffffffff, 0xffffffff}}; static const sigset_t _thr_maskset={{ 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}}; void _thr_signal_block(struct pthread *curthread) { if (curthread->sigblock > 0) { curthread->sigblock++; return; } __sys_sigprocmask(SIG_BLOCK, &_thr_maskset, &curthread->sigmask); curthread->sigblock++; } void _thr_signal_unblock(struct pthread *curthread) { if (--curthread->sigblock == 0) __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL); } int _thr_send_sig(struct pthread *thread, int sig) { return thr_kill(thread->tid, sig); } static inline void remove_thr_signals(sigset_t *set) { if (SIGISMEMBER(*set, SIGCANCEL)) SIGDELSET(*set, SIGCANCEL); } static const sigset_t * thr_remove_thr_signals(const sigset_t *set, sigset_t *newset) { *newset = *set; remove_thr_signals(newset); return (newset); } static void sigcancel_handler(int sig __unused, siginfo_t *info __unused, ucontext_t *ucp) { struct pthread *curthread = _get_curthread(); int err; if (THR_IN_CRITICAL(curthread)) return; err = errno; check_suspend(curthread); check_cancel(curthread, ucp); errno = err; } typedef void (*ohandler)(int sig, int code, struct sigcontext *scp, char *addr, __sighandler_t *catcher); /* * The signal handler wrapper is entered with all signal masked. */ static void thr_sighandler(int sig, siginfo_t *info, void *_ucp) { struct pthread *curthread; ucontext_t *ucp; struct sigaction act; struct usigaction *usa; int err; err = errno; curthread = _get_curthread(); ucp = _ucp; usa = __libc_sigaction_slot(sig); _thr_rwl_rdlock(&usa->lock); act = usa->sigact; _thr_rwl_unlock(&usa->lock); errno = err; curthread->deferred_run = 0; /* * if a thread is in critical region, for example it holds low level locks, * try to defer the signal processing, however if the signal is synchronous * signal, it means a bad thing has happened, this is a programming error, * resuming fault point can not help anything (normally causes deadloop), * so here we let user code handle it immediately. */ if (THR_IN_CRITICAL(curthread) && SIGISMEMBER(_thr_deferset, sig)) { memcpy(&curthread->deferred_sigact, &act, sizeof(struct sigaction)); memcpy(&curthread->deferred_siginfo, info, sizeof(siginfo_t)); curthread->deferred_sigmask = ucp->uc_sigmask; /* mask all signals, we will restore it later. */ ucp->uc_sigmask = _thr_deferset; return; } handle_signal(&act, sig, info, ucp); } static void handle_signal(struct sigaction *actp, int sig, siginfo_t *info, ucontext_t *ucp) { struct pthread *curthread = _get_curthread(); ucontext_t uc2; __siginfohandler_t *sigfunc; int cancel_point; int cancel_async; int cancel_enable; int in_sigsuspend; int err; /* add previous level mask */ SIGSETOR(actp->sa_mask, ucp->uc_sigmask); /* add this signal's mask */ if (!(actp->sa_flags & SA_NODEFER)) SIGADDSET(actp->sa_mask, sig); in_sigsuspend = curthread->in_sigsuspend; curthread->in_sigsuspend = 0; /* * If thread is in deferred cancellation mode, disable cancellation * in signal handler. * If user signal handler calls a cancellation point function, e.g, * it calls write() to write data to file, because write() is a * cancellation point, the thread is immediately cancelled if * cancellation is pending, to avoid this problem while thread is in * deferring mode, cancellation is temporarily disabled. */ cancel_point = curthread->cancel_point; cancel_async = curthread->cancel_async; cancel_enable = curthread->cancel_enable; curthread->cancel_point = 0; if (!cancel_async) curthread->cancel_enable = 0; /* restore correct mask before calling user handler */ __sys_sigprocmask(SIG_SETMASK, &actp->sa_mask, NULL); sigfunc = actp->sa_sigaction; /* * We have already reset cancellation point flags, so if user's code * longjmp()s out of its signal handler, wish its jmpbuf was set * outside of a cancellation point, in most cases, this would be * true. However, there is no way to save cancel_enable in jmpbuf, * so after setjmps() returns once more, the user code may need to * re-set cancel_enable flag by calling pthread_setcancelstate(). */ if ((actp->sa_flags & SA_SIGINFO) != 0) { sigfunc(sig, info, ucp); } else { ((ohandler)sigfunc)(sig, info->si_code, (struct sigcontext *)ucp, info->si_addr, (__sighandler_t *)sigfunc); } err = errno; curthread->in_sigsuspend = in_sigsuspend; curthread->cancel_point = cancel_point; curthread->cancel_enable = cancel_enable; memcpy(&uc2, ucp, sizeof(uc2)); SIGDELSET(uc2.uc_sigmask, SIGCANCEL); /* reschedule cancellation */ check_cancel(curthread, &uc2); errno = err; syscall(SYS_sigreturn, &uc2); } void _thr_ast(struct pthread *curthread) { if (!THR_IN_CRITICAL(curthread)) { check_deferred_signal(curthread); check_suspend(curthread); check_cancel(curthread, NULL); } } /* reschedule cancellation */ static void check_cancel(struct pthread *curthread, ucontext_t *ucp) { if (__predict_true(!curthread->cancel_pending || !curthread->cancel_enable || curthread->no_cancel)) return; /* * Otherwise, we are in defer mode, and we are at * cancel point, tell kernel to not block the current * thread on next cancelable system call. * * There are three cases we should call thr_wake() to * turn on TDP_WAKEUP or send SIGCANCEL in kernel: * 1) we are going to call a cancelable system call, * non-zero cancel_point means we are already in * cancelable state, next system call is cancelable. * 2) because _thr_ast() may be called by * THR_CRITICAL_LEAVE() which is used by rtld rwlock * and any libthr internal locks, when rtld rwlock * is used, it is mostly caused by an unresolved PLT. * Those routines may clear the TDP_WAKEUP flag by * invoking some system calls, in those cases, we * also should reenable the flag. * 3) thread is in sigsuspend(), and the syscall insists * on getting a signal before it agrees to return. */ if (curthread->cancel_point) { if (curthread->in_sigsuspend && ucp) { SIGADDSET(ucp->uc_sigmask, SIGCANCEL); curthread->unblock_sigcancel = 1; _thr_send_sig(curthread, SIGCANCEL); } else thr_wake(curthread->tid); } else if (curthread->cancel_async) { /* * asynchronous cancellation mode, act upon * immediately. */ _pthread_exit_mask(PTHREAD_CANCELED, ucp? &ucp->uc_sigmask : NULL); } } static void check_deferred_signal(struct pthread *curthread) { ucontext_t *uc; struct sigaction act; siginfo_t info; int uc_len; if (__predict_true(curthread->deferred_siginfo.si_signo == 0 || curthread->deferred_run)) return; curthread->deferred_run = 1; uc_len = __getcontextx_size(); uc = alloca(uc_len); getcontext(uc); if (curthread->deferred_siginfo.si_signo == 0) { curthread->deferred_run = 0; return; } __fillcontextx2((char *)uc); act = curthread->deferred_sigact; uc->uc_sigmask = curthread->deferred_sigmask; memcpy(&info, &curthread->deferred_siginfo, sizeof(siginfo_t)); /* remove signal */ curthread->deferred_siginfo.si_signo = 0; handle_signal(&act, info.si_signo, &info, uc); } static void check_suspend(struct pthread *curthread) { uint32_t cycle; if (__predict_true((curthread->flags & (THR_FLAGS_NEED_SUSPEND | THR_FLAGS_SUSPENDED)) != THR_FLAGS_NEED_SUSPEND)) return; if (curthread == _single_thread) return; if (curthread->force_exit) return; /* * Blocks SIGCANCEL which other threads must send. */ _thr_signal_block(curthread); /* * Increase critical_count, here we don't use THR_LOCK/UNLOCK * because we are leaf code, we don't want to recursively call * ourself. */ curthread->critical_count++; THR_UMUTEX_LOCK(curthread, &(curthread)->lock); while ((curthread->flags & THR_FLAGS_NEED_SUSPEND) != 0) { curthread->cycle++; cycle = curthread->cycle; /* Wake the thread suspending us. */ _thr_umtx_wake(&curthread->cycle, INT_MAX, 0); /* * if we are from pthread_exit, we don't want to * suspend, just go and die. */ if (curthread->state == PS_DEAD) break; curthread->flags |= THR_FLAGS_SUSPENDED; THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); _thr_umtx_wait_uint(&curthread->cycle, cycle, NULL, 0); THR_UMUTEX_LOCK(curthread, &(curthread)->lock); } THR_UMUTEX_UNLOCK(curthread, &(curthread)->lock); curthread->critical_count--; _thr_signal_unblock(curthread); } void _thr_signal_init(int dlopened) { struct sigaction act, nact, oact; struct usigaction *usa; sigset_t oldset; int sig, error; if (dlopened) { __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset); for (sig = 1; sig <= _SIG_MAXSIG; sig++) { if (sig == SIGCANCEL) continue; error = __sys_sigaction(sig, NULL, &oact); if (error == -1 || oact.sa_handler == SIG_DFL || oact.sa_handler == SIG_IGN) continue; usa = __libc_sigaction_slot(sig); usa->sigact = oact; nact = oact; remove_thr_signals(&usa->sigact.sa_mask); nact.sa_flags &= ~SA_NODEFER; nact.sa_flags |= SA_SIGINFO; nact.sa_sigaction = thr_sighandler; nact.sa_mask = _thr_maskset; (void)__sys_sigaction(sig, &nact, NULL); } __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); } /* Install SIGCANCEL handler. */ SIGFILLSET(act.sa_mask); act.sa_flags = SA_SIGINFO; act.sa_sigaction = (__siginfohandler_t *)&sigcancel_handler; __sys_sigaction(SIGCANCEL, &act, NULL); /* Unblock SIGCANCEL */ SIGEMPTYSET(act.sa_mask); SIGADDSET(act.sa_mask, SIGCANCEL); __sys_sigprocmask(SIG_UNBLOCK, &act.sa_mask, NULL); } void -_thr_sigact_unload(struct dl_phdr_info *phdr_info) +_thr_sigact_unload(struct dl_phdr_info *phdr_info __unused) { #if 0 struct pthread *curthread = _get_curthread(); struct urwlock *rwlp; struct sigaction *actp; struct usigaction *usa; struct sigaction kact; void (*handler)(int); int sig; _thr_signal_block(curthread); for (sig = 1; sig <= _SIG_MAXSIG; sig++) { usa = __libc_sigaction_slot(sig); actp = &usa->sigact; retry: handler = actp->sa_handler; if (handler != SIG_DFL && handler != SIG_IGN && __elf_phdr_match_addr(phdr_info, handler)) { rwlp = &usa->lock; _thr_rwl_wrlock(rwlp); if (handler != actp->sa_handler) { _thr_rwl_unlock(rwlp); goto retry; } actp->sa_handler = SIG_DFL; actp->sa_flags = SA_SIGINFO; SIGEMPTYSET(actp->sa_mask); if (__sys_sigaction(sig, NULL, &kact) == 0 && kact.sa_handler != SIG_DFL && kact.sa_handler != SIG_IGN) __sys_sigaction(sig, actp, NULL); _thr_rwl_unlock(rwlp); } } _thr_signal_unblock(curthread); #endif } void _thr_signal_prefork(void) { int i; for (i = 1; i <= _SIG_MAXSIG; ++i) _thr_rwl_rdlock(&__libc_sigaction_slot(i)->lock); } void _thr_signal_postfork(void) { int i; for (i = 1; i <= _SIG_MAXSIG; ++i) _thr_rwl_unlock(&__libc_sigaction_slot(i)->lock); } void _thr_signal_postfork_child(void) { int i; for (i = 1; i <= _SIG_MAXSIG; ++i) { bzero(&__libc_sigaction_slot(i) -> lock, sizeof(struct urwlock)); } } void _thr_signal_deinit(void) { } int __thr_sigaction(int sig, const struct sigaction *act, struct sigaction *oact) { struct sigaction newact, oldact, oldact2; sigset_t oldset; struct usigaction *usa; int ret, err; if (!_SIG_VALID(sig) || sig == SIGCANCEL) { errno = EINVAL; return (-1); } ret = 0; err = 0; usa = __libc_sigaction_slot(sig); __sys_sigprocmask(SIG_SETMASK, &_thr_maskset, &oldset); _thr_rwl_wrlock(&usa->lock); if (act != NULL) { oldact2 = usa->sigact; newact = *act; /* * if a new sig handler is SIG_DFL or SIG_IGN, * don't remove old handler from __libc_sigact[], * so deferred signals still can use the handlers, * multiple threads invoking sigaction itself is * a race condition, so it is not a problem. */ if (newact.sa_handler != SIG_DFL && newact.sa_handler != SIG_IGN) { usa->sigact = newact; remove_thr_signals(&usa->sigact.sa_mask); newact.sa_flags &= ~SA_NODEFER; newact.sa_flags |= SA_SIGINFO; newact.sa_sigaction = thr_sighandler; newact.sa_mask = _thr_maskset; /* mask all signals */ } ret = __sys_sigaction(sig, &newact, &oldact); if (ret == -1) { err = errno; usa->sigact = oldact2; } } else if (oact != NULL) { ret = __sys_sigaction(sig, NULL, &oldact); err = errno; } if (oldact.sa_handler != SIG_DFL && oldact.sa_handler != SIG_IGN) { if (act != NULL) oldact = oldact2; else if (oact != NULL) oldact = usa->sigact; } _thr_rwl_unlock(&usa->lock); __sys_sigprocmask(SIG_SETMASK, &oldset, NULL); if (ret == 0) { if (oact != NULL) *oact = oldact; } else { errno = err; } return (ret); } int __thr_sigprocmask(int how, const sigset_t *set, sigset_t *oset) { const sigset_t *p = set; sigset_t newset; if (how != SIG_UNBLOCK) { if (set != NULL) { newset = *set; SIGDELSET(newset, SIGCANCEL); p = &newset; } } return (__sys_sigprocmask(how, p, oset)); } __weak_reference(_pthread_sigmask, pthread_sigmask); int _pthread_sigmask(int how, const sigset_t *set, sigset_t *oset) { if (__thr_sigprocmask(how, set, oset)) return (errno); return (0); } int _sigsuspend(const sigset_t * set) { sigset_t newset; return (__sys_sigsuspend(thr_remove_thr_signals(set, &newset))); } int __thr_sigsuspend(const sigset_t * set) { struct pthread *curthread; sigset_t newset; int ret, old; curthread = _get_curthread(); old = curthread->in_sigsuspend; curthread->in_sigsuspend = 1; _thr_cancel_enter(curthread); ret = __sys_sigsuspend(thr_remove_thr_signals(set, &newset)); _thr_cancel_leave(curthread, 1); curthread->in_sigsuspend = old; if (curthread->unblock_sigcancel) { curthread->unblock_sigcancel = 0; SIGEMPTYSET(newset); SIGADDSET(newset, SIGCANCEL); __sys_sigprocmask(SIG_UNBLOCK, &newset, NULL); } return (ret); } int _sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec * timeout) { sigset_t newset; return (__sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info, timeout)); } /* * Cancellation behavior: * Thread may be canceled at start, if thread got signal, * it is not canceled. */ int __thr_sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec * timeout) { struct pthread *curthread = _get_curthread(); sigset_t newset; int ret; _thr_cancel_enter(curthread); ret = __sys_sigtimedwait(thr_remove_thr_signals(set, &newset), info, timeout); _thr_cancel_leave(curthread, (ret == -1)); return (ret); } int _sigwaitinfo(const sigset_t *set, siginfo_t *info) { sigset_t newset; return (__sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info)); } /* * Cancellation behavior: * Thread may be canceled at start, if thread got signal, * it is not canceled. */ int __thr_sigwaitinfo(const sigset_t *set, siginfo_t *info) { struct pthread *curthread = _get_curthread(); sigset_t newset; int ret; _thr_cancel_enter(curthread); ret = __sys_sigwaitinfo(thr_remove_thr_signals(set, &newset), info); _thr_cancel_leave(curthread, ret == -1); return (ret); } int _sigwait(const sigset_t *set, int *sig) { sigset_t newset; return (__sys_sigwait(thr_remove_thr_signals(set, &newset), sig)); } /* * Cancellation behavior: * Thread may be canceled at start, if thread got signal, * it is not canceled. */ int __thr_sigwait(const sigset_t *set, int *sig) { struct pthread *curthread = _get_curthread(); sigset_t newset; int ret; do { _thr_cancel_enter(curthread); ret = __sys_sigwait(thr_remove_thr_signals(set, &newset), sig); _thr_cancel_leave(curthread, (ret != 0)); } while (ret == EINTR); return (ret); } int __thr_setcontext(const ucontext_t *ucp) { ucontext_t uc; if (ucp == NULL) { errno = EINVAL; return (-1); } if (!SIGISMEMBER(uc.uc_sigmask, SIGCANCEL)) return __sys_setcontext(ucp); (void) memcpy(&uc, ucp, sizeof(uc)); SIGDELSET(uc.uc_sigmask, SIGCANCEL); return (__sys_setcontext(&uc)); } int __thr_swapcontext(ucontext_t *oucp, const ucontext_t *ucp) { ucontext_t uc; if (oucp == NULL || ucp == NULL) { errno = EINVAL; return (-1); } if (SIGISMEMBER(ucp->uc_sigmask, SIGCANCEL)) { (void) memcpy(&uc, ucp, sizeof(uc)); SIGDELSET(uc.uc_sigmask, SIGCANCEL); ucp = &uc; } return (__sys_swapcontext(oucp, ucp)); } Index: head/lib/libthr/thread/thr_spec.c =================================================================== --- head/lib/libthr/thread/thr_spec.c (revision 318538) +++ head/lib/libthr/thread/thr_spec.c (revision 318539) @@ -1,243 +1,243 @@ /* * Copyright (c) 1995 John Birrell . * All rights reserved. * * 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. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include "namespace.h" #include #include #include #include #include #include #include "un-namespace.h" #include "libc_private.h" #include "thr_private.h" -struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX]; +static struct pthread_key _thread_keytable[PTHREAD_KEYS_MAX]; __weak_reference(_pthread_key_create, pthread_key_create); __weak_reference(_pthread_key_delete, pthread_key_delete); __weak_reference(_pthread_getspecific, pthread_getspecific); __weak_reference(_pthread_setspecific, pthread_setspecific); int _pthread_key_create(pthread_key_t *key, void (*destructor)(void *)) { struct pthread *curthread; int i; _thr_check_init(); curthread = _get_curthread(); THR_LOCK_ACQUIRE(curthread, &_keytable_lock); for (i = 0; i < PTHREAD_KEYS_MAX; i++) { if (_thread_keytable[i].allocated == 0) { _thread_keytable[i].allocated = 1; _thread_keytable[i].destructor = destructor; _thread_keytable[i].seqno++; THR_LOCK_RELEASE(curthread, &_keytable_lock); *key = i + 1; return (0); } } THR_LOCK_RELEASE(curthread, &_keytable_lock); return (EAGAIN); } int _pthread_key_delete(pthread_key_t userkey) { struct pthread *curthread; int key, ret; key = userkey - 1; if ((unsigned int)key >= PTHREAD_KEYS_MAX) return (EINVAL); curthread = _get_curthread(); THR_LOCK_ACQUIRE(curthread, &_keytable_lock); if (_thread_keytable[key].allocated) { _thread_keytable[key].allocated = 0; ret = 0; } else { ret = EINVAL; } THR_LOCK_RELEASE(curthread, &_keytable_lock); return (ret); } void _thread_cleanupspecific(void) { struct pthread *curthread; void (*destructor)(void *); const void *data; int i, key; curthread = _get_curthread(); if (curthread->specific == NULL) return; THR_LOCK_ACQUIRE(curthread, &_keytable_lock); for (i = 0; i < PTHREAD_DESTRUCTOR_ITERATIONS && curthread->specific_data_count > 0; i++) { for (key = 0; key < PTHREAD_KEYS_MAX && curthread->specific_data_count > 0; key++) { destructor = NULL; if (_thread_keytable[key].allocated && (curthread->specific[key].data != NULL)) { if (curthread->specific[key].seqno == _thread_keytable[key].seqno) { data = curthread->specific[key].data; destructor = _thread_keytable[key]. destructor; } curthread->specific[key].data = NULL; curthread->specific_data_count--; } else if (curthread->specific[key].data != NULL) { /* * This can happen if the key is * deleted via pthread_key_delete * without first setting the value to * NULL in all threads. POSIX says * that the destructor is not invoked * in this case. */ curthread->specific[key].data = NULL; curthread->specific_data_count--; } /* * If there is a destructor, call it with the * key table entry unlocked. */ if (destructor != NULL) { THR_LOCK_RELEASE(curthread, &_keytable_lock); destructor(__DECONST(void *, data)); THR_LOCK_ACQUIRE(curthread, &_keytable_lock); } } } THR_LOCK_RELEASE(curthread, &_keytable_lock); munmap(curthread->specific, PTHREAD_KEYS_MAX * sizeof(struct pthread_specific_elem)); curthread->specific = NULL; if (curthread->specific_data_count > 0) { stderr_debug("Thread %p has exited with leftover " "thread-specific data after %d destructor iterations\n", curthread, PTHREAD_DESTRUCTOR_ITERATIONS); } } int _pthread_setspecific(pthread_key_t userkey, const void *value) { struct pthread *pthread; void *tmp; pthread_key_t key; key = userkey - 1; if ((unsigned int)key >= PTHREAD_KEYS_MAX || !_thread_keytable[key].allocated) return (EINVAL); pthread = _get_curthread(); if (pthread->specific == NULL) { tmp = mmap(NULL, PTHREAD_KEYS_MAX * sizeof(struct pthread_specific_elem), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (tmp == MAP_FAILED) return (ENOMEM); pthread->specific = tmp; } if (pthread->specific[key].data == NULL) { if (value != NULL) pthread->specific_data_count++; } else if (value == NULL) pthread->specific_data_count--; pthread->specific[key].data = value; pthread->specific[key].seqno = _thread_keytable[key].seqno; return (0); } void * _pthread_getspecific(pthread_key_t userkey) { struct pthread *pthread; const void *data; pthread_key_t key; /* Check if there is specific data. */ key = userkey - 1; if ((unsigned int)key >= PTHREAD_KEYS_MAX) return (NULL); pthread = _get_curthread(); /* Check if this key has been used before. */ if (_thread_keytable[key].allocated && pthread->specific != NULL && pthread->specific[key].seqno == _thread_keytable[key].seqno) { /* Return the value: */ data = pthread->specific[key].data; } else { /* * This key has not been used before, so return NULL * instead. */ data = NULL; } return (__DECONST(void *, data)); } void _thr_tsd_unload(struct dl_phdr_info *phdr_info) { struct pthread *curthread; void (*destructor)(void *); int key; curthread = _get_curthread(); THR_LOCK_ACQUIRE(curthread, &_keytable_lock); for (key = 0; key < PTHREAD_KEYS_MAX; key++) { if (!_thread_keytable[key].allocated) continue; destructor = _thread_keytable[key].destructor; if (destructor == NULL) continue; if (__elf_phdr_match_addr(phdr_info, destructor)) _thread_keytable[key].destructor = NULL; } THR_LOCK_RELEASE(curthread, &_keytable_lock); } Index: head/lib/libthr/thread/thr_stack.c =================================================================== --- head/lib/libthr/thread/thr_stack.c (revision 318538) +++ head/lib/libthr/thread/thr_stack.c (revision 318539) @@ -1,318 +1,335 @@ /* * Copyright (c) 2001 Daniel Eischen * Copyright (c) 2000-2001 Jason Evans * All rights reserved. * * 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 AUTHORS 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 AUTHORS 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include "thr_private.h" /* Spare thread stack. */ struct stack { LIST_ENTRY(stack) qe; /* Stack queue linkage. */ size_t stacksize; /* Stack size (rounded up). */ size_t guardsize; /* Guard size. */ void *stackaddr; /* Stack address. */ }; /* * Default sized (stack and guard) spare stack queue. Stacks are cached * to avoid additional complexity managing mmap()ed stack regions. Spare * stacks are used in LIFO order to increase cache locality. */ static LIST_HEAD(, stack) dstackq = LIST_HEAD_INITIALIZER(dstackq); /* * Miscellaneous sized (non-default stack and/or guard) spare stack queue. * Stacks are cached to avoid additional complexity managing mmap()ed * stack regions. This list is unordered, since ordering on both stack * size and guard size would be more trouble than it's worth. Stacks are * allocated from this cache on a first size match basis. */ static LIST_HEAD(, stack) mstackq = LIST_HEAD_INITIALIZER(mstackq); /** * Base address of the last stack allocated (including its red zone, if * there is one). Stacks are allocated contiguously, starting beyond the * top of the main stack. When a new stack is created, a red zone is * typically created (actually, the red zone is mapped with PROT_NONE) above * the top of the stack, such that the stack will not be able to grow all * the way to the bottom of the next stack. This isn't fool-proof. It is * possible for a stack to grow by a large amount, such that it grows into * the next stack, and as long as the memory within the red zone is never * accessed, nothing will prevent one thread stack from trouncing all over * the next. * * low memory * . . . . . . . . . . . . . . . . . . * | | * | stack 3 | start of 3rd thread stack * +-----------------------------------+ * | | * | Red Zone (guard page) | red zone for 2nd thread * | | * +-----------------------------------+ * | stack 2 - _thr_stack_default | top of 2nd thread stack * | | * | | * | | * | | * | stack 2 | * +-----------------------------------+ <-- start of 2nd thread stack * | | * | Red Zone | red zone for 1st thread * | | * +-----------------------------------+ * | stack 1 - _thr_stack_default | top of 1st thread stack * | | * | | * | | * | | * | stack 1 | * +-----------------------------------+ <-- start of 1st thread stack * | | (initial value of last_stack) * | Red Zone | * | | red zone for main thread * +-----------------------------------+ * | USRSTACK - _thr_stack_initial | top of main thread stack * | | ^ * | | | * | | | * | | | stack growth * | | * +-----------------------------------+ <-- start of main thread stack * (USRSTACK) * high memory * */ static char *last_stack = NULL; /* * Round size up to the nearest multiple of * _thr_page_size. */ static inline size_t round_up(size_t size) { if (size % _thr_page_size != 0) size = ((size / _thr_page_size) + 1) * _thr_page_size; return size; } void _thr_stack_fix_protection(struct pthread *thrd) { mprotect((char *)thrd->attr.stackaddr_attr + round_up(thrd->attr.guardsize_attr), round_up(thrd->attr.stacksize_attr), _rtld_get_stack_prot()); } static void singlethread_map_stacks_exec(void) { int mib[2]; struct rlimit rlim; u_long usrstack; size_t len; mib[0] = CTL_KERN; mib[1] = KERN_USRSTACK; len = sizeof(usrstack); if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &usrstack, &len, NULL, 0) == -1) return; if (getrlimit(RLIMIT_STACK, &rlim) == -1) return; mprotect((void *)(uintptr_t)(usrstack - rlim.rlim_cur), rlim.rlim_cur, _rtld_get_stack_prot()); } void __thr_map_stacks_exec(void) { struct pthread *curthread, *thrd; struct stack *st; if (!_thr_is_inited()) { singlethread_map_stacks_exec(); return; } curthread = _get_curthread(); THREAD_LIST_RDLOCK(curthread); LIST_FOREACH(st, &mstackq, qe) mprotect((char *)st->stackaddr + st->guardsize, st->stacksize, _rtld_get_stack_prot()); LIST_FOREACH(st, &dstackq, qe) mprotect((char *)st->stackaddr + st->guardsize, st->stacksize, _rtld_get_stack_prot()); TAILQ_FOREACH(thrd, &_thread_gc_list, gcle) _thr_stack_fix_protection(thrd); TAILQ_FOREACH(thrd, &_thread_list, tle) _thr_stack_fix_protection(thrd); THREAD_LIST_UNLOCK(curthread); } int _thr_stack_alloc(struct pthread_attr *attr) { struct pthread *curthread = _get_curthread(); struct stack *spare_stack; size_t stacksize; size_t guardsize; char *stackaddr; /* * Round up stack size to nearest multiple of _thr_page_size so * that mmap() * will work. If the stack size is not an even * multiple, we end up initializing things such that there is * unused space above the beginning of the stack, so the stack * sits snugly against its guard. */ stacksize = round_up(attr->stacksize_attr); guardsize = round_up(attr->guardsize_attr); attr->stackaddr_attr = NULL; attr->flags &= ~THR_STACK_USER; /* * Use the garbage collector lock for synchronization of the * spare stack lists and allocations from usrstack. */ THREAD_LIST_WRLOCK(curthread); /* * If the stack and guard sizes are default, try to allocate a stack * from the default-size stack cache: */ if ((stacksize == THR_STACK_DEFAULT) && (guardsize == _thr_guard_default)) { if ((spare_stack = LIST_FIRST(&dstackq)) != NULL) { /* Use the spare stack. */ LIST_REMOVE(spare_stack, qe); attr->stackaddr_attr = spare_stack->stackaddr; } } /* * The user specified a non-default stack and/or guard size, so try to * allocate a stack from the non-default size stack cache, using the * rounded up stack size (stack_size) in the search: */ else { LIST_FOREACH(spare_stack, &mstackq, qe) { if (spare_stack->stacksize == stacksize && spare_stack->guardsize == guardsize) { LIST_REMOVE(spare_stack, qe); attr->stackaddr_attr = spare_stack->stackaddr; break; } } } if (attr->stackaddr_attr != NULL) { /* A cached stack was found. Release the lock. */ THREAD_LIST_UNLOCK(curthread); } else { /* * Allocate a stack from or below usrstack, depending * on the LIBPTHREAD_BIGSTACK_MAIN env variable. */ if (last_stack == NULL) last_stack = _usrstack - _thr_stack_initial - _thr_guard_default; /* Allocate a new stack. */ stackaddr = last_stack - stacksize - guardsize; /* * Even if stack allocation fails, we don't want to try to * use this location again, so unconditionally decrement * last_stack. Under normal operating conditions, the most * likely reason for an mmap() error is a stack overflow of * the adjacent thread stack. */ last_stack -= (stacksize + guardsize); /* Release the lock before mmap'ing it. */ THREAD_LIST_UNLOCK(curthread); /* Map the stack and guard page together, and split guard page from allocated space: */ if ((stackaddr = mmap(stackaddr, stacksize + guardsize, _rtld_get_stack_prot(), MAP_STACK, -1, 0)) != MAP_FAILED && (guardsize == 0 || mprotect(stackaddr, guardsize, PROT_NONE) == 0)) { stackaddr += guardsize; } else { if (stackaddr != MAP_FAILED) munmap(stackaddr, stacksize + guardsize); stackaddr = NULL; } attr->stackaddr_attr = stackaddr; } if (attr->stackaddr_attr != NULL) return (0); else return (-1); } +/* + * Disable this warning from clang: + * + * cast from 'char *' to + * 'struct stack *' increases required alignment from 1 to 8 + * [-Werror,-Wcast-align] + * spare_stack = (struct stack *) + */ +#ifdef __clang__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wcast-align" +#endif + /* This function must be called with _thread_list_lock held. */ void _thr_stack_free(struct pthread_attr *attr) { struct stack *spare_stack; if ((attr != NULL) && ((attr->flags & THR_STACK_USER) == 0) && (attr->stackaddr_attr != NULL)) { spare_stack = (struct stack *) ((char *)attr->stackaddr_attr + attr->stacksize_attr - sizeof(struct stack)); spare_stack->stacksize = round_up(attr->stacksize_attr); spare_stack->guardsize = round_up(attr->guardsize_attr); spare_stack->stackaddr = attr->stackaddr_attr; if (spare_stack->stacksize == THR_STACK_DEFAULT && spare_stack->guardsize == _thr_guard_default) { /* Default stack/guard size. */ LIST_INSERT_HEAD(&dstackq, spare_stack, qe); } else { /* Non-default stack/guard size. */ LIST_INSERT_HEAD(&mstackq, spare_stack, qe); } attr->stackaddr_attr = NULL; } } + +#ifdef __clang__ +#pragma GCC diagnostic pop +#endif Index: head/lib/libthr/thread/thr_symbols.c =================================================================== --- head/lib/libthr/thread/thr_symbols.c (revision 318538) +++ head/lib/libthr/thread/thr_symbols.c (revision 318539) @@ -1,59 +1,63 @@ /* * Copyright (c) 2004 David Xu * All rights reserved. * * 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. * 3. Neither the name of the author nor the names of any co-contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL 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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include #include #include #include #include "thr_private.h" +#ifdef __clang__ +#pragma GCC diagnostic ignored "-Wmissing-variable-declarations" +#endif + /* A collection of symbols needed by debugger */ /* int _libthr_debug */ int _thread_off_tcb = offsetof(struct pthread, tcb); int _thread_off_tid = offsetof(struct pthread, tid); int _thread_off_next = offsetof(struct pthread, tle.tqe_next); int _thread_off_attr_flags = offsetof(struct pthread, attr.flags); int _thread_off_linkmap = offsetof(Obj_Entry, linkmap); int _thread_off_tlsindex = offsetof(Obj_Entry, tlsindex); int _thread_off_report_events = offsetof(struct pthread, report_events); int _thread_off_event_mask = offsetof(struct pthread, event_mask); int _thread_off_event_buf = offsetof(struct pthread, event_buf); int _thread_size_key = sizeof(struct pthread_key); int _thread_off_key_allocated = offsetof(struct pthread_key, allocated); int _thread_off_key_destructor = offsetof(struct pthread_key, destructor); int _thread_max_keys = PTHREAD_KEYS_MAX; int _thread_off_dtv = DTV_OFFSET; int _thread_off_state = offsetof(struct pthread, state); int _thread_state_running = PS_RUNNING; int _thread_state_zoombie = PS_DEAD; Index: head/lib/libthr/thread/thr_umtx.c =================================================================== --- head/lib/libthr/thread/thr_umtx.c (revision 318538) +++ head/lib/libthr/thread/thr_umtx.c (revision 318539) @@ -1,374 +1,374 @@ /* * Copyright (c) 2005 David Xu * All rights reserved. * * 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 unmodified, 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 AUTHOR ``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 AUTHOR 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 __FBSDID("$FreeBSD$"); #include "thr_private.h" #include "thr_umtx.h" #ifndef HAS__UMTX_OP_ERR int _umtx_op_err(void *obj, int op, u_long val, void *uaddr, void *uaddr2) { if (_umtx_op(obj, op, val, uaddr, uaddr2) == -1) return (errno); return (0); } #endif void _thr_umutex_init(struct umutex *mtx) { static const struct umutex default_mtx = DEFAULT_UMUTEX; *mtx = default_mtx; } void _thr_urwlock_init(struct urwlock *rwl) { static const struct urwlock default_rwl = DEFAULT_URWLOCK; *rwl = default_rwl; } int __thr_umutex_lock(struct umutex *mtx, uint32_t id) { uint32_t owner; if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0) return (_umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0)); for (;;) { owner = mtx->m_owner; if ((owner & ~UMUTEX_CONTESTED) == 0 && atomic_cmpset_acq_32(&mtx->m_owner, owner, id | owner)) return (0); if (owner == UMUTEX_RB_OWNERDEAD && atomic_cmpset_acq_32(&mtx->m_owner, owner, id | UMUTEX_CONTESTED)) return (EOWNERDEAD); if (owner == UMUTEX_RB_NOTRECOV) return (ENOTRECOVERABLE); /* wait in kernel */ _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0); } } #define SPINLOOPS 1000 int __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id) { uint32_t owner; int count; if (!_thr_is_smp) return (__thr_umutex_lock(mtx, id)); if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0) return (_umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, 0, 0)); for (;;) { count = SPINLOOPS; while (count--) { owner = mtx->m_owner; if ((owner & ~UMUTEX_CONTESTED) == 0 && atomic_cmpset_acq_32(&mtx->m_owner, owner, id | owner)) return (0); if (__predict_false(owner == UMUTEX_RB_OWNERDEAD) && atomic_cmpset_acq_32(&mtx->m_owner, owner, id | UMUTEX_CONTESTED)) return (EOWNERDEAD); if (__predict_false(owner == UMUTEX_RB_NOTRECOV)) return (ENOTRECOVERABLE); CPU_SPINWAIT; } /* wait in kernel */ _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, 0, 0); } } int __thr_umutex_timedlock(struct umutex *mtx, uint32_t id, const struct timespec *abstime) { struct _umtx_time *tm_p, timeout; size_t tm_size; uint32_t owner; int ret; if (abstime == NULL) { tm_p = NULL; tm_size = 0; } else { timeout._clockid = CLOCK_REALTIME; timeout._flags = UMTX_ABSTIME; timeout._timeout = *abstime; tm_p = &timeout; tm_size = sizeof(timeout); } for (;;) { if ((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) { /* try to lock it */ owner = mtx->m_owner; if ((owner & ~UMUTEX_CONTESTED) == 0 && atomic_cmpset_acq_32(&mtx->m_owner, owner, id | owner)) return (0); if (__predict_false(owner == UMUTEX_RB_OWNERDEAD) && atomic_cmpset_acq_32(&mtx->m_owner, owner, id | UMUTEX_CONTESTED)) return (EOWNERDEAD); if (__predict_false(owner == UMUTEX_RB_NOTRECOV)) return (ENOTRECOVERABLE); /* wait in kernel */ ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_WAIT, 0, (void *)tm_size, __DECONST(void *, tm_p)); } else { ret = _umtx_op_err(mtx, UMTX_OP_MUTEX_LOCK, 0, (void *)tm_size, __DECONST(void *, tm_p)); if (ret == 0 || ret == EOWNERDEAD || ret == ENOTRECOVERABLE) break; } if (ret == ETIMEDOUT) break; } return (ret); } int -__thr_umutex_unlock(struct umutex *mtx, uint32_t id) +__thr_umutex_unlock(struct umutex *mtx) { return (_umtx_op_err(mtx, UMTX_OP_MUTEX_UNLOCK, 0, 0, 0)); } int __thr_umutex_trylock(struct umutex *mtx) { return (_umtx_op_err(mtx, UMTX_OP_MUTEX_TRYLOCK, 0, 0, 0)); } int __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling, uint32_t *oldceiling) { return (_umtx_op_err(mtx, UMTX_OP_SET_CEILING, ceiling, oldceiling, 0)); } int _thr_umtx_wait(volatile long *mtx, long id, const struct timespec *timeout) { if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 && timeout->tv_nsec <= 0))) return (ETIMEDOUT); return (_umtx_op_err(__DEVOLATILE(void *, mtx), UMTX_OP_WAIT, id, 0, __DECONST(void*, timeout))); } int _thr_umtx_wait_uint(volatile u_int *mtx, u_int id, const struct timespec *timeout, int shared) { if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 && timeout->tv_nsec <= 0))) return (ETIMEDOUT); return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, 0, __DECONST(void*, timeout))); } int _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int id, int clockid, const struct timespec *abstime, int shared) { struct _umtx_time *tm_p, timeout; size_t tm_size; if (abstime == NULL) { tm_p = NULL; tm_size = 0; } else { timeout._clockid = clockid; timeout._flags = UMTX_ABSTIME; timeout._timeout = *abstime; tm_p = &timeout; tm_size = sizeof(timeout); } return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAIT_UINT : UMTX_OP_WAIT_UINT_PRIVATE, id, (void *)tm_size, __DECONST(void *, tm_p))); } int _thr_umtx_wake(volatile void *mtx, int nr_wakeup, int shared) { return (_umtx_op_err(__DEVOLATILE(void *, mtx), shared ? UMTX_OP_WAKE : UMTX_OP_WAKE_PRIVATE, nr_wakeup, 0, 0)); } void _thr_ucond_init(struct ucond *cv) { bzero(cv, sizeof(struct ucond)); } int _thr_ucond_wait(struct ucond *cv, struct umutex *m, const struct timespec *timeout, int flags) { struct pthread *curthread; if (timeout && (timeout->tv_sec < 0 || (timeout->tv_sec == 0 && timeout->tv_nsec <= 0))) { curthread = _get_curthread(); _thr_umutex_unlock(m, TID(curthread)); return (ETIMEDOUT); } return (_umtx_op_err(cv, UMTX_OP_CV_WAIT, flags, m, __DECONST(void*, timeout))); } int _thr_ucond_signal(struct ucond *cv) { if (!cv->c_has_waiters) return (0); return (_umtx_op_err(cv, UMTX_OP_CV_SIGNAL, 0, NULL, NULL)); } int _thr_ucond_broadcast(struct ucond *cv) { if (!cv->c_has_waiters) return (0); return (_umtx_op_err(cv, UMTX_OP_CV_BROADCAST, 0, NULL, NULL)); } int __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, const struct timespec *tsp) { struct _umtx_time timeout, *tm_p; size_t tm_size; if (tsp == NULL) { tm_p = NULL; tm_size = 0; } else { timeout._timeout = *tsp; timeout._flags = UMTX_ABSTIME; timeout._clockid = CLOCK_REALTIME; tm_p = &timeout; tm_size = sizeof(timeout); } return (_umtx_op_err(rwlock, UMTX_OP_RW_RDLOCK, flags, (void *)tm_size, tm_p)); } int __thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp) { struct _umtx_time timeout, *tm_p; size_t tm_size; if (tsp == NULL) { tm_p = NULL; tm_size = 0; } else { timeout._timeout = *tsp; timeout._flags = UMTX_ABSTIME; timeout._clockid = CLOCK_REALTIME; tm_p = &timeout; tm_size = sizeof(timeout); } return (_umtx_op_err(rwlock, UMTX_OP_RW_WRLOCK, 0, (void *)tm_size, tm_p)); } int __thr_rwlock_unlock(struct urwlock *rwlock) { return (_umtx_op_err(rwlock, UMTX_OP_RW_UNLOCK, 0, NULL, NULL)); } void _thr_rwl_rdlock(struct urwlock *rwlock) { int ret; for (;;) { if (_thr_rwlock_tryrdlock(rwlock, URWLOCK_PREFER_READER) == 0) return; ret = __thr_rwlock_rdlock(rwlock, URWLOCK_PREFER_READER, NULL); if (ret == 0) return; if (ret != EINTR) PANIC("rdlock error"); } } void _thr_rwl_wrlock(struct urwlock *rwlock) { int ret; for (;;) { if (_thr_rwlock_trywrlock(rwlock) == 0) return; ret = __thr_rwlock_wrlock(rwlock, NULL); if (ret == 0) return; if (ret != EINTR) PANIC("wrlock error"); } } void _thr_rwl_unlock(struct urwlock *rwlock) { if (_thr_rwlock_unlock(rwlock)) PANIC("unlock error"); } Index: head/lib/libthr/thread/thr_umtx.h =================================================================== --- head/lib/libthr/thread/thr_umtx.h (revision 318538) +++ head/lib/libthr/thread/thr_umtx.h (revision 318539) @@ -1,270 +1,270 @@ /*- * Copyright (c) 2005 David Xu * All rights reserved. * * 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 AUTHOR 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 AUTHOR 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. * * $FreeBSD$ */ #ifndef _THR_FBSD_UMTX_H_ #define _THR_FBSD_UMTX_H_ #include #include #ifdef __LP64__ #define DEFAULT_UMUTEX {0,0,{0,0},0,{0,0}} #else #define DEFAULT_UMUTEX {0,0,{0,0},0,0,{0,0}} #endif #define DEFAULT_URWLOCK {0,0,0,0,{0,0,0,0}} int _umtx_op_err(void *, int op, u_long, void *, void *) __hidden; int __thr_umutex_lock(struct umutex *mtx, uint32_t id) __hidden; int __thr_umutex_lock_spin(struct umutex *mtx, uint32_t id) __hidden; int __thr_umutex_timedlock(struct umutex *mtx, uint32_t id, const struct timespec *timeout) __hidden; -int __thr_umutex_unlock(struct umutex *mtx, uint32_t id) __hidden; +int __thr_umutex_unlock(struct umutex *mtx) __hidden; int __thr_umutex_trylock(struct umutex *mtx) __hidden; int __thr_umutex_set_ceiling(struct umutex *mtx, uint32_t ceiling, uint32_t *oldceiling) __hidden; void _thr_umutex_init(struct umutex *mtx) __hidden; void _thr_urwlock_init(struct urwlock *rwl) __hidden; int _thr_umtx_wait(volatile long *mtx, long exp, const struct timespec *timeout) __hidden; int _thr_umtx_wait_uint(volatile u_int *mtx, u_int exp, const struct timespec *timeout, int shared) __hidden; int _thr_umtx_timedwait_uint(volatile u_int *mtx, u_int exp, int clockid, const struct timespec *timeout, int shared) __hidden; int _thr_umtx_wake(volatile void *mtx, int count, int shared) __hidden; int _thr_ucond_wait(struct ucond *cv, struct umutex *m, const struct timespec *timeout, int flags) __hidden; void _thr_ucond_init(struct ucond *cv) __hidden; int _thr_ucond_signal(struct ucond *cv) __hidden; int _thr_ucond_broadcast(struct ucond *cv) __hidden; int __thr_rwlock_rdlock(struct urwlock *rwlock, int flags, const struct timespec *tsp) __hidden; int __thr_rwlock_wrlock(struct urwlock *rwlock, const struct timespec *tsp) __hidden; int __thr_rwlock_unlock(struct urwlock *rwlock) __hidden; /* Internal used only */ void _thr_rwl_rdlock(struct urwlock *rwlock) __hidden; void _thr_rwl_wrlock(struct urwlock *rwlock) __hidden; void _thr_rwl_unlock(struct urwlock *rwlock) __hidden; static inline int _thr_umutex_trylock(struct umutex *mtx, uint32_t id) { if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id)) return (0); if (__predict_false((uint32_t)mtx->m_owner == UMUTEX_RB_OWNERDEAD) && atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_RB_OWNERDEAD, id | UMUTEX_CONTESTED)) return (EOWNERDEAD); if (__predict_false((uint32_t)mtx->m_owner == UMUTEX_RB_NOTRECOV)) return (ENOTRECOVERABLE); if ((mtx->m_flags & UMUTEX_PRIO_PROTECT) == 0) return (EBUSY); return (__thr_umutex_trylock(mtx)); } static inline int _thr_umutex_trylock2(struct umutex *mtx, uint32_t id) { if (atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_UNOWNED, id) != 0) return (0); if ((uint32_t)mtx->m_owner == UMUTEX_CONTESTED && __predict_true((mtx->m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) == 0) && atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_CONTESTED, id | UMUTEX_CONTESTED)) return (0); if (__predict_false((uint32_t)mtx->m_owner == UMUTEX_RB_OWNERDEAD) && atomic_cmpset_acq_32(&mtx->m_owner, UMUTEX_RB_OWNERDEAD, id | UMUTEX_CONTESTED)) return (EOWNERDEAD); if (__predict_false((uint32_t)mtx->m_owner == UMUTEX_RB_NOTRECOV)) return (ENOTRECOVERABLE); return (EBUSY); } static inline int _thr_umutex_lock(struct umutex *mtx, uint32_t id) { if (_thr_umutex_trylock2(mtx, id) == 0) return (0); return (__thr_umutex_lock(mtx, id)); } static inline int _thr_umutex_lock_spin(struct umutex *mtx, uint32_t id) { if (_thr_umutex_trylock2(mtx, id) == 0) return (0); return (__thr_umutex_lock_spin(mtx, id)); } static inline int _thr_umutex_timedlock(struct umutex *mtx, uint32_t id, const struct timespec *timeout) { if (_thr_umutex_trylock2(mtx, id) == 0) return (0); return (__thr_umutex_timedlock(mtx, id, timeout)); } static inline int _thr_umutex_unlock2(struct umutex *mtx, uint32_t id, int *defer) { uint32_t flags, owner; bool noncst; flags = mtx->m_flags; noncst = (flags & UMUTEX_NONCONSISTENT) != 0; if ((flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0) { if (atomic_cmpset_rel_32(&mtx->m_owner, id, noncst ? UMUTEX_RB_NOTRECOV : UMUTEX_UNOWNED)) return (0); - return (__thr_umutex_unlock(mtx, id)); + return (__thr_umutex_unlock(mtx)); } do { owner = mtx->m_owner; if (__predict_false((owner & ~UMUTEX_CONTESTED) != id)) return (EPERM); } while (__predict_false(!atomic_cmpset_rel_32(&mtx->m_owner, owner, noncst ? UMUTEX_RB_NOTRECOV : UMUTEX_UNOWNED))); if ((owner & UMUTEX_CONTESTED) != 0) { if (defer == NULL || noncst) (void)_umtx_op_err(mtx, UMTX_OP_MUTEX_WAKE2, flags, 0, 0); else *defer = 1; } return (0); } static inline int _thr_umutex_unlock(struct umutex *mtx, uint32_t id) { return (_thr_umutex_unlock2(mtx, id, NULL)); } static inline int _thr_rwlock_tryrdlock(struct urwlock *rwlock, int flags) { int32_t state, wrflags; if ((flags & URWLOCK_PREFER_READER) != 0 || (rwlock->rw_flags & URWLOCK_PREFER_READER) != 0) wrflags = URWLOCK_WRITE_OWNER; else wrflags = URWLOCK_WRITE_OWNER | URWLOCK_WRITE_WAITERS; state = rwlock->rw_state; while (!(state & wrflags)) { if (__predict_false(URWLOCK_READER_COUNT(state) == URWLOCK_MAX_READERS)) return (EAGAIN); if (atomic_cmpset_acq_32(&rwlock->rw_state, state, state + 1)) return (0); state = rwlock->rw_state; } return (EBUSY); } static inline int _thr_rwlock_trywrlock(struct urwlock *rwlock) { int32_t state; state = rwlock->rw_state; while ((state & URWLOCK_WRITE_OWNER) == 0 && URWLOCK_READER_COUNT(state) == 0) { if (atomic_cmpset_acq_32(&rwlock->rw_state, state, state | URWLOCK_WRITE_OWNER)) return (0); state = rwlock->rw_state; } return (EBUSY); } static inline int _thr_rwlock_rdlock(struct urwlock *rwlock, int flags, struct timespec *tsp) { if (_thr_rwlock_tryrdlock(rwlock, flags) == 0) return (0); return (__thr_rwlock_rdlock(rwlock, flags, tsp)); } static inline int _thr_rwlock_wrlock(struct urwlock *rwlock, struct timespec *tsp) { if (_thr_rwlock_trywrlock(rwlock) == 0) return (0); return (__thr_rwlock_wrlock(rwlock, tsp)); } static inline int _thr_rwlock_unlock(struct urwlock *rwlock) { int32_t state; state = rwlock->rw_state; if ((state & URWLOCK_WRITE_OWNER) != 0) { if (atomic_cmpset_rel_32(&rwlock->rw_state, URWLOCK_WRITE_OWNER, 0)) return (0); } else { for (;;) { if (__predict_false(URWLOCK_READER_COUNT(state) == 0)) return (EPERM); if (!((state & (URWLOCK_WRITE_WAITERS | URWLOCK_READ_WAITERS)) != 0 && URWLOCK_READER_COUNT(state) == 1)) { if (atomic_cmpset_rel_32(&rwlock->rw_state, state, state - 1)) return (0); state = rwlock->rw_state; } else { break; } } } return (__thr_rwlock_unlock(rwlock)); } #endif