Commit graph

70 commits

Author SHA1 Message Date
Lenny Maiorani f912a48315 Userland: Change static const variables to static constexpr
`static const` variables can be computed and initialized at run-time
during initialization or the first time a function is called. Change
them to `static constexpr` to ensure they are computed at
compile-time.

This allows some removal of `strlen` because the length of the
`StringView` can be used which is pre-computed at compile-time.
2022-03-18 19:58:57 +01:00
Linus Groh e31d1620a8 LibPthread: Define SEM_FAILED in semaphore.h
This makes the _multiprocessing module from the Python port build. :^)
2022-03-16 18:03:52 +00:00
Ali Mohammad Pur 7eafa58af8 Revert "LibPthread: Partially implement pthread_cleanup_(push pop)"
This reverts commit 5d51e26caf.
The threadlocal Vector was somehow misaligned, causing UBSAN to be sad
about calling a misaligned method (either the dtor or .is_empty()) on
it.
For now, let's revert this and avoid the CI flake.
Fixes #12957.
2022-03-11 04:12:21 +03:30
Ali Mohammad Pur 5d51e26caf LibPthread: Partially implement pthread_cleanup_(push pop)
These are also supposed to run on cancellation, but we don't support
cancellation.
2022-03-08 22:29:53 +01:00
Ali Mohammad Pur 99f9860854 LibPthread: Add PTHREAD_CANCELED
This is just the expected return value of pthread_join() when it fails.
2022-02-12 12:16:59 +01:00
kleines Filmröllchen 2f50d8f4d3 AK+LibC+LibPthread: Introduce NoAllocationGuard
NoAllocationGuard is an RAII stack guard that prevents allocations
while it exists. This is done through a thread-local global flag which
causes malloc to crash on a VERIFY if it is false. The guard allows for
recursion.

The intended use case for this class is in real-time audio code. In such
code, allocations are really bad, and this is an easy way of dynamically
enforcing the no-allocations rule while giving the user good feedback if
it is violated. Before real-time audio code is executed, e.g. in LibDSP,
a NoAllocationGuard is instantiated. This is not done with this commit,
as currently some code in LibDSP may still incorrectly allocate in real-
time situations.

Other use cases for the Kernel have also been added, so this commit
builds on the previous to add the support both in Userland and in the
Kernel.
2022-01-11 00:08:58 +01:00
Brian Gianforcaro c6f745de27 LibPthread: Validate the clock argument in pthread_condattr_setclock 2022-01-09 11:15:06 +03:30
Brian Gianforcaro 84962fcc9b LibPthread: Implement pthread_condattr_getclock
I noticed this was missing while adding spec comments a bit ago.
It's small and easy enough to implement, might as well make us
more POSIX compliant.
2022-01-09 11:15:06 +03:30
Brian Gianforcaro 856fc76083 LibPthread: Remove bad spec link for pthread_{get/set}name_np APIs
I had somehow incorrectly added this link in a previous check-in.

Reported-by: Nico Weber <thakis@chromium.org>
2022-01-07 01:18:57 -08:00
Brian Gianforcaro f4dd388caf LibPthread: Add POSIX spec link for pthread_sigmask API 2022-01-07 01:18:57 -08:00
Brian Gianforcaro 29716b9624 LibPthread: Add POSIX spec links for pthread_once API 2022-01-07 01:18:57 -08:00
Brian Gianforcaro 800f14298d LibPthread: Add POSIX spec links for pthread_cond API 2022-01-07 01:18:57 -08:00
Brian Gianforcaro 54e15821cd LibPthread: Add POSIX spec links for semaphore APIs 2022-01-07 01:18:57 -08:00
Brian Gianforcaro 954fa724b3 LibPthread: Add POSIX spec comments to our implementations 2021-12-21 18:16:48 -08:00
Jelle Raaijmakers 30580ed7e4 LibPthread: Initialize conditions with realtime clock
All the way back in commit 1670ee5aba, the default clock for
condition variables was set to `CLOCK_MONOTONIC`, because there was no
other clock available.

However, if a condition variable is initialized without any additional
attributes by an application, they sometimes assume that the absolute
time that is passed to e.g. `pthread_cond_timedwait` is actually based
on a realtime clock, as can be seen here in SDL2:

6f419bdf5f/src/thread/pthread/SDL_syscond.c (L99)

Additionally, the glibc implementation defaults to a realtime clock:

aac54dcd37/nptl/pthread_cond_init.c (L42)

