aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/TaskScheduler.h
blob: c9029a002dbf80b6c7b59264aa0cc36dd2268b59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef TRINITYCORE_TASK_SCHEDULER_H
#define TRINITYCORE_TASK_SCHEDULER_H

#include "Duration.h"
#include "Optional.h"
#include "Random.h"
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <memory>
#include <utility>
#include <set>

class TaskContext;

/// The TaskScheduler class provides the ability to schedule std::function's in the near future.
/// Use TaskScheduler::Update to update the scheduler.
/// Popular methods are:
/// * Schedule (Schedules a std::function which will be executed in the near future).
/// * Schedules an asynchronous function which will be executed at the next update tick.
/// * Cancel, Delay & Reschedule (Methods to manipulate already scheduled tasks).
/// Tasks are organized in groups (uint), multiple tasks can have the same group id,
/// you can provide a group or not, but keep in mind that you can only manipulate specific tasks through its group id!
/// Tasks callbacks use the function signature void(TaskContext) where TaskContext provides
/// access to the function schedule plan which makes it possible to repeat the task
/// with the same duration or a new one.
/// It also provides access to the repeat counter which is useful for task that repeat itself often
/// but behave different every time (spoken event dialogs for example).
class TC_COMMON_API TaskScheduler
{
    friend class TaskContext;

    // Time definitions (use steady clock)
    typedef std::chrono::steady_clock clock_t;
    typedef clock_t::time_point timepoint_t;
    typedef clock_t::duration duration_t;

    // Task group type
    typedef uint32 group_t;
    // Task repeated type
    typedef uint32 repeated_t;
    // Task handle type
    typedef std::function<void(TaskContext)> task_handler_t;
    // Predicate type
    typedef std::function<bool()> predicate_t;
    // Success handle type
    typedef std::function<void()> success_t;

    class Task
    {
        friend class TaskContext;
        friend class TaskScheduler;

        timepoint_t _end;
        duration_t _duration;
        Optional<group_t> _group;
        repeated_t _repeated;
        task_handler_t _task;

    public:
        // All Argument construct
        Task(timepoint_t end, duration_t duration, Optional<group_t> group,
            repeated_t const repeated, task_handler_t task)
                : _end(end), _duration(duration), _group(group), _repeated(repeated), _task(std::move(task)) { }

        // Minimal Argument construct
        Task(timepoint_t end, duration_t duration, task_handler_t task)
            : _end(end), _duration(duration), _group(std::nullopt), _repeated(0), _task(std::move(task)) { }

        // Copy construct
        Task(Task const&) = delete;
        // Move construct
        Task(Task&&) = delete;
        // Copy Assign
        Task& operator= (Task const&) = default;
        // Move Assign
        Task& operator= (Task&& right) = delete;

        ~Task() = default;

        // Order tasks by its end
        std::weak_ordering operator<=> (Task const& other) const
        {
            return std::compare_weak_order_fallback(_end, other._end);
        }

        // Compare tasks with its end
        bool operator== (Task const& other) const
        {
            return _end == other._end;
        }

        // Returns true if the task is in the given group
        inline bool IsInGroup(group_t const group) const
        {
            return _group == group;
        }
    };

    typedef std::shared_ptr<Task> TaskContainer;

    /// Container which provides Task order, insert and reschedule operations.
    struct Compare
    {
        bool operator() (TaskContainer const& left, TaskContainer const& right) const
        {
            return (*left.get()) < (*right.get());
        }
    };

    class TC_COMMON_API TaskQueue
    {
        std::multiset<TaskContainer, Compare> container;

    public:
        // Pushes the task in the container
        void Push(TaskContainer&& task);

        /// Pops the task out of the container
        TaskContainer Pop();

        TaskContainer const& First() const;

        void Clear();

        void RemoveIf(std::function<bool(TaskContainer const&)> const& filter);

        void ModifyIf(std::function<bool(TaskContainer const&)> const& filter);

        bool IsEmpty() const;
    };

