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