History log of /freebsd-current/lib/libthr/thread/thr_private.h
Revision Date Author Comments
# ec2b6b16 08-May-2024 Brooks Davis <brooks@FreeBSD.org>

libthr: avoid varargs in fcntl and openat interposers

Align these signatures with the ones in syscalls.master (and thus
libsys.h). There's no reason to do va_args twice and in some ABIs
(e.g,, CheriABI) you can't access fixed arguments as varargs if you
weren't called with varargs signature.

Reviewed by: imp, kib, jhibbits
Obtained from: CheriBSD
Differential Revision: https://reviews.freebsd.org/D45126


# 220aa0f4 19-Apr-2024 Konstantin Belousov <kib@FreeBSD.org>

libthr: add pthread_sigqueue(3)

PR: 278459
Reviewed by: markj
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D44867


# f8bbbce4 06-Mar-2024 Konstantin Belousov <kib@FreeBSD.org>

libthr: remove explicit sys/cdefs.h includes

Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# aadb4a1b 04-Jan-2024 Olivier Certner <olce@FreeBSD.org>

pthread_attr_get_np(): Use malloc(), report ENOMEM, don't tamper on error

Similarly as in the previous commit, using calloc() instead of malloc()
is useless here in the regular case since the subsequent call to
cpuset_getaffinify() is going to completely fill the allocated memory.

However, there is an additional complication. This function tries to
allocate memory to hold the cpuset if it previously wasn't, and does so
before the thread lock is acquired, which can fail on a bad thread ID.
In this case, it is necessary to deallocate the memory allocated in this
function so that the attributes object appears unmodified to the caller
when an error is returned. Without this, a subsequent call to
pthread_attr_getaffinity_np() would expose uninitialized memory (not
a security problem per se, since it comes from the same process) instead
of returning a full mask as it would before the failing call to
pthread_attr_get_np(). So the caller would be able to notice a change
in the state of the attributes object even if pthread_attr_get_np()
reported failure, which would be quite surprising. A similar problem
that could occur on failure of cpuset_setaffinity() has been fixed.

Finally, we shall always report memory allocation failure. This already
goes for pthread_attr_init(), so, if for nothing else, just be
consistent.

Reviewed by: emaste, kib
Approved by: emaste (mentor)
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D43329


# 0dc52b72 14-Aug-2023 Minsoo Choo <minsoochoo0122@proton.me>

libc: export pthread_getname_np stub

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


# b3e76948 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: two-line .h pattern

Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/


# 6f49eafb 20-May-2023 Konstantin Belousov <kib@FreeBSD.org>

libthr rtld locks: do not leak URWLOCK_READ_WAITERS into child

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


# 4d846d26 10-May-2023 Warner Losh <imp@FreeBSD.org>

spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD

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


# e03c7f50 13-Sep-2022 Konstantin Belousov <kib@FreeBSD.org>

libthr: extract code to get main stack base and size into helpers

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


# f75b1ff6 17-Jan-2022 Mark Johnston <markj@FreeBSD.org>

Revert "libthr: Use kern.stacktop for thread stack calculation."

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


# 78df56cc 13-Oct-2021 Dawid Gorecki <dgr@semihalf.com>

libthr: Use kern.stacktop for thread stack calculation.

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


# 4d9128da 07-Apr-2021 Konstantin Belousov <kib@FreeBSD.org>

rtld: make dlerror() thread-local

PR: 95339
Discussed with: arichardson
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D29633


# 21f749da 10-Jan-2021 Konstantin Belousov <kib@FreeBSD.org>

libthr: wrap pdfork(2), same as fork(2).

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


# fc908e50 08-Feb-2020 Konstantin Belousov <kib@FreeBSD.org>

Use sigfastblock(2) for masking signals in libthr.

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


# 65174f68 21-Aug-2019 Konstantin Belousov <kib@FreeBSD.org>

Fix _pthread_cancel_enter() and _pthread_cancel_leave() jmptable entries.

PR: 240022
Reported by: Andrew Gierth <andrew@tao11.riddles.org.uk>
Sponsored by: The FreeBSD Foundation
MFC after: 3 days


# 3638455c 31-Jul-2019 Konstantin Belousov <kib@FreeBSD.org>

Avoid conflicts with libc symbols in libthr jump table.

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


# 0ab1bfc7 31-Jul-2019 Konstantin Belousov <kib@FreeBSD.org>

Avoid conflicts with libc symbols in libthr jump table.

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


# 5d00c5a6 29-Mar-2019 Konstantin Belousov <kib@FreeBSD.org>

Fix initial exec TLS mode for dynamically loaded shared objects.

If dso uses initial exec TLS mode, rtld tries to allocate TLS in
static space. If there is no space left, the dlopen(3) fails. If space
if allocated, initial content from PT_TLS segment is distributed to
all threads' pcbs, which was missed and caused un-initialized TLS
segment for such dso after dlopen(3).

The mode is auto-detected either due to the relocation used, or if the
DF_STATIC_TLS dynamic flag is set. In the later case, the TLS segment
is tried to allocate earlier, which increases chance of the dlopen(3)
to succeed. LLD was recently fixed to properly emit the flag, ld.bdf
did it always.

Initial test by: dumbbell
Tested by: emaste (amd64), ian (arm)
Tested by: Gerald Aryeetey <aryeeteygerald_rogers.com> (arm64)
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks
Differential revision: https://reviews.freebsd.org/D19072


# 381c2d2e 29-Jan-2019 Konstantin Belousov <kib@FreeBSD.org>

Untangle jemalloc and mutexes initialization.

The need to use libc malloc(3) from some places in libthr always
caused issues. For instance, per-thread key allocation was switched to
use plain mmap(2) to get storage, because some third party mallocs
used keys for implementation of calloc(3).

Even more important, libthr calls calloc(3) during initialization of
pthread mutexes, and jemalloc uses pthread mutexes. Jemalloc provides
some way to both postpone the initialization, and to make
initialization to use specialized allocator, but this is very fragile
and often breaks. See the referenced PR for another example.

Add the small malloc implementation used by rtld, to libthr. Use it in
thr_spec.c and for mutexes initialization. This avoids the issues with
mutual dependencies between malloc and libthr in principle. The
drawback is that some more allocations are not interceptable for
alternate malloc implementations. There should be not too much memory
use from this allocator, and the alternative, direct use of mmap(2) is
obviously worse.

PR: 235211
MFC after: 2 weeks
Sponsored by: The FreeBSD Foundation
Differential revision: https://reviews.freebsd.org/D18988


# 52af9967 03-Dec-2018 Brooks Davis <brooks@FreeBSD.org>

Remove declarations of syscalls not used in libthr.

Reviewed by: kib
Sponsored by: DARPA, AFRL


# ad8c236b 29-Nov-2018 Eric van Gyzen <vangyzen@FreeBSD.org>

_thr_setthreaded() cannot fail; change return type to void

Also remove logic to avoid unnecessary stores to the global variable.
Thread creation and destruction are heavy enough that any supposed savings
is in the noise.

MFC after: 2 weeks
Sponsored by: Dell EMC Isilon


# 4627d47b 17-Aug-2018 Konstantin Belousov <kib@FreeBSD.org>

Add pthread_get_name_np(3).

The function retrieves the thread name previously set by
pthread_set_name_np(3). The name is cached in the process memory.

Requested by: Willem Jan Withagen <wjw@digiware.nl>
Man page update: Yuri Pankov <yuripv@yuripv.net>
Reviewed by: ian (previous version)
Discussed with: arichardson, bjk (man page)
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks
Differential revision: https://reviews.freebsd.org/D16702


# 5e53a4f9 25-Nov-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

lib: further adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 2-Clause license, however the tool I
was using mis-identified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.


# 37a6f461 02-Oct-2017 Michael Zhilin <mizhka@FreeBSD.org>

[libthr] revert change of visibility of _thread_keytable to unbreak debugger

