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>
|
|
|
|
#include <LibThreading/Lock.h>
|
|
|
|
#include <LibThreading/Thread.h>
|
2021-07-04 18:01:01 +02:00
|
|
|
#include <unistd.h>
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2021-05-22 18:47:42 +02:00
|
|
|
static Threading::Lockable<Queue<Function<void()>>>* s_all_actions;
|
|
|
|
static Threading::Thread* s_background_thread;
|
2021-07-04 18:01:01 +02:00
|
|
|
static int s_notify_pipe_fds[2];
|
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
|
|
|
{
|
|
|
|
while (true) {
|
2021-07-04 18:01:01 +02:00
|
|
|
char buffer[1];
|
|
|
|
auto nread = read(s_notify_pipe_fds[0], buffer, sizeof(buffer));
|
|
|
|
if (nread < 0) {
|
|
|
|
perror("read");
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Function<void()>> work_items;
|
2019-08-25 18:55:56 +03:00
|
|
|
{
|
2021-05-22 18:47:42 +02:00
|
|
|
Threading::Locker locker(s_all_actions->lock());
|
2019-08-25 18:55:56 +03:00
|
|
|
|
2021-07-04 18:01:01 +02:00
|
|
|
while (!s_all_actions->resource().is_empty()) {
|
|
|
|
work_items.append(s_all_actions->resource().dequeue());
|
|
|
|
}
|
2019-08-25 18:55:56 +03:00
|
|
|
}
|
2021-07-04 18:01:01 +02:00
|
|
|
|
|
|
|
for (auto& work_item : work_items)
|
2019-08-25 18:55:56 +03:00
|
|
|
work_item();
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-25 18:55:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void init()
|
|
|
|
{
|
2021-07-04 18:01:01 +02:00
|
|
|
if (pipe(s_notify_pipe_fds) < 0) {
|
|
|
|
perror("pipe");
|
|
|
|
_exit(1);
|
|
|
|
}
|
2021-05-22 18:47:42 +02:00
|
|
|
s_all_actions = new Threading::Lockable<Queue<Function<void()>>>();
|
|
|
|
s_background_thread = &Threading::Thread::construct(background_thread_func).leak_ref();
|
2019-08-25 18:55:56 +03:00
|
|
|
s_background_thread->set_name("Background thread");
|
|
|
|
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-04 18:01:01 +02:00
|
|
|
Locker locker(s_all_actions->lock());
|
|
|
|
s_all_actions->resource().enqueue(move(work));
|
|
|
|
char ch = 'x';
|
|
|
|
if (write(s_notify_pipe_fds[1], &ch, sizeof(ch)) < 0) {
|
|
|
|
perror("write");
|
|
|
|
_exit(1);
|
|
|
|
}
|
2019-08-25 18:55:56 +03:00
|
|
|
}
|