Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::network_send_jthread_worker Class Reference

Worker thread for sending buffered logs with jthread compatibility. More...

Collaboration diagram for kcenon::logger::network_send_jthread_worker:
Collaboration graph

Public Types

using send_callback = std::function<void()>
 

Public Member Functions

 network_send_jthread_worker (send_callback callback)
 
 ~network_send_jthread_worker ()
 
void start ()
 
void stop ()
 
void notify_work ()
 

Private Attributes

send_callback callback_
 
compat_jthread thread_
 
std::atomic< bool > running_ {false}
 
std::atomic< bool > has_work_ {false}
 

Detailed Description

Worker thread for sending buffered logs with jthread compatibility.

Uses std::jthread with std::stop_token for cooperative cancellation where available, falls back to std::thread with manual stop mechanism.

Definition at line 52 of file network_writer.cpp.

Member Typedef Documentation

◆ send_callback

Definition at line 54 of file network_writer.cpp.

Constructor & Destructor Documentation

◆ network_send_jthread_worker()

kcenon::logger::network_send_jthread_worker::network_send_jthread_worker ( send_callback callback)
inlineexplicit

Definition at line 56 of file network_writer.cpp.

57 : callback_(std::move(callback))
58 {}

◆ ~network_send_jthread_worker()

kcenon::logger::network_send_jthread_worker::~network_send_jthread_worker ( )
inline

Definition at line 60 of file network_writer.cpp.

References stop().

Here is the call graph for this function:

Member Function Documentation

◆ notify_work()

void kcenon::logger::network_send_jthread_worker::notify_work ( )
inline

Definition at line 105 of file network_writer.cpp.

105 {
106 has_work_.store(true, std::memory_order_release);
107 }

References has_work_.

◆ start()

void kcenon::logger::network_send_jthread_worker::start ( )
inline

Definition at line 64 of file network_writer.cpp.

64 {
65 if (running_.exchange(true, std::memory_order_acq_rel)) {
66 return; // Already started
67 }
68
69#if LOGGER_HAS_JTHREAD
70 auto callback = callback_;
71 thread_ = compat_jthread([callback](std::stop_token stop_token) {
72 while (!stop_token.stop_requested()) {
73 if (callback) {
74 callback();
75 }
76 std::this_thread::sleep_for(std::chrono::milliseconds(10));
77 }
78 });
79#else
80 // Create worker thread with manual stop source
81 // Note: We use the stop_source created by compat_jthread to ensure
82 // request_stop() correctly signals the worker loop
83 auto callback = callback_;
84 thread_ = compat_jthread([callback](simple_stop_source& stop) {
85 while (!stop.stop_requested()) {
86 if (callback) {
87 callback();
88 }
89 std::this_thread::sleep_for(std::chrono::milliseconds(10));
90 }
91 });
92#endif
93 }

References callback_, running_, stop(), and thread_.

Here is the call graph for this function:

◆ stop()

void kcenon::logger::network_send_jthread_worker::stop ( )
inline

Definition at line 95 of file network_writer.cpp.

95 {
96 if (!running_.exchange(false, std::memory_order_acq_rel)) {
97 return; // Already stopped
98 }
99
100 // Request stop and join thread
102 thread_.join();
103 }
void request_stop()
Request the thread to stop.
void join()
Wait for thread to complete.

References kcenon::logger::async::compat_jthread::join(), kcenon::logger::async::compat_jthread::request_stop(), running_, and thread_.

Referenced by start(), and ~network_send_jthread_worker().

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ callback_

send_callback kcenon::logger::network_send_jthread_worker::callback_
private

Definition at line 110 of file network_writer.cpp.

Referenced by start().

◆ has_work_

std::atomic<bool> kcenon::logger::network_send_jthread_worker::has_work_ {false}
private

Definition at line 113 of file network_writer.cpp.

113{false};

Referenced by notify_work().

◆ running_

std::atomic<bool> kcenon::logger::network_send_jthread_worker::running_ {false}
private

Definition at line 112 of file network_writer.cpp.

112{false};

Referenced by start(), and stop().

◆ thread_

compat_jthread kcenon::logger::network_send_jthread_worker::thread_
private

Definition at line 111 of file network_writer.cpp.

Referenced by start(), and stop().


The documentation for this class was generated from the following file: