feat/support_fiber #2

Merged
tqcq merged 57 commits from feat/support_fiber into master 2024-06-21 10:33:52 +08:00
3 changed files with 21 additions and 5 deletions
Showing only changes of commit 13f2838375 - Show all commits

View File

@ -1,5 +1,7 @@
#include "tile/base/object_pool.h"
#include "tile/base/thread/spinlock.h"
#include "gtest/gtest.h"
#include <thread>
@ -48,6 +50,14 @@ constexpr std::size_t PoolTraits<D>::kLowWaterMark;
constexpr std::size_t PoolTraits<D>::kHighWaterMark;
constexpr std::chrono::milliseconds PoolTraits<D>::kMaxIdle;
template <typename Rep, typename Period>
static void BusySleep(std::chrono::duration<Rep, Period> dur) {
auto end = ReadCoarseSteadyClock() + dur;
while (ReadCoarseSteadyClock() < end) {
RelaxCpu();
}
}
namespace object_pool {
TEST(ThreadLocalPool, All) {
@ -62,7 +72,8 @@ TEST(ThreadLocalPool, All) {
ptrs.clear();
}
for (int i = 0; i != 200; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
// std::this_thread::sleep_for(std::chrono::milliseconds(10));
BusySleep(std::chrono::milliseconds(10));
Get<C>().Reset(); // Trigger wash out if possible.
}
ASSERT_EQ(PoolTraits<C>::kHighWaterMark + PoolTraits<C>::kLowWaterMark,
@ -73,13 +84,15 @@ TEST(ThreadLocalPool, All) {
ASSERT_EQ(PoolTraits<C>::kHighWaterMark + PoolTraits<C>::kLowWaterMark,
alive);
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
BusySleep(std::chrono::milliseconds(5000));
// std::this_thread::sleep_for(std::chrono::milliseconds(5000));
for (int i = 0; i != 100; ++i) {
Get<C>().Reset(); // We have a limit on objects to loop per call. So we may
// need several calls to lower alive objects to low-water
// mark.
std::this_thread::sleep_for(
std::chrono::milliseconds(10)); // The limit on wash interval..
BusySleep(std::chrono::milliseconds(10));
// std::this_thread::sleep_for(
// std::chrono::milliseconds(10)); // The limit on wash interval..
}
// Low-water mark. + 1 for the object just freed (as it's fresh and won't be
// affected by low-water mark.).

View File

@ -9,7 +9,7 @@
// aarch64 isb
// arm v6 yield
#if defined(__x86_64__)
#if defined(__x86_64__) || defined(__i386__)
#define TILE_CPU_RELAX() asm volatile("pause" ::: "memory")
#elif defined(__aarch64__)
#define TILE_CPU_RELAX() asm volatile("isb" ::)
@ -30,4 +30,5 @@ void Spinlock::LockSlow() noexcept {
} while (locked_.exchange(true, std::memory_order_acquire));
}
void RelaxCpu() noexcept { TILE_CPU_RELAX(); }
} // namespace tile

View File

@ -29,6 +29,8 @@ private:
private:
std::atomic<bool> locked_{false};
};
void RelaxCpu() noexcept;
} // namespace tile
#endif // TILE_BASE_THREAD_SPINLOCK_H