Line data Source code
1 : /**
2 : Copyright (c) 2021 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #include "XLScheduler.h"
25 :
26 : namespace STAPPLER_VERSIONIZED stappler::xenolith {
27 :
28 21 : Scheduler::Scheduler() { }
29 :
30 42 : Scheduler::~Scheduler() {
31 21 : unscheduleAll();
32 42 : }
33 :
34 21 : bool Scheduler::init() {
35 21 : return true;
36 : }
37 :
38 2409 : void Scheduler::unschedule(const void *ptr) {
39 2409 : if (_currentTarget == ptr) {
40 0 : _currentNode->removed = true;
41 : } else {
42 2409 : _list.erase(ptr);
43 : }
44 2409 : }
45 :
46 42 : void Scheduler::unscheduleAll() {
47 42 : _list.clear();
48 42 : _tmp.clear();
49 42 : }
50 :
51 2409 : void Scheduler::schedulePerFrame(SchedulerFunc &&callback, void *target, int32_t priority, bool paused) {
52 2409 : if (_locked) {
53 : // prevent to insert new callbacks when update in progress
54 945 : _tmp.emplace_back(ScheduledTemporary{move(callback), target, priority, paused});
55 : } else {
56 1464 : _list.emplace(target, priority, move(callback), paused);
57 : }
58 2409 : }
59 :
60 9538 : void Scheduler::update(const UpdateTime &time) {
61 9538 : _locked = true;
62 :
63 9538 : _list.foreach([&, this] (void *target, int64_t priority, SchedulerCallback &cb) {
64 306228 : _currentTarget = target;
65 306228 : _currentNode = &cb;
66 306228 : if (!cb.paused && cb.callback) {
67 269801 : cb.callback(time);
68 : }
69 306228 : _currentNode = nullptr;
70 306228 : _currentTarget = nullptr;
71 306228 : return cb.removed;
72 : });
73 :
74 9538 : _locked = false;
75 75575 : for (auto &it : _tmp) {
76 66037 : _list.emplace(it.target, it.priority, move(it.callback), it.paused);
77 : }
78 9538 : }
79 :
80 777 : bool Scheduler::isPaused(void *ptr) const {
81 777 : if (auto v = _list.find(ptr)) {
82 777 : return v->paused;
83 : }
84 0 : return false;
85 : }
86 :
87 777 : void Scheduler::resume(void *ptr) {
88 777 : if (auto v = _list.find(ptr)) {
89 777 : v->paused = false;
90 : }
91 777 : }
92 :
93 105 : void Scheduler::pause(void *ptr) {
94 105 : if (auto v = _list.find(ptr)) {
95 105 : v->paused = true;
96 : }
97 105 : }
98 :
99 21 : bool Scheduler::empty() const {
100 21 : return _list.empty() && _tmp.empty();
101 : }
102 :
103 : }
|