8#ifndef INCLUDED_BDLMT_THROTTLE
9#define INCLUDED_BDLMT_THROTTLE
272#include <bdlscm_version.h>
286#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
287#include <bsl_chrono.h>
290#include <bsl_climits.h>
314 enum { k_BILLION = 1000 * 1000 * 1000 };
316 static const Int64 k_ALLOW_ALL = LLONG_MIN;
317 static const Int64 k_ALLOW_NONE = LLONG_MAX;
318 static const Int64 k_MAX_SECONDS = LLONG_MAX / k_BILLION;
319 static const Int64 k_MIN_SECONDS = LLONG_MIN / k_BILLION;
346 template <
int t_MAX_SIMULTANEOUS_ACTIONS,
378#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
400 const bsl::chrono::system_clock&);
423 const bsl::chrono::steady_clock&);
525template <
int t_MAX_SIMULTANEOUS_ACTIONS,
531 BSLMF_ASSERT(t_MAX_SIMULTANEOUS_ACTIONS || t_NANOSECONDS_PER_ACTION);
533 ? t_MAX_SIMULTANEOUS_ACTIONS
535 t_NANOSECONDS_PER_ACTION);
540 0 == t_MAX_SIMULTANEOUS_ACTIONS ? Throttle::k_ALLOW_NONE
541 : t_NANOSECONDS_PER_ACTION ? t_NANOSECONDS_PER_ACTION
542 : Throttle::k_ALLOW_ALL;
546 : t_MAX_SIMULTANEOUS_ACTIONS;
558#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
561 int maxSimultaneousActions,
562 Int64 nanosecondsPerAction,
563 const bsl::chrono::system_clock&)
572 int maxSimultaneousActions,
573 Int64 nanosecondsPerAction,
574 const bsl::chrono::steady_clock&)
632#define BDLMT_THROTTLE_INIT(maxSimultaneousActions, \
633 nanosecondsPerAction) { \
634 { -BloombergLP::bdlmt::Throttle::k_TEN_YEARS_NANOSECONDS }, \
635 BloombergLP::bdlmt::Throttle_InitHelper< \
636 (maxSimultaneousActions), \
637 (nanosecondsPerAction)>::k_npaValue, \
638 bsl::integral_constant<BloombergLP::bsls::Types::Int64, \
639 1LL * (maxSimultaneousActions) * (nanosecondsPerAction)>::value, \
640 BloombergLP::bdlmt::Throttle_InitHelper< \
641 (maxSimultaneousActions), \
642 (nanosecondsPerAction)>::k_msaValue, \
643 BloombergLP::bsls::SystemClockType::e_MONOTONIC \
646#define BDLMT_THROTTLE_INIT_REALTIME(maxSimultaneousActions, \
647 nanosecondsPerAction) { \
648 { -BloombergLP::bdlmt::Throttle::k_TEN_YEARS_NANOSECONDS }, \
649 BloombergLP::bdlmt::Throttle_InitHelper< \
650 (maxSimultaneousActions), \
651 (nanosecondsPerAction)>::k_npaValue, \
652 bsl::integral_constant<BloombergLP::bsls::Types::Int64, \
653 1LL * (maxSimultaneousActions) * (nanosecondsPerAction)>::value, \
654 BloombergLP::bdlmt::Throttle_InitHelper< \
655 (maxSimultaneousActions), \
656 (nanosecondsPerAction)>::k_msaValue, \
657 BloombergLP::bsls::SystemClockType::e_REALTIME \
660#define BDLMT_THROTTLE_INIT_ALLOW_ALL BDLMT_THROTTLE_INIT(1, 0)
661#define BDLMT_THROTTLE_INIT_ALLOW_NONE BDLMT_THROTTLE_INIT(0, 1)
667#define BDLMT_THROTTLE_IF(maxSimultaneousActions, \
668 nanosecondsPerAction) \
669 if (bool bdlmt_throttle_iFsToP = false) {} \
671 for (static BloombergLP::bdlmt::Throttle bdlmt_throttle_iFtHrOtTlE = \
672 BDLMT_THROTTLE_INIT((maxSimultaneousActions), \
673 (nanosecondsPerAction)); \
674 !bdlmt_throttle_iFsToP; \
675 bdlmt_throttle_iFsToP = true) \
676 if (bdlmt_throttle_iFtHrOtTlE.requestPermission())
678#define BDLMT_THROTTLE_IF_REALTIME(maxSimultaneousActions, \
679 nanosecondsPerAction) \
680 if (bool bdlmt_throttle_iFsToP = false) {} \
682 for (static BloombergLP::bdlmt::Throttle bdlmt_throttle_iFtHrOtTlE = \
683 BDLMT_THROTTLE_INIT_REALTIME((maxSimultaneousActions), \
684 (nanosecondsPerAction)); \
685 !bdlmt_throttle_iFsToP; \
686 bdlmt_throttle_iFsToP = true) \
687 if (bdlmt_throttle_iFtHrOtTlE.requestPermission())
689#define BDLMT_THROTTLE_IF_ALLOW_ALL BDLMT_THROTTLE_IF(1, 0)
690#define BDLMT_THROTTLE_IF_ALLOW_NONE BDLMT_THROTTLE_IF(0, 1)
Definition bdlmt_throttle.h:527
static const bsls::Types::Int64 k_npaValue
Definition bdlmt_throttle.h:539
static const int k_msaValue
Definition bdlmt_throttle.h:544
Definition bdlmt_throttle.h:306
bsls::SystemClockType::Enum d_clockType
Definition bdlmt_throttle.h:340
int maxSimultaneousActions() const
Definition bdlmt_throttle.h:617
void initialize(int maxSimultaneousActions, Int64 nanosecondsPerAction, bsls::SystemClockType::Enum clockType=bsls::SystemClockType::e_MONOTONIC)
bool requestPermission(int numActions, const bsls::TimeInterval &now)
AtomicTypes::Int64 d_prevLeakTime
Definition bdlmt_throttle.h:327
Int64 d_nanosecondsPerTotalReset
Definition bdlmt_throttle.h:333
int d_maxSimultaneousActions
Definition bdlmt_throttle.h:336
int nextPermit(bsls::TimeInterval *result, int numActions) const
int requestPermissionIfValid(bool *result, int numActions, const bsls::TimeInterval &now)
Int64 d_nanosecondsPerAction
Definition bdlmt_throttle.h:330
bool requestPermission()
Definition bdlmt_throttle.h:583
Int64 nanosecondsPerAction() const
Definition bdlmt_throttle.h:623
bsls::SystemClockType::Enum clockType() const
Definition bdlmt_throttle.h:611
int requestPermissionIfValid(bool *result, int numActions)
Definition bdlmt_throttle.h:596
static const Int64 k_TEN_YEARS_NANOSECONDS
Definition bdlmt_throttle.h:323
bool requestPermission(const bsls::TimeInterval &now)
Definition bsls_timeinterval.h:307
#define BSLMF_ASSERT(expr)
Definition bslmf_assert.h:231
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition bdlmt_eventscheduler.h:550
static const bsls::Types::Int64 k_NANOSECONDS_PER_DAY
Definition bdlt_timeunitratio.h:228
Definition bsls_atomicoperations.h:836
Definition bsls_atomicoperations_default.h:333
Enum
Definition bsls_systemclocktype.h:119
@ e_MONOTONIC
Definition bsls_systemclocktype.h:128
@ e_REALTIME
Definition bsls_systemclocktype.h:122
static TimeInterval now(SystemClockType::Enum clockType)
Definition bsls_systemtime.h:177
long long Int64
Definition bsls_types.h:134