Commit Graph

63 Commits

Author SHA1 Message Date
Sam Atkins
269ce258b4 LibCore: Replace uses of JsonObject::get_deprecated()/get_ptr() 2023-01-26 09:57:14 -05:00
Sam Atkins
1dd6b7f5b7 AK+Everywhere: Rename JsonObject::get() to ::get_deprecated()
This is a preparatory step to making `get()` return `ErrorOr`.
2023-01-17 19:52:52 -05:00
kleines Filmröllchen
2475f6a641 LibCore: Explain EventLoop and reorder some members in the header
This hopefully makes EventLoop easier to understand.
2023-01-11 11:49:05 +01:00
Linus Groh
57dc179b1f Everywhere: Rename to_{string => deprecated_string}() where applicable
This will make it easier to support both string types at the same time
while we convert code, and tracking down remaining uses.

One big exception is Value::to_string() in LibJS, where the name is
dictated by the ToString AO.
2022-12-06 08:54:33 +01:00
Sam Atkins
0fc673e759 LibCore: Mark connections to InspectorServer as MSG_NOSIGNAL
If InspectorServer closes for some reason at the wrong time, there is no
need for the inspected application to terminate.
2022-12-03 14:27:05 +01:00
Andrew Kaster
828441852f Everywhere: Replace uses of __serenity__ with AK_OS_SERENITY
Now that we have OS macros for essentially every supported OS, let's try
to use them everywhere.
2022-10-10 12:23:12 +02:00
Peter Elliott
7af5eef0dd SystemServer+LoginServer+Userland: Switch to sid-based sockets
This commit does three things atomically:
- switch over Core::Account+SystemServer+LoginServer to sid based socket
  names.
- change socket names with %uid to %sid.
- add/update necessary pledges and unveils.

Userland: Switch over servers to sid based sockets

Userland: Properly pledge and unveil for sid based sockets
2022-10-03 11:11:29 +02:00
Andrew Kaster
8f38367d46 LibCore: Only include Account.h on Serenity in EventLoop and System
Core::Acount is only used within ``#ifdef __serenity__`` blocks in these
files, so guard the inclusion of Account.h in the same way.

This fixes the Android build of these files.
2022-09-22 11:07:21 -04:00
kleines Filmröllchen
c0079cf963 LibCore: Make EventLoop's connection to InspectorServer work again
This path was changed to the uid templated version without actually
using the template resolver function in Account.
2022-08-17 16:40:42 -07:00
Lucas CHOLLET
c248569d6b Base: Launch InspectorServer at session start-up 2022-08-14 21:52:35 +01:00
kleines Filmröllchen
125122a9ab LibAudio: Prevent racy eternal deadlock of the audio enqueue thread
The audio enqueuer thread goes to sleep when there is no more audio data
present, and through normal Core::EventLoop events it can be woken up.
However, that waking up only happens when the thread is not currently
running, so that the wake-up events don't queue up and cause weirdness.
The atomic variable responsible for keeping track of whether the thread
is active can lead to a racy deadlock however, where the audio enqueuer
thread will never wake up again despite there being audio data to
enqueue. Consider this scenario:

- Main thread calls into async_enqueue. It detects that according to the
  atomic variable, the other thread is still running, skipping the event
  queue wake.
- Enqueuer thread has just finished playing the last chunk of audio and
  detects that there is no audio left. It enters the if block with the
  dbgln "Reached end of provided audio data..."
- Main thread enqueues audio, making the user sample queue non-empty.
- Enqueuer thread does not check this condition again, instead setting
  the atomic variable to indicate that it is not running. It exits into
  an event loop sleep.
- Main thread exits async_enqueue. The calling audio enqueuing system
  (see e.g. Piano, but all of them function similarly) will wait until
  the enqueuer thread has played enough samples before async_enqueue is
  called again. However, since the enqueuer thread will never play any
  audio, this condition is never fulfilled and audio playback deadlocks