Fix regression by r318539. The sysutils/pstack uses library libthread_db to
read information about threads state. The function pt_ta_new makes lookup of
several key symbols including _thread_keytable. But r318539 mades this field
static. It causes silent ignore of libthr library by pstack and as result
sysutils/pstack doesn't output any thread information.

This fix changes this field back to non-static.

MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D11738


# 718fb5ba 26-May-2017 Eric van Gyzen <vangyzen@FreeBSD.org>

libthr: fix warnings at WARNS=6

Fix more warnings about redundant declarations.

Reviewed by: kib emaste
MFC after: 3 days
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D10932


# 5a6d7b72 23-May-2017 Eric van Gyzen <vangyzen@FreeBSD.org>

libthr: fix warnings from GCC when WARNS=6

Fix warnings about:
- redundant declarations
- a local variable shadowing a global function (dlinfo)
- an old-style function definition (with an empty parameter list)
- a variable that is possibly used uninitialized

"make tinderbox" passes this time, except for a few unrelated
kernel failures.

Reviewed by: kib
MFC after: 3 days
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D10870


# 3f8455b0 18-Mar-2017 Eric van Gyzen <vangyzen@FreeBSD.org>

Add clock_nanosleep()

Add a clock_nanosleep() syscall, as specified by POSIX.
Make nanosleep() a wrapper around it.

Attach the clock_nanosleep test from NetBSD. Adjust it for the
FreeBSD behavior of updating rmtp only when interrupted by a signal.
I believe this to be POSIX-compliant, since POSIX mentions the rmtp
parameter only in the paragraph about EINTR. This is also what
Linux does. (NetBSD updates rmtp unconditionally.)

Copy the whole nanosleep.2 man page from NetBSD because it is complete
and closely resembles the POSIX description. Edit, polish, and reword it
a bit, being sure to keep any relevant text from the FreeBSD page.

Reviewed by: kib, ngie, jilles
MFC after: 3 weeks
Relnotes: yes
Sponsored by: Dell EMC
Differential Revision: https://reviews.freebsd.org/D10020


# 649702c5 28-Jan-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

Make use of clang nullability attributes.

Replace uses of the GCC __nonnull__ attribute with the clang nullability
qualifiers. The replacement should be transparent for clang developers as
the new qualifiers will produce the same warnings and will be useful for
static checkers but will not cause aggressive optimizations.

GCC will not produce such warnings and developers will have to use
upgraded GCC ports built with the system headers from r312538.

Hinted by: Apple's Libc-1158.20.4, Bionic libc
MFC after: 11.1 Release

Differential Revision: https://reviews.freebsd.org/D9004


# bf890e48 27-Jul-2016 Konstantin Belousov <kib@FreeBSD.org>

Remove empty initializer for the once facility. It was not needed
since r179417.

Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 3a7d122f 01-Jun-2016 Conrad Meyer <cem@FreeBSD.org>

libthr: Add vprintf variant of _thread_printf, formatted PANIC()

No ABI change.

Reviewed by: kib
Sponsored by: EMC / Isilon Storage Division
Differential Revision: https://reviews.freebsd.org/D6672


# 6180f50b 29-May-2016 Konstantin Belousov <kib@FreeBSD.org>

Stop inlining the struct ucond definition into struct pthread_cond.
This avoids unneccessary casts and make the calls to _thr_ucond_*()
functions less questionable.

The c_spare field was not included into struct pthread_cond, so the
change modifies libthr ABI for shared condvars. But since an off-page
does not legitimately contains any other data past the struct
pthread_cond, the change keeps shared condvars from pre- and post-
changed libthr compatible. Also note that the whole struct ucond was
never copied in or out by kernel.

For private condvars, the privately allocated memory was never exposed
outside libthr.

Sponsored by: The FreeBSD Foundation


# 2a339d9e 17-May-2016 Konstantin Belousov <kib@FreeBSD.org>

Add implementation of robust mutexes, hopefully close enough to the
intention of the POSIX IEEE Std 1003.1TM-2008/Cor 1-2013.

A robust mutex is guaranteed to be cleared by the system upon either
thread or process owner termination while the mutex is held. The next
mutex locker is then notified about inconsistent mutex state and can
execute (or abandon) corrective actions.

The patch mostly consists of small changes here and there, adding
neccessary checks for the inconsistent and abandoned conditions into
existing paths. Additionally, the thread exit handler was extended to
iterate over the userspace-maintained list of owned robust mutexes,
unlocking and marking as terminated each of them.

The list of owned robust mutexes cannot be maintained atomically
synchronous with the mutex lock state (it is possible in kernel, but
is too expensive). Instead, for the duration of lock or unlock
operation, the current mutex is remembered in a special slot that is
also checked by the kernel at thread termination.

Kernel must be aware about the per-thread location of the heads of
robust mutex lists and the current active mutex slot. When a thread
touches a robust mutex for the first time, a new umtx op syscall is
issued which informs about location of lists heads.

The umtx sleep queues for PP and PI mutexes are split between
non-robust and robust.

Somewhat unrelated changes in the patch:
1. Style.
2. The fix for proper tdfind() call use in umtxq_sleep_pi() for shared
pi mutexes.
3. Removal of the userspace struct pthread_mutex m_owner field.
4. The sysctl kern.ipc.umtx_vnode_persistent is added, which controls
the lifetime of the shared mutex associated with a vnode' page.

Reviewed by: jilles (previous version, supposedly the objection was fixed)
Discussed with: brooks, Martin Simmons <martin@lispworks.com> (some aspects)
Tested by: pho
Sponsored by: The FreeBSD Foundation


# 841ecd47 04-Apr-2016 Konstantin Belousov <kib@FreeBSD.org>

Remove unused variable. It was write-only before r297139.

Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 6044c03a 22-Mar-2016 Konstantin Belousov <kib@FreeBSD.org>

Apparently there are some popular programs around which assume that it
is safe to call pthread_mutex_init() on the same shared mutex several
times. POSIX claims that the behaviour in this case is undefined.

Make this working by only allowing one caller to initialize the mutex.
Other callers either see already completed initialization and do
nothing, or busy-loop yielding while designated initializer finishes.
Also make the API requirements loose by initializing mutexes on other
pthread_mutex*() calls if they see uninitialized shared mutex.

Only mutexes provide the hack for now, but it could be also
implemented for other process shared primitives from libthr.

Reported and tested by: "Oleg V. Nauman" <oleg@opentransfer.com>
Sponsored by: The FreeBSD Foundation


# 53fd961f 21-Mar-2016 Konstantin Belousov <kib@FreeBSD.org>

Lock pshared_lock shared around fork, to ensure that the COW snapshot
of the pshared hash in child is consistent and can be safely used.

Reported and tested by: "Oleg V. Nauman" <oleg@opentransfer.com>
Sponsored by: The FreeBSD Foundation


# 1bdbd705 28-Feb-2016 Konstantin Belousov <kib@FreeBSD.org>

Implement process-shared locks support for libthr.so.3, without
breaking the ABI. Special value is stored in the lock pointer to
indicate shared lock, and offline page in the shared memory is
allocated to store the actual lock.

Reviewed by: vangyzen (previous version)
Discussed with: deischen, emaste, jhb, rwatson,
Martin Simmons <martin@lispworks.com>
Tested by: pho
Sponsored by: The FreeBSD Foundation


# bd43f069 08-Feb-2016 Konstantin Belousov <kib@FreeBSD.org>

If libthr.so is dlopened without RTLD_GLOBAL flag, the libthr symbols
do not participate in the global symbols namespace, but rtld locks are
still replaced and functions are interposed. In particular,
__pthread_map_stacks_exec is resolved to the libc version. If a
library is loaded later, which requires adjustment of the stack
protection mode, rtld calls into libc __pthread_map_stacks_exec due to
the symbols scope. The libc version might recurse into binder and
recursively acquire rtld bind lock, causing the hang.

