diff --git a/include/dwarfs/progress.h b/include/dwarfs/progress.h index 414ef97c..409c74b8 100644 --- a/include/dwarfs/progress.h +++ b/include/dwarfs/progress.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -33,8 +34,6 @@ #include #include -#include - #include #include @@ -63,10 +62,15 @@ class progress { speedometer speed{std::chrono::seconds(5)}; }; - using status_function_type = - folly::Function; + using update_function_type = std::function; + + using status_function_type = + std::function; + + progress(); + explicit progress(update_function_type func); + progress(update_function_type func, std::chrono::microseconds interval); - progress(folly::Function&& func, unsigned interval_ms); ~progress() noexcept; void set_status_function(status_function_type status_fun); @@ -149,7 +153,7 @@ class progress { void add_context(std::shared_ptr const& ctx) const; mutable std::mutex running_mx_; - bool running_; + bool running_{false}; mutable std::mutex mx_; std::condition_variable cond_; std::shared_ptr status_fun_; diff --git a/src/dwarfs/progress.cpp b/src/dwarfs/progress.cpp index 1ccb7fb1..97d75105 100644 --- a/src/dwarfs/progress.cpp +++ b/src/dwarfs/progress.cpp @@ -30,10 +30,15 @@ namespace dwarfs { -progress::progress(folly::Function&& func, - unsigned interval_ms) +progress::progress() {} + +progress::progress(update_function_type func) + : progress(std::move(func), std::chrono::seconds(1)) {} + +progress::progress(update_function_type func, + std::chrono::microseconds interval) : running_(true) - , thread_([this, interval_ms, func = std::move(func)]() mutable { + , thread_([this, interval, func = std::move(func)]() mutable { folly::setThreadName("progress"); #ifdef _WIN32 ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_HIGHEST); @@ -41,21 +46,23 @@ progress::progress(folly::Function&& func, std::unique_lock lock(running_mx_); while (running_) { func(*this, false); - cond_.wait_for(lock, std::chrono::milliseconds(interval_ms)); + cond_.wait_for(lock, interval); } func(*this, true); }) { } progress::~progress() noexcept { - try { - { - std::lock_guard lock(running_mx_); - running_ = false; + if (running_) { + try { + { + std::lock_guard lock(running_mx_); + running_ = false; + } + cond_.notify_all(); + thread_.join(); + } catch (...) { } - cond_.notify_all(); - thread_.join(); - } catch (...) { } } diff --git a/src/mkdwarfs_main.cpp b/src/mkdwarfs_main.cpp index c0c605f6..2636f512 100644 --- a/src/mkdwarfs_main.cpp +++ b/src/mkdwarfs_main.cpp @@ -369,6 +369,7 @@ void validate(boost::any& v, std::vector const& values, int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { using namespace folly::gen; + using namespace std::chrono_literals; const size_t num_cpu = std::max(folly::hardware_concurrency(), 1u); static constexpr size_t const kDefaultMaxActiveBlocks{1}; @@ -1062,10 +1063,10 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { } } - unsigned interval_ms = + auto interval = pg_mode == console_writer::NONE || pg_mode == console_writer::SIMPLE - ? 2000 - : 200; + ? 2000ms + : 200ms; filesystem_writer_options fswopts; fswopts.max_queue_size = mem_limit; @@ -1088,7 +1089,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { LOG_PROXY(debug_logger_policy, lgr); - folly::Function updater; + progress::update_function_type updater; if (options.debug_filter_function) { updater = [](progress&, bool) {}; @@ -1096,7 +1097,7 @@ int mkdwarfs_main(int argc, sys_char** argv, iolayer const& iol) { updater = [&](progress& p, bool last) { lgr.update(p, last); }; } - progress prog(std::move(updater), interval_ms); + progress prog(std::move(updater), interval); // No more streaming to iol.err after this point as this would // cause a race with the progress thread. diff --git a/test/compat_test.cpp b/test/compat_test.cpp index 3b2f6a7a..828835b9 100644 --- a/test/compat_test.cpp +++ b/test/compat_test.cpp @@ -1115,7 +1115,7 @@ TEST_P(rewrite, filesystem_rewrite) { worker_group wg(lgr, os, "rewriter", 2); block_compressor bc("null"); - progress prog([](const progress&, bool) {}, 1000); + progress prog; std::ostringstream rewritten, idss; auto rewrite_fs = [&](auto& fsw, auto const& mm) { diff --git a/test/dwarfs_benchmark.cpp b/test/dwarfs_benchmark.cpp index 8f13dc5a..99ba751a 100644 --- a/test/dwarfs_benchmark.cpp +++ b/test/dwarfs_benchmark.cpp @@ -120,7 +120,7 @@ std::string make_filesystem(::benchmark::State const& state) { auto os = test::os_access_mock::create_test_instance(); worker_group wg(lgr, *os, "writer", 4); - progress prog([](const progress&, bool) {}, 1000); + progress prog; auto sf = std::make_shared(lgr, prog, cfg); diff --git a/test/dwarfs_test.cpp b/test/dwarfs_test.cpp index 5a3c404c..71f14dbd 100644 --- a/test/dwarfs_test.cpp +++ b/test/dwarfs_test.cpp @@ -78,7 +78,7 @@ build_dwarfs(logger& lgr, std::shared_ptr input, std::unique_ptr local_prog; if (!prog) { - local_prog = std::make_unique([](const progress&, bool) {}, 1000); + local_prog = std::make_unique(); prog = local_prog.get(); } @@ -161,7 +161,7 @@ void basic_end_to_end_test(std::string const& compressor, input->set_access_fail("/somedir/ipsum.py"); } - auto prog = progress([](const progress&, bool) {}, 1000); + progress prog; auto scr = std::make_shared(); @@ -928,7 +928,7 @@ class filter_test debug_filter_output(oss, exclude, pe, mode); }; - progress prog([](const progress&, bool) {}, 1000); + progress prog; worker_group wg(lgr, *input, "worker", 1); auto sf = std::make_shared(lgr, prog, segmenter_factory::config{}); diff --git a/test/segmenter_benchmark.cpp b/test/segmenter_benchmark.cpp index e26b3827..0278a98a 100644 --- a/test/segmenter_benchmark.cpp +++ b/test/segmenter_benchmark.cpp @@ -139,7 +139,7 @@ void run_segmenter_test(unsigned iters, unsigned granularity, for (unsigned i = 0; i < iters; ++i) { dwarfs::test::test_logger lgr; - dwarfs::progress prog([](dwarfs::progress const&, bool) {}, 1000); + dwarfs::progress prog; auto blkmgr = std::make_shared(); std::vector> written;