Skip to content

Commit 45bb89e

Browse files
LucasCholletspholz
authored andcommitted
SystemServer: Don't use sleep to wait for the GPU to appear
Using a FileWatcher allows us to get back to work as soon as the file is created. On my system, it makes us wait 11ms instead of more than a second.
1 parent 9829d4d commit 45bb89e

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Userland/Services/SystemServer/main.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
#include <LibCore/Event.h>
1919
#include <LibCore/EventLoop.h>
2020
#include <LibCore/File.h>
21+
#include <LibCore/FileWatcher.h>
2122
#include <LibCore/System.h>
23+
#include <LibCore/Timer.h>
2224
#include <LibMain/Main.h>
2325
#include <errno.h>
2426
#include <fcntl.h>
@@ -124,20 +126,30 @@ static ErrorOr<void> activate_services(Core::ConfigFile const& config)
124126
static ErrorOr<void> activate_base_services_based_on_system_mode()
125127
{
126128
if (g_system_mode == graphical_system_mode) {
127-
bool found_gpu_device = false;
128-
for (int attempt = 0; attempt < 10; attempt++) {
129-
struct stat file_state;
130-
int rc = lstat("/dev/gpu/connector0", &file_state);
131-
if (rc == 0) {
132-
found_gpu_device = true;
133-
break;
134-
}
135-
sleep(1);
136-
}
137-
if (!found_gpu_device) {
129+
bool done_searching_for_gpu = false;
130+
131+
auto timeout = Core::Timer::create_single_shot(10000, [&]() {
138132
dbgln("WARNING: No device nodes at /dev/gpu/ directory after 10 seconds. This is probably a sign of disabled graphics functionality.");
139133
dbgln("To cope with this, graphical mode will not be enabled.");
140134
g_system_mode = text_system_mode;
135+
136+
done_searching_for_gpu = true;
137+
});
138+
139+
auto watcher = TRY(Core::FileWatcher::create());
140+
watcher->on_change = [&](Core::FileWatcherEvent const& event) {
141+
if (event.event_path != "/dev/gpu/connector0"sv)
142+
return;
143+
done_searching_for_gpu = true;
144+
};
145+
146+
TRY(watcher->add_watch("/dev/gpu/", Core::FileWatcherEvent::Type::ChildCreated));
147+
148+
// The GPU might have appeared while we were setting up the watcher.
149+
// Only wait for the file if we can't stat it.
150+
if (Core::System::lstat("/dev/gpu/connector0"sv).is_error()) {
151+
timeout->start();
152+
Core::EventLoop::current().spin_until([&]() { return done_searching_for_gpu; });
141153
}
142154
}
143155

0 commit comments

Comments
 (0)