diff --git a/framework/CMakeLists.txt b/framework/CMakeLists.txt index 127d59bc439c5bb1d5f320cffd8cab5472d526d5..65b7279de849b2d3da07e86aa0e8c0916915817e 100644 --- a/framework/CMakeLists.txt +++ b/framework/CMakeLists.txt @@ -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) diff --git a/framework/cybertron/cybertron.h b/framework/cybertron/cybertron.h index 2edb26555ede1b1bbbcc8e466b5000adc1eeff50..747ba28e30deeaa31b7105a6c16d6d1c3c39536d 100644 --- a/framework/cybertron/cybertron.h +++ b/framework/cybertron/cybertron.h @@ -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 CreateNode(const std::string& node_name, const std::string& name_space = ""); + +template +std::unique_ptr> CreateTask(const std::string& name, Function&& f, + const uint8_t num_threads = 1) { + if (!OK()) { + return nullptr; + } + std::unique_ptr> task( + new Task<>(name, std::forward(f), num_threads)); + return std::move(task); +} + +template +std::unique_ptr> CreateTask(const std::string& name, Function&& f, + const uint8_t& num_threads = 1) { + if (!OK()) { + return nullptr; + } + std::unique_ptr> task( + new Task(name, std::forward(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_ diff --git a/framework/examples/user_defined_task.cpp b/framework/examples/user_defined_task.cpp index 87f550b22e9cfeba2e4c331a27515f659df78b1f..ecfc317f279dfec6eafc064fe3f68725ccd2ef70 100644 --- a/framework/examples/user_defined_task.cpp +++ b/framework/examples/user_defined_task.cpp @@ -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& 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 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( + "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(); 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;