1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:04:59 +00:00
serenity/Userland/Libraries
Zaggy1024 dcfec8bfbe LibC: Expect mutexes to be locked and needing wake when calling futex()
tl;dr: This fixes deadlocks that would occur in most applications in
Serenity when SMP was enabled.

As an optimization to `pthread_mutex_unlock()`, if only one thread
holds a mutex lock, it will avoid calling `futex()` to wake, since no
threads are actually waiting in `futex()`.

If a thread manages to synchronously unlock and relock the same mutex,
the state will be set to indicate that a wake is not needed, regardless
of whether any other threads are waiting for the lock. This should be
fine, as a thread that is waiting will set the mutex to needing a wake
and check if it is unlocked to unblock itself in the same atomic
operation.

However, when `pthread_mutex_lock()` was called with only one thread
holding the mutex lock, instead of telling `futex()` to atomically
sleep the thread if the state is set to indicate that a wake is
needed, the first wait would check if the state was set to indicate a
wake was _not_ needed. This means it is possible for the call to
`futex()` to wait when any subsequent call to `pthread_mutex_unlock()`
would not call `futex()` to wake, causing it to wait forever.

After that, any other thread that tries to take the lock will see that
the lock is taken and also `futex()` wait. Despite the fact that these
other threads would set the state to needing a wake, there will be no
unblocked thread holding the lock to actually wake them.

