10 #include "../ma_common.h"
15 typedef std::function<void(std::atomic<bool>&)>
task_t;
21 static uint8_t worker_id = 0u;
22 static const char* hex_literals =
"0123456789ABCDEF";
27 _worker_name.reserve(_worker_name.length() + (
sizeof(uint8_t) << 1) + 1);
30 _worker_name += hex_literals[worker_id >> 4];
31 _worker_name += hex_literals[worker_id & 0x0f];
35 if (!_worker_handler->
start(
this)) {
36 delete _worker_handler;
43 _worker_handler->
stop();
44 delete _worker_handler;
48 template <
typename Callable>
inline void submit(Callable&& callable) {
49 const Guard guard(_task_queue_lock);
50 _task_queue.push(std::forward<Callable>(callable));
51 MA_LOGD(
"E",
"Executor::submit: %s, task count = %zu", _worker_name.c_str(), _task_queue.size());
55 const Guard guard(_task_queue_lock);
56 while (!_task_queue.empty()) _task_queue.pop();
65 const Guard guard(_task_queue_lock);
66 if (!_task_queue.empty()) [[likely]] {
67 task = std::move(_task_queue.front());
70 if (task) [[likely]] {
71 _task_reload.store(
false);
73 if (_task_reload.load()) [[unlikely]]
74 _task_queue.push(std::move(task));
82 Mutex _task_queue_lock;
83 std::atomic<bool> _task_reload;
84 std::string _worker_name;
87 std::queue<task_t> _task_queue;
Definition: ma_executor.hpp:17
~Executor()
Definition: ma_executor.hpp:41
Executor(std::size_t stack_size=MA_SEVER_AT_EXECUTOR_STACK_SIZE, std::size_t priority=MA_SEVER_AT_EXECUTOR_TASK_PRIO)
Definition: ma_executor.hpp:19
static void c_run(void *this_pointer)
Definition: ma_executor.hpp:79
void run()
Definition: ma_executor.hpp:60
void submit(Callable &&callable)
Definition: ma_executor.hpp:48
void cancel()
Definition: ma_executor.hpp:54
bool start(void *arg=nullptr)
static void sleep(ma_tick_t tick)
static ma_tick_t fromMilliseconds(uint32_t ms)
#define MA_LOGD(TAG,...)
Definition: ma_debug.h:79
#define MA_ASSERT(expr)
Definition: ma_debug.h:119
#define MA_EXECUTOR_WORKER_NAME_PREFIX
Definition: ma_definations.h:11
#define MA_SEVER_AT_EXECUTOR_TASK_PRIO
Definition: ma_server_at.h:17
#define MA_SEVER_AT_EXECUTOR_STACK_SIZE
Definition: ma_server_at.h:13
std::function< void(std::atomic< bool > &)> task_t
Definition: ma_executor.hpp:15