Make libc __pthread_map_stacks_exec() interposed, which synchronizes
rtld locks and version of the stack exec hook when libthr loaded,
regardless of the symbol scope control or symbol resolution order.

The __pthread_map_stacks_exec() symbol is removed from the private
version in libthr since libc symbol now operates correctly in presence
of libthr.

Reported and tested by: markj
Sponsored by: The FreeBSD Foundation
MFC after: 2 weeks


# 17981398 20-Dec-2015 Jilles Tjoelker <jilles@FreeBSD.org>

libthr: Don't use both __sys_open() and __sys_openat().


# be070eb8 09-Mar-2015 Jung-uk Kim <jkim@FreeBSD.org>

Fix a typo in comment and explain the reason.


# 45468c53 14-Feb-2015 Konstantin Belousov <kib@FreeBSD.org>

Properly interpose libc spinlocks, was missed in r276630. In
particular, stdio locking was affected.

Reported and tested by: "Matthew D. Fuller" <fullermd@over-yonder.net>
Sponsored by: The FreeBSD Foundation
MFC after: 3 days


# 20fe2c94 21-Jan-2015 Andrew Turner <andrew@FreeBSD.org>

Merge all the copies of _tcb_ctor and _tcb_dtor.

The amd64, i386, and sparc64 versions were identical, with the one
difference where the former two used inline asm instead of _tcb_get. I
have compared the function before and after replacing the asm with _tcb_get
and found the object files to be identical.

The arm, mips, and powerpc versions were almost identical. The only
difference was the powerpc version used an alignment of 1 where arm and
mips used 16. As this is an increase in alignment is will be safe.

Along with this arm, mips, and powerpc all passed, when initial was true,
the value returned from _tcb_get as the first argument to
_rtld_allocate_tls. This would then return this pointer back to the caller.
We can remove these extra calls by checking if initial is set and setting
the thread control block directly. As this is what the sparc64 code does
we can use it directly.

As after these observations all the architectures can now have identical
code we can merge them into a common file.

Differential Revision: https://reviews.freebsd.org/D1556
Reviewed by: kib
Sponsored by: The FreeBSD Foundation


# 397d851d 11-Jan-2015 Konstantin Belousov <kib@FreeBSD.org>

Reduce the size of the interposing table and amount of
cancellation-handling code in the libthr. Translate some syscalls
into their more generic counterpart, and remove translated syscalls
from the table.

List of the affected syscalls:
creat, open -> openat
raise -> thr_kill
sleep, usleep -> nanosleep
pause -> sigsuspend
wait, wait3, waitpid -> wait4

Suggested and reviewed by: jilles (previous version)
Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 1a744fef 04-Jan-2015 Konstantin Belousov <kib@FreeBSD.org>

Avoid calling internal libc function through PLT or accessing data
though GOT, by staticizing and hiding. Add setter for
__error_selector to hide it as well.

Suggested and reviewed by: jilles
Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 8495e8b1 03-Jan-2015 Konstantin Belousov <kib@FreeBSD.org>

Fix known issues which blow up the process after dlopen("libthr.so")
(or loading a dso linked to libthr.so into process which was not
linked against threading library).

- Remove libthr interposers of the libc functions, including
__error(). Instead, functions calls are indirected through the
interposing table, similar to how pthread stubs in libc are already
done. Libc by default points either to syscall trampolines or to
existing libc implementations. On libthr load, libthr rewrites the
pointers to the cancellable implementations already in libthr. The
interposition table is separate from pthreads stubs indirection
table to not pull pthreads stubs into static binaries.

- Postpone the malloc(3) internal mutexes initialization until libthr
is loaded. This avoids recursion between calloc(3) and static
pthread_mutex_t initialization.

- Reinstall signal handlers with wrapper on libthr load. The
_rtld_is_dlopened(3) is used to avoid useless calls to sigaction(2)
when libthr is statically referenced from the main binary.

In the process, fix openat(2), swapcontext(2) and setcontext(2)
interposing. The libc symbols were exported at different versions
than libthr interposers. Export both libc and libthr versions from
libc now, with default set to the higher version from libthr.

Remove unused and disconnected swapcontext(3) userspace implementation
from libc/gen.

No objections from: deischen
Tested by: pho, antoine (exp-run) (previous versions)
Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 294246bb 24-Nov-2014 Ed Maste <emaste@FreeBSD.org>

Revert r274772: it is not valid on MIPS

Reported by: sbruno


# 688fd61a 20-Nov-2014 Ed Maste <emaste@FreeBSD.org>

Use canonical __PIC__ flag

It is automatically set when -fPIC is passed to the compiler.

Reviewed by: dim, kib
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D1179


# 0a9655a0 23-Nov-2013 Konstantin Belousov <kib@FreeBSD.org>

If check_deferred_signal() execution needs binding of PLT symbol,
unlocking the rtld bind lock results in the processing of ast and
recursing into the check_deferred_signal(). Nested execution of
check_deferred_signal() delivers the signal to user code and clears
si_signo. On return, top-level check_deferred_signal() frame
continues delivering the same signal one more time, but now with zero
si_signo.

Fix this by adding a flag to indicate that deferred delivery is
running, so check_deferred_signal() should avoid doing anything. Since
user signal handler is allowed to modify the passed machine context to
make return from the signal handler to cause arbitrary jump, or do
longjmp(). For this case, also clear the flag in thr_sighandler(),
since kernel signal delivery means that nested delivery code should
not run right now.

Reported by: Vitaly Magerya <vmagerya@gmail.com>
Reviewed by: davidxu, jilles
Tested by: pho
Sponsored by: The FreeBSD Foundation
MFC after: 1 week


# 706b04b6 12-Apr-2013 Jilles Tjoelker <jilles@FreeBSD.org>

libthr: Remove _thr_rtld_fini(), unused since r245630.


# a7b84c65 26-Aug-2012 David Xu <davidxu@FreeBSD.org>

In suspend_common(), don't wait for a thread which is in creation, because
pthread_suspend_all_np() may have already suspended its parent thread.
Add locking code in pthread_suspend_all_np() to only allow one thread
to suspend other threads, this eliminates a deadlock where two or more
threads try to suspend each others.


# e220a13a 11-Aug-2012 David Xu <davidxu@FreeBSD.org>

MFp4:
Further decreases unexpected context switches by defering mutex wakeup
until internal sleep queue lock is released.


# 84ac0fb8 03-May-2012 David Xu <davidxu@FreeBSD.org>

MFp4:
Enqueue thread in LIFO, this can cause starvation, but it gives better
performance. Use _thr_queuefifo to control the frequency of FIFO vs LIFO,
you can use environment string LIBPTHREAD_QUEUE_FIFO to configure the
variable.


# 6e047a24 18-Apr-2012 George V. Neville-Neil <gnn@FreeBSD.org>

Set SIGCANCEL to SIGTHR as part of some cleanup of DTrace code.

Reviewed by: davidxu@
MFC after: 1 week


# 17ce6063 04-Apr-2012 David Xu <davidxu@FreeBSD.org>

umtx operation UMTX_OP_MUTEX_WAKE has a side-effect that it accesses
a mutex after a thread has unlocked it, it event writes data to the mutex
memory to clear contention bit, there is a race that other threads
can lock it and unlock it, then destroy it, so it should not write
data to the mutex memory if there isn't any waiter.
The new operation UMTX_OP_MUTEX_WAKE2 try to fix the problem. It
requires thread library to clear the lock word entirely, then
call the WAKE2 operation to check if there is any waiter in kernel,
and try to wake up a thread, if necessary, the contention bit is set again
by the operation. This also mitgates the chance that other threads find
the contention bit and try to enter kernel to compete with each other
to wake up sleeping thread, this is unnecessary. With this change, the
mutex owner is no longer holding the mutex until it reaches a point
where kernel umtx queue is locked, it releases the mutex as soon as
possible.
Performance is improved when the mutex is contensted heavily. On Intel
i3-2310M, the runtime of a benchmark program is reduced from 26.87 seconds
to 2.39 seconds, it even is better than UMTX_OP_MUTEX_WAKE which is
deprecated now. http://people.freebsd.org/~davidxu/bench/mutex_perf.c