This commit fixes that by allowing the event loop to not enqueue an
event that already exists, therefore overloading the audio enqueuer
event loop by at maximum one message in weird situations. We entirely
get rid of the atomic variable and the race condition is prevented.
2022-07-22 19:35:41 +01:00
FrHun
720e0a096f LibCore: InspectorServerConnection send responses with multiple writes 2022-07-15 12:38:35 +02:00
sin-ack
3f3f45580a Everywhere: Add sv suffix to strings relying on StringView(char const*)
Each of these strings would previously rely on StringView's char const*
constructor overload, which would call __builtin_strlen on the string.
Since we now have operator ""sv, we can replace these with much simpler
versions. This opens the door to being able to remove
StringView(char const*).

No functional changes.
2022-07-12 23:11:35 +02:00
Jelle Raaijmakers
f8c81b2f01 LibCore: Remove fixed EventLoop FIXME
This was resolved in commit f25123df66.
2022-04-27 22:59:15 +02:00
Jelle Raaijmakers
f25123df66 LibCore: Remove main event loop
The main event loop functionality was used in just two places where the
alternative is a bit simpler. Remove it in favor of referencing the
event loop directly, or just invoking `EventLoop::current()`.

Note that we don't need locking in the constructor since we're now only
modifying a thread-local `Vector`. We also don't need locking in the
old call sites to `::with_main_locked()` since we already lock the
event loop in the subsequent `::post_event()` invocation.
2022-04-27 11:54:37 +02:00
Jelle Raaijmakers
9e2a619fdc LibCore: Remove unused EventLoop::is_main_event_loop() 2022-04-27 11:54:37 +02:00
Sam Atkins
f240def414 LibCore: Allow inspecting any process launched with MAKE_INSPECTABLE=1
For now, EventLoop and Application still have a make_inspectable
parameter, so that when working on an application you can temporarily
hard-code it to be inspectable rather than having to set the env var
each time.
2022-04-18 12:57:34 +02:00
Sam Atkins
3b1e063d30 LibCore+Everywhere: Make Core::Stream::read() return Bytes
A mistake I've repeatedly made is along these lines:
```c++
auto nread = TRY(source_file->read(buffer));
TRY(destination_file->write(buffer));
```

It's a little clunky to have to create a Bytes or StringView from the
buffer's data pointer and the nread, and easy to forget and just use
the buffer. So, this patch changes the read() function to return a
Bytes of the data that were just read.

The other read_foo() methods will be modified in the same way in
subsequent commits.

Fixes #13687
2022-04-16 13:27:51 -04:00
Idan Horowitz
086969277e Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
kleines Filmröllchen
e9a286f9e7 LibCore: Make EventLoop connect to InspectorServer if requested
I regressed this at some point by inverting the condition here.
2022-03-25 12:05:17 +01:00
javabird25
1770534d94 LibCore: Verify that EventLoop is initialized in its static API 2022-03-10 18:09:27 -08:00
Lenny Maiorani
ea58b8d927 Libraries: Use default constructors/destructors in LibCore
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#cother-other-default-operation-rules

"The compiler is more likely to get the default semantics right and
you cannot implement these functions better than the compiler."
2022-03-10 18:04:26 -08:00
Andreas Kling
1a323ec8d4 LibCore: Exit get_next_timer_expiration() sooner if possible
If we find a timer that needs to be fired immediately, we can stop
looking through the remaining timers.

This significantly reduces time spent in get_next_timer_expiration()
on ACID3. Of course, with a better data structure, we could reduce
time spent further. I've left a FIXME about that.
2022-02-15 23:24:41 +01:00
kleines Filmröllchen
704bb361bb LibCore: Allow event loops on other threads to wake up
Because the wake pipe is thread-local, it was previously not possible
to wake an event loop across a thread. Therefore, this commit
rearchitects event loop waking by making the wake function a member of
the event loop itself and having it keep a pointer to its thread's wake
pipe. The global wake() function calls wake on the current thread's
event loop.