...so we probably should do so as well :^)
2021-11-24 19:44:57 +01:00
Ali Mohammad Pur 72a45a472a LibPthread: Correct nonsensical loop exit condition in RWLock unlock
The loop should terminate after the exchange happens, we shouldn't
repeat the operation until the count hits zero.
Fixes #10241.
2021-09-28 12:34:51 +03:30
Ali Mohammad Pur b63ea3bad1 LibPthread: Calculate the correct lock value in RWLock {rd,un}lock
The previous version was putting the old count in the control bits,
which is all kinds of wrong.
2021-09-28 12:32:46 +03:30
Andreas Kling 4226b662cd Kernel+Userland: Remove global futexes
We only ever use private futexes, so it doesn't make sense to carry
around all the complexity required for global (cross-process) futexes.
2021-08-17 01:21:47 +02:00
Ali Mohammad Pur e37f9fa7db LibPthread+Kernel: Add pthread_kill() and the thread_kill syscall 2021-07-09 15:36:50 +02:00
Sergey Bugaev 65b0642dd0 LibC+LibPthread: Use FUTEX_PRIVATE_FLAG in more places
Performance go brrrrr
2021-07-07 17:08:40 +02:00
Sergey Bugaev 5aa629717e LibPthread: Fix some assertions 2021-07-06 17:25:34 +02:00
Sergey Bugaev 78f5c4a4c2 LibPthread: Fix ordering
It would be enough to use relaxed ordering here if it weren't for
the mutex, which we also need to store and retrieve. To ensure the
pthread_cond_broadcast() call sees the store, use release and acquire
as appropriate. Thankfully, both of these are on the slow paths.
2021-07-06 17:25:34 +02:00
Andreas Kling 90e5fd5b53 LibC+LibPthread: Add PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
This is a common but non-standard way of initializing a pthread_mutex_t
in recursive mode.
2021-07-05 23:30:15 +02:00
Sergey Bugaev 690141ff8b LibPthread: Reimplement semaphores
This implementation does not use locking or condition variables
internally; it's purely based on atomics and futexes.

Notably, concurrent sem_wait() and sem_post() calls can run *completely
in parallel* without slowing each other down, as long as there are empty
slots for them all to succeed without blocking.

Additionally, sem_wait() never executes an atomic operation with release
ordering, and sem_post() never executes an atomic operation with acquire
ordering (unless you count the syscall). This means the compiler and the
hardware are free to reorder code *into* the critical section.
2021-07-05 20:26:01 +02:00
Sergey Bugaev 00d8dbe739 LibPthread: Reimplement condition variables
This implementation features a fast path for pthread_cond_signal() and
pthread_cond_broadcast() for the case there's no thread waiting, and
does not exhibit the "thundering herd" issue in
pthread_cond_broadcast().

Fixes https://github.com/SerenityOS/serenity/issues/8432
2021-07-05 20:26:01 +02:00
Sergey Bugaev 8fee93d868 LibC: Add futex_wait() and futex_wake() helpers
These are convinient wrappers over the most used futex operations.
futex_wait() also does some smarts for timeout and clock handling.

Use the new futex_wait() instead of a similar private helper in
LibPthread.
2021-07-05 20:26:01 +02:00
Gunnar Beutner e1ff30a360 Toolchain+Userland: Enable TLS for x86_64
This is not technically a toolchain change, but it does require
rebuilding the toolchain for x86_64 (and just that).
2021-07-04 01:07:28 +02:00
Gunnar Beutner 16b9a2d2e1 Kernel+LibPthread: Add support for usermode threads on x86_64 2021-07-01 17:22:22 +02:00
Gunnar Beutner 93c741018e Kernel+LibPthread: Remove m_ prefix for public members 2021-07-01 17:22:22 +02:00
Gunnar Beutner fe2716df21 Kernel: Disable __thread and TLS on x86_64 for now
They're not yet properly supported.
2021-06-30 15:13:30 +02:00
Gunnar Beutner c0bd2c0691 LibPthread: Remove redundant return statement
The pthread_exit() function doesn't return and is marked as such.
2021-06-30 15:13:30 +02:00
Gunnar Beutner c9a8dfa1bf Userland: Add more TODO()s for arch-specific code
This enables building more of the userspace applications for x86_64.
2021-06-24 09:27:13 +02:00
Jelle Raaijmakers 40ddb734ee LibPthread: Correct error check in sem_post and sem_wait 2021-06-01 08:29:56 +02:00
Gunnar Beutner 42d667645d Kernel: Make sure we free the thread stack on thread exit
This adds two new arguments to the thread_exit system call which let
a thread unmap an arbitrary VM range on thread exit. LibPthread
uses this functionality to unmap the thread stack.