    /// Contains a self reference to track if this object was deleted or not.
    std::shared_ptr<TaskScheduler> self_reference;

    /// The current time point (now)
    timepoint_t _now;

    /// The Task Queue which contains all task objects.
    TaskQueue _task_holder;

    typedef std::queue<std::function<void()>> AsyncHolder;

    /// Contains all asynchronous tasks which will be invoked at
    /// the next update tick.
    AsyncHolder _asyncHolder;

    predicate_t _predicate;

    static bool EmptyValidator()
    {
        return true;
    }

public:
    TaskScheduler();

    template<typename P>
    explicit TaskScheduler(P&& predicate)
        : self_reference(this, [](TaskScheduler const*) { }), _now(clock_t::now()), _predicate(std::forward<P>(predicate)) { }

    TaskScheduler(TaskScheduler const&) = delete;
    TaskScheduler(TaskScheduler&&) = delete;
    TaskScheduler& operator= (TaskScheduler const&) = delete;
    TaskScheduler& operator= (TaskScheduler&&) = delete;

    ~TaskScheduler();

    /// Sets a validator which is asked if tasks are allowed to be executed.
    template<typename P>
    TaskScheduler& SetValidator(P&& predicate)
    {
        _predicate = std::forward<P>(predicate);
        return *this;
    }

    /// Clears the validator which is asked if tasks are allowed to be executed.
    TaskScheduler& ClearValidator();

    /// Update the scheduler to the current time.
    /// Calls the optional callback on successfully finish.
    TaskScheduler& Update(success_t const& callback = nullptr);

    /// Update the scheduler with a difftime in ms.
    /// Calls the optional callback on successfully finish.
    TaskScheduler& Update(size_t milliseconds, success_t const& callback = nullptr);

    /// Update the scheduler with a difftime.
    /// Calls the optional callback on successfully finish.
    TaskScheduler& Update(duration_t difftime, success_t const& callback = nullptr);

    /// Schedule an callable function that is executed at the next update tick.
    /// Its safe to modify the TaskScheduler from within the callable.
    TaskScheduler& Async(std::function<void()> callable);

    /// Schedule an event with a fixed rate.
    /// Never call this from within a task context! Use TaskContext::Schedule instead!
    TaskScheduler& Schedule(duration_t time,
        task_handler_t task)
    {
        return this->ScheduleAt(_now, time, std::move(task));
    }

    /// Schedule an event with a fixed rate.
    /// Never call this from within a task context! Use TaskContext::Schedule instead!
    TaskScheduler& Schedule(duration_t time,
        group_t const group, task_handler_t task)
    {
        return this->ScheduleAt(_now, time, group, std::move(task));
    }

    /// Schedule an event with a randomized rate between min and max rate.
    /// Never call this from within a task context! Use TaskContext::Schedule instead!
    TaskScheduler& Schedule(std::chrono::milliseconds min,
        std::chrono::milliseconds max, task_handler_t task)
    {
        return this->Schedule(::randtime(min, max), std::move(task));
    }

    /// Schedule an event with a fixed rate.
    /// Never call this from within a task context! Use TaskContext::Schedule instead!
    TaskScheduler& Schedule(std::chrono::milliseconds min,
        std::chrono::milliseconds max, group_t const group,
        task_handler_t task)
    {
        return this->Schedule(::randtime(min, max), group, std::move(task));
    }

    /// Cancels all tasks.
    /// Never call this from within a task context! Use TaskContext::CancelAll instead!
    TaskScheduler& CancelAll();

    /// Cancel all tasks of a single group.
    /// Never call this from within a task context! Use TaskContext::CancelGroup instead!
    TaskScheduler& CancelGroup(group_t group);

    /// Cancels all groups in the given std::vector.
    /// Hint: Use std::initializer_list for this: "{1, 2, 3, 4}"
    TaskScheduler& CancelGroupsOf(std::vector<group_t> const& groups);

    /// Delays all tasks with the given duration.
    TaskScheduler& DelayAll(duration_t duration);

