Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
mock_executor Class Reference
Inheritance diagram for mock_executor:
Inheritance graph
Collaboration diagram for mock_executor:
Collaboration graph

Public Member Functions

 mock_executor (size_t num_workers=4)
 
 ~mock_executor ()
 
Result< std::future< void > > execute (std::unique_ptr< IJob > &&job) override
 Execute a job with Result-based error handling.
 
Result< std::future< void > > execute_delayed (std::unique_ptr< IJob > &&job, std::chrono::milliseconds delay) override
 Execute a job with delay.
 
size_t worker_count () const override
 Get the number of worker threads.
 
bool is_running () const override
 Check if the executor is running.
 
size_t pending_tasks () const override
 Get the number of pending tasks.
 
void shutdown (bool wait_for_completion) override
 Shutdown the executor gracefully.
 
- Public Member Functions inherited from kcenon::common::interfaces::IExecutor
virtual ~IExecutor ()=default
 
virtual ~IExecutor ()=default
 

Private Member Functions

void work_loop ()
 

Private Attributes

size_t num_workers_
 
std::atomic< bool > running_
 
std::atomic< size_t > pending_count_ {0}
 
std::vector< std::thread > workers_
 
std::queue< std::function< void()> > tasks_
 
std::mutex queue_mutex_
 
std::condition_variable queue_cv_
 

Detailed Description

Simple mock executor for demonstration

Examples
executor_example.cpp.

Definition at line 34 of file executor_example.cpp.

Constructor & Destructor Documentation

◆ mock_executor()

mock_executor::mock_executor ( size_t num_workers = 4)
inline
Examples
executor_example.cpp.

Definition at line 36 of file executor_example.cpp.

37 : num_workers_(num_workers), running_(true) {
38 // Start worker threads
39 for (size_t i = 0; i < num_workers_; ++i) {
40 workers_.emplace_back([this] { work_loop(); });
41 }
42 }
std::vector< std::thread > workers_
std::atomic< bool > running_

References num_workers_, work_loop(), and workers_.

Here is the call graph for this function:

◆ ~mock_executor()

mock_executor::~mock_executor ( )
inline
Examples
executor_example.cpp.

Definition at line 44 of file executor_example.cpp.

44 {
45 shutdown(true);
46 }
void shutdown(bool wait_for_completion) override
Shutdown the executor gracefully.

References shutdown().

Here is the call graph for this function:

Member Function Documentation

◆ execute()

Result< std::future< void > > mock_executor::execute ( std::unique_ptr< IJob > && job)
inlineoverridevirtual

Execute a job with Result-based error handling.

Parameters
jobThe job to execute
Returns
Result containing future or error

Job-based execution provides better control and testability

Implements kcenon::common::interfaces::IExecutor.

Examples
executor_example.cpp.

Definition at line 49 of file executor_example.cpp.

49 {
50 if (!job) {
51 return error_info(1, "Job is null", "mock_executor");
52 }
53
54 auto promise = std::make_shared<std::promise<void>>();
55 auto future = promise->get_future();
56
57 // Use shared_ptr to make lambda copy-constructible
58 auto shared_job = std::shared_ptr<IJob>(std::move(job));
59
60 {
61 std::lock_guard<std::mutex> lock(queue_mutex_);
62 tasks_.emplace([shared_job, promise]() {
63 try {
64 auto result = shared_job->execute();
65 if (result.is_err()) {
66 const auto& err = result.error();
67 promise->set_exception(
68 std::make_exception_ptr(
69 std::runtime_error(err.message)));
70 } else {
71 promise->set_value();
72 }
73 } catch (...) {
74 promise->set_exception(std::current_exception());
75 }
76 });
78 }
79 queue_cv_.notify_one();
80
81 return ok(std::move(future));
82 }
const error_info & error() const
Get error reference.
Definition core.h:405
std::queue< std::function< void()> > tasks_
std::atomic< size_t > pending_count_
std::mutex queue_mutex_
std::condition_variable queue_cv_
VoidResult err(const error_info &error)
Factory function to create error VoidResult.
Definition core.cppm:432
VoidResult ok()
Create a successful void result.
Definition utilities.h:71
Standard error information used by Result<T>.
Definition core.cppm:106

References kcenon::common::err(), kcenon::common::Result< T >::error(), kcenon::common::ok(), pending_count_, queue_cv_, queue_mutex_, and tasks_.

Referenced by main().

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

◆ execute_delayed()

Result< std::future< void > > mock_executor::execute_delayed ( std::unique_ptr< IJob > && job,
std::chrono::milliseconds delay )
inlineoverridevirtual

Execute a job with delay.

Parameters
jobThe job to execute
delayThe delay before execution
Returns
Result containing future or error

Implements kcenon::common::interfaces::IExecutor.

Examples
executor_example.cpp.

Definition at line 84 of file executor_example.cpp.

86 {
87 if (!job) {
88 return error_info(1, "Job is null", "mock_executor");
89 }
90
91 auto promise = std::make_shared<std::promise<void>>();
92 auto future = promise->get_future();
93
94 // Use shared_ptr to make lambda copy-constructible
95 auto shared_job = std::shared_ptr<IJob>(std::move(job));
96
97 {
98 std::lock_guard<std::mutex> lock(queue_mutex_);
99 tasks_.emplace([shared_job, promise, delay]() {
100 std::this_thread::sleep_for(delay);
101 try {
102 auto result = shared_job->execute();
103 if (result.is_err()) {
104 const auto& err = result.error();
105 promise->set_exception(
106 std::make_exception_ptr(
107 std::runtime_error(err.message)));
108 } else {
109 promise->set_value();
110 }
111 } catch (...) {
112 promise->set_exception(std::current_exception());
113 }
114 });
116 }
117 queue_cv_.notify_one();
118
119 return ok(std::move(future));
120 }