This also fixes a bug in BackgroundAction: it should wake the event loop
it was created on, instead of the current thread's event loop.
2022-02-13 23:06:53 +01:00
kleines Filmröllchen
716a3429fa LibCore: Fix event loop stacks on non-main threads
Previously, event loop stacks on non-main threads would always crash
because the condition for "am I the lowest-stacked loop" was still
"am I the main loop", which of course is no longer sensible. A simple
switch to `is_instantiated` fixes this.
2022-02-13 23:06:53 +01:00
sin-ack
04d68117c2 LibCore: Remove Core::LocalSocket :^) 2022-02-06 10:28:19 +01:00
sin-ack
0249c92313 LibCore: Use Core::Stream::LocalSocket in InspectorServerConnection 2022-02-06 10:28:19 +01:00
Andreas Kling
3bab93c5e7 LibCore: Make Core::s_main_event_loop actually global
This was accidentally per-TU, as it was declared "static" in the header.
2022-01-25 09:13:40 +01:00
kleines Filmröllchen
df6b9cdb0c LibCore+LibC: Enforce the global event loop ban in code
It's a bad idea to have a global event loop in a client application as
that will cause an initialization-order fiasco in ASAN. Therefore, LibC
now has a flag "s_global_initializers_ran" which is false until _entry
in crt0 runs, which in turn only gets called after all the global
initializers were actually executed. The EventLoop constructor checks
the flag and crashes the program if it is being called as a global
constructor. A note next to the VERIFY_NOT_REACHED() informs the
developer of these things and how we usually instantiate event loops.

The upshot of this is that global event loops will cause a crash before
any undefined behavior is hit.
2022-01-23 15:21:10 +01:00
kleines Filmröllchen
888faa3c9f LibCore: Fix signal handling race condition in EventLoop
The event loop is responsible for handling POSIX signals while it's
running. The signal handler adds the signals to a wake pipe which is
then read after the select'ing code in wait_for_event. Problems happen,
however, when another signal comes in after the select wake: the signal
will interrupt the next syscall, the `read` from the wake pipe, and the
resulting EINTR in wait_for_event causes the program to crash. This is
undesirable. Instead, we want to retry reading as long as we're
interrupted.
2022-01-23 15:21:10 +01:00
kleines Filmröllchen
a501b9c7df LibCore: Create wake pipe on each thread
After the previous change, the wake pipe was only being created on the
main thread by the main event loop. This change utilizes a flag to
always initialize the wake pipe on other threads. Because the pipe is
quite expensive (it will count towards the file descriptor limit, for
instance), we do the initialization "lazily": Only when an event loop is
constructed and it notices that there hasn't been a wake pipe created on
its thread, it will create the pipe. Conversely, this means that there
are no pipes on threads that never use an event loop.
2022-01-23 15:21:10 +01:00
kleines Filmröllchen
69c1910037 LibCore: Allow EventLoops to run on multiple threads safely
The event loop system was previously very singletony to the point that
there's only a single event loop stack per process and only one event
loop (the topmost) can run at a time. This commit simply makes the event
loop stack and related structures thread-local so that each thread has
an isolated event loop system.

Some things are kept at a global level and synchronized with the new
MutexProtected: The main event loop needs to still be obtainable from
anywhere, as it closes down the application when it exits. The ID
allocator is global as IDs should not be shared even between threads.
And for the inspector server connection, the same as for the main loop
holds.

Note that currently, the wake pipe is only created by the main thread,
so notifications don't work on other threads.

This removes the temporary mutex fix for notifiers, introduced in
0631d3fed5 .
2022-01-23 15:21:10 +01:00
mjz19910
3102d8e160 Everywhere: Fix many spelling errors 2022-01-07 10:56:59 +01:00
Andreas Kling
0631d3fed5 LibCore: Guard access to EventLoop notifiers set with a mutex
This fixes a CI flake we've been seeing lately in TestLibCoreStream.

The solution itself is somewhat of a stop-gap as there are more thorough
event loop threading improvements in the works.
2022-01-07 00:50:26 +01:00
Jelle Raaijmakers
558fd5b166 LibCore: Make EventLoop::pump() return event count
Sometimes, pumping the event loop will cause new events to be
generated. For example, an IPC message could be delivered which then
dispatches a new event to be handled by the GUI. To the invoker of
`EventLoop::pump()`, it is not obvious if any events were processed at
all.