# e70bf9d5 15-Mar-2012 David Xu <davidxu@FreeBSD.org>

When destroying a barrier, waiting all threads exit the barrier,
this makes it possible a thread received PTHREAD_BARRIER_SERIAL_THREAD
immediately free memory area of the barrier.


# e7004bf4 06-Feb-2012 David Xu <davidxu@FreeBSD.org>

Plug a memory leak. When a cached thread is reused, don't clear sleep
queue pointers, just reuse it.

PR: 164828
MFC after: 1 week


# da2fcff7 08-Jan-2011 Konstantin Belousov <kib@FreeBSD.org>

Implement the __pthread_map_stacks_exec() for libthr.

Stack creation code is changed to call _rtld_get_stack_prot() to get
the stack protection right. There is a race where thread is created
during dlopen() of dso that requires executable stacks. Then,
_rtld_get_stack_prot() may return PROT_READ | PROT_WRITE, but thread
is still not linked into the thread list. In this case, the callback
misses the thread stack, and rechecks the required protection
afterward.

Reviewed by: davidxu


# d1078b0b 21-Dec-2010 David Xu <davidxu@FreeBSD.org>

MFp4:

- Add flags CVWAIT_ABSTIME and CVWAIT_CLOCKID for umtx kernel based
condition variable, this should eliminate an extra system call to get
current time.

- Add sub-function UMTX_OP_NWAKE_PRIVATE to wake up N channels in single
system call. Create userland sleep queue for condition variable, in most
cases, thread will wait in the queue, the pthread_cond_signal will defer
thread wakeup until the mutex is unlocked, it tries to avoid an extra
system call and a extra context switch in time window of pthread_cond_signal
and pthread_mutex_unlock.

The changes are part of process-shared mutex project.


# 7f25f6c7 25-Oct-2010 David Xu <davidxu@FreeBSD.org>

Get cpuset in pthread_attr_get_np() and free it in pthread_attr_destroy().

MFC after: 7 days


# de1e74c6 19-Oct-2010 David Xu <davidxu@FreeBSD.org>

Revert revision 214007, I realized that MySQL wants to resolve
a silly rwlock deadlock problem, the deadlock is caused by writer
waiters, if a thread has already locked a reader lock, and wants to
acquire another reader lock, it will be blocked by writer waiters,
but we had already fixed it years ago.


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# a6b9b59e 17-Oct-2010 David Xu <davidxu@FreeBSD.org>

Add pthread_rwlockattr_setkind_np and pthread_rwlockattr_getkind_np, the
functions set or get pthread_rwlock type, current supported types are:
PTHREAD_RWLOCK_PREFER_READER_NP,
PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
PTHREAD_RWLOCK_PREFER_WRITER_NP,
default is PTHREAD_RWLOCK_PREFER_WRITER_NONCECURSIVE_NP, this maintains
binary compatible with old code.


# 72248801 29-Sep-2010 David Xu <davidxu@FreeBSD.org>

change code to use unwind.h.


# bbb64c21 27-Sep-2010 David Xu <davidxu@FreeBSD.org>

In current code, statically initialized and destroyed object have
same null value, the code can not distinguish between them, to
fix the problem, now a destroyed object is assigned to a non-null
value, and it will be rejected by some pthread functions.
PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP is changed to number 1, so that
adaptive mutex can be statically initialized correctly.


# 9f1dc4c1 25-Sep-2010 David Xu <davidxu@FreeBSD.org>

Add missing field.


# f4213b90 24-Sep-2010 David Xu <davidxu@FreeBSD.org>

To support stack unwinding for cancellation points, add -fexceptions flag
for them, two functions _pthread_cancel_enter and _pthread_cancel_leave
are added to let thread enter and leave a cancellation point, it also
makes it possible that other functions can be cancellation points in
libraries without having to be rewritten in libthr.


# 93ea4a71 24-Sep-2010 David Xu <davidxu@FreeBSD.org>

In most cases, cancel_point and cancel_async needn't be checked again,
because cancellation is almostly checked at cancellation points.


# 4173ebef 19-Sep-2010 David Xu <davidxu@FreeBSD.org>

Because atfork lock is held while forking, a thread cancellation triggered
by atfork handler is unsafe, use intenal flag no_cancel to disable it.


# 3832fd24 14-Sep-2010 David Xu <davidxu@FreeBSD.org>

add code to support stack unwinding when thread exits. note that only
defer-mode cancellation works, asynchrnous mode does not work because
it lacks of libuwind's support. stack unwinding is not enabled unless
LIBTHR_UNWIND_STACK is defined in Makefile.


# 707ee815 14-Sep-2010 David Xu <davidxu@FreeBSD.org>

Move back IN_GCLIST flag into field tlflags, since thread list and gc list
still share same lock.


# cbadc1d7 13-Sep-2010 David Xu <davidxu@FreeBSD.org>

Fix copy&paste problem.


# a9b764e2 13-Sep-2010 David Xu <davidxu@FreeBSD.org>

Convert thread list lock from mutex to rwlock.


# ada33a6e 31-Aug-2010 David Xu <davidxu@FreeBSD.org>

Change atfork lock from mutex to rwlock, also make mutexes used by malloc()
module private type, when private type mutex is locked/unlocked, thread
critical region is entered or leaved. These changes makes fork()
async-signal safe which required by POSIX. Note that user's atfork handler
still needs to be async-signal safe, but it is not problem of libthr, it
is user's responsiblity.


# 02c3c858 31-Aug-2010 David Xu <davidxu@FreeBSD.org>

Add signal handler wrapper, the reason to add it becauses there are
some cases we want to improve:
1) if a thread signal got a signal while in cancellation point,
it is possible the TDP_WAKEUP may be eaten by signal handler
if the handler called some interruptibly system calls.
2) In signal handler, we want to disable cancellation.
3) When thread holding some low level locks, it is better to
disable signal, those code need not to worry reentrancy,
sigprocmask system call is avoided because it is a bit expensive.
The signal handler wrapper works in this way:
1) libthr installs its signal handler if user code invokes sigaction
to install its handler, the user handler is recorded in internal
array.
2) when a signal is delivered, libthr's signal handler is invoke,
libthr checks if thread holds some low level lock or is in critical
region, if it is true, the signal is buffered, and all signals are
masked, once the thread leaves critical region, correct signal
mask is restored and buffered signal is processed.
3) before user signal handler is invoked, cancellation is temporarily
disabled, after user signal handler is returned, cancellation state
is restored, and pending cancellation is rescheduled.


# ed0ee6af 26-Aug-2010 David Xu <davidxu@FreeBSD.org>

Unregister thread specific data destructor when a corresponding dso
is unloaded.


# 1ac3d502 26-Aug-2010 David Xu <davidxu@FreeBSD.org>

eliminate unused code.


# 47536ff6 24-Aug-2010 Konstantin Belousov <kib@FreeBSD.org>

The __hidden definition is provided by sys/cdefs.h.

MFC after: 2 weeks


# 5cf22195 24-Aug-2010 David Xu <davidxu@FreeBSD.org>

Add wrapper for setcontext() and swapcontext(), the wrappers
unblock SIGCANCEL which is needed by thread cancellation.


# ea246b63 23-Aug-2010 Konstantin Belousov <kib@FreeBSD.org>

On shared object unload, in __cxa_finalize, call and clear all installed
atexit and __cxa_atexit handlers that are either installed by unloaded
dso, or points to the functions provided by the dso.

Use _rtld_addr_phdr to locate segment information from the address of
private variable belonging to the dso, supplied by crtstuff.c. Provide
utility function __elf_phdr_match_addr to do the match of address against
dso executable segment.

