8#ifndef INCLUDED_BDLMT_EVENTSCHEDULER
9#define INCLUDED_BDLMT_EVENTSCHEDULER
508#include <bdlscm_version.h>
537#include <bsl_functional.h>
538#include <bsl_memory.h>
539#include <bsl_optional.h>
540#include <bsl_string.h>
541#include <bsl_utility.h>
543#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
546#include <bsl_chrono.h>
554class EventSchedulerTestTimeSource_Data;
593 EventData& operator=(
const EventData&);
609 : d_callback(bsl::allocator_arg, basicAllocator, callback)
610 , d_nowOffset(bsl::allocator_arg, basicAllocator, nowOffset)
618 EventData(
const EventData& original,
620 : d_callback(bsl::allocator_arg, basicAllocator, original.d_callback)
621 , d_nowOffset(bsl::allocator_arg, basicAllocator, original.d_nowOffset)
634 struct RecurringEventData {
656 RecurringEventData& operator=(
const RecurringEventData&);
674 : d_interval(interval)
675 , d_callback(bsl::allocator_arg, basicAllocator, callback)
676 , d_nowOffset(bsl::allocator_arg, basicAllocator, nowOffset)
685 RecurringEventData(
const RecurringEventData& original,
687 : d_interval(original.d_interval)
688 , d_callback(bsl::allocator_arg, basicAllocator, original.d_callback)
689 , d_nowOffset(bsl::allocator_arg, basicAllocator, original.d_nowOffset)
690 , d_eventIdx(original.d_eventIdx)
732 static const char s_defaultThreadName[16];
777 bool d_dispatcherAwaited;
782 *d_currentRecurringEvent;
791 unsigned int d_waitCount;
820#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
829 template <
class t_CLOCK,
class t_DURATION>
831 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& absTime);
844 template <
class t_CLOCK,
849 const bsl::chrono::time_point<t_CLOCK,
850 t_DURATION>& absTime,
851 const bsl::chrono::duration<t_REP_TYPE,
852 t_PERIOD_TYPE>& interval,
880 void dispatchEvents();
892 void releaseCurrentEvents();
905 const EventData& eventData);
916 const EventData& eventData);
934 const RecurringEventData& eventData,
955 const RecurringEventData& eventData,
1014#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1023 const bsl::chrono::system_clock&,
1037 const bsl::chrono::system_clock&,
1050 const bsl::chrono::steady_clock&,
1064 const bsl::chrono::steady_clock&,
1123#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1132 const bsl::chrono::system_clock&,
1146 const bsl::chrono::system_clock&,
1159 const bsl::chrono::steady_clock&,
1173 const bsl::chrono::steady_clock&,
1272#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1279 template <
class t_CLOCK,
class t_DURATION>
1281 const Event *handle,
1282 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime);
1300#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1312 template <
class t_CLOCK,
class t_DURATION>
1314 const Event *handle,
1315 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime);
1333#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1343 template <
class t_CLOCK,
class t_DURATION>
1345 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1347 template <
class t_CLOCK,
class t_DURATION>
1350 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1366#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1374 template <
class t_CLOCK,
class t_DURATION>
1377 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
1407#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1424 template <
class t_CLOCK,
1426 class t_PERIOD_TYPE,
1428 void scheduleRecurringEvent(
1429 const bsl::chrono::duration<t_REP_TYPE,
1430 t_PERIOD_TYPE>& interval,
1432 const bsl::chrono::time_point<t_CLOCK,
1433 t_DURATION>& startEpochTime =
1435 template <
class t_CLOCK,
1437 class t_PERIOD_TYPE,
1439 void scheduleRecurringEvent(
1441 const bsl::chrono::duration<t_REP_TYPE,
1442 t_PERIOD_TYPE>& interval,
1444 const bsl::chrono::time_point<t_CLOCK,
1445 t_DURATION>& startEpochTime =
1474#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1492 template <
class t_CLOCK,
1494 class t_PERIOD_TYPE,
1496 void scheduleRecurringEventRaw(
1498 const bsl::chrono::duration<t_REP_TYPE,
1499 t_PERIOD_TYPE>& interval,
1501 const bsl::chrono::time_point<t_CLOCK,
1502 t_DURATION>& startEpochTime =
1671 typedef EventScheduler::RecurringEventData RecurringEventData;
1804: d_handle(original.d_handle)
1818 d_handle = rhs.d_handle;
1834bdlmt::EventSchedulerEventHandle::
1837 return (
const Event*)((
const EventQueue::Pair*)d_handle);
1855: d_handle(original.d_handle)
1876 d_handle = rhs.d_handle;
1886bdlmt::EventSchedulerRecurringEventHandle::operator
1889 return (
const RecurringEvent*)((
const RecurringEventQueue::Pair*)d_handle);
1900 *EventScheduler::castToQueuePair(
const Event *handle)
1903 static_cast<const void *
>(handle));
1908 *EventScheduler::castToQueuePair(
const RecurringEvent *handle)
1911 static_cast<const void *
>(handle));
1914#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1916template <
class t_CLOCK,
class t_DURATION>
1919 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& absTime)
1921 using namespace bsl::chrono;
1923 auto now = t_CLOCK::now();
1924 microseconds offset = duration_cast<microseconds>(absTime -
now);
1927 return 0 == offset.count() && absTime >
now
1932template <
class t_CLOCK,
1935 class t_PERIOD_TYPE>
1938 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& absTime,
1939 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
1944 return timeUntilTrigger(absTime + eventIndex * interval);
1954 int ret = d_eventQueue.
remove(itemPtr);
1959 itemPtr->data().d_callback = 0;
1970 int ret = d_recurringQueue.
remove(itemPtr);
1979 itemPtr->data().d_callback = 0;
1988 d_eventQueue.releaseReferenceRaw(castToQueuePair(handle));
1994 d_recurringQueue.releaseReferenceRaw(castToQueuePair(handle));
1997#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
1998template <
class t_CLOCK,
class t_DURATION>
2000 const Event *handle,
2001 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime)
2005 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2018 timeUntilTrigger<t_CLOCK, t_DURATION>,
2022 int ret = d_eventQueue.
updateR(h,
2023 (
now() + offsetFromNow).totalMicroseconds(),
2026 if (0 == ret && isNewTop) {
2027 d_queueCondition.
signal();
2032template <
class t_CLOCK,
class t_DURATION>
2034 const Event *handle,
2035 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& newEpochTime)
2039 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2053 timeUntilTrigger<t_CLOCK, t_DURATION>,
2058 (
now() + offsetFromNow).totalMicroseconds(),
2063 d_queueCondition.
signal();
2065 if (d_currentEvent != h) {
2074 if (d_currentEvent != h) {
2078 d_dispatcherAwaited =
true;
2079 d_iterationCondition.
wait(&d_mutex);
2091 scheduleEvent(epochTime,
2092 EventData(callback, EventScheduler::returnZero));
2100 scheduleEvent(event, epochTime, EventData(callback, returnZero));
2103#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
2104template <
class t_CLOCK,
class t_DURATION>
2106void EventScheduler::scheduleEvent(
2107 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
2110 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2111 scheduleEvent(epochTime.time_since_epoch(), callback);
2115 scheduleEvent(
now() + offsetFromNow,
2118 timeUntilTrigger<t_CLOCK, t_DURATION>,
2123template <
class t_CLOCK,
class t_DURATION>
2124void EventScheduler::scheduleEvent(
2126 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
2131 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2132 scheduleEvent(event, epochTime.time_since_epoch(), callback);
2137 scheduleEvent(event,
2138 now() + offsetFromNow,
2141 timeUntilTrigger<t_CLOCK, t_DURATION>,
2146template <
class t_CLOCK,
class t_DURATION>
2149 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& epochTime,
2154 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2158 using namespace bsl::chrono;
2161 duration_cast<microseconds>(epochTime.time_since_epoch()).count();
2162 if (startTime < d_cachedNow) {
2163 startTime = d_cachedNow;
2178 d_queueCondition.
signal();
2185void EventScheduler::scheduleRecurringEvent(
2195 scheduleRecurringEventRaw(0, interval, callback, startEpochTime);
2199void EventScheduler::scheduleRecurringEvent(
2211 scheduleRecurringEvent(
2213 RecurringEventData(interval, callback, returnZeroInt),
2217#ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY
2218template <
class t_CLOCK,
2220 class t_PERIOD_TYPE,
2222void EventScheduler::scheduleRecurringEvent(
2223 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
2225 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& startEpochTime)
2227 BSLS_ASSERT(bsl::chrono::microseconds(1) <= interval);
2229 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2230 scheduleRecurringEvent(interval,
2232 startEpochTime.time_since_epoch());
2235 using namespace bsl::chrono;
2239 scheduleRecurringEventRaw(
2251 now() + offsetFromNow);
2255template <
class t_CLOCK,
2257 class t_PERIOD_TYPE,
2259void EventScheduler::scheduleRecurringEvent(
2260 RecurringEventHandle *event,
2261 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
2263 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& startEpochTime)
2266 BSLS_ASSERT(bsl::chrono::microseconds(1) <= interval);
2268 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2269 scheduleRecurringEvent(event,
2272 startEpochTime.time_since_epoch());
2275 using namespace bsl::chrono;
2279 scheduleRecurringEvent(
2291 now() + offsetFromNow);
2295template <
class t_CLOCK,
2297 class t_PERIOD_TYPE,
2299void EventScheduler::scheduleRecurringEventRaw(
2300 RecurringEvent **event,
2301 const bsl::chrono::duration<t_REP_TYPE, t_PERIOD_TYPE>& interval,
2303 const bsl::chrono::time_point<t_CLOCK, t_DURATION>& startEpochTime)
2306 BSLS_ASSERT(bsl::chrono::microseconds(1) <= interval);
2308 if (bslmt::ChronoUtil::isMatchingClock<t_CLOCK>(d_clockType)) {
2309 scheduleRecurringEventRaw(event,
2312 startEpochTime.time_since_epoch());
2315 using namespace bsl::chrono;
2319 scheduleRecurringEventRaw(
2331 now() + offsetFromNow);
2338EventScheduler::Event*
2341 return reinterpret_cast<Event*
>(d_eventQueue.addPairReferenceRaw(
2342 castToQueuePair(handle)));
2351 d_recurringQueue.addPairReferenceRaw(
2352 castToQueuePair(handle)));
2371 return d_currentTimeFunctor();
2377 return d_eventQueue.
length();
2383 return d_recurringQueue.
length();
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
Definition bdlcc_skiplist.h:804
int remove(const Pair *reference)
Definition bdlcc_skiplist.h:3303
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlcc_skiplist.h:3866
int length() const
Return the number of items in this list.
Definition bdlcc_skiplist.h:3513
SkipListPairHandle< bsls::Types::Int64, EventData > PairHandle
Definition bdlcc_skiplist.h:828
void addRawR(Pair **result, const KEY &key, const DATA &data, bool *newFrontFlag=0)
Definition bdlcc_skiplist.h:3216
SkipListPair< bsls::Types::Int64, RecurringEventData > Pair
Definition bdlcc_skiplist.h:827
int updateR(const Pair *reference, const KEY &newKey, bool *newFrontFlag=0, bool allowDuplicates=true)
Definition bdlcc_skiplist.h:3392
static Bind< bslmf::Nil, t_FUNC, Bind_BoundTuple0 > bind(t_FUNC func)
Definition bdlf_bind.h:1835
Definition bdlm_metricsregistry.h:306
Definition bdlm_metricsregistry.h:197
Definition bdlmt_eventscheduler.h:1616
EventSchedulerEventHandle & operator=(const EventSchedulerEventHandle &rhs)
Definition bdlmt_eventscheduler.h:1816
EventScheduler::Event Event
Definition bdlmt_eventscheduler.h:1629
EventSchedulerEventHandle()
Create a new handle object that does not refer to an event.
Definition bdlmt_eventscheduler.h:1797
~EventSchedulerEventHandle()
Destroy this object and release the managed reference, if any.
Definition bdlmt_eventscheduler.h:1809
void release()
Release the reference (if any) held by this object.
Definition bdlmt_eventscheduler.h:1823
Definition bdlmt_eventscheduler.h:1668
EventSchedulerRecurringEventHandle()
Create a new handle object.
Definition bdlmt_eventscheduler.h:1848
void release()
Release the reference managed by this handle, if any.
Definition bdlmt_eventscheduler.h:1866
~EventSchedulerRecurringEventHandle()
Destroy this object and release the managed reference, if any.
Definition bdlmt_eventscheduler.h:1860
EventScheduler::RecurringEvent RecurringEvent
Definition bdlmt_eventscheduler.h:1683
EventSchedulerRecurringEventHandle & operator=(const EventSchedulerRecurringEventHandle &rhs)
Definition bdlmt_eventscheduler.h:1873
Definition bdlmt_eventscheduler.h:1735
bsls::TimeInterval advanceTime(bsls::TimeInterval amount)
bsls::TimeInterval now() const
BSLMF_NESTED_TRAIT_DECLARATION(EventSchedulerTestTimeSource, bslma::UsesBslmaAllocator)
EventSchedulerTestTimeSource(EventScheduler *scheduler, bslma::Allocator *basicAllocator=0)
Definition bdlmt_eventscheduler.h:566
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:2369
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:2362
RecurringEvent * addRecurringEventRefRaw(RecurringEvent *handle) const
Definition bdlmt_eventscheduler.h:2348
int cancelEvent(EventHandle *handle)
int numRecurringEvents() const
Definition bdlmt_eventscheduler.h:2381
EventScheduler(bslma::Allocator *basicAllocator)
bslma::Allocator * allocator() const
Return the allocator used by this object to supply memory.
Definition bdlmt_eventscheduler.h:2389
bsls::TimeInterval nextPendingEventTime() const
int start(const bslmt::ThreadAttributes &threadAttributes)
void releaseEventRaw(Event *handle)
Definition bdlmt_eventscheduler.h:1986
EventSchedulerEventHandle EventHandle
Definition bdlmt_eventscheduler.h:717
bsl::optional< bsls::TimeInterval > scheduledEventTime(const RecurringEventHandle &handle) const
int cancelEvent(const Event *handle)
Definition bdlmt_eventscheduler.h:1950
EventScheduler(const Dispatcher &dispatcherFunctor, bslma::Allocator *basicAllocator=0)
int cancelEventAndWait(const RecurringEvent *handle)
int cancelEventAndWait(RecurringEventHandle *handle)
EventSchedulerRecurringEventHandle RecurringEventHandle
Definition bdlmt_eventscheduler.h:719
void cancelAllEventsAndWait()
bsls::SystemClockType::Enum clockType() const
Definition bdlmt_eventscheduler.h:2356
bsl::optional< bsls::TimeInterval > scheduledEventTime(const EventHandle &handle) const
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:723
void scheduleRecurringEventRaw(RecurringEvent **event, const bsls::TimeInterval &interval, const bsl::function< void()> &callback, const bsls::TimeInterval &startEpochTime=bsls::TimeInterval(0))
Event * addEventRefRaw(Event *handle) const
Definition bdlmt_eventscheduler.h:2339
int numEvents() const
Return the number of pending one-time events in this scheduler.
Definition bdlmt_eventscheduler.h:2375
Definition bslstl_stringview.h:471
Definition bslstl_string.h:1252
Forward declaration.
Definition bslstl_function.h:946
Definition bslstl_optional.h:2043
Definition bslstl_sharedptr.h:1838
Definition bslma_allocator.h:545
Definition bslmt_condition.h:220
int wait(Mutex *mutex)
Definition bslmt_condition.h:430
void signal()
Definition bslmt_condition.h:402
Definition bslmt_lockguard.h:234
Definition bslmt_mutex.h:317
Definition bslmt_threadattributes.h:361
Definition bsls_atomic.h:896
Definition bsls_atomic.h:1205
Types::Uint64 loadAcquire() const
Definition bsls_atomic.h:2270
Definition bsls_timeinterval.h:307
BSLS_KEYWORD_CONSTEXPR_CPP14 bsls::Types::Int64 totalMicroseconds() const
Definition bsls_timeinterval.h:1472
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
#define BSLS_REVIEW(X)
Definition bsls_review.h:1019
const PlaceHolder< 1 > _1
Definition bdlmt_eventscheduler.h:550
Definition bdlmt_eventscheduler.h:709
Definition bdlmt_eventscheduler.h:715
Definition bslma_usesbslmaallocator.h:344
Imp::Handle Handle
Definition bslmt_threadutil.h:389
static bsls::Types::Uint64 selfIdAsUint64()
Definition bslmt_threadutil.h:1187
Enum
Definition bsls_systemclocktype.h:119
long long Int64
Definition bsls_types.h:134