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
21namespace ove {
22
30template <typename F>
31concept WorkHandler = std::convertible_to<F, ove_work_fn>;
32
44template <size_t StackSize = 0>
45class Workqueue {
46public:
57 Workqueue(const char *name, ove_prio_t prio)
58 requires (StackSize > 0)
59 {
60#ifdef CONFIG_OVE_ZERO_HEAP
61 static_assert(StackSize > 0,
62 "StackSize must be > 0 in zero-heap mode");
63 int err = ove_workqueue_init(&handle_, &storage_, name,
64 prio, StackSize, stack_);
65#else
66 int err = ove_workqueue_create(&handle_, name, prio,
67 StackSize);
68#endif
69 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
70 }
71
76 if (!handle_) return;
77#ifdef CONFIG_OVE_ZERO_HEAP
78 ove_workqueue_deinit(handle_);
79#else
80 ove_workqueue_destroy(handle_);
81#endif
82 }
83
84 Workqueue(const Workqueue &) = delete;
85 Workqueue &operator=(const Workqueue &) = delete;
86
87#ifdef CONFIG_OVE_ZERO_HEAP
88 Workqueue(Workqueue &&) = delete;
89 Workqueue &operator=(Workqueue &&) = delete;
90#else
95 Workqueue(Workqueue &&other) noexcept : handle_(other.handle_) {
96 other.handle_ = nullptr;
97 }
98
104 Workqueue &operator=(Workqueue &&other) noexcept {
105 if (this != &other) {
106 if (handle_) ove_workqueue_destroy(handle_);
107 handle_ = other.handle_;
108 other.handle_ = nullptr;
109 }
110 return *this;
111 }
112#endif
113
118 bool valid() const { return handle_ != nullptr; }
119
124 ove_workqueue_t handle() const { return handle_; }
125
126private:
127 ove_workqueue_t handle_ = nullptr;
128#ifdef CONFIG_OVE_ZERO_HEAP
129 ove_workqueue_storage_t storage_ = {};
130 OVE_THREAD_STACK_MEMBER_(stack_,
131 StackSize > 0 ? StackSize : 1);
132#endif
133};
134
145class Work {
146public:
154 template <typename F>
155 explicit Work(F handler) requires WorkHandler<F> {
156#ifdef CONFIG_OVE_ZERO_HEAP
157 int err = ove_work_init_static(&handle_, &storage_,
158 handler);
159#else
160 int err = ove_work_init(&handle_, handler);
161#endif
162 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
163 }
164
169 if (!handle_) return;
170#ifndef CONFIG_OVE_ZERO_HEAP
171 ove_work_free(handle_);
172#endif
173 }
174
175 Work(const Work &) = delete;
176 Work &operator=(const Work &) = delete;
177
178#ifdef CONFIG_OVE_ZERO_HEAP
179 Work(Work &&) = delete;
180 Work &operator=(Work &&) = delete;
181#else
186 Work(Work &&other) noexcept : handle_(other.handle_) {
187 other.handle_ = nullptr;
188 }
189
195 Work &operator=(Work &&other) noexcept {
196 if (this != &other) {
197 if (handle_) ove_work_free(handle_);
198 handle_ = other.handle_;
199 other.handle_ = nullptr;
200 }
201 return *this;
202 }
203#endif
204
211 template <size_t S>
212 [[nodiscard]] int submit(Workqueue<S> &wq) {
213 return ove_work_submit(wq.handle(), handle_);
214 }
215
223 template <size_t S>
224 [[nodiscard]] int submit_delayed(Workqueue<S> &wq,
225 uint32_t delay_ms) {
226 return ove_work_submit_delayed(wq.handle(), handle_,
227 delay_ms);
228 }
229
237 [[nodiscard]] int cancel() {
238 return ove_work_cancel(handle_);
239 }
240
245 bool valid() const { return handle_ != nullptr; }
246
251 ove_work_t handle() const { return handle_; }
252
253private:
254 ove_work_t handle_ = nullptr;
255#ifdef CONFIG_OVE_ZERO_HEAP
256 ove_work_storage_t storage_ = {};
257#endif
258};
259
260} /* namespace ove */
261
262#endif /* CONFIG_OVE_WORKQUEUE */
RAII wrapper representing a single deferred work item.
Definition workqueue.hpp:145
int submit_delayed(Workqueue< S > &wq, uint32_t delay_ms)
Submits the work item to a workqueue with a delay.
Definition workqueue.hpp:224
int cancel()
Attempts to cancel a pending work item.
Definition workqueue.hpp:237
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition workqueue.hpp:245
Work(Work &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition workqueue.hpp:186
Work & operator=(Work &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition workqueue.hpp:195
int submit(Workqueue< S > &wq)
Submits the work item to a workqueue for immediate execution.
Definition workqueue.hpp:212
Work(F handler)
Constructs a work item with the given handler function.
Definition workqueue.hpp:155
ove_work_t handle() const
Returns the raw oveRTOS work handle.
Definition workqueue.hpp:251
~Work()
Destroys the work item, freeing its kernel resource (heap mode).
Definition workqueue.hpp:168
RAII wrapper around an oveRTOS workqueue (dedicated worker thread).
Definition workqueue.hpp:45
Workqueue(Workqueue &&other) noexcept
Move constructor — transfers ownership of the kernel handle.
Definition workqueue.hpp:95
bool valid() const
Returns true if the underlying kernel handle is non-null.
Definition workqueue.hpp:118
Workqueue(const char *name, ove_prio_t prio)
Constructs and starts the workqueue.
Definition workqueue.hpp:57
ove_workqueue_t handle() const
Returns the raw oveRTOS workqueue handle.
Definition workqueue.hpp:124
~Workqueue()
Destroys the workqueue and terminates the worker thread.
Definition workqueue.hpp:75
Workqueue & operator=(Workqueue &&other) noexcept
Move-assignment operator — transfers ownership of the kernel handle.
Definition workqueue.hpp:104
Concept satisfied by any callable convertible to ove_work_fn.
Definition workqueue.hpp:31
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19
Common type definitions and concepts for the C++ wrapper layer.