Commit fd46ca62 authored by tqcq's avatar tqcq
Browse files

feat update

parent ec6f45eb
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -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
+20 −0
Original line number Diff line number Diff line
#include <gtest/gtest.h>
#include <sled/async/async.h>
#include <sled/log/log.h>
#include <sled/system/fiber/wait_group.h>
#include <sled/system/thread.h>

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<std::atomic<bool>> values(count);
    // std::vector<bool> 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; }
}
+30 −7
Original line number Diff line number Diff line
#include "sled/log/log.h"
#include "sled/synchronization/mutex.h"
#include "sled/time_utils.h"
#include <atomic>
#include <ctime>
@@ -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; }
    }

@@ -103,18 +104,40 @@ SetLogLevel(LogLevel level)
    g_log_level = level;
}

static std::atomic<uint32_t> g_current_id(0);
static std::atomic<uint32_t> g_request_id(0);

struct Waiter {
    Waiter(uint32_t id, std::atomic<uint32_t> &current_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<uint32_t> &current_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);
    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;
}