summaryrefslogtreecommitdiff
path: root/indra/llcommon/lleventtimer.cpp
AgeCommit message (Collapse)Author
2024-05-15Manual whitespace cleanup (fix_whitespace.py).Nat Goodspeed
2024-05-09Add "Timers" LLEventAPI, actually a LazyEventAPI, for LL::Timers.Nat Goodspeed
Rename LL::Timers::scheduleRepeating() to scheduleEvery().
2024-05-07Refactor LLLater -> LL::Timers to accommodate nonpositive repeats.Nat Goodspeed
In the previous design, the tick() method ran each task exactly once. doPeriodically() was implemented by posting a functor that would, after calling the specified callable, repost itself at (timestamp + interval). The trouble with that design is that it required (interval > 0). A nonpositive interval would result in looping over any timers with nonpositive repetition intervals without ever returning from the tick() method. To avoid that, doPeriodically() contained an llassert(interval > 0). Unfortunately the viewer failed that constraint immediately at login, leading to the suspicion that eliminating every such usage might require a protracted search. Lifting that restriction required a redesign. Now the priority queue stores a callable returning bool, and the tick() method itself contains the logic to repost a recurring task -- but defers doing so until after it stops looping over ready tasks, ensuring that a task with a nonpositive interval will at least wait until the following tick() call. This simplifies not only doPeriodically(), but also doAtTime(). The previous split of doAtTime() into doAtTime1() and doAtTime2() was only to accommodate the needs of the Periodic functor class. Ditch Periodic. Per feedback from NickyD, rename doAtTime() to scheduleAt(), which wraps its passed nullary callable into a callable that unconditionally returns true (so tick() will run it only once). Rename the doAfterInterval() method to scheduleAfter(), which similarly wraps its nullary callable. However, the legacy doAfterInterval() free function remains. scheduleAfter() also loses its llassert(seconds > 0). Rename the doPeriodically() method to scheduleRepeating(). However, the legacy doPeriodically() free function remains. Add internal scheduleAtRepeating(), whose role is to accept both a specific timestamp and a repetition interval (which might be ignored, depending on the callable). scheduleAtRepeating() now contains the real logic to add a task. Rename getRemaining() to timeUntilCall(), hopefully resolving the question of "remaining what?" Expand the std::pair metadata stored in Timers's auxiliary unordered_map to a Metadata struct containing the repetition interval plus two bools to mediate deferred cancel() processing. Rename HandleMap to MetaMap, mHandles to mMeta. Defend against the case when cancel(handle) is reached during the call to that handle's callable. Meta::mRunning is set for the duration of that call. When cancel() sees mRunning, instead of immediately deleting map entries, it sets mCancel. Upon return from a task's callable, tick() notices mCancel and behaves as if the callable returned true to stop the series of calls. To guarantee that mRunning doesn't inadvertently remain set even in the case of an exception, introduce local RAII class TempSet whose constructor accepts a non-const variable reference and a desired value. The constructor captures the current value and sets the desired value; the destructor restores the previous value. Defend against exception in a task's callable, and stop calling that task. Use LOG_UNHANDLED_EXCEPTION() to report it.
2024-05-02Introduce LLLater::getRemaining(handle).Nat Goodspeed
Some timer use cases need to know not only whether the timer is active, but how much time remains before it (next) fires. Introduce LLLater::mDoneTimes to track, for each handle, the timestamp at which it's expected to fire. We can't just look up the target timestamp in mQueue's func_at entry because there's no documented way to navigate from a handle_type to a node iterator or pointer. Nor can we store it in mHandles because of order dependency: we need the mDoneTimes iterator so we can bind it into the Periodic functor for doPeriodically(), but we need the mQueue handle to store in mHandles. If we could find the mQueue node from the new handle, we could update the func_at entry after emplace() -- but if we could find the mQueue node from a handle, we wouldn't need to store the target timestamp separately anyway. Split LLLater::doAtTime() into internal doAtTime1() and doAtTime2(): the first creates an mDoneTimes entry and returns an iterator, the second finishes creating new mQueue and mHandles entries based on that mDoneTimes entry. This lets doPeriodically()'s Periodic bind the mDoneTimes iterator. Then instead of continually incrementing an internal data member, it increments the mDoneTimes entry to set the next upcoming timestamp. That lets getRemaining() report the next upcoming timestamp rather than only the original one. Add LLEventTimer::isRunning() and getRemaining(), forwarding to its LLLater handle. Fix various LLEventTimer subclass references to mEventTimer.stop(), etc. Fix non-inline LLEventTimer subclass tick() overrides for bool, not BOOL. Remove LLAppViewer::idle() call to LLEventTimer::updateClass(). Since LLApp::stepFrame() already calls LLCallbackList::callFunctions(), assume we've already handled that every tick.
2024-05-02WIP: In llcallbacklist.h, add singleton LLLater for time delays.Nat Goodspeed
The big idea is to reduce the number of per-tick callbacks asking, "Is it time yet? Is it time yet?" We do that for LLEventTimer and LLEventTimeout. LLLater presents doAtTime(LLDate), with doAfterInterval() and doPeriodically() methods implemented using doAtTime(). All return handles. The free functions doAfterInterval() and doPeriodically() now forward to the corresponding LLLater methods. LLLater also presents isRunning(handle) and cancel(handle). LLLater borrows the tactic of LLEventTimer: while there's at least one running timer, it registers an LLCallbackList tick() callback to service ready timers. But instead of looping over all of them asking, "Are you ready?" it keeps them in a priority queue ordered by desired timestamp, and only touches those whose timestamp has been reached. Also, it honors a maximum time slice: once the ready timers have run for longer than the limit, it defers processing other ready timers to the next tick() call. The intent is to consume fewer cycles per tick() call, both by the management machinery and the timers themselves. Revamp LLCallbackList to accept C++ callables in addition to (classic C function pointer, void*) pairs. Make addFunction() return a handle (different than LLLater handles) that can be passed to a new deleteFunction() overload, since std::function instances can't be compared for equality. In fact, implement LLCallbackList using boost::signals2::signal, which provides almost exactly what we want. LLCallbackList continues to accept (function pointer, void*) pairs, but now we store a lambda that calls the function pointer with that void*. It takes less horsing around to create a C++ callable from a (function pointer, void*) pair than the other way around. For containsFunction() and deleteFunction(), such pairs are the keys for a lookup table whose values are handles. Instead of having a static global LLCallbackList gIdleCallbacks, make LLCallbackList an LLSingleton to guarantee initialization. For backwards compatibility, gIdleCallbacks is now a macro for LLCallbackList::instance(). Move doOnIdleOneTime() and doOnIdleRepeating() functions to LLCallbackList methods, but for backwards compatibility continue providing free functions. Reimplement LLEventTimer using LLLater::doPeriodically(). One implication is that LLEventTimer need no longer be derived from LLInstanceTracker, which we used to iterate over all instances every tick. Give it start() and stop() methods, since some subclasses (e.g. LLFlashTimer) used to call its member LLTimer's start() and stop(). Remove updateClass(): LLCallbackList::callFunctions() now takes care of that. Remove LLToastLifeTimer::start() and stop(), since LLEventTimer now provides those. Remove getRemainingTimeF32(), since LLLater does not (yet) provide that feature. While at it, make LLEventTimer::tick() return bool instead of BOOL, and change existing overrides. Make LLApp::stepFrame() call LLCallbackList::callFunctions() instead of LLEventTimer::updateClass(). We could have refactored LLEventTimer to use the mechanism now built into LLLater, but frankly the LLEventTimer API is rather clumsy. You MUST derive a subclass and override tick(), and you must instantiate your subclass on the heap because, when your tick() override returns false, LLEventTimer deletes its subclass instance. The LLLater API is simpler to use, and LLEventTimer is much simplified by using it. Merge lleventfilter.h's LLEventTimeoutBase into LLEventTimeout, and likewise merge LLEventThrottleBase into LLEventThrottle. The separation was for testability, but now that they're no longer based on LLTimer, it becomes harder to use dummy time for testing. Temporarily skip tests based on LLEventTimeoutBase and LLEventThrottleBase. Instead of listening for LLEventPump("mainloop") ticks and using LLTimer, LLEventTimeout now uses LLLater::doAfterInterval(). Instead of LLTimer and LLEventTimeout, LLEventThrottle likewise now uses LLLater::doAfterInterval(). Recast a couple local LLEventTimeout pre-lambda callable classes with lambdas. Dignify F64 with a new typedef LLDate::timestamp. LLDate heavily depends on that as its base time representation, but there are those who question use of floating-point for time. This is a step towards insulating us from any future change.
2020-03-25DRTVWR-494: Streamline LLEventTimer::updateClass().Nat Goodspeed
No need to capture a separate list of completed LLEventTimer instances to delete after the primary loop, since at this point we're looping over a snapshot and can directly delete each completed timer.
2020-03-25DRTVWR-494: Defend LLInstanceTracker against multi-thread usage.Nat Goodspeed
The previous implementation went to some effort to crash if anyone attempted to create or destroy an LLInstanceTracker subclass instance during traversal. That restriction is manageable within a single thread, but becomes unworkable if it's possible that a given subclass might be used on more than one thread. Remove LLInstanceTracker::instance_iter, beginInstances(), endInstances(), also key_iter, beginKeys() and endKeys(). Instead, introduce key_snapshot() and instance_snapshot(), the only means of iterating over LLInstanceTracker instances. (These are intended to resemble functions, but in fact the current implementation simply presents the classes.) Iterating over a captured snapshot defends against container modifications during traversal. The term 'snapshot' reminds the coder that a new instance created during traversal will not be considered. To defend against instance deletion during traversal, a snapshot stores std::weak_ptrs which it lazily dereferences, skipping on the fly any that have expired. Dereferencing instance_snapshot::iterator gets you a reference rather than a pointer. Because some use cases want to delete all existing instances, add an instance_snapshot::deleteAll() method that extracts the pointer. Those cases used to require explicitly copying instance pointers into a separate container; instance_snapshot() now takes care of that. It remains the caller's responsibility to ensure that all instances of that LLInstanceTracker subclass were allocated on the heap. Replace unkeyed static LLInstanceTracker::getInstance(T*) -- which returned nullptr if that instance had been destroyed -- with new getWeak() method returning std::weak_ptr<T>. Caller must detect expiration of that weak_ptr. Adjust tests accordingly. Use of std::weak_ptr to detect expired instances requires engaging std::shared_ptr in the constructor. We now store shared_ptrs in the static containers (std::map for keyed, std::set for unkeyed). Make LLInstanceTrackerBase a template parameterized on the type of the static data it manages. For that reason, hoist static data class declarations out of the class definitions to an LLInstanceTrackerStuff namespace. Remove the static atomic sIterationNestDepth and its methods incrementDepth(), decrementDepth() and getDepth(), since they were used only to forbid creation and destruction during traversal. Add a std::mutex to static data. Introduce an internal LockStatic class that locks the mutex while providing a pointer to static data, making that the only way to access the static data. The LLINSTANCETRACKER_DTOR_NOEXCEPT macro goes away because we no longer expect ~LLInstanceTracker() to throw an exception in test programs. That affects LLTrace::StatBase as well as LLInstanceTracker itself. Adapt consumers to the new LLInstanceTracker API.
2015-11-10remove execute permission from many files that should not have itOz Linden
2013-03-29Update Mac and Windows breakpad builds to latestGraham Madarasz
2011-07-26EXP-1021 FIX Position of web content browser dictates position of profile ↵Richard Nelson
panel - profile panel position changes do not persist EXP-1030 FIX Search button toggle can get out of synch in bottom bar made llinstancetracker::iterator do own nested level management (removing need for separate guard) added support for filename= to floaters can pass in arbitrary window_class to floaters
2010-08-13Change license from GPL to LGPL (version 2.1)Oz Linden
2010-02-06EXT-5055 LLInstanceTracker promotes some dangerous patterns - detect themTofu Linden
2010-02-06move LLEventTimer into its own source module. everyone includes it, almost ↵Tofu Linden
no-one wants it. now I can dick with it a bit without rebuilding the world, at least. :)