feat/support_fiber #2
@ -1,5 +1,7 @@
|
|||||||
#include "tile/base/object_pool.h"
|
#include "tile/base/object_pool.h"
|
||||||
|
|
||||||
|
#include "tile/base/thread/spinlock.h"
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
@ -48,6 +50,14 @@ constexpr std::size_t PoolTraits<D>::kLowWaterMark;
|
|||||||
constexpr std::size_t PoolTraits<D>::kHighWaterMark;
|
constexpr std::size_t PoolTraits<D>::kHighWaterMark;
|
||||||
constexpr std::chrono::milliseconds PoolTraits<D>::kMaxIdle;
|
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 {
|
namespace object_pool {
|
||||||
|
|
||||||
TEST(ThreadLocalPool, All) {
|
TEST(ThreadLocalPool, All) {
|
||||||
@ -62,7 +72,8 @@ TEST(ThreadLocalPool, All) {
|
|||||||
ptrs.clear();
|
ptrs.clear();
|
||||||
}
|
}
|
||||||
for (int i = 0; i != 200; ++i) {
|
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.
|
Get<C>().Reset(); // Trigger wash out if possible.
|
||||||
}
|
}
|
||||||
ASSERT_EQ(PoolTraits<C>::kHighWaterMark + PoolTraits<C>::kLowWaterMark,
|
ASSERT_EQ(PoolTraits<C>::kHighWaterMark + PoolTraits<C>::kLowWaterMark,
|
||||||
@ -73,13 +84,15 @@ TEST(ThreadLocalPool, All) {
|
|||||||
ASSERT_EQ(PoolTraits<C>::kHighWaterMark + PoolTraits<C>::kLowWaterMark,
|
ASSERT_EQ(PoolTraits<C>::kHighWaterMark + PoolTraits<C>::kLowWaterMark,
|
||||||
alive);
|
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) {
|
for (int i = 0; i != 100; ++i) {
|
||||||
Get<C>().Reset(); // We have a limit on objects to loop per call. So we may
|
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
|
// need several calls to lower alive objects to low-water
|
||||||
// mark.
|
// mark.
|
||||||
std::this_thread::sleep_for(
|
BusySleep(std::chrono::milliseconds(10));
|
||||||
std::chrono::milliseconds(10)); // The limit on wash interval..
|
// 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
|
// Low-water mark. + 1 for the object just freed (as it's fresh and won't be
|
||||||
// affected by low-water mark.).
|
// affected by low-water mark.).
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
// aarch64 isb
|
// aarch64 isb
|
||||||
// arm v6 yield
|
// arm v6 yield
|
||||||
|
|
||||||
#if defined(__x86_64__)
|
#if defined(__x86_64__) || defined(__i386__)
|
||||||
#define TILE_CPU_RELAX() asm volatile("pause" ::: "memory")
|
#define TILE_CPU_RELAX() asm volatile("pause" ::: "memory")
|
||||||
#elif defined(__aarch64__)
|
#elif defined(__aarch64__)
|
||||||
#define TILE_CPU_RELAX() asm volatile("isb" ::)
|
#define TILE_CPU_RELAX() asm volatile("isb" ::)
|
||||||
@ -30,4 +30,5 @@ void Spinlock::LockSlow() noexcept {
|
|||||||
} while (locked_.exchange(true, std::memory_order_acquire));
|
} while (locked_.exchange(true, std::memory_order_acquire));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RelaxCpu() noexcept { TILE_CPU_RELAX(); }
|
||||||
} // namespace tile
|
} // namespace tile
|
||||||
|
@ -29,6 +29,8 @@ private:
|
|||||||
private:
|
private:
|
||||||
std::atomic<bool> locked_{false};
|
std::atomic<bool> locked_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void RelaxCpu() noexcept;
|
||||||
} // namespace tile
|
} // namespace tile
|
||||||
|
|
||||||
#endif // TILE_BASE_THREAD_SPINLOCK_H
|
#endif // TILE_BASE_THREAD_SPINLOCK_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user