Fixes #7267.
2021-05-29 15:53:08 +02:00
Gunnar Beutner f63f471b87 LibPthread: Make some variables static 2021-05-29 15:53:08 +02:00
Linus Groh d60ebbbba6 Revert "Userland: static vs non-static constexpr variables"
This reverts commit 800ea8ea96.

Booting the system no longer worked after these changes.
2021-05-21 10:30:52 +01:00
Lenny Maiorani 800ea8ea96 Userland: static vs non-static constexpr variables
Problem:
- `static` variables consume memory and sometimes are less
  optimizable.
- `static const` variables can be `constexpr`, usually.
- `static` function-local variables require an initialization check
  every time the function is run.

Solution:
- If a global `static` variable is only used in a single function then
  move it into the function and make it non-`static` and `constexpr`.
- Make all global `static` variables `constexpr` instead of `const`.
- Change function-local `static const[expr]` variables to be just
  `constexpr`.
2021-05-21 10:07:06 +01:00
Linus Groh 0aab774343 Everywhere: Fix a bunch of typos 2021-05-17 17:48:55 +01:00
Jean-Baptiste Boric e16894af5a LibC: Do not include errno.h inside unistd.h
POSIX does not mandate this, therefore let's not do it.
2021-05-14 22:24:02 +02:00
Gunnar Beutner 759acdb938 LibC: Partially implement pthread_setcancel{state,type}()
With those partially implemented I can start to clone the SerenityOS
git repository via HTTPS.

The download still fails half-way through because SSL_read returns
an error for reasons I haven't investigated yet.
2021-05-10 17:44:18 +01:00
Gunnar Beutner 6e80b9fd01 LibPthread: Add implementation for pthread_mutexattr_gettype 2021-05-08 19:14:21 +02:00
Brian Gianforcaro 9f07627f58 LibPthread: Implement pthread_spinlock_t API.
This change implements the pthread user space spinlock API. The
stress-ng Port requires a functioning version to work correctly.

To facilitate the requirements of the posix specification for the API
we implement the spinlock so that the owning tid is the value stored
in the spinlock. This gives us the proper ownership semantics needed
to implement the proper error handling.
2021-05-02 20:59:38 +02:00
Jelle Raaijmakers d1f33ec795 LibPthread: Use realtime clock for futex_wait()
If we get an absolute time passed to one of the pthread_*wait methods,
this is not an absolute monotonic time but rather an absolute wall
time. This means that we also need to pass FUTEX_CLOCK_REALTIME to the
futex syscall to ensure we're not using the monotonic clock.
2021-04-27 09:19:55 +02:00
Gunnar Beutner 549d9bd3ea LibC: Move the __pthread_mutex_trylock function to LibC
Let's move this to LibC because the dynamic loader depends
on this function.
2021-04-25 10:14:50 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Brian Gianforcaro 5a31ca06db LibPthread: Add non functional pthread_attr_[set|get]scope stubs
Standard: https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_attr_getscope.html

Needed for https://fio.readthedocs.io
2021-04-21 13:13:23 +02:00
Gunnar Beutner 2520ccfca4 LibPthread: Add stubs for pthread_spin_* functions
The stress-ng port depends on these now that it detects we have
thread support.
2021-04-20 21:08:17 +02:00
Gunnar Beutner 88cebb05ad LibC+LibPthread: Implement function forwarding for libpthread
GCC will insert various calls to pthread functions when compiling
C++ code with static initializers, even when the user doesn't link
their program against libpthread explicitly.

This is used to make static initializers thread-safe, e.g. when
building a library that does not itself use thread functionality
and thus does not link against libpthread - but is intended to
be used with other code that does use libpthread explicitly.

This makes these symbols available in libc.
2021-04-20 21:08:17 +02:00
Gunnar Beutner bd08f9188a Pthread: Add stubs for pthread_cleanup_{push,pop}
The stubs are necessary to make the xz port properly detect pthread
support. The two functions are only used in the configure script and
nowhere else.
2021-04-19 17:55:35 +02:00
Gunnar Beutner 4075b306f8 LibC+LibPthread: Make sure TLS keys are destroyed after everything else
This ensures that __thread variables can be used when global destructors
are being invoked.
2021-04-18 10:52:05 +02:00