8#ifndef INCLUDED_BDLMT_EVENTSCHEDULER
9#define INCLUDED_BDLMT_EVENTSCHEDULER
481#include <bdlscm_version.h>
510#include <bsl_functional.h>
511#include <bsl_memory.h>
512#include <bsl_string.h>
513#include <bsl_utility.h>
515#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
518#include <bsl_chrono.h>
526class EventSchedulerTestTimeSource_Data;
563 EventData& operator=(
const EventData&);
579 : d_callback(bsl::allocator_arg, basicAllocator, callback)
580 , d_nowOffset(bsl::allocator_arg, basicAllocator, nowOffset)
588 EventData(
const EventData& original,
590 : d_callback(bsl::allocator_arg, basicAllocator, original.d_callback)
591 , d_nowOffset(bsl::allocator_arg, basicAllocator, original.d_nowOffset)
602 struct RecurringEventData {
624 RecurringEventData& operator=(
const RecurringEventData&);
642 : d_interval(interval)
643 , d_callback(bsl::allocator_arg, basicAllocator, callback)
644 , d_nowOffset(bsl::allocator_arg, basicAllocator, nowOffset)
653 RecurringEventData(
const RecurringEventData& original,
655 : d_interval(original.d_interval)
656 , d_callback(bsl::allocator_arg, basicAllocator, original.d_callback)
657 , d_nowOffset(bsl::allocator_arg, basicAllocator, original.d_nowOffset)
658 , d_eventIdx(original.d_eventIdx)
698 static const char s_defaultThreadName[16];
741 bool d_dispatcherAwaited;
746 *d_currentRecurringEvent;
755 unsigned int d_waitCount;
777#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
785 template <
class t_CLOCK,
class t_DURATION>
787 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& absTime);
798 template <
class t_CLOCK,
803 const bsl::chrono::time_point<t_CLOCK,
804 t_DURATION>& absTime,
805 const bsl::chrono::duration<t_REP_TYPE,
806 t_PERIOD_TYPE>& interval,
831 void dispatchEvents();
843 void releaseCurrentEvents();
855 const EventData& eventData);
865 const EventData& eventData);
881 const RecurringEventData& eventData,
900 const RecurringEventData& eventData,
958#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
967 const bsl::chrono::system_clock&,
981 const bsl::chrono::system_clock&,
994 const bsl::chrono::steady_clock&,
1008 const bsl::chrono::steady_clock&,
1067#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1076 const bsl::chrono::system_clock&,
1090 const bsl::chrono::system_clock&,
1103 const bsl::chrono::steady_clock&,
1117 const bsl::chrono::steady_clock&,
1207#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1214 template <
class t_CLOCK,
class t_DURATION>
1216 const Event *handle,
1217 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime);
1234#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1245 template <
class t_CLOCK,
class t_DURATION>
1247 const Event *handle,
1248 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime);
1266#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1276 template <
class t_CLOCK,
class t_DURATION>
1278 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1280 template <
class t_CLOCK,
class t_DURATION>
1283 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1299#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1307 template <
class t_CLOCK,
class t_DURATION>
1310 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1338#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1353 template <
class t_CLOCK,
1355 class t_PERIOD_TYPE,
1357 void scheduleRecurringEvent(
1358 const bsl::chrono::duration<t_REP_TYPE,
1359 t_PERIOD_TYPE>& interval,
1361 const bsl::chrono::time_point<t_CLOCK,
1362 t_DURATION>& startEpochTime =
1364 template <
class t_CLOCK,
1366 class t_PERIOD_TYPE,
1368 void scheduleRecurringEvent(
1370 const bsl::chrono::duration<t_REP_TYPE,
1371 t_PERIOD_TYPE>& interval,
1373 const bsl::chrono::time_point<t_CLOCK,
1374 t_DURATION>& startEpochTime =
1401#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1417 template <
class t_CLOCK,
1419 class t_PERIOD_TYPE,
1421 void scheduleRecurringEventRaw(
1423 const bsl::chrono::duration<t_REP_TYPE,
1424 t_PERIOD_TYPE>& interval,
1426 const bsl::chrono::time_point<t_CLOCK,
1427 t_DURATION>& startEpochTime =
1583 typedef EventScheduler::RecurringEventData RecurringEventData;
1703: d_handle(original.d_handle)
1717 d_handle = rhs.d_handle;
1733bdlmt::EventSchedulerEventHandle::
1736 return (
const Event*)((
const EventQueue::Pair*)d_handle);
1754: d_handle(original.d_handle)
1775 d_handle = rhs.d_handle;
1785bdlmt::EventSchedulerRecurringEventHandle::operator
1788 return (
const RecurringEvent*)((
const RecurringEventQueue::Pair*)d_handle);
1797#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1799template <
class t_CLOCK,
class t_DURATION>
1802 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& absTime)
1806 auto now = t_CLOCK::now();
1807 microseconds offset = duration_cast<microseconds>(absTime -
now);
1810 return 0 == offset.count() && absTime >
now
1815template <
class t_CLOCK,
1818 class t_PERIOD_TYPE>
1821 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& absTime,
1822 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
1827 return timeUntilTrigger(absTime + eventIndex * interval);
1837 reinterpret_cast<const void*
>(handle));
1839 return d_eventQueue.
remove(itemPtr);
1847 reinterpret_cast<const void*
>(handle));
1849 return d_recurringQueue.
remove(itemPtr);
1856 reinterpret_cast<void*
>(handle)));
1862 d_recurringQueue.releaseReferenceRaw(
1864 reinterpret_cast<void*
>(handle)));
1867#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1868template <
class t_CLOCK,
class t_DURATION>
1870 const Event *handle,
1871 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime)
1875 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
1881 reinterpret_cast<const void *
>(handle));
1889 timeUntilTrigger<t_CLOCK, t_DURATION>,
1893 int ret = d_eventQueue.
updateR(h,
1894 (
now() + offsetFromNow).totalMicroseconds(),
1897 if (0 == ret && isNewTop) {
1898 d_queueCondition.
signal();
1903template <
class t_CLOCK,
class t_DURATION>
1905 const Event *handle,
1906 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime)
1910 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
1918 reinterpret_cast<const void *
>(handle));
1926 timeUntilTrigger<t_CLOCK, t_DURATION>,
1931 (
now() + offsetFromNow).totalMicroseconds(),
1936 d_queueCondition.
signal();
1938 if (d_currentEvent != h) {
1947 if (d_currentEvent != h) {
1951 d_dispatcherAwaited =
true;
1952 d_iterationCondition.
wait(&d_mutex);
1964 scheduleEvent(epochTime,
1965 EventData(callback, EventScheduler::returnZero));
1973 scheduleEvent(event, epochTime, EventData(callback, returnZero));
1976#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1977template <
class t_CLOCK,
class t_DURATION>
1979void EventScheduler::scheduleEvent(
1980 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1983 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
1984 scheduleEvent(epochTime.time_since_epoch(), callback);
1988 scheduleEvent(
now() + offsetFromNow,
1991 timeUntilTrigger<t_CLOCK, t_DURATION>,
1996template <
class t_CLOCK,
class t_DURATION>
1997void EventScheduler::scheduleEvent(
1999 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
2004 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2005 scheduleEvent(event, epochTime.time_since_epoch(), callback);
2010 scheduleEvent(event,
2011 now() + offsetFromNow,
2014 timeUntilTrigger<t_CLOCK, t_DURATION>,
2019template <
class t_CLOCK,
class t_DURATION>
2022 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
2027 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2033 microseconds stime =
2034 duration_cast<microseconds>(epochTime.time_since_epoch());
2048 d_queueCondition.
signal();
2055void EventScheduler::scheduleRecurringEvent(
2065 scheduleRecurringEventRaw(0, interval, callback, startEpochTime);
2069void EventScheduler::scheduleRecurringEvent(
2081 scheduleRecurringEvent(
2083 RecurringEventData(interval, callback, returnZeroInt),
2087#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
2088template <
class t_CLOCK,
2090 class t_PERIOD_TYPE,
2092void EventScheduler::scheduleRecurringEvent(
2093 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
2095 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& startEpochTime)
2097 BSLS_ASSERT(bsl::chrono::microseconds(1) <= interval);
2099 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2100 scheduleRecurringEvent(interval,
2102 startEpochTime.time_since_epoch());
2109 scheduleRecurringEventRaw(
2121 now() + offsetFromNow);
2125template <
class t_CLOCK,
2127 class t_PERIOD_TYPE,
2129void EventScheduler::scheduleRecurringEvent(
2131 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
2133 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& startEpochTime)
2136 BSLS_ASSERT(bsl::chrono::microseconds(1) <= interval);
2138 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2139 scheduleRecurringEvent(event,
2142 startEpochTime.time_since_epoch());
2149 scheduleRecurringEvent(
2161 now() + offsetFromNow);
2165template <
class t_CLOCK,
2167 class t_PERIOD_TYPE,
2169void EventScheduler::scheduleRecurringEventRaw(
2171 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
2173 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& startEpochTime)
2176 BSLS_ASSERT(bsl::chrono::microseconds(1) <= interval);
2178 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2179 scheduleRecurringEventRaw(event,
2182 startEpochTime.time_since_epoch());
2189 scheduleRecurringEventRaw(
2201 now() + offsetFromNow);
2212 reinterpret_cast<void*
>(handle));
2213 return reinterpret_cast<Event*
>(d_eventQueue.addPairReferenceRaw(h));
2222 reinterpret_cast<void*
>(handle));
2224 d_recurringQueue.addPairReferenceRaw(h));
2243 return d_currentTimeFunctor();
2249 return d_eventQueue.
length();
2255 return d_recurringQueue.
length();
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
Definition bdlcc_skiplist.h:693
void release()
Release the reference (if any) managed by this SkipListPairHandle.
Definition bdlcc_skiplist.h:1852
Definition bdlcc_skiplist.h:657
DATA & data() const
Return a reference to the modifiable "data" of this pair.
Definition bdlcc_skiplist.h:1786
Definition bdlcc_skiplist.h:783
int remove(const Pair *reference)
Definition bdlcc_skiplist.h:3235
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlcc_skiplist.h:3772
int length() const
Return the number of items in this list.
Definition bdlcc_skiplist.h:3419
void addRawR(Pair **result, const KEY &key, const DATA &data, bool *newFrontFlag=0)
Definition bdlcc_skiplist.h:3148
int updateR(const Pair *reference, const KEY &newKey, bool *newFrontFlag=0, bool allowDuplicates=true)
Definition bdlcc_skiplist.h:3324
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1830
Definition bdlm_metricsregistry.h:287
Definition bdlm_metricsregistry.h:199
Definition bdlmt_eventscheduler.h:1528
EventSchedulerEventHandle & operator=(const EventSchedulerEventHandle &rhs)
Definition bdlmt_eventscheduler.h:1715
EventScheduler::Event Event
Definition bdlmt_eventscheduler.h:1541
EventSchedulerEventHandle()
Create a new handle object that does not refer to an event.
Definition bdlmt_eventscheduler.h:1696
~EventSchedulerEventHandle()
Destroy this object and release the managed reference, if any.
Definition bdlmt_eventscheduler.h:1708
void release()
Release the reference (if any) held by this object.
Definition bdlmt_eventscheduler.h:1722
Definition bdlmt_eventscheduler.h:1580
EventSchedulerRecurringEventHandle()
Create a new handle object.
Definition bdlmt_eventscheduler.h:1747
void release()
Release the reference managed by this handle, if any.
Definition bdlmt_eventscheduler.h:1765
~EventSchedulerRecurringEventHandle()
Destroy this object and release the managed reference, if any.
Definition bdlmt_eventscheduler.h:1759
EventScheduler::RecurringEvent RecurringEvent
Definition bdlmt_eventscheduler.h:1595
EventSchedulerRecurringEventHandle & operator=(const EventSchedulerRecurringEventHandle &rhs)
Definition bdlmt_eventscheduler.h:1772
Definition bdlmt_eventscheduler.h:1646
EventSchedulerTestTimeSource(EventScheduler *scheduler)
bsls::TimeInterval advanceTime(bsls::TimeInterval amount)
bsls::TimeInterval now() const
Definition bdlmt_eventscheduler.h:538
EventScheduler(const Dispatcher &dispatcherFunctor, const bsl::chrono::steady_clock &, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
EventScheduler(bsls::SystemClockType::Enum clockType, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
bsls::TimeInterval now() const
Definition bdlmt_eventscheduler.h:2241
int cancelEvent(RecurringEventHandle *handle)
int cancelEventAndWait(const Event *handle)
int rescheduleEvent(const Event *handle, const bsls::TimeInterval &newEpochTime)
EventScheduler(const Dispatcher &dispatcherFunctor, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
EventScheduler(const Dispatcher &dispatcherFunctor, bsls::SystemClockType::Enum clockType, bslma::Allocator *basicAllocator=0)
bool isInDispatcherThread() const
Definition bdlmt_eventscheduler.h:2234
RecurringEvent * addRecurringEventRefRaw(RecurringEvent *handle) const
Definition bdlmt_eventscheduler.h:2218
int cancelEvent(EventHandle *handle)
int numRecurringEvents() const
Definition bdlmt_eventscheduler.h:2253
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlmt_eventscheduler.h:2261
EventScheduler(const bsl::chrono::steady_clock &, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
bsls::TimeInterval nextPendingEventTime() const
EventScheduler(bslma::Allocator *basicAllocator=0)
int start(const bslmt::ThreadAttributes &threadAttributes)
void releaseEventRaw(Event *handle)
Definition bdlmt_eventscheduler.h:1853
EventSchedulerEventHandle EventHandle
Definition bdlmt_eventscheduler.h:683
int cancelEvent(const Event *handle)
Definition bdlmt_eventscheduler.h:1833
EventScheduler(const Dispatcher &dispatcherFunctor, bslma::Allocator *basicAllocator=0)
int cancelEventAndWait(const RecurringEvent *handle)
EventScheduler(const Dispatcher &dispatcherFunctor, const bsl::chrono::system_clock &, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
EventScheduler(const bsl::chrono::steady_clock &, bslma::Allocator *basicAllocator=0)
EventScheduler(const bsl::chrono::system_clock &, bslma::Allocator *basicAllocator=0)
int cancelEventAndWait(RecurringEventHandle *handle)
EventSchedulerRecurringEventHandle RecurringEventHandle
Definition bdlmt_eventscheduler.h:685
void cancelAllEventsAndWait()
EventScheduler(const bsl::chrono::system_clock &, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
bsls::SystemClockType::Enum clockType() const
Definition bdlmt_eventscheduler.h:2228
EventScheduler(const Dispatcher &dispatcherFunctor, const bsl::chrono::system_clock &, bslma::Allocator *basicAllocator=0)
BSLMF_NESTED_TRAIT_DECLARATION(EventScheduler, bslma::UsesBslmaAllocator)
int cancelEventAndWait(EventHandle *handle)
EventScheduler(const Dispatcher &dispatcherFunctor, bsls::SystemClockType::Enum clockType, const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
EventScheduler(bsls::SystemClockType::Enum clockType, bslma::Allocator *basicAllocator=0)
EventScheduler(const bsl::string_view &eventSchedulerName, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator=0)
void scheduleEventRaw(Event **event, const bsls::TimeInterval &epochTime, const bsl::function< void()> &callback)
int rescheduleEventAndWait(const Event *handle, const bsls::TimeInterval &newEpochTime)
bsl::function< void(const bsl::function< void()> &)> Dispatcher
Defines a type alias for the dispatcher functor type.
Definition bdlmt_eventscheduler.h:689
void scheduleRecurringEventRaw(RecurringEvent **event, const bsls::TimeInterval &interval, const bsl::function< void()> &callback, const bsls::TimeInterval &startEpochTime=bsls::TimeInterval(0))
EventScheduler(const Dispatcher &dispatcherFunctor, const bsl::chrono::steady_clock &, bslma::Allocator *basicAllocator=0)
Event * addEventRefRaw(Event *handle) const
Definition bdlmt_eventscheduler.h:2209
int numEvents() const
Return the number of pending one-time events in this scheduler.
Definition bdlmt_eventscheduler.h:2247
Definition bslstl_stringview.h:441
Definition bslstl_string.h:1281
Forward declaration.
Definition bslstl_function.h:934
Definition bslstl_sharedptr.h:1830
Definition bslma_allocator.h:457
Definition bslmt_condition.h:220
int wait(Mutex *mutex)
Definition bslmt_condition.h:423
void signal()
Definition bslmt_condition.h:395
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:315
Definition bslmt_threadattributes.h:356
Definition bsls_atomic.h:1195
Types::Uint64 loadAcquire() const
Definition bsls_atomic.h:2249
Definition bsls_timeinterval.h:301
BSLS_KEYWORD_CONSTEXPR_CPP14 bsls::Types::Int64 totalMicroseconds() const
Definition bsls_timeinterval.h:1397
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
#define BSLS_REVIEW(X)
Definition bsls_review.h:949
const PlaceHolder< 1 > _1
Definition bdlmt_eventscheduler.h:522
Definition bslstl_chrono.h:146
Definition bdlmt_eventscheduler.h:677
Definition bdlmt_eventscheduler.h:681
Definition bslma_usesbslmaallocator.h:343
Imp::Handle Handle
Definition bslmt_threadutil.h:385
static bsls::Types::Uint64 selfIdAsUint64()
Definition bslmt_threadutil.h:1100
Enum
Definition bsls_systemclocktype.h:117
long long Int64
Definition bsls_types.h:132