Call back into libthr from __cxa_finalize using weak
__pthread_cxa_finalize symbol to remove any atfork handler which
function points into unloaded object.

The rtld needs private __pthread_cxa_finalize symbol to not require
resolution of the weak undefined symbol at initialization time. This
cannot work, since rtld is relocated before sym_zero is set up.

Idea by: kan
Reviewed by: kan (previous version)
MFC after: 3 weeks


# 635f917a 19-Aug-2010 David Xu <davidxu@FreeBSD.org>

In current implementation, thread cancellation is done in signal handler,
which does not know what is the state of interrupted system call, for
example, open() system call opened a file and the thread is still cancelled,
result is descriptor leak, there are other problems which can cause resource
leak or undeterminable side effect when a thread is cancelled. However, this
is no longer true in new implementation.

In defering mode, a thread is canceled if cancellation request is pending and
later the thread enters a cancellation point, otherwise, a later
pthread_cancel() just causes SIGCANCEL to be sent to the target thread, and
causes target thread to abort system call, userland code in libthr then checks
cancellation state, and cancels the thread if needed. For example, the
cancellation point open(), the thread may be canceled at start,
but later, if it opened a file descriptor, it is not canceled, this avoids
file handle leak. Another example is read(), a thread may be canceled at start
of the function, but later, if it read some bytes from a socket, the thread
is not canceled, the caller then can decide if it should still enable cancelling
or disable it and continue reading data until it thinks it has read all
bytes of a packet, and keeps a protocol stream in health state, if user ignores
partly reading of a packet without disabling cancellation, then second iteration
of read loop cause the thread to be cancelled.
An exception is that the close() cancellation point always closes a file handle
despite whether the thread is cancelled or not.

The old mechanism is still kept, for a functions which is not so easily to
fix a cancellation problem, the rough mechanism is used.

Reviewed by: kib@


# fe0506d7 09-Mar-2010 Marcel Moolenaar <marcel@FreeBSD.org>

Create the altix project branch. The altix project will add support
for the SGI Altix 350 to FreeBSD/ia64. The hardware used for porting
is a two-module system, consisting of a base compute module and a
CPU expansion module. SGI's NUMAFlex architecture can be an excellent
platform to test CPU affinity and NUMA-aware features in FreeBSD.


# 9b0f1823 04-Jan-2010 David Xu <davidxu@FreeBSD.org>

Use umtx to implement process sharable semaphore, to make this work,
now type sema_t is a structure which can be put in a shared memory area,
and multiple processes can operate it concurrently.
User can either use mmap(MAP_SHARED) + sem_init(pshared=1) or use sem_open()
to initialize a shared semaphore.
Named semaphore uses file system and is located in /tmp directory, and its
file name is prefixed with 'SEMD', so now it is chroot or jail friendly.
In simplist cases, both for named and un-named semaphore, userland code
does not have to enter kernel to reduce/increase semaphore's count.
The semaphore is designed to be crash-safe, it means even if an application
is crashed in the middle of operating semaphore, the semaphore state is
still safely recovered by later use, there is no waiter counter maintained
by userland code.
The main semaphore code is in libc and libthr only has some necessary stubs,
this makes it possible that a non-threaded application can use semaphore
without linking to thread library.
Old semaphore implementation is kept libc to maintain binary compatibility.
The kernel ksem API is no longer used in the new implemenation.

Discussed on: threads@


# 29670497 11-Oct-2009 Jilles Tjoelker <jilles@FreeBSD.org>

Make openat(2) a cancellation point.

This is required by POSIX and matches open(2).

Reviewed by: kib, jhb
MFC after: 1 month


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# 83a07587 08-Jun-2008 David Xu <davidxu@FreeBSD.org>

Make pthread_cleanup_push() and pthread_cleanup_pop() as a pair of macros,
use stack space to keep cleanup information, this eliminates overhead of
calling malloc() and free() in thread library.

Discussed on: thread@


# 850f4d66 29-May-2008 David Xu <davidxu@FreeBSD.org>

- Reduce function call overhead for uncontended case.
- Remove unused flags MUTEX_FLAGS_* and their code.
- Check validity of the timeout parameter in mutex_self_lock().


# 8d6a11a0 28-Apr-2008 David Xu <davidxu@FreeBSD.org>

Use UMTX_OP_WAIT_UINT_PRIVATE and UMTX_OP_WAKE_PRIVATE to save
time in kernel(avoid VM lookup).


# caad30a4 02-Apr-2008 David Xu <davidxu@FreeBSD.org>

put THR_CRITICAL_LEAVE into do .. while statement.


# a6cba940 02-Apr-2008 David Xu <davidxu@FreeBSD.org>

add __hidden suffix to _umtx_op_err, this eliminates PLT.


# 7a30bcf0 02-Apr-2008 David Xu <davidxu@FreeBSD.org>

Add pthread_setaffinity_np and pthread_getaffinity_np to libc namespace.


# 8b873a23 02-Apr-2008 David Xu <davidxu@FreeBSD.org>

Remove unused functions.


# d6e0eb0a 02-Apr-2008 David Xu <davidxu@FreeBSD.org>

Replace function _umtx_op with _umtx_op_err, the later function directly
returns errno, because errno can be mucked by user's signal handler and
most of pthread api heavily depends on errno to be correct, this change
should improve stability of the thread library.


# 8bf1a48c 01-Apr-2008 David Xu <davidxu@FreeBSD.org>

Replace userland rwlock with a pure kernel based rwlock, the new
implementation does not switch pointers when it resumes waiters.

Asked by: jeff


# 5ab512bb 30-Mar-2008 David Xu <davidxu@FreeBSD.org>

Rewrite rwlock to user atomic operations to change rwlock state, this
eliminates internal mutex lock contention when most rwlock operations
are read.

Orignal patch provided by: jeff


# 54dff16b 05-Mar-2008 David Xu <davidxu@FreeBSD.org>

Use cpuset defined in pthread_attr for newly created thread, for now,
we set scheduling parameters and cpu binding fully in userland, and
because default scheduling policy is SCHED_RR (time-sharing), we set
default sched_inherit to PTHREAD_SCHED_INHERIT, this saves a system
call.


# 76a9679f 03-Mar-2008 David Xu <davidxu@FreeBSD.org>

If a new thread is created, it inherits current thread's signal masks,
however if current thread is executing cancellation handler, signal
SIGCANCEL may have already been blocked, this is unexpected, unblock the
signal in new thread if this happens.

MFC after: 1 week


# 54c9b47c 03-Mar-2008 David Xu <davidxu@FreeBSD.org>

Include cpuset.h, unbreak compiling.


# a759db94 03-Mar-2008 David Xu <davidxu@FreeBSD.org>

implement pthread_attr_getaffinity_np and pthread_attr_setaffinity_np.


# 093fcf16 13-Dec-2007 David Xu <davidxu@FreeBSD.org>

1. Add function pthread_mutex_setspinloops_np to turn a mutex's spin
loop count.
2. Add function pthread_mutex_setyieldloops_np to turn a mutex's yield
loop count.
3. Make environment variables PTHREAD_SPINLOOPS and PTHREAD_YIELDLOOPS
to be only used for turnning PTHREAD_MUTEX_ADAPTIVE_NP mutex.


# 6fdfcacb 20-Nov-2007 David Xu <davidxu@FreeBSD.org>

Remove umtx_t definition, use type long directly, add wrapper function
_thr_umtx_wait_uint() for umtx operation UMTX_OP_WAIT_UINT, use the
function in semaphore operations, this fixed compiler warnings.


# 7416cdab 29-Oct-2007 David Xu <davidxu@FreeBSD.org>

