feat/support_fiber #2
@ -1,37 +0,0 @@
|
||||
#include "tile/fiber/detail/fiber.h"
|
||||
|
||||
#include "tile/base/random.h"
|
||||
#include "tile/base/string.h"
|
||||
|
||||
#include "benchmark/benchmark.h"
|
||||
|
||||
namespace tile {
|
||||
namespace fiber {
|
||||
namespace detail {
|
||||
void Benchmark_FiberSwitch(benchmark::State &state) {
|
||||
std::unique_ptr<Fiber> master_fiber;
|
||||
|
||||
int switch_cnt = 0;
|
||||
master_fiber = Fiber::Create([&] {
|
||||
while (state.KeepRunning()) {
|
||||
std::unique_ptr<Fiber> worker_fiber1 =
|
||||
Fiber::Create([&] { ++switch_cnt; });
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
worker_fiber1->Resume();
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::duration<double>>(
|
||||
end - start);
|
||||
state.SetIterationTime(elapsed.count());
|
||||
}
|
||||
});
|
||||
|
||||
master_fiber->Resume();
|
||||
state.counters["switch_cnt"] = switch_cnt;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace fiber
|
||||
} // namespace tile
|
||||
|
||||
BENCHMARK(tile::fiber::detail::Benchmark_FiberSwitch)->UseManualTime();
|
@ -1,52 +0,0 @@
|
||||
#include "tile/base/random.h"
|
||||
#include "tile/fiber/detail/fiber.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
namespace tile {
|
||||
namespace fiber {
|
||||
namespace detail {
|
||||
|
||||
TEST(Fiber, Base) {
|
||||
constexpr auto kMaxCnt = 1; // 100 * 1000;
|
||||
int cnt = 0;
|
||||
int resume_cnt = 0;
|
||||
|
||||
// 0 -> master fiber
|
||||
// [1, 9] -> worker fibers
|
||||
std::unique_ptr<Fiber> master_fiber;
|
||||
|
||||
master_fiber = Fiber::Create([&] {
|
||||
TILE_LOG_INFO("master fiber");
|
||||
// ASSERT_EQ(cnt, 0);
|
||||
ASSERT_EQ(GetMasterFiber(), master_fiber.get());
|
||||
ASSERT_EQ(GetCurrentFiber(), master_fiber.get());
|
||||
|
||||
ASSERT_EQ(cnt, 0);
|
||||
while (cnt < kMaxCnt) {
|
||||
std::unique_ptr<Fiber> worker_fiber = Fiber::Create([&] {
|
||||
while (cnt < kMaxCnt) {
|
||||
ASSERT_EQ(GetCurrentFiber(), worker_fiber.get());
|
||||
++cnt;
|
||||
master_fiber->Resume();
|
||||
}
|
||||
});
|
||||
|
||||
while (cnt < kMaxCnt) {
|
||||
int old = cnt;
|
||||
worker_fiber->Resume();
|
||||
ASSERT_EQ(old + 1, cnt);
|
||||
ASSERT_EQ(GetCurrentFiber(), master_fiber.get());
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_EQ(cnt, kMaxCnt);
|
||||
});
|
||||
|
||||
SetUpMasterFiber(master_fiber.get());
|
||||
SetUpCurrentFiber(nullptr);
|
||||
master_fiber->Resume();
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace fiber
|
||||
} // namespace tile
|
@ -1,35 +0,0 @@
|
||||
#ifndef TILE_FIBER_THIS_FIBER_H
|
||||
#define TILE_FIBER_THIS_FIBER_H
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "tile/base/chrono.h"
|
||||
#include "tile/fiber/fiber.h"
|
||||
#include <chrono>
|
||||
|
||||
namespace tile {
|
||||
namespace this_fiber {
|
||||
|
||||
Fiber::Id GetId();
|
||||
|
||||
void Yield();
|
||||
|
||||
void SleepUntil(std::chrono::steady_clock expires_at);
|
||||
|
||||
void SleepFor(std::chrono::nanoseconds expires_in);
|
||||
|
||||
template <typename Clock, typename Duration>
|
||||
void SleepUntil(const std::chrono::time_point<Clock, Duration> &expires_at) {
|
||||
return SleepUntil(ReadSteadyClock() + (expires_at - Clock::now()));
|
||||
}
|
||||
|
||||
template <typename Rep, typename Period>
|
||||
void SleepFor(const std::chrono::duration<Rep, Period> &expires_in) {
|
||||
return SleepFor(
|
||||
std::chrono::duration_cast<std::chrono::nanoseconds>(expires_in));
|
||||
}
|
||||
|
||||
} // namespace this_fiber
|
||||
} // namespace tile
|
||||
|
||||
#endif // TILE_FIBER_THIS_FIBER_H
|
Loading…
x
Reference in New Issue
Block a user