2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2020-01-24 16:45:29 +03:00
|
|
|
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
|
2021-07-04 19:01:01 +03:00
|
|
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-09-18 10:49:51 +03:00
|
|
|
#include <AK/Queue.h>
|
2021-05-22 19:47:42 +03:00
|
|
|
#include <LibThreading/BackgroundAction.h>
|
2021-07-09 12:14:57 +03:00
|
|
|
#include <LibThreading/Mutex.h>
|
2021-05-22 19:47:42 +03:00
|
|
|
#include <LibThreading/Thread.h>
|
2021-07-04 19:01:01 +03:00
|
|
|
#include <unistd.h>
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2021-07-07 15:23:06 +03:00
|
|
|
static pthread_mutex_t s_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static pthread_cond_t s_condition = PTHREAD_COND_INITIALIZER;
|
|
|
|
static Queue<Function<void()>>* s_all_actions;
|
2021-05-22 19:47:42 +03:00
|
|
|
static Threading::Thread* s_background_thread;
|
2024-04-24 03:57:28 +03:00
|
|
|
static Atomic<bool> s_background_thread_should_run = true;
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2021-04-26 20:09:04 +03:00
|
|
|
static intptr_t background_thread_func()
|
2019-08-25 18:55:56 +03:00
|
|
|
{
|
2021-07-07 15:23:06 +03:00
|
|
|
Vector<Function<void()>> actions;
|
2024-04-24 03:57:28 +03:00
|
|
|
while (s_background_thread_should_run.load(AK::MemoryOrder::memory_order_acquire)) {
|
2021-07-07 15:23:06 +03:00
|
|
|
pthread_mutex_lock(&s_mutex);
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2024-04-24 03:57:28 +03:00
|
|
|
while (s_all_actions->is_empty() && s_background_thread_should_run.load(AK::MemoryOrder::memory_order_acquire))
|
2021-07-07 15:23:06 +03:00
|
|
|
pthread_cond_wait(&s_condition, &s_mutex);
|
2021-07-04 19:01:01 +03:00
|
|
|
|
2021-07-07 15:23:06 +03:00
|
|
|
while (!s_all_actions->is_empty())
|
|
|
|
actions.append(s_all_actions->dequeue());
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&s_mutex);
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2024-04-24 03:57:28 +03:00
|
|
|
for (auto& action : actions) {
|
|
|
|
if (s_background_thread_should_run.load(AK::MemoryOrder::memory_order_acquire))
|
|
|
|
action();
|
|
|
|
}
|
2021-07-07 15:23:06 +03:00
|
|
|
actions.clear();
|
|
|
|
}
|
2024-04-24 03:57:28 +03:00
|
|
|
return 0;
|
2019-08-25 18:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
2021-07-07 15:23:06 +03:00
|
|
|
s_all_actions = new Queue<Function<void()>>;
|
2022-10-26 03:00:41 +03:00
|
|
|
s_background_thread = &Threading::Thread::construct(background_thread_func, "Background Thread"sv).leak_ref();
|
2019-08-25 18:55:56 +03:00
|
|
|
s_background_thread->start();
|
|
|
|
}
|
|
|
|
|
2024-04-24 03:57:28 +03:00
|
|
|
void Threading::quit_background_thread()
|
|
|
|
{
|
|
|
|
s_background_thread_should_run.store(false, AK::MemoryOrder::memory_order_release);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&s_mutex);
|
|
|
|
pthread_cond_broadcast(&s_condition);
|
|
|
|
pthread_mutex_unlock(&s_mutex);
|
|
|
|
|
|
|
|
MUST(s_background_thread->join());
|
|
|
|
|
|
|
|
delete s_all_actions;
|
|
|
|
s_background_thread->unref();
|
|
|
|
s_all_actions = nullptr;
|
|
|
|
s_background_thread = nullptr;
|
|
|
|
|
|
|
|
s_background_thread_should_run.store(true, AK::MemoryOrder::memory_order_release);
|
|
|
|
}
|
|
|
|
|
2021-07-04 19:01:01 +03:00
|
|
|
Threading::Thread& Threading::BackgroundActionBase::background_thread()
|
2019-08-25 18:55:56 +03:00
|
|
|
{
|
2021-07-04 19:01:01 +03:00
|
|
|
if (s_background_thread == nullptr)
|
2019-08-25 18:55:56 +03:00
|
|
|
init();
|
2021-07-04 19:01:01 +03:00
|
|
|
return *s_background_thread;
|
2019-08-25 18:55:56 +03:00
|
|
|
}
|
|
|
|
|
2021-07-04 19:01:01 +03:00
|
|
|
void Threading::BackgroundActionBase::enqueue_work(Function<void()> work)
|
2019-08-25 18:55:56 +03:00
|
|
|
{
|
2021-07-04 19:01:01 +03:00
|
|
|
if (s_all_actions == nullptr)
|
2019-08-25 18:55:56 +03:00
|
|
|
init();
|
2021-07-07 15:23:06 +03:00
|
|
|
|
|
|
|
pthread_mutex_lock(&s_mutex);
|
|
|
|
s_all_actions->enqueue(move(work));
|
|
|
|
pthread_cond_broadcast(&s_condition);
|
|
|
|
pthread_mutex_unlock(&s_mutex);
|
2019-08-25 18:55:56 +03:00
|
|
|
}
|