By making it wait only if the state indicates to other threads that a
wake is needed, heavily contended mutexes should no longer cause
deadlocks. Most applications would encounter these deadlocks due to the
mutex used by `malloc()`, some sooner than others. The worst offenders
(other than Ladybird) were most likely VideoPlayer and SoundPlayer.
2023-08-09 05:31:35 +02:00
..
LibArchive LibArchive: Extract logic for calculating ZIP statistics 2023-07-30 22:16:40 +01:00
LibAudio Userland: Open files for save in write-only mode 2023-08-08 15:13:43 +02:00
LibC LibC: Expect mutexes to be locked and needing wake when calling futex() 2023-08-09 05:31:35 +02:00
LibCards AK: Make "foo"_string infallible 2023-08-07 16:03:27 +02:00
LibChess Userland: Prefer _string over _short_string 2023-08-08 07:37:21 +02:00
LibCMake LibSyntax+Libraries: Replace TextStyle with Gfx::TextAttributes 2023-03-15 14:55:49 +01:00
LibCodeComprehension AK: Make "foo"_string infallible 2023-08-07 16:03:27 +02:00
LibCompress LibCompress/Brotli: Remove CanonicalCode::clear() 2023-07-22 07:10:47 +02:00
LibConfig Userland: Convert config listener callbacks to use StringView 2023-06-27 15:37:00 +01:00
LibCore Userland: Prefer _string over _short_string 2023-08-08 07:37:21 +02:00
LibCoredump LibCoredump: Avoid unnecessary conversion to DeprecatedString 2023-05-27 17:30:55 +02:00
LibCpp Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibCrypt Everywhere: Resolve conflicts with LibC and libc++ 2023-06-27 12:40:38 +02:00
LibCrypto Userland: Prefer _string over _short_string 2023-08-08 07:37:21 +02:00
LibDebug LibDebug: Keep track of the SetEpilogueBegin LineProgram OpCode 2023-06-27 12:40:38 +02:00
LibDesktop Everywhere: Use MonotonicTime instead of Duration 2023-05-24 23:18:07 +02:00
LibDeviceTree Everywhere: Remove unused DeprecatedString includes 2023-04-09 22:00:54 +02:00
LibDiff LibDiff+patch: Support multiple patches in a single patch file 2023-07-30 07:47:22 +01:00
LibDNS AK: Rename Stream::write_entire_buffer to Stream::write_until_depleted 2023-03-13 15:16:20 +00:00
LibDSP Userland: Prefer _string over _short_string 2023-08-08 07:37:21 +02:00
LibEDID Everywhere: Use nested namespace qualifiers 2023-07-12 10:05:42 +03:30
LibELF Kernel: Consolidate finding the ELF stack size with validation 2023-07-10 21:08:31 -06:00
LibFileSystem LibFileSystem: Add FileSystem::is_regular_file 2023-07-13 10:29:30 +01:00
LibFileSystemAccessClient LibFSAC+Userland: Pass options for FSAC::open_file() using a struct 2023-06-11 09:40:17 +01:00
LibGemini Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibGfx LibGfx/JPEGXL: Implement the ANS decoder 2023-08-08 20:03:16 +02:00
LibGL Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibGLSL AK: Make "foo"_string infallible 2023-08-07 16:03:27 +02:00
LibGPU LibGPU: Remove DeprecatedString usage 2023-01-30 13:49:52 -05:00
LibGUI Userland: Prefer _string over _short_string 2023-08-08 07:37:21 +02:00
LibHTTP AK: Serialize URL hosts with 'concept-host-serializer' 2023-07-31 05:18:51 +02:00
LibIDL LibIDL: Avoid unnecessary copies of imported paths 2023-06-15 08:09:16 +02:00
LibImageDecoderClient LibImageDecoderClient: Fix minor const-correctness issue 2023-02-21 00:54:04 +01:00
LibIMAP Userland: Rename Core::Object to Core::EventReceiver 2023-08-06 20:39:51 +02:00
LibIPC Userland: Rename Core::Object to Core::EventReceiver 2023-08-06 20:39:51 +02:00
LibJS LibJS: Enable await test that used to crash in AST mode 2023-08-08 15:09:53 +02:00
LibKeyboard Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibLine LibLine: Refresh the line after case-change operations 2023-08-07 22:51:49 +03:30
LibLocale LibTimeZone+Userland: Change timezone functions to use UnixDateTime 2023-05-24 23:18:07 +02:00
LibMain Everywhere: Explicitly link all binaries against the LibC target 2022-11-01 14:49:09 +00:00
LibManual Userland: Prefer _string over _short_string 2023-08-08 07:37:21 +02:00
LibMarkdown LibMarkdown: Add render_for_raw_print methods to Node derived classes 2023-08-07 13:24:25 -06:00
LibPartition Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibPCIDB Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibPDF LibPDF: Ensure all subpaths are closed before filling paths 2023-07-25 13:42:40 +02:00
LibProtocol Ladybird: Add WebSocket server for use by Lagom networking 2023-08-03 09:55:20 +02:00
LibRegex LibRegex: Keep track of instruction positions for backwards tree jumps 2023-08-05 16:40:04 +02:00
LibSanitizer Everywhere: Resolve conflicts with LibC and libc++ 2023-06-27 12:40:38 +02:00
LibSoftGPU LibGfx+Everywhere: Change Gfx::Rect to be endpoint exclusive 2023-05-23 12:35:42 +02:00
LibSQL LibSQL: Remove Core::EventReceiver parent from SQL::Relation 2023-08-07 14:38:38 -04:00
LibSymbolication LibSymbolication: Use Core::File instead of Core::DeprecatedFile 2023-04-27 07:27:14 +02:00
LibSyntax Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibSystem Everywhere: Resolve conflicts with LibC and libc++ 2023-06-27 12:40:38 +02:00
LibTest LibJS: Rip out the AST interpreter :^) 2023-08-08 13:07:13 +02:00
LibTextCodec LibTextCodec: Add "get output encoding" from the Encoding specification 2023-06-19 06:12:26 +02:00
LibThreading LibThreading: Make Thread keep itself alive while its action is running 2023-08-07 10:40:34 -06:00
LibTimeZone Everywhere: Resolve conflicts with LibC and libc++ 2023-06-27 12:40:38 +02:00
LibTLS LibTLS: Allow applications to provide a custom default CA Cert path 2023-08-02 05:44:43 +02:00
LibUnicode LibUnicode: Perform code point case conversion lookups in constant time 2023-07-28 05:28:50 +02:00
LibUSBDB Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibVideo Userland: Rename Core::Object to Core::EventReceiver 2023-08-06 20:39:51 +02:00
LibVirtGPU Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibVT Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LibWasm LibWasm: Implement Element section segment type 4 2023-07-08 15:13:25 +02:00
LibWeb LibWeb: Add namespaces to Attribute selectors 2023-08-08 20:49:45 +01:00
LibWebSocket Userland: Rename Core::Object to Core::EventReceiver 2023-08-06 20:39:51 +02:00
LibWebView Ladybird+LibWebView+Browser: Remove ability to run with AST interpreter 2023-08-08 13:07:13 +02:00
LibX86 LibX86: Remove i686 support 2022-12-28 11:53:41 +01:00
LibXML LibXML: Actually append resolved references when parsing content 2023-07-23 16:09:12 +02:00
CMakeLists.txt LibFileSystem: Create LibFileSystem 2023-03-21 19:03:21 +00:00