    /// Delays all tasks with a random duration between min and max.
    TaskScheduler& DelayAll(std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->DelayAll(::randtime(min, max));
    }

    /// Delays all tasks of a group with the given duration.
    TaskScheduler& DelayGroup(group_t const group, duration_t duration);

    /// Delays all tasks of a group with a random duration between min and max.
    TaskScheduler& DelayGroup(group_t const group,
        std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->DelayGroup(group, ::randtime(min, max));
    }

    /// Reschedule all tasks with a given duration.
    TaskScheduler& RescheduleAll(duration_t duration);

    /// Reschedule all tasks with a random duration between min and max.
    TaskScheduler& RescheduleAll(std::chrono::milliseconds min, std::chrono::milliseconds max)
    {
        return this->RescheduleAll(::randtime(min, max));
    }

    /// Reschedule all tasks of a group with the given duration.
    TaskScheduler& RescheduleGroup(group_t const group, duration_t duration);

    /// Reschedule all tasks of a group with a random duration between min and max.
    TaskScheduler& RescheduleGroup(group_t const group,
        std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->RescheduleGroup(group, ::randtime(min, max));
    }

private:
    /// Insert a new task to the enqueued tasks.
    TaskScheduler& InsertTask(TaskContainer task);

    TaskScheduler& ScheduleAt(timepoint_t end,
        duration_t time, task_handler_t task);

    /// Schedule an event with a fixed rate.
    /// Never call this from within a task context! Use TaskContext::schedule instead!
    TaskScheduler& ScheduleAt(timepoint_t end,
        duration_t time,
        group_t const group, task_handler_t task);

    /// Dispatch remaining tasks
    void Dispatch(success_t const& callback);
};

class TC_COMMON_API TaskContext
{
    friend class TaskScheduler;

    /// Associated task
    TaskScheduler::TaskContainer _task;

    /// Owner
    std::weak_ptr<TaskScheduler> _owner;

    /// Marks the task as consumed
    std::shared_ptr<bool> _consumed;

    /// Dispatches an action safe on the TaskScheduler
    TaskContext& Dispatch(std::function<TaskScheduler&(TaskScheduler&)> const& apply);

public:
    // Empty constructor
    TaskContext()
        : _task(), _owner(), _consumed(std::make_shared<bool>(true)) { }

    // Construct from task and owner
    explicit TaskContext(TaskScheduler::TaskContainer&& task, std::weak_ptr<TaskScheduler>&& owner)
        : _task(std::move(task)), _owner(std::move(owner)), _consumed(std::make_shared<bool>(false)) { }

    // Copy construct
    TaskContext(TaskContext const& right) = default;

    // Move construct
    TaskContext(TaskContext&& right) noexcept = default;

    // Copy assign
    TaskContext& operator=(TaskContext const& right) = default;

    // Move assign
    TaskContext& operator=(TaskContext&& right) noexcept = default;

    ~TaskContext() = default;

    /// Returns true if the owner was deallocated and this context has expired.
    bool IsExpired() const;

    /// Returns true if the event is in the given group
    bool IsInGroup(TaskScheduler::group_t const group) const;

    /// Sets the event in the given group
    TaskContext& SetGroup(TaskScheduler::group_t const group);

    /// Removes the group from the event
    TaskContext& ClearGroup();

    /// Returns the repeat counter which increases every time the task is repeated.
    TaskScheduler::repeated_t GetRepeatCounter() const;

    /// Repeats the event and sets a new duration.
    /// std::chrono::seconds(5) for example.
    /// This will consume the task context, its not possible to repeat the task again
    /// from the same task context!
    TaskContext& Repeat(TaskScheduler::duration_t duration);

    /// Repeats the event with the same duration.
    /// This will consume the task context, its not possible to repeat the task again
    /// from the same task context!
    TaskContext& Repeat()
    {
        return Repeat(_task->_duration);
    }

