aboutsummaryrefslogtreecommitdiff
path: root/lib/libthr/thread
Commit message (Collapse)AuthorAgeFilesLines
...
* libthr: thr_attr.c: EINVAL, not ENOTSUP, on invalid argumentsOlivier Certner2024-01-041-13/+7
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On first read, POSIX may seem ambiguous about the return code for some scheduling-related pthread functions on invalid arguments. But a more thorough reading and a bit of standards archeology strongly suggests that this case should be handled by EINVAL and that ENOTSUP is reserved for implementations providing only part of the functionality required by the POSIX option POSIX_PRIORITY_SCHEDULING (e.g., if an implementation doesn't support SCHED_FIFO, it should return ENOTSUP on a call to, e.g., sched_setscheduler() with 'policy' SCHED_FIFO). This reading is supported by the second sentence of the very definition of ENOTSUP, as worded in CAE/XSI Issue 5 and POSIX Issue 6: "The implementation does not support this feature of the Realtime Feature Group.", and the fact that an additional ENOTSUP case was added to pthread_setschedparam() in Issue 6, which introduces SCHED_SPORADIC, saying that pthread_setschedparam() may return it when attempting to dynamically switch to SCHED_SPORADIC on systems that doesn't support that. glibc, illumos and NetBSD also support that reading by always returning EINVAL, and OpenBSD as well, since it always returns EINVAL but the corresponding code has a comment suggesting returning ENOTSUP for SCHED_FIFO and SCHED_RR, which it effectively doesn't support. Additionally, always returning EINVAL fixes inconsistencies where EINVAL would be returned on some out-of-range values and ENOTSUP on others. Reviewed by: markj Approved by: markj (mentor) MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D43006
* libthr: thr_attr.c: Clarity, whitespace and styleOlivier Certner2024-01-041-278/+193
| | | | | | | | | | Also, remove most comments, which don't add value. Reviewed by: emaste Approved by: markj (mentor) MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D43005
* lib: Automated cleanup of cdefs and other formattingWarner Losh2023-11-278-8/+0
| | | | | | | | | | | | | | | | Apply the following automated changes to try to eliminate no-longer-needed sys/cdefs.h includes as well as now-empty blank lines in a row. Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/ Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/ Remove /\n+#if.*\n#endif.*\n+/ Remove /^#if.*\n#endif.*\n/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/ Sponsored by: Netflix
* libc: export pthread_getname_np stubMinsoo Choo2023-08-203-3/+6
| | | | | | | | | | | | | | | pthread_getname_np needs to be provided by libc in order to import jemalloc 5.3.0. A stub implementation for libc pthread_getname_np() is added for _pthread_stubs.c, which always reports empty name for the main thread. Internal _pthread_getname_np() is not exported, but provided for libc own use. Reviewed by: kib MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D41461
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-161-1/+0
| | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-1656-112/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* Remove $FreeBSD$: two-line .h patternWarner Losh2023-08-162-4/+0
| | | | Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
* thr_mutex.c: styleKonstantin Belousov2023-07-261-2/+2
| | | | | | | | Reindend and re-fill the statement. Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D41150
* libthr: trigger library initialization on rwlock callsKonstantin Belousov2023-07-161-2/+11
| | | | | | Reviewed by: tijl Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libthr: some style in thr_rwlock.cKonstantin Belousov2023-07-161-2/+2
| | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week
* libthr: Patch to reduce latency to acquire+release a pthread mutex.Greg Becker2023-07-081-4/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The acquisition and release of an uncontended default/normal pthread mutex on FreeBSD is suprisingly slow, e.g., pthread wrlocks and binary semaphores both exhibit roughly 33% lower latency, while default/normal mutexes on Linux exhibit roughly 67% lower latency than FreeBSD. This is likely explained by the fact that AFAICT in the best case to acquire an uncontended mutex on Linux one need touch only 1 page and read+modify only 1 cacheline, whereas on FreeBSD we need to touch at least 4 pages, read 6 cachelines, and modify at least 4 cachelines. This patch does not address the pthread mutex architecture. Instead, it improves performance by adding the __always_inline attribute to mutex_lock_common() and mutex_unlock_common() to encourage constant folding and propagation, thereby lowering the latency to acquire and release a mutex due to a shorter code path with fewer compares, jumps, and mispredicts. With this patch on a stock build I see a reduction in latency of roughly 7% for default/normal mutexes, and 17% for robust mutexes. When built without PTHREADS_ASSERTIONS enabled I see a reduction in latency of roughly 15% and 26%, respectively. Suprisingly, I see similar reductions in latency for heavily contended mutexes. By default, this patch increases the size of libthr.so.3 by 2448 bytes, but when built without PTHREAD_ASSERTIONS enabled it only increases by 448 bytes. Reviewed by: jhb (previous version), kib MFC after: 1 week Differential revision: https://reviews.freebsd.org/D40912
* libthr: Add src.conf variable WITHOUT_PTHREADS_ASSERTIONSGreg Becker2023-07-071-1/+1
| | | | | | | | | | | | | | This patch fixes a bug which prevents building libthr without _PTHREADS_INVARIANTS defined. The default remains to build libthr with -D_PTHREADS_INVARIANTS. However, with this patch, if one builds libthr with WITHOUT_PTHREADS_ASSERTIONS=true then the latency to acquire+release a default pthread mutex is reduced by roughly 5%, and a robust mutex by roughly 18% (as measured by a simple synthetic test on a Xeon E5-2697a based machine). Reviewed by: jhb, kib, mjg MFC after: 1 week Differential revision: https://reviews.freebsd.org/D40900
* libpthread: allocate rwlocks and spinlocks in dedicated cachelinesGreg Becker2023-06-272-2/+6
| | | | | | | | | | | Reduces severe performance degradation due to false-sharing. Note that this does not account for hardware which can perform adjacent cacheline prefetch. [mjg: massaged the commit message and the patch to use aligned_alloc instead of malloc] PR: 272238 MFC after: 1 week
* libthr rtld locks: do not leak URWLOCK_READ_WAITERS into childKonstantin Belousov2023-05-223-0/+17
| | | | | | | | | | | | | | | Since there is only the current thread in the child, no pending readers exist. Clear the bit, since it confuses future attempts to acquire write ownership of the rtld locks, due to URWLOCK_PREFER_READERS flag. To be future-proof, clear all state about pending writers and readers. PR: 271490 Reported and tested by: KJ Tsanaktsidis <kj@kjtsanaktsidis.id.au> Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D40178
* spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSDWarner Losh2023-05-1228-28/+28
| | | | | | | | | The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch up to that fact and revert to their recommended match of BSD-2-Clause. Discussed with: pfg MFC After: 3 days Sponsored by: Netflix
* umtx: allow to configure minimal timeout (in nanoseconds)Konstantin Belousov2023-04-181-0/+11
| | | | | | | | PR: 270785 Reviewed by: markj, mav Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D39584
* libthr pshared: correct a bug in allocationKonstantin Belousov2023-02-011-5/+10
| | | | | | | | | | | | | | | | | When __thr_pshared_offpage() is called for allocation, it must not use the cached offpage for the key. Instead, the cached offpage must be unmapped and removed from the cache, if any. It is legitimate for the user code to unmap the shared lock object without destroying it, and then mapping something over the freed VA to carry another shared lock. In this case the cached offpage must be un-cached. PR: 269277 Reported by: rau8344@gmail.com Reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D38345
* libthr: add pshared_destroy() helperKonstantin Belousov2023-02-011-5/+12
| | | | | | | Rewviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D38345
* libthr: Trim trailing whitespaces in pthread_attr_affinity.Dmitry Chagin2023-01-291-3/+3
| | | | MFC after: 1 week
* libthr: Fix pthread_attr_[g|s]etaffinity_np to match it's manual and the kernel.Dmitry Chagin2023-01-291-2/+4
| | | | | | | | | | | | | | | | | | | | | Since f35093f8 semantics of a thread affinity functions is changed to be a compatible with Linux: In case of getaffinity(), the minimum cpuset_t size that the kernel permits is the maximum CPU id, present in the system, / NBBY bytes, the maximum size is not limited. In case of setaffinity(), the kernel does not limit the size of the user-provided cpuset_t, internally using only the meaningful part of the set, where the upper bound is the maximum CPU id, present in the system, no larger than the size of the kernel cpuset_t. To match pthread_attr_[g|s]etaffinity_np checks of the user-provided cpusets to the kernel behavior export the minimum cpuset_t size allowed by running kernel via new sysctl kern.sched.cpusetsizemin and use it in checks. Reviewed by: Differential Revision: https://reviews.freebsd.org/D38112 MFC after: 1 week
* libthr(3): Fix a typo in a source code commentGordon Bergling2023-01-111-1/+1
| | | | | | - s/extentions/extensions/ MFC after: 3 days
* libthr: extract code to get main stack base and size into helpersKonstantin Belousov2022-09-163-36/+46
| | | | | | | | Reviewed by: brooks, imp (previous version) Discussed with: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36540
* libc, libthr: use AT_USRSTACK{BASE,LIM} instead of sysctl("kern.usrstack") ↵Konstantin Belousov2022-09-162-18/+33
| | | | | | | | | | and get_rlimit(RLIMIT_STACK) Reviewed by: brooks, imp (previous version) Discussed with: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36540
* libthr: use nitems() for mib lengthKonstantin Belousov2022-09-162-5/+4
| | | | | | | | | Suggested by: imp Reviewed by: brooks, imp (previous version) Discussed with: markj Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D36540
* Remove PAGE_SIZE from libthrAndrew Turner2022-05-037-11/+15
| | | | | | | | | | | | | | | | | In libthr we use PAGE_SIZE when allocating memory with mmap and to check various structs will fit into a single page so we can use this allocator for them. Ask the kernel for the page size on init for use by the page allcator and add a new machine dependent macro to hold the smallest page size the architecture supports to check the structure is small enough. This allows us to use the same libthr on arm64 with either 4k or 16k pages. Reviewed by: kib, markj, imp Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34984
* Use the existing page size in libthrAndrew Turner2022-05-031-1/+1
| | | | | | | | | Rather than calling getpagesize() twice use the value saved after the first call to size a mmap allocation. Reviewed by: kib, markj Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D34983
* Use uintptr_t for return type of _Unwind_GetCFA.John Baldwin2022-01-271-2/+2
| | | | | | | This matches the type in other unwind headers. Reviewed by: dim, emaste Differential Revision: https://reviews.freebsd.org/D34050
* Use an unsigned 64-bit integer for exception class.John Baldwin2022-01-271-2/+2
| | | | | | | | | | This matches the type in other unwind headers (LLVM libunwind, libcxxrt, glibc). NB: include/unwind.h is not installed but is only used by libthr Reviewed by: imp, dim, emaste Differential Revision: https://reviews.freebsd.org/D34049
* libthr: Use TLS_TCB_* in _tcb_[cd]tor.John Baldwin2022-01-251-2/+2
| | | | | | | | | This matches libc and rtld in using the alignment (TLS_TCB_ALIGN) from machine/tls.h instead of hardcoding 16. Reviewed by: kib Sponsored by: The University of Cambridge, Google Inc. Differential Revision: https://reviews.freebsd.org/D34023
* Revert "libthr: Use kern.stacktop for thread stack calculation."Mark Johnston2022-01-173-24/+19
| | | | | | | | | | | | | | | The current ASLR stack gap feature will be removed, and with that the need for this change, and the kern.stactop sysctl, is gone. Moreover, the approach taken in this revision does not provide compatibility for old copies of libthr.so, and the revision should have also updated __libc_map_stacks_exec(). This reverts commit 78df56ccfcb40013a3e6904bd6d39836220c3550. Reviewed by: kib MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D33704
* libthr: Remove the DTV_OFFSET macro.John Baldwin2021-12-091-1/+1
| | | | | | | | | | This macro is confusing as it is not related to the similarly named TLS_DTV_OFFSET. Instead, replace its one use with the desired expression which is the same on all platforms. Reviewed by: kib, emaste, jrtc27 Sponsored by: The University of Cambridge, Google Inc. Differential Revision: https://reviews.freebsd.org/D33345
* libthr: Use kern.stacktop for thread stack calculation.Dawid Gorecki2021-10-153-19/+24
| | | | | | | | | | | | | | | | | | | | Use the new kern.stacktop sysctl to retrieve the address of stack top instead of kern.usrstack. kern.usrstack does not have any knowledge of the stack gap, so this can cause problems with thread stacks. Using kern.stacktop sysctl should fix most of those problems. kern.usrstack is used as a fallback when kern.stacktop cannot be read. Rename usrstack variables to stacktop to reflect this change. Fixes problems with firefox and thunderbird not starting with stack gap enabled. PR: 239873 Reviewed by: kib Obtained from: Semihalf Sponsored by: Stormshield MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D31898
* rtld: make dlerror() thread-localKonstantin Belousov2021-04-102-0/+25
| | | | | | | | PR: 95339 Discussed with: arichardson Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D29633
* rtld: workaround for broken ABIKonstantin Belousov2021-04-101-0/+8
| | | | | | | | | | | | | | | | Right now, libthr does not initialize RtldLockInfo.rtli_version when calling _rtld_thread_init(), which makes versioning the interface troublesome. Add a workaround: if the calling object of _rtld_thread_init() exports the "_pli_rtli_version" symbol, then consider rtli_version initialized. Otherwise, forcibly set it to RTLI_VERSION_ONE, currently defined as RTLI_VERSION. Export "_pli_rtli_version" from libthr and properly initialize rtli_version. Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D29633
* libthr malloc: support recursion on thr_malloc_umtx.Konstantin Belousov2021-01-121-2/+11
| | | | | | | | | | | | | | One possible way the recursion can happen is during fork: suppose that fork is called from early code that did not triggered jemalloc(3) initialization yet. Then we lock thr_malloc lock, and call malloc_prefork() that might require initialization of jemalloc pthread_mutexes, calling into libthr malloc. It is safe to allow recursion for this occurence. PR: 252579 Reported by: Vasily Postnicov <shamaz.mazum@gmail.com> MFC after: 1 week Sponsored by: The FreeBSD Foundation
* libthr: wrap pdfork(2), same as fork(2).Konstantin Belousov2021-01-113-7/+61
| | | | | | | | | | Without wrapping, rtld services and malloc(3) are not guaranteed to operate correctly in the forked child. Reviewed by: markj MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D28088
* Ensure that threading library is initialized in pthread_mutex_init().Konstantin Belousov2020-11-301-0/+2
| | | | | | | | | | | | We need at least thr_malloc ready. The situation is possible e.g. in case of libthr being listed in DT_NEEDED before some of its consumers. Reported and tested by: lev Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=368190
* libc: Add pthread_attr_get_np(3) stub, reporting ESRCH.Konstantin Belousov2020-11-281-0/+1
| | | | | | | | | | | | | | | This seems to be required by recent clang asan. I do not see other way than put the symbol under FBSD_1.0 version. PR: 251112 Reported by: Andrew Stitcher <astitcher@apache.org> Reviewed by: emaste Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D27389 Notes: svn path=/head/; revision=368125
* Style.Konstantin Belousov2020-11-261-18/+18
| | | | | | | | Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=368082
* Add pthread_getname_np() and pthread_setname_np() aliases forKonstantin Belousov2020-06-101-17/+59
| | | | | | | | | | | | | | | pthread_get_name_np() and pthread_set_name_np(). This re-applies r361770 after compatibility fixes. Reviewed by: antoine, jkim, markj Tested by: antoine (exp-run) Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D25117 Notes: svn path=/head/; revision=362032
* Revert r361770 "Add pthread_getname_np() and pthread_setname_np() aliases" ↵Konstantin Belousov2020-06-041-2/+0
| | | | | | | | | | | | for now. It is not compatible enough with Linux. Requested by: antoine, jkim Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=361784
* Add pthread_getname_np() and pthread_setname_np() aliasesKonstantin Belousov2020-06-031-0/+2
| | | | | | | | | | | | | | for pthread_get_name_np() and pthread_set_name_np(), to be compatible with Linux. PR: 238404 Proposed and reviewed by: markj Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D25117 Notes: svn path=/head/; revision=361770
* Add pthread_peekjoin_np(3).Konstantin Belousov2020-02-151-7/+26
| | | | | | | | | | | | | | The function allows to peek at the thread exit status and even see return value, without joining (and thus finally destroying) the target thread. Reviewed by: markj Sponsored by: The FreeBSD Foundation (kib) MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D23676 Notes: svn path=/head/; revision=357985
* Return success, instead of ESRCH, from pthread_cancel(3) applied to theKonstantin Belousov2020-02-131-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | exited but not yet joined thread. Before, if the thread exited but was not yet joined, we returned ESRCH. According to IEEE Std 1003.1™-2017 recommendation in the description of pthread_cancel(3): If an implementation detects use of a thread ID after the end of its lifetime, it is recommended that the function should fail and report an [ESRCH] error. So it seems desirable to not return ESRCH until the lifetime of the thread ID ends. According to the section 2.9.2 Thread IDs, The lifetime of a thread ID ends after the thread terminates if it was created with the detachstate attribute set to PTHREAD_CREATE_DETACHED or if pthread_detach() or pthread_join() has been called for that thread. In other words, lifetime for thread ID of exited but not yet joined thread did not ended yet. Prompted by: cperciva Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=357894
* Use sigfastblock(2) for masking signals in libthr.Konstantin Belousov2020-02-094-6/+69
| | | | | | | | | | | | | | | | | Ensure proper handshake to transfer sigfastblock(2) blocking word ownership from rtld to libthr. Unfortunately sigfastblock(2) is not enough to stop intercepting signals in libthr, because critical sections must ensure more than just signal blocking. Tested by: pho Disscussed with: cem, emaste, jilles Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D12773 Notes: svn path=/head/; revision=357698
* Fix destruction of the robust mutexes.Konstantin Belousov2019-09-231-1/+5
| | | | | | | | | | | | | | | If robust mutex' owner terminated, causing kernel-assisted state recovery, and then pthread_mutex_destroy() is executed as the next action, assert is triggered about mutex still being on the list. Ignore the mutex linkage in pthread_mutex_destroy() for shared robust mutexes with dead owner, same as for enqueue_mutex(). Reported by: avg Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=352620
* Fix _pthread_cancel_enter() and _pthread_cancel_leave() jmptable entries.Konstantin Belousov2019-08-213-4/+8
| | | | | | | | | | PR: 240022 Reported by: Andrew Gierth <andrew@tao11.riddles.org.uk> Sponsored by: The FreeBSD Foundation MFC after: 3 days Notes: svn path=/head/; revision=351349
* Avoid conflicts with libc symbols in libthr jump table.Konstantin Belousov2019-07-311-2/+0
| | | | | | | | | | | | | | | | | | | In some corner cases of static linking and unexpected libraries order on the linker command line, libc symbol might preempt the same libthr symbol, in which case libthr jump table points back to libc causing either infinite recursion or loop. Handle all of such symbols by using private libthr names for them, ensuring that the right pointers are installed into the table. In collaboration with: arichardson PR: 239475 Tested by: pho MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D21088 Notes: svn path=/head/; revision=350483
* Avoid conflicts with libc symbols in libthr jump table.Konstantin Belousov2019-07-3121-216/+353
| | | | | | | | | | | | | | | | | | | In some corner cases of static linking and unexpected libraries order on the linker command line, libc symbol might preempt the same libthr symbol, in which case libthr jump table points back to libc causing either infinite recursion or loop. Handle all of such symbols by using private libthr names for them, ensuring that the right pointers are installed into the table. In collaboration with: arichardson PR: 239475 Tested by: pho MFC after: 2 weeks Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D21088 Notes: svn path=/head/; revision=350481
* Restore ability to pass NULL name argument to pthread_set_name_np(3)Konstantin Belousov2019-07-111-1/+1
| | | | | | | | | | | to clear the thread name. PR: 239142 Submitted by: Lewis Donzis <lew@perftech.com> MFC after: 3 days Notes: svn path=/head/; revision=349912