提交 36cae1fe 编写于 作者: fengqikai1414's avatar fengqikai1414 提交者: Liangliang Zhang

cybertron: update custom task api

上级 e601aa23
......@@ -97,5 +97,11 @@ install(DIRECTORY "cybertron"
PATTERN "*.h"
)
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/cybertron"
DESTINATION "include"
FILES_MATCHING
PATTERN "*.pb.h"
)
enable_testing()
#add_subdirectory(cybertron/test)
......@@ -27,19 +27,52 @@
#include "cybertron/time/time.h"
#include "cybertron/timer/timer.h"
#define LOG_DEBUG ADEBUG
#define LOG_INFO AINFO
#define LOG_WARN AWARN
#define LOG_ERROR AERROR
#define XLOG_ERROR(...) printf(__VA_ARGS__);
namespace apollo {
namespace cybertron {
std::unique_ptr<Node> CreateNode(const std::string& node_name,
const std::string& name_space = "");
template <typename Function>
std::unique_ptr<Task<>> CreateTask(const std::string& name, Function&& f,
const uint8_t num_threads = 1) {
if (!OK()) {
return nullptr;
}
std::unique_ptr<Task<>> task(
new Task<>(name, std::forward<Function>(f), num_threads));
return std::move(task);
}
template <typename T, typename Function>
std::unique_ptr<Task<T>> CreateTask(const std::string& name, Function&& f,
const uint8_t& num_threads = 1) {
if (!OK()) {
return nullptr;
}
std::unique_ptr<Task<T>> task(
new Task<T>(name, std::forward<Function>(f), num_threads));
return std::move(task);
}
inline static void Yield() {
if (croutine::CRoutine::GetCurrentRoutine()) {
croutine::CRoutine::Yield();
} else {
std::this_thread::yield();
}
}
inline static void USleep(useconds_t usec) {
auto routine = croutine::CRoutine::GetCurrentRoutine();
if (routine == nullptr) {
std::this_thread::sleep_for(std::chrono::microseconds{usec});
} else {
routine->Sleep(usec);
}
}
} // namespace cybertron
} // namespace apollo
#endif // CYBERTRON_CYBERTRON_H_
......@@ -22,8 +22,8 @@
static const uint8_t num_threads = 3;
using apollo::cybertron::proto::Driver;
using apollo::cybertron::Task;
using apollo::cybertron::proto::Driver;
struct Message {
uint64_t msg_id;
......@@ -34,21 +34,24 @@ struct Message {
void AsyncDataProcessor() {
for (;;) {
AERROR << "AsyncDataProcesor is running.";
usleep(5000000);
apollo::cybertron::USleep(5000000);
}
}
void TaskProcessor(const std::shared_ptr<Message>& msg) {
AERROR << "Task Processor[" << msg->task_id
<< "] is running: " << msg->msg_id;
usleep(100000);
apollo::cybertron::USleep(100000);
}
int main(int argc, char* argv[]) {
// Init
apollo::cybertron::Init(argv[0]);
Task<> task0("async_data_processor", &AsyncDataProcessor);
Task<Message> task1("task_processor", &TaskProcessor, num_threads);
// Task<> task0("async_data_processor", &AsyncDataProcessor);
auto task0 = apollo::cybertron::CreateTask("async_data_processor",
&AsyncDataProcessor);
auto task1 = apollo::cybertron::CreateTask<Message>(
"task_processor", &TaskProcessor, num_threads);
// Run
uint64_t i = 0;
......@@ -57,9 +60,11 @@ int main(int argc, char* argv[]) {
auto msg = std::make_shared<Message>();
msg->msg_id = i++;
msg->task_id = j;
task1.Execute(msg);
task1->Execute(msg);
apollo::cybertron::Yield();
apollo::cybertron::USleep(10000);
}
task1.Wait();
task1->Wait();
}
AERROR << "All task are finished.";
return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册