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