wxMaxima
Loading...
Searching...
No Matches
BackgroundQueue.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2024 the wxMaxima team
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
26#ifndef BACKGROUNDQUEUE_H
27#define BACKGROUNDQUEUE_H
28
29#include "Version.h" // stop_token / stop_source (real ones or shims)
30#include <condition_variable>
31#include <deque>
32#include <functional>
33#include <memory>
34#include <mutex>
35#include <thread>
36#include <utility>
37#include <vector>
38
46{
47public:
49 enum class Priority
50 {
51 High,
52 Low
53 };
54
59 void RequestStop();
60
63 void Wait();
64
66 bool Pending() const;
67
68 ~BackgroundTask() = default;
69
70private:
71 friend class BackgroundQueue;
72 explicit BackgroundTask(std::function<void(stop_token)> fn)
73 : m_fn(std::move(fn)) {}
74
76 void Run();
77
78 enum class State { Queued, Running, Done };
79
80 std::function<void(stop_token)> m_fn;
81 stop_source m_stopSource;
82 mutable std::mutex m_mutex;
83 std::condition_variable m_doneCond;
84 State m_state = State::Queued;
85};
86
98{
99public:
106 static std::shared_ptr<BackgroundTask>
107 Add(BackgroundTask::Priority priority, std::function<void(stop_token)> task);
108
110 static int Workers();
111
112 BackgroundQueue(const BackgroundQueue &) = delete;
113 BackgroundQueue &operator=(const BackgroundQueue &) = delete;
114
115private:
118
119 static BackgroundQueue &Get();
120
121 void Enqueue(const std::shared_ptr<BackgroundTask> &task,
122 BackgroundTask::Priority priority);
125 void Drop(BackgroundTask *task);
126 void WorkerLoop();
127
128 friend class BackgroundTask;
129
130 std::mutex m_mutex;
131 std::condition_variable m_cond;
132 std::deque<std::shared_ptr<BackgroundTask>> m_high;
133 std::deque<std::shared_ptr<BackgroundTask>> m_low;
134 std::vector<std::thread> m_workers;
135 bool m_shutdown = false;
136};
137
138#endif // BACKGROUNDQUEUE_H
A process-wide thread pool that runs BackgroundTasks, highest priority first.
Definition: BackgroundQueue.h:98
static int Workers()
The number of worker threads the pool runs.
Definition: BackgroundQueue.cpp:75
static std::shared_ptr< BackgroundTask > Add(BackgroundTask::Priority priority, std::function< void(stop_token)> task)
Enqueue a task at the given priority and return a handle to it.
Definition: BackgroundQueue.cpp:125
A unit of work that has been handed to the BackgroundQueue.
Definition: BackgroundQueue.h:46
Priority
The two priority levels the queue serves. High is always served first.
Definition: BackgroundQueue.h:50
void RequestStop()
Ask the task to stop at its next cancellation point. If it has not been started yet it is dropped fro...
Definition: BackgroundQueue.cpp:50
bool Pending() const
Whether the task is still waiting in the queue or currently running.
Definition: BackgroundQueue.cpp:63
void Wait()
Block until the task has finished (or was dropped before it started). Returns immediately if that alr...
Definition: BackgroundQueue.cpp:57