Skip to content

Commit

Permalink
Use thread yield primitive for busy waits
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomShaper committed Oct 21, 2024
1 parent 44fa552 commit affd1de
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 7 deletions.
4 changes: 4 additions & 0 deletions core/os/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ class Thread {

_FORCE_INLINE_ static bool is_main_thread() { return caller_id == MAIN_ID; } // Gain a tiny bit of perf here because there is no need to validate caller_id here, because only main thread will be set as 1.

_FORCE_INLINE_ static void yield() { std::this_thread::yield(); }

static Error set_name(const String &p_name);

ID start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings());
Expand Down Expand Up @@ -176,6 +178,8 @@ class Thread {

_FORCE_INLINE_ static bool is_main_thread() { return true; }

_FORCE_INLINE_ static void yield() {}

static Error set_name(const String &p_name) { return ERR_UNAVAILABLE; }

void start(Thread::Callback p_callback, void *p_user, const Settings &p_settings = Settings()) {}
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3018,7 +3018,7 @@ void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
current_index = tdata.max_index.get();
ep->step(reimport_files[current_index].path.get_file(), current_index, false);
}
OS::get_singleton()->delay_usec(1);
Thread::yield();
} while (!WorkerThreadPool::get_singleton()->is_group_task_completed(group_task));

WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ void EditorLog::_add_log_line(LogMessage &p_message, bool p_replace_previous) {
// Force sync last line update (skip if number of unprocessed log messages is too large to avoid editor lag).
if (log->get_pending_paragraphs() < 100) {
while (!log->is_finished()) {
::OS::get_singleton()->delay_usec(1);
Thread::yield();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5454,7 +5454,7 @@ bool EditorNode::immediate_confirmation_dialog(const String &p_text, const Strin
cd->popup_centered();

while (true) {
OS::get_singleton()->delay_usec(1);
Thread::yield();
DisplayServer::get_singleton()->process_events();
Main::iteration();
if (singleton->immediate_dialog_confirmed || !cd->is_visible()) {
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/editor/editor_scene_importer_blend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ bool EditorFileSystemImportFormatSupportQueryBlend::query() {
confirmed = false;

while (true) {
OS::get_singleton()->delay_usec(1);
Thread::yield();
DisplayServer::get_singleton()->process_events();
Main::iteration();
if (!configure_blender_dialog->is_visible() || confirmed) {
Expand Down
2 changes: 1 addition & 1 deletion scene/main/http_request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void HTTPRequest::_thread_func(void *p_userdata) {
if (exit) {
break;
}
OS::get_singleton()->delay_usec(1);
Thread::yield();
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/core/threads/test_worker_thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void static_test_daemon(void *p_arg) {

static void static_busy_task(void *p_arg) {
while (!exit.is_set()) {
OS::get_singleton()->delay_usec(1);
Thread::yield();
}
}

Expand Down Expand Up @@ -150,7 +150,7 @@ TEST_CASE("[WorkerThreadPool] Run a yielding daemon as the only hope for other t
}

while (counter[1].get() != legit_tasks_count) {
OS::get_singleton()->delay_usec(1);
Thread::yield();
}

exit.set();
Expand Down

0 comments on commit affd1de

Please sign in to comment.