Skip to content

Commit 532d8e2

Browse files
committed
Merge branch 'develop'
2 parents 94e39c3 + 8692a23 commit 532d8e2

38 files changed

+850
-129
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
See projects:
2020

21-
- [cpp-socks5](https://github.com/cpp-main/cpp-socks5);
22-
- [protocal-adapter](https://github.com/cpp-main/protocal-adapter)
21+
- [cpp-socks5](https://github.com/cpp-main/cpp-socks5), learn how to use TCP and how to organize the project;
22+
- [protocal-adapter](https://github.com/cpp-main/protocal-adapter), learn how to use TCP, UDP, MQTT, UART;
23+
- [cpp-tbox-for-ros](https://github.com/cpp-main/cpp-tbox-for-ros), learn how to how to integrate into the ROS 2 development environment, and how to use `rclcpp`;
2324

2425
# Features
2526

README_CN.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919
参考项目:
2020

21-
- [cpp-socks5](https://gitee.com/cpp-master/cpp-socks5)
22-
- [protocal-adapter](https://gitee.com/cpp-master/protocal-adapter)
21+
- [cpp-socks5](https://gitee.com/cpp-master/cpp-socks5),参考如何进行TCP通信,以及如何组织工程;
22+
- [protocal-adapter](https://gitee.com/cpp-master/protocal-adapter),参考如何进行 TCP, UDP, MQTT, UART 通信;
23+
- [cpp-tbox-for-ros](https://gitee.com/cpp-master/cpp-tbox-for-ros),参考如何集成到ROS2开发环境,以及如何例用 rclcpp 进行通信;
2324

2425
# 特点
2526

examples/main/02_more_than_one_apps/app2/app.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ App::~App()
3434
LogTag();
3535
}
3636

37-
void App::onFillDefaultConfig(tbox::Json &cfg)
37+
void App::onFillDefaultConfig(tbox::Json &cfg) const
3838
{
3939
cfg["ok"] = true;
4040
}

examples/main/02_more_than_one_apps/app2/app.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class App : public tbox::main::Module
3131
~App();
3232

3333
protected:
34-
virtual void onFillDefaultConfig(tbox::Json &cfg) override;
34+
virtual void onFillDefaultConfig(tbox::Json &cfg) const override;
3535
virtual bool onInit(const tbox::Json &cfg) override;
3636
virtual bool onStart() override;
3737
virtual void onStop() override;

examples/main/03_nc_client_and_echo_server/echo_server/app.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ App::~App()
3939
CHECK_DELETE_RESET_OBJ(server_);
4040
}
4141

42-
void App::onFillDefaultConfig(Json &cfg)
42+
void App::onFillDefaultConfig(Json &cfg) const
4343
{
4444
cfg["bind"] = "127.0.0.1:12345";
4545
}

examples/main/03_nc_client_and_echo_server/echo_server/app.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class App : public main::Module
3333
App(main::Context &ctx);
3434
virtual ~App() override;
3535

36-
virtual void onFillDefaultConfig(Json &cfg) override;
36+
virtual void onFillDefaultConfig(Json &cfg) const override;
3737
virtual bool onInit(const tbox::Json &cfg) override;
3838
virtual bool onStart() override;
3939
virtual void onStop() override;

examples/main/03_nc_client_and_echo_server/nc_client/app.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ App::~App()
4141
CHECK_DELETE_RESET_OBJ(client_);
4242
}
4343

44-
void App::onFillDefaultConfig(Json &cfg)
44+
void App::onFillDefaultConfig(Json &cfg) const
4545
{
4646
cfg["server"] = "127.0.0.1:12345";
4747
}

examples/main/03_nc_client_and_echo_server/nc_client/app.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class App : public main::Module
3434
App(tbox::main::Context &ctx);
3535
virtual ~App() override;
3636

37-
virtual void onFillDefaultConfig(Json &cfg) override;
37+
virtual void onFillDefaultConfig(Json &cfg) const override;
3838
virtual bool onInit(const tbox::Json &cfg) override;
3939
virtual bool onStart() override;
4040
virtual void onStop() override;

examples/run/echo_server/echo_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ App::~App()
3939
CHECK_DELETE_RESET_OBJ(server_);
4040
}
4141

42-
void App::onFillDefaultConfig(Json &cfg)
42+
void App::onFillDefaultConfig(Json &cfg) const
4343
{
4444
cfg["bind"] = "127.0.0.1:12345";
4545
}

examples/run/echo_server/echo_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class App : public main::Module
3333
App(main::Context &ctx);
3434
virtual ~App() override;
3535

36-
virtual void onFillDefaultConfig(Json &cfg) override;
36+
virtual void onFillDefaultConfig(Json &cfg) const override;
3737
virtual bool onInit(const tbox::Json &cfg) override;
3838
virtual bool onStart() override;
3939
virtual void onStop() override;

examples/run/nc_client/nc_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ App::~App()
4141
CHECK_DELETE_RESET_OBJ(client_);
4242
}
4343

44-
void App::onFillDefaultConfig(Json &cfg)
44+
void App::onFillDefaultConfig(Json &cfg) const
4545
{
4646
cfg["server"] = "127.0.0.1:12345";
4747
}

examples/run/nc_client/nc_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class App : public main::Module
3434
App(tbox::main::Context &ctx);
3535
virtual ~App() override;
3636

37-
virtual void onFillDefaultConfig(Json &cfg) override;
37+
virtual void onFillDefaultConfig(Json &cfg) const override;
3838
virtual bool onInit(const tbox::Json &cfg) override;
3939
virtual bool onStart() override;
4040
virtual void onStop() override;

modules/base/log_impl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ void LogPrintfFunc(const char *module_id, const char *func_name, const char *fil
142142

143143
if (fmt != nullptr) {
144144
if (with_args) {
145-
uint32_t buff_size = std::min(2048lu, _LogTextMaxLength) + 1;
145+
constexpr size_t kMaxBuffSize = 2048lu;
146+
uint32_t buff_size = std::min(kMaxBuffSize, _LogTextMaxLength) + 1;
146147

147148
for (;;) {
148149
va_list args;

modules/eventx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_definitions(-DMODULE_ID="tbox.eventx")
3030
set(TBOX_LIBRARY_NAME tbox_eventx)
3131

3232
set(TBOX_EVENTX_HEADERS
33+
thread_executor.h
3334
thread_pool.h
3435
timer_pool.h
3536
timeout_monitor.hpp

modules/eventx/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ LIB_VERSION_Y = 0
2525
LIB_VERSION_Z = 2
2626

2727
HEAD_FILES = \
28+
thread_executor.h \
2829
thread_pool.h \
2930
timer_pool.h \
3031
timeout_monitor.hpp \

modules/eventx/thread_executor.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* .============.
3+
* // M A K E / \
4+
* // C++ DEV / \
5+
* // E A S Y / \/ \
6+
* ++ ----------. \/\ .
7+
* \\ \ \ /\ /
8+
* \\ \ \ /
9+
* \\ \ \ /
10+
* -============'
11+
*
12+
* Copyright (c) 2018 Hevake and contributors, all rights reserved.
13+
*
14+
* This file is part of cpp-tbox (https://github.com/cpp-main/cpp-tbox)
15+
* Use of this source code is governed by MIT license that can be found
16+
* in the LICENSE file in the root of the source tree. All contributing
17+
* project authors may be found in the CONTRIBUTORS.md file in the root
18+
* of the source tree.
19+
*/
20+
#ifndef TBOX_EVENTX_THREAD_EXECUTOR_H
21+
#define TBOX_EVENTX_THREAD_EXECUTOR_H
22+
23+
#include <functional>
24+
#include <tbox/base/cabinet_token.h>
25+
26+
namespace tbox {
27+
namespace eventx {
28+
29+
/**
30+
* 线程执行器接口
31+
*/
32+
class ThreadExecutor {
33+
public:
34+
using TaskToken = cabinet::Token;
35+
using NonReturnFunc = std::function<void ()>;
36+
37+
enum class TaskStatus {
38+
kWaiting, //!< 等待中
39+
kExecuting, //!< 执行中
40+
kNotFound, //!< 未找到(可能已完成)
41+
kCleanup, //!< 执行器与被清除
42+
};
43+
44+
enum class CancelResult {
45+
kSuccess, //!< 成功
46+
kExecuting, //!< 该任务正在执行
47+
kNotFound, //!< 没有找到该任务(可能已完成)
48+
kCleanup, //!< 执行器与被清除
49+
};
50+
51+
public:
52+
/**
53+
* 使用worker线程执行某个函数
54+
*
55+
* \param backend_task 让worker线程执行的函数对象
56+
*
57+
* \return TaskToken 任务Token
58+
*/
59+
virtual TaskToken execute(NonReturnFunc &&backend_task) = 0;
60+
virtual TaskToken execute(const NonReturnFunc &backend_task) = 0;
61+
62+
/**
63+
* 使用worker线程执行某个函数,并在完成之后在主线程执行指定的回调函数
64+
*
65+
* \param backend_task 让worker线程执行的函数对象
66+
* \param main_cb 任务完成后,由主线程执行的回调函数对象
67+
* \param prio 任务优先级[-2, -1, 0, 1, 2],越小优先级越高
68+
*
69+
* \return TaskToken 任务Token
70+
*/
71+
virtual TaskToken execute(NonReturnFunc &&backend_task, NonReturnFunc &&main_cb) = 0;
72+
virtual TaskToken execute(const NonReturnFunc &backend_task, const NonReturnFunc &main_cb) = 0;
73+
74+
75+
//! 获取任务的状态
76+
virtual TaskStatus getTaskStatus(TaskToken task_token) const = 0;
77+
78+
//! 取消任务
79+
virtual CancelResult cancel(TaskToken task_token) = 0;
80+
};
81+
82+
}
83+
}
84+
85+
#endif //TBOX_EVENTX_THREAD_EXECUTOR_H

modules/eventx/thread_pool.cpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,7 @@ bool ThreadPool::initialize(ssize_t min_thread_num, ssize_t max_thread_num)
126126
return true;
127127
}
128128

129-
ThreadPool::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, int prio)
130-
{
131-
return execute(std::move(backend_task), nullptr, prio);
132-
}
133-
134-
ThreadPool::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, int prio)
135-
{
136-
return execute(backend_task, nullptr, prio);
137-
}
138-
139-
ThreadPool::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, NonReturnFunc &&main_cb, int prio)
129+
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, NonReturnFunc &&main_cb, int prio)
140130
{
141131
RECORD_SCOPE();
142132
TaskToken token;
@@ -180,14 +170,44 @@ ThreadPool::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, NonRetur
180170
return token;
181171
}
182172

183-
ThreadPool::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, const NonReturnFunc &main_cb, int prio)
173+
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, const NonReturnFunc &main_cb, int prio)
184174
{
185175
NonReturnFunc backend_task_copy(backend_task);
186176
NonReturnFunc main_cb_copy(main_cb);
187177
return execute(std::move(backend_task_copy), std::move(main_cb_copy), prio);
188178
}
189179

190-
ThreadPool::TaskStatus ThreadPool::getTaskStatus(TaskToken task_token) const
180+
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, int prio)
181+
{
182+
return execute(std::move(backend_task), nullptr, prio);
183+
}
184+
185+
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, int prio)
186+
{
187+
return execute(backend_task, nullptr, prio);
188+
}
189+
190+
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task)
191+
{
192+
return execute(std::move(backend_task), 0);
193+
}
194+
195+
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task)
196+
{
197+
return execute(backend_task, 0);
198+
}
199+
200+
ThreadExecutor::TaskToken ThreadPool::execute(NonReturnFunc &&backend_task, NonReturnFunc &&main_cb)
201+
{
202+
return execute(std::move(backend_task), std::move(main_cb), 0);
203+
}
204+
205+
ThreadExecutor::TaskToken ThreadPool::execute(const NonReturnFunc &backend_task, const NonReturnFunc &main_cb)
206+
{
207+
return execute(backend_task, main_cb, 0);
208+
}
209+
210+
ThreadExecutor::TaskStatus ThreadPool::getTaskStatus(TaskToken task_token) const
191211
{
192212
std::lock_guard<std::mutex> lg(d_->lock);
193213

@@ -200,20 +220,14 @@ ThreadPool::TaskStatus ThreadPool::getTaskStatus(TaskToken task_token) const
200220
return TaskStatus::kNotFound;
201221
}
202222

