38 #include <condition_variable> 48 pPlatformService_{pPlatformService},
68 std::lock_guard<std::mutex> m(mutex_);
71 if(u64NewSize != u64MaxSize_)
75 "NEM %03hu BitPool::%s current pool size %ju, new pool size %ju",
82 if(u64CurrentSize_ > u64NewSize)
85 u64CurrentSize_ = u64NewSize;
89 u64MaxSize_ = u64NewSize;
100 std::lock_guard<std::mutex> m(mutex_);
103 if(u64MaxSize_ == 0 || u64Request == 0)
110 std::uint64_t u64Acquired{};
121 u64Acquired += doDrainPool(u64Request - u64Acquired, currentTime, waitIntervalMicroseconds);
125 "NEM %03hu BitPool::%s request %ju, acquired %ju, pool remaining %ju, %4.2f%%",
131 100.0f * (1.0f - (
static_cast<float>(u64MaxSize_ - u64CurrentSize_) /
132 static_cast<float>(u64MaxSize_))));
135 if(!bFullFill || u64Acquired == u64Request)
145 std::condition_variable cond{};
147 std::unique_lock<std::mutex> lock(mutex);
150 if(cond.wait_until(lock,currentTime + waitIntervalMicroseconds) != std::cv_status::timeout) {
158 return u64Request - u64Acquired;
167 std::lock_guard<std::mutex> m(mutex_);
169 return u64CurrentSize_;
175 EMANE::Utils::BitPool::doDrainPool(std::uint64_t u64Request,
179 std::uint64_t u64Acquired{};
182 doFillPool(requestTime);
185 if(u64Request > u64CurrentSize_)
188 std::uint64_t u64Outstanding{u64Request - u64CurrentSize_};
191 u64Acquired = u64CurrentSize_;
197 float fSeconds{
static_cast<float>(u64Outstanding) / static_cast<float>(u64MaxSize_)};
203 if(intervalMicroseconds > FillIntervalMicroseconds)
205 intervalMicroseconds = FillIntervalMicroseconds;
211 u64Acquired = u64Request;
214 u64CurrentSize_ -= u64Request;
217 intervalMicroseconds = Microseconds::zero();
221 lastRequestTime_ = requestTime;
230 EMANE::Utils::BitPool::doFillPool(
const TimePoint & requestTime)
239 if(deltaTMicroseconds >= FillIntervalMicroseconds)
242 u64CurrentSize_ = u64MaxSize_;
249 Microseconds diffMicroseconds{FillIntervalMicroseconds - (FillIntervalMicroseconds - deltaTMicroseconds)};
252 fFillRemainder_ += (
static_cast<float>(diffMicroseconds.count()) /
253 static_cast<float>(FillIntervalMicroseconds.count())) * u64MaxSize_;
256 if(fFillRemainder_ >= 1.0f)
258 std::uint64_t u64Fill{
static_cast<std::uint64_t
>(fFillRemainder_)};
261 if((u64CurrentSize_ + u64Fill) <= u64MaxSize_)
263 u64CurrentSize_ = u64CurrentSize_ + u64Fill;
267 u64CurrentSize_ = u64MaxSize_;
271 fFillRemainder_ -= u64Fill;
BitPool(PlatformServiceProvider *pPlatformService, NEMId id)
#define LOGGER_VERBOSE_LOGGING(logger, level, fmt, args...)
void setMaxSize(std::uint64_t u64NewSize)
std::uint64_t get(std::uint64_t u64Request, bool bFullFill=true)
std::chrono::microseconds Microseconds
std::chrono::duration< double > DoubleSeconds
std::uint64_t getCurrentSize()
Clock::time_point TimePoint