Lightweight 0.20260617.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/// Portable async backend that offloads blocking ODBC work to a worker thread.
12///
13/// Every blocking operation for the connection runs on a per-connection @ref StrandExecutor
14/// (so the connection is serialized), and the awaiting coroutine resumes on the injected
15/// resume scheduler. This backend works with every ODBC driver on every platform and is the
16/// fallback whenever native driver async is unavailable.
17///
18/// This type is header-only (all members are inline), so it is intentionally not marked with
19/// the DLL export macro.
21{
22 public:
23 /// Constructs the backend.
24 ///
25 /// @param dbWorkers The shared worker-thread pool that actually runs blocking work.
26 /// @param resume The scheduler used to resume coroutines (typically the app run loop).
27 /// @note Not @c noexcept: constructing the strand allocates its shared state.
29 _strand { dbWorkers },
30 _resume { resume }
31 {
32 }
33
34 [[nodiscard]] StrandExecutor& Strand() noexcept override
35 {
36 return _strand;
37 }
38
39 [[nodiscard]] IResumeScheduler& ResumeScheduler() noexcept override
40 {
41 return _resume;
42 }
43
44 private:
45 StrandExecutor _strand;
46 IResumeScheduler& _resume;
47};
48
49} // namespace Lightweight::Async
StrandExecutor & Strand() noexcept override
IResumeScheduler & ResumeScheduler() noexcept override
ThreadOffloadBackend(IExecutor &dbWorkers, IResumeScheduler &resume)