oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
workqueue.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#ifdef CONFIG_OVE_WORKQUEUE
17
18#include <ove/workqueue.h>
19#include <ove/types.hpp>
20#include <ove/error.hpp>
21
22namespace ove
23{
24
32template <typename F>
33concept WorkHandler = std::convertible_to<F, ove_work_fn>;
34
46template <size_t StackSize = 0> class Workqueue
47{
48 public:
59 Workqueue(const char *name, ove_prio_t prio)
60 requires(StackSize > 0)
61 {
62#ifdef CONFIG_OVE_ZERO_HEAP
63 static_assert(StackSize > 0, "StackSize must be > 0 in zero-heap mode");
64 int err = ove_workqueue_init(&handle_, &storage_, name, prio, StackSize, stack_);
65#else
66 int err = ove_workqueue_create(&handle_, name, prio, StackSize);
67#endif
68 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
69 }
70
74 ~Workqueue() noexcept
75 {
76 if (!handle_)
77 return;
78#ifdef CONFIG_OVE_ZERO_HEAP
79 ove_workqueue_deinit(handle_);
80#else
81 ove_workqueue_destroy(handle_);
82#endif
83 }
84
85 Workqueue(const Workqueue &) = delete;
86 Workqueue &operator=(const Workqueue &) = delete;
87
88#ifdef CONFIG_OVE_ZERO_HEAP
89 Workqueue(Workqueue &&) = delete;
90 Workqueue &operator=(Workqueue &&) = delete;
91#else
96 Workqueue(Workqueue &&other) noexcept : handle_(other.handle_)
97 {
98 other.handle_ = nullptr;
99 }
100
106 Workqueue &operator=(Workqueue &&other) noexcept
107 {
108 if (this != &other) {
109 if (handle_)
110 ove_workqueue_destroy(handle_);
111 handle_ = other.handle_;
112 other.handle_ = nullptr;
113 }
114 return *this;
115 }
116#endif
117
122 bool valid() const
123 {
124 return handle_ != nullptr;
125 }
126
131 ove_workqueue_t handle() const
132 {
133 return handle_;
134 }
135
136 private:
137 ove_workqueue_t handle_ = nullptr;
138#ifdef CONFIG_OVE_ZERO_HEAP
139 ove_workqueue_storage_t storage_ = {};
140 OVE_THREAD_STACK_MEMBER_(stack_, StackSize > 0 ? StackSize : 1);
141#endif
142};
143
154class Work
155{
156 public:
164 template <typename F>
165 explicit Work(F handler)
166 requires WorkHandler<F>
167 {
168#ifdef CONFIG_OVE_ZERO_HEAP
169 int err = ove_work_init_static(&handle_, &storage_, handler);
170#else
171 int err = ove_work_init(&handle_, handler);
172#endif
173 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
174 }
175
179 ~Work() noexcept
180 {
181 if (!handle_)
182 return;
183#ifndef CONFIG_OVE_ZERO_HEAP
184 ove_work_free(handle_);
185#endif
186 }
187
188 Work(const Work &) = delete;
189 Work &operator=(const Work &) = delete;
190
191#ifdef CONFIG_OVE_ZERO_HEAP
192 Work(Work &&) = delete;
193 Work &operator=(Work &&) = delete;
194#else
199 Work(Work &&other) noexcept : handle_(other.handle_)
200 {
201 other.handle_ = nullptr;
202 }
203
209 Work &operator=(Work &&other) noexcept
210 {
211 if (this != &other) {
212 if (handle_)
213 ove_work_free(handle_);
214 handle_ = other.handle_;
215 other.handle_ = nullptr;
216 }
217 return *this;
218 }
219#endif
220
228 template <size_t S> [[nodiscard]] Result<void> submit(Workqueue<S> &wq) noexcept
229 {
230 return from_rc(ove_work_submit(wq.handle(), handle_));
231 }
232
241 template <size_t S>
242 [[nodiscard]] Result<void> submit_delayed(Workqueue<S> &wq, uint32_t delay_ms) noexcept
243 {
244 return from_rc(ove_work_submit_delayed(wq.handle(), handle_, delay_ms));
245 }
246
255 [[nodiscard]] Result<void> cancel() noexcept
256 {
257 return from_rc(ove_work_cancel(handle_));
258 }
259
264 bool valid() const
265 {
266 return handle_ != nullptr;
267 }
268
273 ove_work_t handle() const
274 {
275 return handle_;
276 }
277
278 private:
279 ove_work_t handle_ = nullptr;
280#ifdef CONFIG_OVE_ZERO_HEAP
281 ove_work_storage_t storage_ = {};
282#endif
283};
284
285} /* namespace ove */
286
287#endif /* CONFIG_OVE_WORKQUEUE */
RAII wrapper representing a single deferred work item.
Definition workqueue.hpp:155
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition workqueue.hpp:264
Result< void > cancel() noexcept
Attempts to cancel a pending work item.
Definition workqueue.hpp:255
Work(Work &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition workqueue.hpp:199
Result< void > submit_delayed(Workqueue< S > &wq, uint32_t delay_ms) noexcept
Submits the work item to a workqueue with a delay.
Definition workqueue.hpp:242
Work & operator=(Work &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition workqueue.hpp:209
~Work() noexcept
Destroys the work item, freeing its kernel resource (heap mode).
Definition workqueue.hpp:179
Work(F handler)
Constructs a work item with the given handler function.
Definition workqueue.hpp:165
ove_work_t handle() const
Returns the raw oveRTOS work handle.
Definition workqueue.hpp:273
Result< void > submit(Workqueue< S > &wq) noexcept
Submits the work item to a workqueue for immediate execution.
Definition workqueue.hpp:228
RAII wrapper around an oveRTOS workqueue (dedicated worker thread).
Definition workqueue.hpp:47
~Workqueue() noexcept
Destroys the workqueue and terminates the worker thread.
Definition workqueue.hpp:74
Workqueue(Workqueue &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition workqueue.hpp:96
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition workqueue.hpp:122
Workqueue(const char *name, ove_prio_t prio)
Constructs and starts the workqueue.
Definition workqueue.hpp:59
ove_workqueue_t handle() const
Returns the raw oveRTOS workqueue handle.
Definition workqueue.hpp:131
Workqueue & operator=(Workqueue &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition workqueue.hpp:106
Concept satisfied by any callable convertible to ove_work_fn.
Definition workqueue.hpp:33
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139
Common type definitions and concepts for the C++ wrapper layer.