Add my recent work of adaptive spin mutex code. Use two environments variable
to tune pthread mutex performance:
1. LIBPTHREAD_SPINLOOPS
If a pthread mutex is being locked by another thread, this environment
variable sets total number of spin loops before the current thread
sleeps in kernel, this saves a syscall overhead if the mutex will be
unlocked very soon (well written application code).
2. LIBPTHREAD_YIELDLOOPS
If a pthread mutex is being locked by other threads, this environment
variable sets total number of sched_yield() loops before the currrent
thread sleeps in kernel. if a pthread mutex is locked, the current thread
gives up cpu, but will not sleep in kernel, this means, current thread
does not set contention bit in mutex, but let lock owner to run again
if the owner is on kernel's run queue, and when lock owner unlocks the
mutex, it does not need to enter kernel and do lots of work to resume
mutex waiters, in some cases, this saves lots of syscall overheads for
mutex owner.

In my practice, sometimes LIBPTHREAD_YIELDLOOPS can massively improve performance
than LIBPTHREAD_SPINLOOPS, this depends on application. These two environments
are global to all pthread mutex, there is no interface to set them for each
pthread mutex, the default values are zero, this means spinning is turned off
by default.


# 4aa80591 06-Aug-2007 David Xu <davidxu@FreeBSD.org>

Output error message to STDERR_FILENO.

Approved by: re (bmah)


# 842a092b 19-Dec-2006 David Xu <davidxu@FreeBSD.org>

Check environment variable PTHREAD_ADAPTIVE_SPIN, if it is set, use
it as a default spin cycle count.


# d99f6dac 15-Dec-2006 David Xu <davidxu@FreeBSD.org>

- Remove variable _thr_scope_system, all threads are system scope.
- Rename _thr_smp_cpus to boolean variable _thr_is_smp.
- Define CPU_SPINWAIT macro for each arch, only X86 supports it.


# 4d617f2d 04-Dec-2006 David Xu <davidxu@FreeBSD.org>

Use ucond to implement barrier.


# 2bd2c907 04-Dec-2006 David Xu <davidxu@FreeBSD.org>

Use kernel provided userspace condition variable to implement pthread
condition variable.


# f08e1bf6 24-Nov-2006 David Xu <davidxu@FreeBSD.org>

Eliminate atomic operations in thread cancellation functions, it should
reduce overheads of cancellation points.


# 0b90fa4a 12-Oct-2006 David Xu <davidxu@FreeBSD.org>

Use type pthread_state for thread state.


# e6747c7c 20-Sep-2006 David Xu <davidxu@FreeBSD.org>

use rtprio_thread system call to get or set thread priority.


# bddd24cd 05-Sep-2006 David Xu <davidxu@FreeBSD.org>

Replace internal usage of struct umtx with umutex which can supports
real-time if we want, no functionality is changed.


# 8ab9d78b 27-Aug-2006 David Xu <davidxu@FreeBSD.org>

Use umutex APIs to implement pthread_mutex, member pp_mutexq is added
into pthread structure to keep track of locked PTHREAD_PRIO_PROTECT mutex,
no real mutex code is changed, the mutex locking and unlocking code should
has same performance as before.


# 065dbdc1 07-Aug-2006 David Xu <davidxu@FreeBSD.org>

Axe unused member field.


# 6b73f085 07-Aug-2006 David Xu <davidxu@FreeBSD.org>

Get number of CPUs and ignore spin count on single processor machine.


# 7b4f8f03 12-Jul-2006 David Xu <davidxu@FreeBSD.org>

Use kernel facilities to support real-time scheduling.


# 7fabe0b5 02-Jun-2006 David Xu <davidxu@FreeBSD.org>

Remove unused member.


# b971a730 02-Jun-2006 David Xu <davidxu@FreeBSD.org>

Remove unused member field m_queue.


# 0a5fa455 27-Apr-2006 David Xu <davidxu@FreeBSD.org>

s/long/int.


# 245116ca 27-Apr-2006 David Xu <davidxu@FreeBSD.org>

- Use same priority range returned by kernel's sched_get_priority_min()
and sched_get_priority_max() syscalls.
- Remove unused fields from structure pthread_attr.


# a9794459 08-Apr-2006 David Xu <davidxu@FreeBSD.org>

Do not check validity of timeout if a mutex can be acquired immediately.
Completly drop recursive mutex in pthread_cond_wait and restore recursive
after resumption. Reorganize code to make gcc to generate better code.


# 37a6356b 03-Apr-2006 David Xu <davidxu@FreeBSD.org>

WARNS level 4 cleanup.


# 9ad4b644 27-Mar-2006 David Xu <davidxu@FreeBSD.org>

Remove priority mutex code because it does not work correctly,
to make it work, turnstile like mechanism to support priority
propagating and other realtime scheduling options in kernel
should be available to userland mutex, for the moment, I just
want to make libthr be simple and efficient thread library.

Discussed with: deischen, julian


# b6b894f6 24-Mar-2006 David Xu <davidxu@FreeBSD.org>

Add locking support for rtld.


# 6f716c2f 15-Feb-2006 David Xu <davidxu@FreeBSD.org>

Rework last change of pthread_once, create a function _thr_once_init to
reinitialize its internal locks.


# c5d2fb8d 15-Feb-2006 David Xu <davidxu@FreeBSD.org>

After fork(), reinitialize internal locks for pthread_once().


# 8956297a 04-Feb-2006 David Xu <davidxu@FreeBSD.org>

Now, thread name is stored in kernel, userland no longer has to keep it.


# 9572a734 09-Jan-2006 David Xu <davidxu@FreeBSD.org>

Use macro STATIC_LIB_REQUIRE to declare a symbol should be linked into
static binary.


# a53747d8 09-Jan-2006 David Xu <davidxu@FreeBSD.org>

Tweak macro THR_LOCK_RELEASE a bit for non-PTHREAD_INVARIANTS case.


# bc414752 05-Jan-2006 David Xu <davidxu@FreeBSD.org>

Refine thread suspension code, now thread suspension is a blockable
operation, the caller is blocked util target threads are really
suspended, also avoid suspending a thread when it is holding a
critical lock.
Fix a bug in _thr_ref_delete which tests a never set flag.


# 8429e734 20-Dec-2005 David Xu <davidxu@FreeBSD.org>

Hide umtx API symbols as well.


# cf905a15 20-Dec-2005 David Xu <davidxu@FreeBSD.org>

1. Retire macro SCLASS, instead simply use language keyword and
put variables in thr_init.c.
2. Hide all global symbols which won't be exported.


# df2cf821 17-Dec-2005 David Xu <davidxu@FreeBSD.org>

Update copyright.


# 53bbdf86 31-Oct-2005 David Xu <davidxu@FreeBSD.org>

Add code to handle timer_delete(). The timer wrapper code is completely
rewritten, now timers created with same sigev_notify_attributes will
run in same thread, this allows user to organize which timers can
run in same thread to save some thread resource.


# 3adc17c5 22-Jun-2005 David Xu <davidxu@FreeBSD.org>

Fix off-by-one nanosecond bug in macro TIMESPEC_ADD.

Reviewed by: deischen
Approved by: re (dwhite)
MFC after : 4 days


# d245d9e1 11-Apr-2005 David Xu <davidxu@FreeBSD.org>

Add debugger event reporting support, current only TD_CREATE and TD_DEATH
events are reported.


# bc1eb018 06-Apr-2005 David Xu <davidxu@FreeBSD.org>

Remove unique id field which is no longer used by debugger.


# a091d823 01-Apr-2005 David Xu <davidxu@FreeBSD.org>

Import my recent 1:1 threading working. some features improved includes:
1. fast simple type mutex.
2. __thread tls works.
3. asynchronous cancellation works ( using signal ).
4. thread synchronization is fully based on umtx, mainly, condition
variable and other synchronization objects were rewritten by using
umtx directly. those objects can be shared between processes via
shared memory, it has to change ABI which does not happen yet.
5. default stack size is increased to 1M on 32 bits platform, 2M for
64 bits platform.
As the result, some mysql super-smack benchmarks show performance is
improved massivly.

Okayed by: jeff, mtm, rwatson, scottl