    /// Repeats the event and set a new duration that is randomized between min and max.
    /// std::chrono::seconds(5) for example.
    /// This will consume the task context, its not possible to repeat the task again
    /// from the same task context!
    TaskContext& Repeat(std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->Repeat(::randtime(min, max));
    }

    /// Schedule a callable function that is executed at the next update tick from within the context.
    /// Its safe to modify the TaskScheduler from within the callable.
    TaskContext& Async(std::function<void()> const& callable);

    /// Schedule an event with a fixed rate from within the context.
    /// Its possible that the new event is executed immediately!
    /// Use TaskScheduler::Async to create a task
    /// which will be called at the next update tick.
    TaskContext& Schedule(TaskScheduler::duration_t time,
        TaskScheduler::task_handler_t task);

    /// Schedule an event with a fixed rate from within the context.
    /// Its possible that the new event is executed immediately!
    /// Use TaskScheduler::Async to create a task
    /// which will be called at the next update tick.
    TaskContext& Schedule(TaskScheduler::duration_t time,
        TaskScheduler::group_t const group, TaskScheduler::task_handler_t task);

    /// Schedule an event with a randomized rate between min and max rate from within the context.
    /// Its possible that the new event is executed immediately!
    /// Use TaskScheduler::Async to create a task
    /// which will be called at the next update tick.
    TaskContext& Schedule(std::chrono::milliseconds min,
        std::chrono::milliseconds max, TaskScheduler::task_handler_t task)
    {
        return this->Schedule(::randtime(min, max), std::move(task));
    }

    /// Schedule an event with a randomized rate between min and max rate from within the context.
    /// Its possible that the new event is executed immediately!
    /// Use TaskScheduler::Async to create a task
    /// which will be called at the next update tick.
    TaskContext& Schedule(std::chrono::milliseconds min,
        std::chrono::milliseconds max, TaskScheduler::group_t const group,
        TaskScheduler::task_handler_t task)
    {
        return this->Schedule(::randtime(min, max), group, std::move(task));
    }

    /// Cancels all tasks from within the context.
    TaskContext& CancelAll();

    /// Cancel all tasks of a single group from within the context.
    TaskContext& CancelGroup(TaskScheduler::group_t const group);

    /// Cancels all groups in the given std::vector from within the context.
    /// Hint: Use std::initializer_list for this: "{1, 2, 3, 4}"
    TaskContext& CancelGroupsOf(std::vector<TaskScheduler::group_t> const& groups);

    /// Delays all tasks with the given duration from within the context.
    TaskContext& DelayAll(TaskScheduler::duration_t duration);

    /// Delays all tasks with a random duration between min and max from within the context.
    TaskContext& DelayAll(std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->DelayAll(::randtime(min, max));
    }

    /// Delays all tasks of a group with the given duration from within the context.
    TaskContext& DelayGroup(TaskScheduler::group_t const group, TaskScheduler::duration_t duration);

    /// Delays all tasks of a group with a random duration between min and max from within the context.
    TaskContext& DelayGroup(TaskScheduler::group_t const group,
        std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->DelayGroup(group, ::randtime(min, max));
    }

    /// Reschedule all tasks with the given duration.
    TaskContext& RescheduleAll(TaskScheduler::duration_t duration);

    /// Reschedule all tasks with a random duration between min and max.
    TaskContext& RescheduleAll(std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->RescheduleAll(::randtime(min, max));
    }

    /// Reschedule all tasks of a group with the given duration.
    TaskContext& RescheduleGroup(TaskScheduler::group_t const group, TaskScheduler::duration_t duration);

    /// Reschedule all tasks of a group with a random duration between min and max.
    TaskContext& RescheduleGroup(TaskScheduler::group_t const group,
        std::chrono::milliseconds min,
        std::chrono::milliseconds max)
    {
        return this->RescheduleGroup(group, ::randtime(min, max));
    }

private:
    /// Asserts if the task was consumed already.
    void AssertOnConsumed() const;

    /// Invokes the associated hook of the task.
    void Invoke();
};

#endif /// TRINITYCORE_TASK_SCHEDULER_H