Lightweight 0.20260921.0
Loading...
Searching...
No Matches
ThreadOffloadBackend.hpp
1// SPDX-License-Identifier: Apache-2.0
2#pragma once
3
4#include "Backend.hpp"
5#include "Executor.hpp"
6#include "StrandExecutor.hpp"
7
8namespace Lightweight::Async
9{
10
11/// @ingroup Async
12/// Portable async backend that offloads blocking ODBC work to a worker thread.
13///
14/// Every blocking operation for the connection runs on a per-connection @ref StrandExecutor
15/// (so the connection is serialized), and the awaiting coroutine resumes on the injected
16/// resume scheduler. This backend works with every ODBC driver on every platform and is the
17/// fallback whenever native driver async is unavailable.
18///
19/// This type is header-only (all members are inline), so it is intentionally not marked with
20/// the DLL export macro.
22{
23 public:
24 /// Constructs the backend.
25 ///
26 /// @param dbWorkers The shared worker-thread pool that actually runs blocking work.
27 /// @param resume The scheduler used to resume coroutines (typically the app run loop).
28 /// @note Not @c noexcept: constructing the strand allocates its shared state.
30 _strand { dbWorkers },
31 _resume { resume }
32 {
33 }
34
35 [[nodiscard]] StrandExecutor& Strand() noexcept override
36 {
37 return _strand;
38 }
39
40 [[nodiscard]] IResumeScheduler& ResumeScheduler() noexcept override
41 {
42 return _resume;
43 }
44
45 private:
46 StrandExecutor _strand;
47 IResumeScheduler& _resume;
48};
49
50} // namespace Lightweight::Async
StrandExecutor & Strand() noexcept override
IResumeScheduler & ResumeScheduler() noexcept override
ThreadOffloadBackend(IExecutor &dbWorkers, IResumeScheduler &resume)