References kcenon::common::err(), kcenon::common::Result< T >::error(), kcenon::common::ok(), pending_count_, queue_cv_, queue_mutex_, and tasks_.

Referenced by main().

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

◆ is_running()

bool mock_executor::is_running ( ) const
inlineoverridevirtual

Check if the executor is running.

Returns
true if running, false otherwise

Implements kcenon::common::interfaces::IExecutor.

Examples
executor_example.cpp.

Definition at line 126 of file executor_example.cpp.

126 {
127 return running_;
128 }

References running_.

Referenced by main().

Here is the caller graph for this function:

◆ pending_tasks()

size_t mock_executor::pending_tasks ( ) const
inlineoverridevirtual

Get the number of pending tasks.

Returns
Number of tasks waiting to be executed

Implements kcenon::common::interfaces::IExecutor.

Examples
executor_example.cpp.

Definition at line 130 of file executor_example.cpp.

130 {
131 return pending_count_;
132 }

References pending_count_.

Referenced by main().

Here is the caller graph for this function:

◆ shutdown()

void mock_executor::shutdown ( bool wait_for_completion)
inlineoverridevirtual

Shutdown the executor gracefully.

Parameters
wait_for_completionWait for all pending tasks to complete

Implements kcenon::common::interfaces::IExecutor.

Examples
executor_example.cpp.

Definition at line 134 of file executor_example.cpp.

134 {
135 if (!running_) return;
136
137 if (wait_for_completion) {
138 // Wait for all tasks to complete
139 std::unique_lock<std::mutex> lock(queue_mutex_);
140 queue_cv_.wait(lock, [this] { return tasks_.empty(); });
141 }
142
143 running_ = false;
144 queue_cv_.notify_all();
145
146 for (auto& worker : workers_) {
147 if (worker.joinable()) {
148 worker.join();
149 }
150 }
151 }

References queue_cv_, queue_mutex_, running_, tasks_, and workers_.

Referenced by main(), and ~mock_executor().

Here is the caller graph for this function:

◆ work_loop()

void mock_executor::work_loop ( )
inlineprivate
Examples
executor_example.cpp.

Definition at line 154 of file executor_example.cpp.

154 {
155 while (running_) {
156 std::function<void()> task;
157
158 {
159 std::unique_lock<std::mutex> lock(queue_mutex_);
160 queue_cv_.wait(lock, [this] {
161 return !tasks_.empty() || !running_;
162 });
163
164 if (!running_ && tasks_.empty()) {
165 break;
166 }
167
168 if (!tasks_.empty()) {
169 task = std::move(tasks_.front());
170 tasks_.pop();
172 }
173 }
174
175 if (task) {
176 task();
177 }
178 }
179 }

References pending_count_, queue_cv_, queue_mutex_, running_, and tasks_.

Referenced by mock_executor().

Here is the caller graph for this function:

◆ worker_count()

size_t mock_executor::worker_count ( ) const
inlineoverridevirtual

Get the number of worker threads.

Returns
Number of available workers

Implements kcenon::common::interfaces::IExecutor.

Examples
executor_example.cpp.

Definition at line 122 of file executor_example.cpp.

122 {
123 return num_workers_;
124 }

References num_workers_.

Referenced by main().

Here is the caller graph for this function:

Member Data Documentation

◆ num_workers_

size_t mock_executor::num_workers_
private
Examples
executor_example.cpp.

Definition at line 181 of file executor_example.cpp.

Referenced by mock_executor(), and worker_count().

◆ pending_count_

std::atomic<size_t> mock_executor::pending_count_ {0}
private
Examples
executor_example.cpp.

Definition at line 183 of file executor_example.cpp.

183{0};

Referenced by execute(), execute_delayed(), pending_tasks(), and work_loop().

◆ queue_cv_

std::condition_variable mock_executor::queue_cv_
private
Examples
executor_example.cpp.

Definition at line 187 of file executor_example.cpp.

Referenced by execute(), execute_delayed(), shutdown(), and work_loop().

◆ queue_mutex_

std::mutex mock_executor::queue_mutex_
private
Examples
executor_example.cpp.

Definition at line 186 of file executor_example.cpp.

Referenced by execute(), execute_delayed(), shutdown(), and work_loop().

◆ running_

std::atomic<bool> mock_executor::running_
private
Examples
executor_example.cpp.

Definition at line 182 of file executor_example.cpp.

Referenced by is_running(), shutdown(), and work_loop().

◆ tasks_

std::queue<std::function<void()> > mock_executor::tasks_
private
Examples
executor_example.cpp.

Definition at line 185 of file executor_example.cpp.

Referenced by execute(), execute_delayed(), shutdown(), and work_loop().

◆ workers_

std::vector<std::thread> mock_executor::workers_
private
Examples
executor_example.cpp.

Definition at line 184 of file executor_example.cpp.

Referenced by mock_executor(), and shutdown().


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