203-
/**
204-
* 返回值如下:
205-
* 0: 取消成功
206-
* 1: 没有找到该任务
207-
* 2: 该任务正在执行
208-
*/
209-
int ThreadPool::cancel(TaskToken token)
223+
ThreadExecutor::CancelResult ThreadPool::cancel(TaskToken token)
210224
{
211225
RECORD_SCOPE();
212226
std::lock_guard<std::mutex> lg(d_->lock);
213227

214228
//! 如果正在执行
215229
if (d_->doing_tasks_token.find(token) != d_->doing_tasks_token.end())
216-
return 2; //! 返回正在执行
230+
return CancelResult::kExecuting; //! 返回正在执行
217231

218232
//! 从高优先级向低优先级遍历,找出优先级最高的任务
219233
for (size_t i = 0; i < d_->undo_tasks_token.size(); ++i) {
@@ -223,12 +237,12 @@ int ThreadPool::cancel(TaskToken token)
223237
if (iter != tasks_token.end()) {
224238
tasks_token.erase(iter);
225239
d_->task_pool.free(d_->undo_tasks_cabinet.free(token));
226-
return 0;
240+
return CancelResult::kSuccess;
227241
}
228242
}
229243
}
230244

231-
return 1; //! 返回没有找到
245+
return CancelResult::kNotFound; //! 返回没有找到
232246
}
233247

234248
void ThreadPool::cleanup()

0 commit comments

Comments
 (0)