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>
|
|
|
|
#include <LibThreading/Lock.h>
|
|
|
|
#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;
|
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;
|
2019-08-25 15:55:56 +00:00
|
|
|
while (true) {
|
2021-07-04 16:01:01 +00:00
|
|
|
|
2021-07-07 12:23:06 +00:00
|
|
|
pthread_mutex_lock(&s_mutex);
|
2019-08-25 15:55:56 +00:00
|
|
|
|
2021-07-07 12:23:06 +00:00
|
|
|
while (s_all_actions->is_empty())
|
|
|
|
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
|
|
|
|
2021-07-07 12:23:06 +00:00
|
|
|
for (auto& action : actions)
|
|
|
|
action();
|
|
|
|
|
|
|
|
actions.clear();
|
|
|
|
}
|
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()>>;
|
2021-05-22 16:47:42 +00:00
|
|
|
s_background_thread = &Threading::Thread::construct(background_thread_func).leak_ref();
|
2019-08-25 15:55:56 +00:00
|
|
|
s_background_thread->set_name("Background thread");
|
|
|
|
s_background_thread->start();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|