Libraries like SDL2 might not have the opportunity to run the event
loop often enough that events can be processed swiftly, since it might
spend time doing other things. This can result in stuttering GUI
interactions.

This changes `EventLoop::pump()` to return the number of processed
events. This functionality will be used by our SDL2 port in another PR.
2022-01-06 11:30:04 +01:00
Michel Hermier
7ea3d40e19 LibCore: Unconditionally report error in EventLoop::wait_for_event
When `select` fails and `VERIFY_NOT_REACHED()` is reached, it is wise
to always have a trace of what went wrong.
2021-12-21 13:15:17 -08:00
Andreas Kling
587f9af960 AK: Make JSON parser return ErrorOr<JsonValue> (instead of Optional)
Also add slightly richer parse errors now that we can include a string
literal with returned errors.

This will allow us to use TRY() when working with JSON data.
2021-11-17 00:21:10 +01:00
Ben Wiederhake
b3e9a4e603 Libraries: Fix visibility of Object-derivative constructors
Derivatives of Core::Object should be constructed through
ClassName::construct(), to avoid handling ref-counted objects with
refcount zero. Fixing the visibility means that misuses like this are
more difficult.
2021-11-02 22:56:53 +01:00
Andreas Kling
fa753ff863 LibCore: Pop the main Core::EventLoop off the stack when destroyed
The main event loop pushes itself onto the event loop stack, and so it
should also pop itself when destroyed.

This will surface attempts to use the event loop stack after the main
event loop has been destroyed.
2021-10-24 01:01:01 +02:00
Andreas Kling
f2b9ec9f8a LibCore: Add Core::EventLoop::spin_until(Function<bool()>)
This function spins the event loop until a goal condition is met.
2021-09-25 19:32:14 +02:00
sin-ack
e9121f8b1f LibCore+Userland: Implement Core::deferred_invoke
Core::deferred_invoke is a way of executing an action after previously
queued events have been processed. It removes the requirement of
having/being a Core::Object subclass in order to defer invocation
through Core::Object::deferred_invoke.

Core::Object::deferred_invoke now delegates to Core::deferred_invoke.
The version with the Object& argument is still present but will be
removed in the following commits.

This commit additionally fixes a new places where the
DeferredInvocationEvent was dispatched to the event loop directly, and
replaces them with the Core::deferred_invoke equivalent.
2021-09-02 03:47:47 +04:30
Andreas Kling
67a0fa2b78 LibCore: Add Core::EventLoop::has_been_instantiated()
This static bool getter can be used to VERIFY that an event loop exists,
in situations where one is expected.

This is helpful if the absence of an event loop would generate strange
and/or loud errors that don't immediately point to this as a cause.
2021-08-26 00:54:27 +02:00
Brian Gianforcaro
da51b8f39d LibCore: Move EventLoop to AK::Time 2021-08-15 12:20:38 +02:00
Andreas Kling
c94c15d45c Everywhere: Replace AK::Singleton => Singleton 2021-08-08 00:03:45 +02:00
Brian Gianforcaro
176e1cbca7 LibCore: Remove unused header includes 2021-08-01 08:10:16 +02:00
Andreas Kling
b8a204c5b9 LibThreading: Rename Lock => Mutex 2021-07-09 11:15:50 +02:00
Daniel Bertalan
2d2747cb15 LibCore+AK: Use proper atomics in Singleton 2021-06-24 17:35:49 +04:30
Andreas Kling
dc65f54c06 AK: Rename Vector::append(Vector) => Vector::extend(Vector)
Let's make it a bit more clear when we're appending the elements from
one vector to the end of another vector.
2021-06-12 13:24:45 +02:00
Ali Mohammad Pur
7ac196974d Everywhere: Replace Vector<T*> with nonnull entries with Vector<T&> 2021-06-08 19:14:24 +02:00
Andreas Kling
b5d73c834f Userland: Rename LibThread => LibThreading
Also rename the "LibThread" namespace to "Threading"
2021-05-22 18:54:22 +02:00