feat update
All checks were successful
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m31s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 1m0s

This commit is contained in:
tqcq 2024-03-28 00:14:38 +08:00
parent ec6f45eb78
commit fd46ca62ae
3 changed files with 52 additions and 8 deletions

View File

@ -30,7 +30,8 @@ FiberScheduler::schedule(async::task_run_handle t)
{ {
static ThreadPool thread_pool; static ThreadPool thread_pool;
auto move_on_copy = sled::MakeMoveOnCopy(t); 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 }// namespace sled

View File

@ -1,10 +1,30 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <sled/async/async.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) TEST(Async, task)
{ {
auto task1 = async::spawn([] { return 42; }).then([](int value) { return value * 3; }).then([](int value) { auto task1 = async::spawn([] { return 42; }).then([](int value) { return value * 3; }).then([](int value) {
EXPECT_EQ(value, 126); EXPECT_EQ(value, 126);
return value;
}); });
task1.wait(); 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; }
} }

View File

@ -1,4 +1,5 @@
#include "sled/log/log.h" #include "sled/log/log.h"
#include "sled/synchronization/mutex.h"
#include "sled/time_utils.h" #include "sled/time_utils.h"
#include <atomic> #include <atomic>
#include <ctime> #include <ctime>
@ -60,7 +61,7 @@ class ScopedAtomicWaiter {
public: public:
ScopedAtomicWaiter(std::atomic_bool &flag) : flag_(flag) ScopedAtomicWaiter(std::atomic_bool &flag) : flag_(flag)
{ {
bool old = flag_.load(); bool old = true;
while (!flag_.compare_exchange_weak(old, false)) { continue; } while (!flag_.compare_exchange_weak(old, false)) { continue; }
} }
@ -79,7 +80,7 @@ GetCurrentUTCTime()
// int64_t now = tp.tv_sec * kNumNanosecsPerSec + tp.tv_nsec; // int64_t now = tp.tv_sec * kNumNanosecsPerSec + tp.tv_nsec;
// std::time_t t = tp.tv_sec; // std::time_t t = tp.tv_sec;
const int64_t now = TimeUTCNanos(); const int64_t now = TimeUTCNanos();
std::time_t t = now / kNumNanosecsPerSec; std::time_t t = now / kNumNanosecsPerSec;
#else #else
std::time_t t = std::time(nullptr); std::time_t t = std::time(nullptr);
#endif #endif
@ -103,18 +104,40 @@ SetLogLevel(LogLevel level)
g_log_level = 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 void
Log(LogLevel level, const char *tag, const char *fmt, const char *file_name, int line, const char *func_name, ...) 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; } if (level < g_log_level) { return; }
static std::atomic_bool allow(true); auto utc_time = GetCurrentUTCTime();
ScopedAtomicWaiter waiter(allow); auto level_str = ToShortString(level);
int len = file_name ? strlen(file_name) : 0; int len = file_name ? strlen(file_name) : 0;
while (len > 0 && file_name[len - 1] != '/') { len--; } while (len > 0 && file_name[len - 1] != '/') { len--; }
auto msg = fmt::format("{} {} {} {}:{}@{}: {}", GetCurrentUTCTime(), ToShortString(level), tag, file_name + len, auto msg = fmt::format("{} {} {} {}:{}@{}: {}", utc_time, level_str, tag, file_name + len, line, func_name, fmt);
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; std::cout << GetConsoleColorPrefix(level) << msg << GetConsoleColorSuffix() << std::endl;
} }