# c5a6625e 06-Mar-2005 Joe Marcus Clarke <marcus@FreeBSD.org>

Increase the default stacksizes:

32-bit 64-bit
main thread 2 MB 4 MB
other threads 1 MB 2 MB

Approved by: mtm
Adapted from: libpthread


# 6004362e 26-Nov-2004 David Schultz <das@FreeBSD.org>

Don't include sys/user.h merely for its side-effect of recursively
including other headers.


# 737afa3c 16-Sep-2004 Mike Makonnen <mtm@FreeBSD.org>

Implement cancellation points in libc interfaces, as specified by POSIX.


# 9027ac47 19-Aug-2004 David Xu <davidxu@FreeBSD.org>

Adjust code to support AMD64, on AMD64, thread needs to set fsbase by
itself before it can execute any other code, so new thread should be
created with all signals are masked until after fsbase is set.


# 7ea419c0 17-Jul-2004 Marcel Moolenaar <marcel@FreeBSD.org>

Define _libthr_debug for use by libthread_db.


# cd28f17d 01-Jul-2004 Marcel Moolenaar <marcel@FreeBSD.org>

Change the thread ID (thr_id_t) used for 1:1 threading from being a
pointer to the corresponding struct thread to the thread ID (lwpid_t)
assigned to that thread. The primary reason for this change is that
libthr now internally uses the same ID as the debugger and the kernel
when referencing to a kernel thread. This allows us to implement the
support for debugging without additional translations and/or mappings.

To preserve the ABI, the 1:1 threading syscalls, including the umtx
locking API have not been changed to work on a lwpid_t. Instead the
1:1 threading syscalls operate on long and the umtx locking API has
not been changed except for the contested bit. Previously this was
the least significant bit. Now it's the most significant bit. Since
the contested bit should not be tested by userland, this change is
not expected to be visible. Just to be sure, UMTX_CONTESTED has been
removed from <sys/umtx.h>.

Reviewed by: mtm@
ABI preservation tested on: i386, ia64


# 03d74100 27-Jun-2004 Mike Makonnen <mtm@FreeBSD.org>

Implement pthread_atfork in libthr. This is mostly from deichen's
work in libpthread.

Submitted by: Dan Nelson <dnelson@allantgroup.com>


# 4cd18a22 19-May-2004 Mike Makonnen <mtm@FreeBSD.org>

Make libthr async-signal-safe without costly signal masking. The guidlines I
followed are: Only 3 functions (pthread_cancel, pthread_setcancelstate,
pthread_setcanceltype) are required to be async-signal-safe by POSIX. None of
the rest of the pthread api is required to be async-signal-safe. This means
that only the three mentioned functions are safe to use from inside
signal handlers.
However, there are certain system/libc calls that are
cancellation points that a caller may call from within a signal handler,
and since they are cancellation points calls have to be made into libthr
to test for cancellation and exit the thread if necessary. So, the
cancellation test and thread exit code paths must be async-signal-safe
as well. A summary of the changes follows:

o Almost all of the code paths that masked signals, as well as locking the
pthread structure now lock only the pthread structure.
o Signals are masked (and left that way) as soon as a thread enters
pthread_exit().
o The active and dead threads locks now explicitly require that signals
are masked.
o Access to the isdead field of the pthread structure is protected by both
the active and dead list locks for writing. Either one is sufficient for
reading.
o The thread state and type fields have been combined into one three-state
switch to make it easier to read without requiring a lock. It doesn't need
a lock for writing (and therefore for reading either) because only the
current thread can write to it and it is an integer value.
o The thread state field of the pthread structure has been eliminated. It
was an unnecessary field that mostly duplicated the flags field, but
required additional locking that would make a lot more code paths require
signal masking. Any truly unique values (such as PS_DEAD) have been
reborn as separate members of the pthread structure.
o Since the mutex and condvar pthread functions are not async-signal-safe
there is no need to muck about with the wait queues when handling
a signal ...
o ... which also removes the need for wrapping signal handlers and sigaction(2).
o The condvar and mutex async-cancellation code had to be revised as a result
of some of these changes, which resulted in semi-unrelated changes which
would have been difficult to work on as a separate commit, so they are
included as well.

The only part of the changes I am worried about is related to locking for
the pthread joining fields. But, I will take a closer look at them once this
mega-patch is committed.


# 0465e53d 28-Mar-2004 Mike Makonnen <mtm@FreeBSD.org>

o Remove more references to SIGTHR
o Remove clock resolution information left over from libc_r


# 1c6f6301 28-Mar-2004 Mike Makonnen <mtm@FreeBSD.org>

Remove the garbage collector thread. All resources are freed
in-line. If the exiting thread cannot release a resource, then
the next thread to exit will release it.


# 7c8aa413 27-Mar-2004 Mike Makonnen <mtm@FreeBSD.org>

Stop using signals for synchronizing threads. The performance penalty
was too much.


# d4d7df5c 19-Feb-2004 Mike Makonnen <mtm@FreeBSD.org>

Implement PThreads barriers and barrier attributes.


# a561651c 18-Feb-2004 Mike Makonnen <mtm@FreeBSD.org>

o Refactor and, among other things, get rid of insane nesting levels.
o Fix mutex priority protocols. Keep separate counts of priority
inheritance and protection mutexes to make things easier.
This will not have much affect since this is only the
userland side, and the rest involves kernel scheduling.


# dec04f43 22-Jan-2004 Mike Makonnen <mtm@FreeBSD.org>

o Implement the pthread_spin_* functions in libthr.
o Man pages


# c40bafac 19-Jan-2004 Mike Makonnen <mtm@FreeBSD.org>

Implement reference counting of read-write locks. This uses
a list in the thread structure to keep track of the locks and
how many times they have been locked. This list is checked
on every lock and unlock. The traversal through the list is
O(n). Most applications don't hold so many locks at once that
this will become a problem. However, if it does become a problem
it might be a good idea to review this once libthr is
off probation and in the optimization cycle.
This fixes:
o deadlock when a thread tries to recursively acquire a
read lock when a writer is waiting on the lock.
o a thread could previously successfully unlock a lock it did not own
o deadlock when a thread tries to acquire a write lock on
a lock it already owns for reading or writing [ this is admittedly
not required by POSIX, but is nice to have ]


# 2b33fc64 30-Dec-2003 Mike Makonnen <mtm@FreeBSD.org>

Make it possible for the library to specify a timeout value when
waiting on a locked mutex. This involves passing a struct timespec
from the pthread mutex locking interfaces all the way down to the
function that suspends the thread until the mutex is released.
The timeout is assumed to be an absolute time (i.e. not relative to
the current time).

Also, in _thread_suspend() make the passed in timespec const.


# f2c3dd08 26-Dec-2003 Mike Makonnen <mtm@FreeBSD.org>

Preparations to make libthr work in multi-threaded fork()ing applications.

o Remove some code duplication between _thread_init(), which is run once
to initialize libthr and the intitial thread, and pthread_create(), which
initializes newly created threads, into a new function called from both
places: init_td_common()
o Move initialization of certain parts of libthr into a separate
function. These include:
- Active threads list and it's lock
- Dead threads list and it's lock & condition variable
- Naming and insertion of the initial thread into the
active threads list.


# 8657fd16 14-Dec-2003 Mike Makonnen <mtm@FreeBSD.org>

Remove _giant_mutex and its associated macros.


# d214f029 09-Dec-2003 Mike Makonnen <mtm@FreeBSD.org>

Take a stab at fixing some of the macro-nightmare.
PTHREAD_NEW_STATE should work as expected now: a thread
marked PS_RUNNING will get sent a SIGTHR.
Still more cleanups necessary.


# 89552201 09-Dec-2003 Mike Makonnen <mtm@FreeBSD.org>

Fix the wrapper function around signals so that a signal handling
thread on one of the mutex or condition variable queues is removed
from those queues before the real signal handler is called.


# 4a7709c5 09-Dec-2003 Mike Makonnen <mtm@FreeBSD.org>

