src/internal/worker_start_log.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #ifndef DETOURMODKIT_INTERNAL_WORKER_START_LOG_HPP | ||
| 2 | #define DETOURMODKIT_INTERNAL_WORKER_START_LOG_HPP | ||
| 3 | |||
| 4 | #include <source_location> | ||
| 5 | #include <string_view> | ||
| 6 | |||
| 7 | namespace DetourModKit::detail | ||
| 8 | { | ||
| 9 | /** | ||
| 10 | * @class WorkerStartLogDeferral | ||
| 11 | * @brief Defers the StoppableWorker constructor record on the current thread. | ||
| 12 | * @details The context must remain alive until scope destruction. The sink must accept that context type and must | ||
| 13 | * not throw. | ||
| 14 | */ | ||
| 15 | class WorkerStartLogDeferral | ||
| 16 | { | ||
| 17 | public: | ||
| 18 | using Sink = void (*)(void *, std::string_view, std::source_location) noexcept; | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @brief Enters one nested deferral scope. | ||
| 22 | * @param context The sink context for this scope. | ||
| 23 | * @param sink The no-throw adapter that records one canonical start line. | ||
| 24 | */ | ||
| 25 | 216 | WorkerStartLogDeferral(void *context, Sink sink) noexcept | |
| 26 | 216 | : m_context(context), m_sink(sink), m_previous(current()) | |
| 27 | { | ||
| 28 | 216 | current() = this; | |
| 29 | 216 | } | |
| 30 | |||
| 31 | /// Leaves one nested deferral scope. | ||
| 32 | 216 | ~WorkerStartLogDeferral() noexcept { current() = m_previous; } | |
| 33 | |||
| 34 | WorkerStartLogDeferral(const WorkerStartLogDeferral &) = delete; | ||
| 35 | WorkerStartLogDeferral &operator=(const WorkerStartLogDeferral &) = delete; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @brief Routes one start line through the current deferral scope. | ||
| 39 | * @param name The worker name. | ||
| 40 | * @param where The original record source. | ||
| 41 | * @return true if a scope consumed the record, even when its sink recorded a drop. | ||
| 42 | */ | ||
| 43 | 229 | [[nodiscard]] static bool defer_start(std::string_view name, std::source_location where) noexcept | |
| 44 | { | ||
| 45 | 229 | WorkerStartLogDeferral *scope = current(); | |
| 46 |
3/4✓ Branch 3 → 4 taken 213 times.
✓ Branch 3 → 5 taken 16 times.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 213 times.
|
229 | if (scope == nullptr || scope->m_sink == nullptr) |
| 47 | { | ||
| 48 | 16 | return false; | |
| 49 | } | ||
| 50 | 213 | scope->m_sink(scope->m_context, name, where); | |
| 51 | 213 | return true; | |
| 52 | } | ||
| 53 | |||
| 54 | private: | ||
| 55 | 877 | [[nodiscard]] static WorkerStartLogDeferral *¤t() noexcept | |
| 56 | { | ||
| 57 | static thread_local WorkerStartLogDeferral *s_current = nullptr; | ||
| 58 | 877 | return s_current; | |
| 59 | } | ||
| 60 | |||
| 61 | void *m_context; | ||
| 62 | Sink m_sink; | ||
| 63 | WorkerStartLogDeferral *m_previous; | ||
| 64 | }; | ||
| 65 | } // namespace DetourModKit::detail | ||
| 66 | |||
| 67 | #endif // DETOURMODKIT_INTERNAL_WORKER_START_LOG_HPP | ||
| 68 |