From 36e9f9f5b55002c10b99032ba3fbfbbab9debf9d Mon Sep 17 00:00:00 2001 From: marty1885 Date: Sat, 10 Dec 2022 11:49:32 +0800 Subject: [PATCH] fix timer causing not able to ctrl-c exit without all timer ending --- .gitignore | 7 +++++++ examples/CMakeLists.txt | 5 ++++- examples/scheduler/main.cpp | 15 +++++++++++++++ gnunetpp/gnunetpp-scheduler.cpp | 11 ++++++++++- gnunetpp/gnunetpp-scheduler.hpp | 1 + gnunetpp/inner/UniqueData.hpp | 30 ++++++++++++++++++++++++++++-- 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 examples/scheduler/main.cpp diff --git a/.gitignore b/.gitignore index 9151a33..b411615 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ build/ .vscode .cache +CMakeCache.txt +CMakeFiles +cmake_install.cmake +Makefile +install_manifest.txt +CMakeLists.txt.user +ninjia.build \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 16ba0fb..e687fee 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,4 +5,7 @@ add_executable(gnunetpp-fs fs/main.cpp) target_link_libraries(gnunetpp-fs gnunetfs gnunetcore gnunetidentity gnunetutil gnunetpp) add_executable(gnunetpp-identity identity/main.cpp) -target_link_libraries(gnunetpp-identity gnunetidentity gnunetcore gnunetutil gnunetpp) \ No newline at end of file +target_link_libraries(gnunetpp-identity gnunetidentity gnunetcore gnunetutil gnunetpp) + +add_executable(gnunetpp-scheduler scheduler/main.cpp) +target_link_libraries(gnunetpp-scheduler gnunetcore gnunetutil gnunetpp) \ No newline at end of file diff --git a/examples/scheduler/main.cpp b/examples/scheduler/main.cpp new file mode 100644 index 0000000..5b8374d --- /dev/null +++ b/examples/scheduler/main.cpp @@ -0,0 +1,15 @@ +#include +#include + +void service(const GNUNET_CONFIGURATION_Handle* cfg) +{ + std::cout << "Hello world will be printed in 5 seconds" << std::endl; + gnunetpp::scheduler::runLater(std::chrono::seconds(5), []() { + std::cout << "Hello, world!" << std::endl; + }); +} + +int main() +{ + gnunetpp::run(service); +} \ No newline at end of file diff --git a/gnunetpp/gnunetpp-scheduler.cpp b/gnunetpp/gnunetpp-scheduler.cpp index 3afb3d4..d005569 100644 --- a/gnunetpp/gnunetpp-scheduler.cpp +++ b/gnunetpp/gnunetpp-scheduler.cpp @@ -84,11 +84,20 @@ void cancel(TaskID id) g_tasks.remove(id); } +void cancelAll() +{ + for(auto& [id, data] : g_tasks) + GNUNET_SCHEDULER_cancel(data.handle); + g_tasks.clear(); +} + static bool running = true; void shutdown() { - if(running) + if(running) { + cancelAll(); GNUNET_SCHEDULER_shutdown(); + } running = false; } diff --git a/gnunetpp/gnunetpp-scheduler.hpp b/gnunetpp/gnunetpp-scheduler.hpp index 7a2a02e..0a27003 100644 --- a/gnunetpp/gnunetpp-scheduler.hpp +++ b/gnunetpp/gnunetpp-scheduler.hpp @@ -15,5 +15,6 @@ TaskID runEvery(std::chrono::duration delay, std::function fn); void runOnShutdown(std::function fn); void run(std::function fn); void cancel(TaskID id); +void cancelAll(); void shutdown(); } \ No newline at end of file diff --git a/gnunetpp/inner/UniqueData.hpp b/gnunetpp/inner/UniqueData.hpp index 5d6f9db..bb5ffdf 100644 --- a/gnunetpp/inner/UniqueData.hpp +++ b/gnunetpp/inner/UniqueData.hpp @@ -44,8 +44,34 @@ struct UniqueData bool contains(size_t id) { - std::lock_guard l(mtx); - return data.find(id) != data.end(); + std::lock_guard l(mtx); + return data.find(id) != data.end(); + } + + auto begin() + { + return data.begin(); + } + + auto end() + { + return data.end(); + } + + auto begin() const + { + return data.begin(); + } + + auto end() const + { + return data.end(); + } + + void clear() + { + std::lock_guard l(mtx); + data.clear(); } protected: std::mutex mtx;