@@ -843,7 +843,7 @@ manapi::future<> manapi::event_loop::stop() {
843843 manapi_log_trace (manapi::debug::LOG_TRACE_MEDIUM, " eventloop:stop() has been finished" );
844844}
845845
846- void manapi::event_loop::wait_all_ () MANAPIHTTP_NOEXCEPT {
846+ void manapi::event_loop::wait_all_ (bool shutdown ) MANAPIHTTP_NOEXCEPT {
847847 if (!(this ->flags & EVENT_LOOP_FLAG_STOPPING)) {
848848 manapi::async::run (this ->stop ());
849849 }
@@ -855,23 +855,22 @@ void manapi::event_loop::wait_all_() MANAPIHTTP_NOEXCEPT {
855855#if MANAPIHTTP_CURL_DEPENDENCY
856856 this ->stop_watcher (std::move (this ->curl_watcher ->timeout_watcher ));
857857#endif
858- std::shared_ptr<ev::idle> idle_tasks;
859-
858+ manapi::timer checker;
860859 MANAPIHTTP_MUST_ALLOC_START
861- manapi::async::current ()->timerpool ()->append_interval_sync (500 , [this ] (const manapi::timer &) -> void {
860+ checker = manapi::async::current ()->timerpool ()->append_interval_sync (500 , [this ] (const manapi::timer &) -> void {
862861 this ->handle_curl_exec_connections ();
863862 this ->handle_curl_check_connections ();
864- });
863+ }). unwrap () ;
865864 MANAPIHTTP_MUST_ALLOC_END
866865 if (this ->loop_ ) {
867- while (::uv_loop_alive (this ->loop ()) || this ->etaskpool_ ->tasks_size ()) {
868- auto etaskpool = dynamic_cast <ethreadpool *>(this ->etaskpool_ .get ());
866+ auto etaskpool = dynamic_cast <ethreadpool *>(this ->etaskpool_ .get ());
869867
868+ while (true ) {
870869 MANAPIHTTP_MUST_ALLOC_START
871- etaskpool->set_notify_cb ([& ] () -> void {
870+ etaskpool->set_notify_cb ([etaskpool, this ] () -> void {
872871 MANAPIHTTP_MUST_ALLOC_START
873872 auto idle_tasks_res = this ->create_watcher_idle (
874- [&idle_tasks, etaskpool, this ] (const ev::shared_idle &w) -> void {
873+ [etaskpool, this ] (const ev::shared_idle &w) -> void {
875874 while (true ) {
876875 if (!etaskpool->try_task ()) {
877876 break ;
@@ -880,7 +879,7 @@ void manapi::event_loop::wait_all_() MANAPIHTTP_NOEXCEPT {
880879 etaskpool->set_notify ();
881880
882881 auto const loop_ = this ->loop ();
883- this ->stop_watcher (std::move (idle_tasks) );
882+ this ->stop_watcher (w );
884883
885884 // try again (idle_tasks can be the last one)
886885 manapi::async::current ()->timerpool ()->stop ();
@@ -891,11 +890,17 @@ void manapi::event_loop::wait_all_() MANAPIHTTP_NOEXCEPT {
891890 });
892891
893892 if (!idle_tasks_res) {
893+ if (!this ->loop_ ) {
894+ manapi_log_warn (" New tasks were added when event loop is deactivated" );
895+ manapi::print_stacktrace ();
896+ return ;
897+ }
898+
894899 idle_tasks_res.err ().log ();
895900 throw std::bad_alloc{};
896901 }
897902
898- idle_tasks = idle_tasks_res.unwrap ();
903+ auto idle_tasks = idle_tasks_res.unwrap ();
899904 if (auto rhs = idle_tasks->start ()) {
900905 manapi_log_error (" %s:%s failed due to %s" , " event loop" , " idle taskpool start" ,
901906 ev::strerror (rhs));
@@ -925,31 +930,36 @@ void manapi::event_loop::wait_all_() MANAPIHTTP_NOEXCEPT {
925930 }
926931 }
927932
928- manapi_log_trace (manapi::debug::LOG_TRACE_MEDIUM, " eventloop:well done" );
933+ checker.stop ();
934+ etaskpool->set_notify_cb (nullptr );
935+
936+ if (shutdown) {
937+ manapi_log_trace (manapi::debug::LOG_TRACE_MEDIUM, " eventloop:well done" );
929938
930- if (auto rhs = ::uv_loop_close (this ->loop_ .get ())) {
939+ if (auto rhs = ::uv_loop_close (this ->loop_ .get ())) {
931940#ifndef MANAPIHTTP_DISABLE_TRACE_HARD
932- ::uv_print_all_handles (this ->loop (), stderr);
941+ ::uv_print_all_handles (this ->loop (), stderr);
933942#endif
934- manapi_log_error (" %s failed due to %s" , " uv_loop_close" , ev::strerror(rhs));
935- }
936- else {
937- this ->loop_ .reset ();
938- }
943+ manapi_log_error (" %s failed due to %s" , " uv_loop_close" , ev::strerror(rhs));
944+ }
945+ else {
946+ this ->loop_ .reset ();
947+ }
939948
940- if (this ->flags & EVENT_LOOP_FLAG_STOPPING) {
941- this ->flags ^= EVENT_LOOP_FLAG_STOPPING;
942- }
943- if (this ->flags & EVENT_LOOP_FLAG_ACTIVE) {
944- this ->flags ^= EVENT_LOOP_FLAG_ACTIVE;
949+ if (this ->flags & EVENT_LOOP_FLAG_STOPPING) {
950+ this ->flags ^= EVENT_LOOP_FLAG_STOPPING;
951+ }
952+ if (this ->flags & EVENT_LOOP_FLAG_ACTIVE) {
953+ this ->flags ^= EVENT_LOOP_FLAG_ACTIVE;
954+ }
945955 }
946956 }
947957}
948958
949- size_t manapi::event_loop::subscribe_finish (std::move_only_function<manapi::future<void >()> cb) {
959+ size_t manapi::event_loop::subscribe_finish (int priority, std::move_only_function<manapi::future<void >()> cb) {
950960 auto id = *reinterpret_cast <const std::size_t *> (&cb);
951961
952- if (!this ->map_finish_cb .insert ({id, std::move (cb)}).second ) {
962+ if (!this ->map_finish_cb .insert ({id, std::make_pair (priority, std:: move (cb) )}).second ) {
953963 THROW_MANAPIHTTP_EXCEPTION (ERR_INTERNAL, " index {} exists" , id);
954964 }
955965
@@ -1080,14 +1090,27 @@ manapi::ev::status_or<std::shared_ptr<manapi::ev::udp>> manapi::event_loop::crea
10801090}
10811091
10821092manapi::future<> manapi::event_loop::_call_on_finish_cb () {
1093+ using item_t = std::pair<int , std::move_only_function<manapi::future<void >()>>;
1094+ std::vector<item_t > values;
1095+ values.reserve (this ->map_finish_cb .size ());
10831096 while (!this ->map_finish_cb .empty ()) {
1084- const auto it = this ->map_finish_cb .begin ();
1085- std::size_t address = it->first ;
1086- if (it->second ) {
1087- auto callback = std::move (it->second );
1088- co_await async::invoke (std::move (callback));
1097+ const auto it = this ->map_finish_cb .extract (this ->map_finish_cb .begin ());
1098+ values.push_back (std::move (it.mapped ()));
1099+ }
1100+
1101+ std::sort (values.begin (), values.end (), +[](const item_t & a, const item_t & b)
1102+ -> bool { return a.first > b.first ; });
1103+
1104+ for (auto &it : values) {
1105+ if (it.second ) {
1106+ try {
1107+ auto callback = std::move (it.second );
1108+ co_await async::invoke (std::move (callback));
1109+ }
1110+ catch (std::exception const &e) {
1111+ manapi_log_ferror (" finish callback failed msg=%s" , e.what ());
1112+ }
10891113 }
1090- this ->map_finish_cb .erase (address);
10911114 }
10921115}
10931116
0 commit comments