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

cybertron: update custom task api

上级 e601aa23
...@@ -97,5 +97,11 @@ install(DIRECTORY "cybertron" ...@@ -97,5 +97,11 @@ install(DIRECTORY "cybertron"
PATTERN "*.h" PATTERN "*.h"
) )
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/cybertron"
DESTINATION "include"
FILES_MATCHING
PATTERN "*.pb.h"
)
enable_testing() enable_testing()
#add_subdirectory(cybertron/test) #add_subdirectory(cybertron/test)
...@@ -27,19 +27,52 @@ ...@@ -27,19 +27,52 @@
#include "cybertron/time/time.h" #include "cybertron/time/time.h"
#include "cybertron/timer/timer.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 apollo {
namespace cybertron { namespace cybertron {
std::unique_ptr<Node> CreateNode(const std::string& node_name, std::unique_ptr<Node> CreateNode(const std::string& node_name,
const std::string& name_space = ""); 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 } // namespace apollo
#endif // CYBERTRON_CYBERTRON_H_ #endif // CYBERTRON_CYBERTRON_H_
...@@ -22,8 +22,8 @@ ...@@ -22,8 +22,8 @@
static const uint8_t num_threads = 3; static const uint8_t num_threads = 3;
using apollo::cybertron::proto::Driver;
using apollo::cybertron::Task; using apollo::cybertron::Task;
using apollo::cybertron::proto::Driver;
struct Message { struct Message {
uint64_t msg_id; uint64_t msg_id;
...@@ -34,21 +34,24 @@ struct Message { ...@@ -34,21 +34,24 @@ struct Message {
void AsyncDataProcessor() { void AsyncDataProcessor() {
for (;;) { for (;;) {
AERROR << "AsyncDataProcesor is running."; AERROR << "AsyncDataProcesor is running.";
usleep(5000000); apollo::cybertron::USleep(5000000);
} }
} }
void TaskProcessor(const std::shared_ptr<Message>& msg) { void TaskProcessor(const std::shared_ptr<Message>& msg) {
AERROR << "Task Processor[" << msg->task_id AERROR << "Task Processor[" << msg->task_id
<< "] is running: " << msg->msg_id; << "] is running: " << msg->msg_id;
usleep(100000); apollo::cybertron::USleep(100000);
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Init // Init
apollo::cybertron::Init(argv[0]); apollo::cybertron::Init(argv[0]);
Task<> task0("async_data_processor", &AsyncDataProcessor); // Task<> task0("async_data_processor", &AsyncDataProcessor);
Task<Message> task1("task_processor", &TaskProcessor, num_threads); auto task0 = apollo::cybertron::CreateTask("async_data_processor",
&AsyncDataProcessor);
auto task1 = apollo::cybertron::CreateTask<Message>(
"task_processor", &TaskProcessor, num_threads);
// Run // Run
uint64_t i = 0; uint64_t i = 0;
...@@ -57,9 +60,11 @@ int main(int argc, char* argv[]) { ...@@ -57,9 +60,11 @@ int main(int argc, char* argv[]) {
auto msg = std::make_shared<Message>(); auto msg = std::make_shared<Message>();
msg->msg_id = i++; msg->msg_id = i++;
msg->task_id = j; 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."; AERROR << "All task are finished.";
return 0; return 0;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册