o Add a wrapper around sigaction(2), so we can insert our own wrapper
around signals.
o Lock the process global signal action table.


# 393441d4 08-Jul-2003 Mike Makonnen <mtm@FreeBSD.org>

When _PTHREADSINVARIANTS is defined SIGABRT is not included
in the set of signals to block.
Also, make the PANIC macro call abort() instead of simply
exiting.


# 659045ff 06-Jul-2003 Mike Makonnen <mtm@FreeBSD.org>

Change all instances of THR_LOCK/UNLOCK, etc to UMTX_*.
It is a more acurate description of the locks they
operate on.


# 96440719 06-Jul-2003 Mike Makonnen <mtm@FreeBSD.org>

There's no need for _umtxtrylock to be a separate function.
Roll it into the pre-existing macro that's used to call it.


# 2234d5be 29-Jun-2003 Mike Makonnen <mtm@FreeBSD.org>

Locking primitives and operations in libthr should use struct umtx,
not spinlock_t. Spinlock_t and the associated functions and macros may
require blocking signals in order for async-safe libc functions to behave
appropriately in libthr. This is undesriable for libthr internal locking.
So, this is the first step in completely separating libthr from libc's
locking primitives.

Three new macros should be used for internal libthr locking from now on:
THR_LOCK, THR_TRYLOCK, THR_UNLOCK.


# c3650700 29-Jun-2003 Mike Makonnen <mtm@FreeBSD.org>

In a critical section, separate the aquisition of the thread lock
and the disabling of signals. What we are really interested in is
keeping track of recursive disabling of signals. We should not
be recursively acquiring thread locks. Any such situations should
be reorganized to not require a recursive lock.

Separating the two out also allows us to block signals independent of
acquiring thread locks. This will be needed in libthr in the near future when
we put the pieces together to protect libc functions that use pthread mutexes
and low level locks.


# 7c916264 29-Jun-2003 John Polstra <jdp@FreeBSD.org>

Make _thread_suspend work with both the old broken sigtimedwait
implementation and the new improved one. We now precompute the
signal set passed to sigtimedwait, using an inverted set when
necessary for compatibility with older kernels.


# 7e216068 28-Jun-2003 Mike Makonnen <mtm@FreeBSD.org>

The move to _retire() a thread in the GC instead of in the thread's
exit function has invalidated the need for _spin[un]lock_pthread().
The _spin[un]lock() functions can now dereference curthread without
the danger that the ldtentry containing the pointer to the thread
has been cleared out from under them.


# b9662ddd 03-Jun-2003 Mike Makonnen <mtm@FreeBSD.org>

Teach recent changes in the umtx structure in the kernel to the libthr
initialiazer.

Found by: tinderbox


# ca1c469c 25-May-2003 Mike Makonnen <mtm@FreeBSD.org>

Decouple the thread stack [de]allocating functions from the 'dead threads list'
lock. It's not really necessary and we don't need the added complexity
or potential for deadlocks.

Approved by: re/blanket libthr


# 12c407a4 25-May-2003 Mike Makonnen <mtm@FreeBSD.org>

Return gracefully, rather than aborting, when the maximum concurrent
threads per process has been reached. Return EAGAIN, as per spec.

Approved by: re/blanket libthr


# d39d6512 25-May-2003 Mike Makonnen <mtm@FreeBSD.org>

_pthread_cancel() breaks the normal lock order of first locking the
joined and then the joiner thread. There isn't an easy (sane?) way
to make it use the correct order without introducing races involving
the target thread and finding which (active or dead) list it is on. So,
after locking the canceled thread it will try to lock the joined thread
and if it fails release the first lock and try again from the top.

Introduce a new function, _spintrylock, which is simply a wrapper arround
umtx_trylock(), to help accomplish this.

Approved by: re/blanket libthr


# 71d09bc8 25-May-2003 Mike Makonnen <mtm@FreeBSD.org>

Start locking up the active and dead threads lists. The active threads
list is protected by a spinlock_t, but the dead list uses a pthread_mutex
because it is necessary to synchronize other threads with the garbage
collector thread. Lock/Unlock macros are used so it's easier to make
changes to the locks in the future.

The 'dead thread list' lock is intended to replace the gc mutex.
This doesn't have any practical ramifications. It simply makes it
clearer what the purpose of the lock is. The gc will use this lock,
instead of the gc mutex, to synchronize access to the dead list with
other threads.

Modify _pthread_exit() to use these two new locks instead of GIANT_LOCK,
and also to properly lock and protect thread state changes,
especially with respect to a joining thread.

The gc thread was also re-arranged to be more organized and less nested.

_pthread_join() was also modified to use the thread list locks. However,
locking and unlocking here needs special care because a thread could find
itself in a position where it's joining an exiting thread that is
waiting on the dead list lock, which this thread (joiner) holds. If the
joiner doesn't take care to lock *and* unlock in the same order they
(the joiner and the joinee) could deadlock against each other.

Approved by: re/blanket libthr


# 6a1899ed 25-May-2003 Mike Makonnen <mtm@FreeBSD.org>

The libthr code makes use of higher-level primitives (pthread_mutex_t and
pthread_cond_t) internaly in addition to the low-level spinlock_t. The
garbage collector mutex and condition variable are two such examples. This
might lead to critical sections nested within critical sections. Implement
a reference counting mechanism so that signals are masked only on the first
entry and unmasked on the last exit.

I'm not sure I like the idea of nested critical sections, but if
the library is going to use the pthread primitives it might be necessary.

Approved by: re/blanket libthr


# 59a47b31 23-May-2003 Mike Makonnen <mtm@FreeBSD.org>

Add two functions: _spinlock_pthread() and _spinunlock_pthread()
that take the address of a struct pthread as their first argument.
_spin[un]lock() just become wrappers arround these two functions.
These new functions are for use in situations where curthread can't be
used. One example is _thread_retire(), where we invalidate the array index
curthread uses to get its pointer..

Approved by: re/blanket libthr


# b32a99e5 23-May-2003 Mike Makonnen <mtm@FreeBSD.org>

EDOOFUS
Prevent one thread from messing up another thread's saved signal
mask by saving it in struct pthread instead of leaving it as a
global variable. D'oh!

Approved by: re/blanket libthr


# 7d9d7ca2 23-May-2003 Mike Makonnen <mtm@FreeBSD.org>

Make WARNS2 clean. The fixes mostly included:
o removed unused variables
o explicit inclusion of header files
o prototypes for externally defined functions

Approved by: re/blanket libthr


# c984b5a7 12-May-2003 Mike Makonnen <mtm@FreeBSD.org>

msg1


# 55ad402a 02-Apr-2003 Jake Burkholder <jake@FreeBSD.org>

- Pass a ucontext_t to _set_curthread. If non-NULL the new thread is set
as curthread in the new context, so that it will be set automatically when
the thread is switched to. This fixes a race where we'd run for a little
while with curthread unset in _thread_start.

Reviewed by: jeff


# 26f52e2f 01-Apr-2003 Jeff Roberson <jeff@FreeBSD.org>

- Define curthread as _get_curthread() and remove all direct calls to
_get_curthread(). This is similar to the kernel's curthread. Doing
this saves stack overhead and is more convenient to the programmer.
- Pass the pointer to the newly created thread to _thread_init().
- Remove _get_curthread_slow().


# 360a5194 01-Apr-2003 Jeff Roberson <jeff@FreeBSD.org>

- Restore old mutex code from libc_r. It is more standards compliant.
This was changed because originally we were blocking on the umtx and
allowing the kernel to do the queueing. It was decided that the
lib should queue and start the threads in the order it decides and the
umtx code would just be used like spinlocks.


# bb535300 31-Mar-2003 Jeff Roberson <jeff@FreeBSD.org>

- Add libthr but don't hook it up to the regular build yet. This is an
adaptation of libc_r for the thr system call interface. This is beta
quality code.