From fd46ca62ae867a9bf91fecd8043974f043cf966c Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Thu, 28 Mar 2024 00:14:38 +0800 Subject: [PATCH] feat update --- src/async/async.cc | 3 ++- src/async/async_test.cc | 20 ++++++++++++++++++++ src/log/log.cc | 37 ++++++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/async/async.cc b/src/async/async.cc index 66f347e..90128c3 100644 --- a/src/async/async.cc +++ b/src/async/async.cc @@ -30,7 +30,8 @@ FiberScheduler::schedule(async::task_run_handle t) { static ThreadPool thread_pool; auto move_on_copy = sled::MakeMoveOnCopy(t); - thread_pool.PostTask([move_on_copy] { move_on_copy.value.run_with_wait_handler(SleepWaitHandler); }); + // thread_pool.PostTask([move_on_copy] { move_on_copy.value.run_with_wait_handler(SleepWaitHandler); }); + thread_pool.submit([move_on_copy] { move_on_copy.value.run_with_wait_handler(SleepWaitHandler); }); } }// namespace sled diff --git a/src/async/async_test.cc b/src/async/async_test.cc index 409a322..33d3ae7 100644 --- a/src/async/async_test.cc +++ b/src/async/async_test.cc @@ -1,10 +1,30 @@ #include #include +#include +#include +#include TEST(Async, task) { auto task1 = async::spawn([] { return 42; }).then([](int value) { return value * 3; }).then([](int value) { EXPECT_EQ(value, 126); + return value; }); task1.wait(); + EXPECT_EQ(126, task1.get()); +} + +TEST(Async, parallel_for) +{ + const int count = 1000; + std::vector> values(count); + // std::vector values(count); + // sled::WaitGroup wg(count); + async::parallel_for(async::irange(0, count), [&values](int x) { + EXPECT_FALSE(values[x]); + values[x] = true; + // wg.Done(); + }); + // wg.Wait(); + for (int i = 0; i < count; i++) { EXPECT_TRUE(values[i]) << i; } } diff --git a/src/log/log.cc b/src/log/log.cc index 3f80e0e..60311eb 100644 --- a/src/log/log.cc +++ b/src/log/log.cc @@ -1,4 +1,5 @@ #include "sled/log/log.h" +#include "sled/synchronization/mutex.h" #include "sled/time_utils.h" #include #include @@ -60,7 +61,7 @@ class ScopedAtomicWaiter { public: ScopedAtomicWaiter(std::atomic_bool &flag) : flag_(flag) { - bool old = flag_.load(); + bool old = true; while (!flag_.compare_exchange_weak(old, false)) { continue; } } @@ -79,7 +80,7 @@ GetCurrentUTCTime() // int64_t now = tp.tv_sec * kNumNanosecsPerSec + tp.tv_nsec; // std::time_t t = tp.tv_sec; const int64_t now = TimeUTCNanos(); - std::time_t t = now / kNumNanosecsPerSec; + std::time_t t = now / kNumNanosecsPerSec; #else std::time_t t = std::time(nullptr); #endif @@ -103,18 +104,40 @@ SetLogLevel(LogLevel level) g_log_level = level; } +static std::atomic g_current_id(0); +static std::atomic g_request_id(0); + +struct Waiter { + Waiter(uint32_t id, std::atomic ¤t_id) : id_(id), current_id_(current_id) {} + + ~Waiter() { current_id_.fetch_add(1); } + + uint32_t id() { return id_; } + + void wait() + { + while (id_ > current_id_.load()) {} + } + +private: + uint32_t id_; + std::atomic ¤t_id_; +}; + void Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int line, const char *func_name, ...) { if (level < g_log_level) { return; } - static std::atomic_bool allow(true); - ScopedAtomicWaiter waiter(allow); - int len = file_name ? strlen(file_name) : 0; + auto utc_time = GetCurrentUTCTime(); + auto level_str = ToShortString(level); + int len = file_name ? strlen(file_name) : 0; while (len > 0 && file_name[len - 1] != '/') { len--; } - auto msg = fmt::format("{} {} {} {}:{}@{}: {}", GetCurrentUTCTime(), ToShortString(level), tag, file_name + len, - line, func_name, fmt); + auto msg = fmt::format("{} {} {} {}:{}@{}: {}", utc_time, level_str, tag, file_name + len, line, func_name, fmt); + + Waiter waiter(g_request_id.fetch_add(1), g_current_id); + waiter.wait(); std::cout << GetConsoleColorPrefix(level) << msg << GetConsoleColorSuffix() << std::endl; }