Embedded Template Library 1.0
Loading...
Searching...
No Matches
callback_timer_deferred_locked.h
1/******************************************************************************
2The MIT License(MIT)
3
4Embedded Template Library.
5https://github.com/ETLCPP/etl
6https://www.etlcpp.com
7
8Copyright(c) 2025 John Wellbelove
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files(the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions :
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27******************************************************************************/
28
29#ifndef ETL_CALLBACK_TIMER_DEFERRED_LOCKED_INCLUDED
30#define ETL_CALLBACK_TIMER_DEFERRED_LOCKED_INCLUDED
31
32#include "platform.h"
33#include "callback_timer_locked.h"
34#include "etl/nullptr.h"
35#include "etl/optional.h"
36#include "priority_queue.h"
37#include "delegate.h"
38
39namespace etl
40{
41 //***************************************************************************
43 //***************************************************************************
44 template <uint_least8_t Max_Timers_, uint32_t Max_Handlers_>
46 {
47 public:
48
49 ETL_STATIC_ASSERT(Max_Timers_ <= 254U, "No more than 254 timers are allowed");
50
51 typedef icallback_timer_locked::callback_type callback_type;
52 typedef icallback_timer_locked::try_lock_type try_lock_type;
53 typedef icallback_timer_locked::lock_type lock_type;
54 typedef icallback_timer_locked::unlock_type unlock_type;
55
56 private:
57
58 typedef icallback_timer_locked::callback_node callback_node;
59
60 public:
61
62 //*******************************************
64 //*******************************************
66 : icallback_timer_locked(timer_array, Max_Timers_)
67 {
68 }
69
70 //*******************************************
72 //*******************************************
73 callback_timer_deferred_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
74 : icallback_timer_locked(timer_array, Max_Timers_)
75 {
76 this->set_locks(try_lock_, lock_, unlock_);
77 }
78
79 //*******************************************
81 //*******************************************
82 bool tick(uint32_t count) final
83 {
84 if (enabled)
85 {
86 if (try_lock())
87 {
88 // We have something to do?
89 bool has_active = !active_list.empty();
90
91 if (has_active)
92 {
93 while (has_active && (count >= active_list.front().delta))
94 {
95 timer_data& timer = active_list.front();
96
97 count -= timer.delta;
98
99 active_list.remove(timer.id, true);
100 remove_callback.call_if(timer.id);
101
102 if (timer.callback.is_valid())
103 {
104 if (!handler_queue.full())
105 {
106 handler_queue.push(callback_node(timer.callback, timer_priorities[timer.id]));
107 }
108 }
109
110 if (timer.repeating)
111 {
112 // Reinsert the timer.
113 timer.delta = timer.period;
114 active_list.insert(timer.id);
115 insert_callback.call_if(timer.id);
116 }
117
118 has_active = !active_list.empty();
119 }
120
121 if (has_active)
122 {
123 // Subtract any remainder from the next due timeout.
124 active_list.front().delta -= count;
125 }
126 }
127
128 unlock();
129
130 return true;
131 }
132 }
133
134 return false;
135 }
136
137 //*******************************************
141 //*******************************************
143 {
144 callback_type work_todo_callback;
145
146 do
147 {
148 lock();
149
150 if (handler_queue.empty())
151 {
152 work_todo_callback.clear();
153 }
154 else
155 {
156 callback_node &work_todo_callback_node = handler_queue.top();
157 work_todo_callback = work_todo_callback_node.callback;
158 handler_queue.pop();
159 }
160
161 unlock();
162
163 work_todo_callback.call_if();
164 } while (work_todo_callback.is_valid());
165 }
166
167 // Overloads
168
169 //*******************************************
171 //*******************************************
172 etl::timer::id::type register_timer(const callback_type& callback_,
173 uint32_t period_,
174 bool repeating_)
175 {
176 return register_timer(callback_, period_, repeating_, 0);
177 }
178
179 //*******************************************
184 //*******************************************
185 etl::timer::id::type register_timer(const callback_type& callback_,
186 uint32_t period_,
187 bool repeating_,
188 uint_least8_t priority_)
189 {
190 etl::timer::id::type id = icallback_timer_locked::register_timer(callback_, period_, repeating_);
191
192 if (id != etl::timer::id::NO_TIMER)
193 {
194 timer_priorities[id] = priority_;
195 }
196
197 return id;
198 }
199
200 private:
201
203 uint_least8_t timer_priorities[Max_Timers_];
204 timer_data timer_array[Max_Timers_];
205 };
206}
207
208#endif
bool tick(uint32_t count) final
Handle the tick call.
Definition callback_timer_deferred_locked.h:82
void handle_deferred(void)
Definition callback_timer_deferred_locked.h:142
callback_timer_deferred_locked()
Constructor.
Definition callback_timer_deferred_locked.h:65
callback_timer_deferred_locked(try_lock_type try_lock_, lock_type lock_, unlock_type unlock_)
Constructor.
Definition callback_timer_deferred_locked.h:73
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_, uint_least8_t priority_)
Definition callback_timer_deferred_locked.h:185
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_)
Register a timer.
Definition callback_timer_deferred_locked.h:172
Definition callback_timer_locked.h:359
Interface for callback timer.
Definition callback_timer_locked.h:49
icallback_timer_locked(timer_data *const timer_array_, const uint_least8_t Max_Timers_)
Constructor.
Definition callback_timer_locked.h:442
void set_locks(try_lock_type try_lock_, lock_type lock_, lock_type unlock_)
Sets the lock and unlock delegates.
Definition callback_timer_locked.h:263
etl::timer::id::type register_timer(const callback_type &callback_, uint32_t period_, bool repeating_)
Register a timer.
Definition callback_timer_locked.h:62
Definition priority_queue.h:461
bitset_ext
Definition absolute.h:39
The configuration of a timer.
Definition callback_timer_locked.h:378
Definition timer.h:88
Common definitions for the timer framework.
Definition timer.h:55