libhatchet
Loading...
Searching...
No Matches
hxtask_queue.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2017-2026 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
8
9#include "libhatchet.h"
10
11// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
12#if HX_USE_MACROS_WITH_MODULE
13#error Header does not provide macros alone.
14#endif
15
16#include "hxvector.hpp"
17#include "hxtask.hpp"
18#include "hxthread.hpp"
19
20HX_NS_BEGIN_
21
26public:
32 class record_t {
33 public:
36
39 bool operator<(const record_t& x_) const { return this->priority < x_.priority; }
40
41#if (HX_HARDENING_MODE) == HX_HARDENING_MODE_DEBUG
42 const char* label;
43 ~record_t() { ::memset(reinterpret_cast<void*>(this), 0xefu, sizeof *this); }
44#endif
45 };
46
50 explicit hxtask_queue(int32_t task_queue_size_, int32_t thread_pool_size_) noexcept;
51
54
60 template<typename callable_t_>
61 hxattr_nodiscard bool all_of(callable_t_&& callable_) const noexcept;
62
68 template<typename callable_t_>
69 hxattr_nodiscard bool any_of(callable_t_&& callable_) const noexcept;
70
75 bool cancel(hxtask* task_) noexcept hxattr_nonnull(2);
76
81 void clear(void) noexcept;
82
84 hxattr_nodiscard bool empty(void) const;
85
91 void enqueue(hxtask* task_, int priority_=0) noexcept hxattr_nonnull(2);
92
99 template<typename callable_t_>
100 hxsize_t erase_if(callable_t_&& callable_) noexcept;
101
104 template<typename callable_t_>
105 void for_each(callable_t_&& callable_) const noexcept;
106
111 template<typename callable_t_>
112 void for_each(callable_t_&& callable_) noexcept;
113
115 hxattr_nodiscard bool full(void) const;
116
120
123
130 void wait_for_all(void) noexcept;
131
132private:
134 hxtask_queue(const hxtask_queue&) = delete;
135 void operator=(const hxtask_queue&) = delete;
136
137 hxvector<record_t> m_tasks_;
138
139#if HX_USE_THREADS
140#define hxtask_queue_lock_ const hxunique_lock lock_(m_mutex_)
141
142 template<int32_t> friend class hxtask_dag_node;
143
144 enum class thread_mode_ : uint8_t {
145 pool_,
146 waiting_,
147 stopping_
148 };
149 enum class run_level_ : uint32_t {
150 running_ = 0x00c0ffeeu,
151 stopped_ = 0xdeadbeefu
152 };
153
154 static hxthread::return_t thread_task_loop_entry_(hxtask_queue* q_) noexcept;
155 static void thread_task_loop_(hxtask_queue& q_, thread_mode_ mode_) noexcept;
156
157 run_level_ m_queue_run_level_;
158 int32_t m_thread_pool_size_;
159 int32_t m_executing_count_;
160 hxthread* m_threads_;
161 mutable hxmutex m_mutex_;
162 hxcondition_variable m_cond_var_new_tasks_;
163 hxcondition_variable m_cond_var_completion_;
164#else
165#define hxtask_queue_lock_ ((void)0)
166#endif
168};
169
170#include "detail/hxtask_queue.inl"
171HX_NS_END_
hxtask_dag_node<max_successors> - A hxtask subclass that implements DAG dependency tracking as a laye...
Definition hxtask_dag_node.hpp:25
record_t - Iterated over by all_of, any_of, erase_if and for_each.
Definition hxtask_queue.hpp:32
hxtask * task
Definition hxtask_queue.hpp:34
const char * label
Definition hxtask_queue.hpp:42
int priority
Definition hxtask_queue.hpp:35
bool operator<(const record_t &x_) const
Orders records by priority so the highest priority sorts last.
Definition hxtask_queue.hpp:39
~record_t()
Definition hxtask_queue.hpp:43
hxtask_queue(int32_t task_queue_size_, int32_t thread_pool_size_) noexcept
Creates a new task queue.
void for_each(callable_t_ &&callable_) const noexcept
Locks the queue and calls callable on each task record.
~hxtask_queue(void)
Waits for all queued and executing tasks to complete before destruction.
hxsize_t erase_if(callable_t_ &&callable_) noexcept
Locks the queue and calls callable on each task.
bool cancel(hxtask *task_) noexcept
Returns true if the task was found and removed.
bool any_of(callable_t_ &&callable_) const noexcept
Locks the queue and calls callable on each task.
void clear(void) noexcept
Removes all queued tasks without executing them.
void wait_for_all(void) noexcept
Execute remaining tasks.
hxsize_t max_size(void) const
Returns the maximum number of tasks that can be queued.
bool all_of(callable_t_ &&callable_) const noexcept
Locks the queue and calls callable on each task.
void enqueue(hxtask *task_, int priority_=0) noexcept
Queues a task for later execution.
hxsize_t size(void) const
Returns the number of queued tasks. Thread-safe.
bool empty(void) const
Returns true when no tasks are queued. Thread-safe.
bool full(void) const
Returns true if the queue capacity has been reached.
hxtask - Pure virtual base class for operations to be performed on a different thread or at a later t...
Definition hxtask.hpp:23
int return_t
The return type of a thread entry point as required by the configured backend.
Definition hxthread.hpp:356
hxvector - Implements std::vector, std::inplace_vector and std::back_insert_iterator with a few thing...
Definition hxvector.hpp:82
#define hxattr_nonnull(...)
hxattr_nonnull - Indicates that a function has args that should not be null.
Definition hxsettings.h:97
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:89
Pure virtual base class for operations to be performed on a different thread or at a later time.
C++ wrappers for POSIX pthreads or C11 threads.h.
Implements std::vector, std::inplace_vector and std::back_insert_iterator with a few things added and...
Provides core macros, memory management and feature detection.
ptrdiff_t hxsize_t
hxsize_t - A signed size type, same as ssize_t or ptrdiff_t.
Definition libhatchet.h:209