etl/include/etl/callback_timer_atomic.h
David Hebbeker 3392a6cd88
add static assert with auto message (#740)
* Resolve all C++ -O2 warnings

Resolve all C++ -O2 warnings

Resolve all C++ -O2 warnings

* Updated release notes and version

* Made unit test suite names unique

* Added etl::bit_v for C++17

* add stringify macro (#741)

* Add include/linux/stringify.h from Linux kernel 2.6.12-rc2

(cherry picked from commit 1da177e4c3)

* Make __stringify support variable argument macros too

For example:

  __stringify(__entry->irq, __entry->ret)

will now convert it to:

  "REC->irq, REC->ret"

It also still supports single arguments as the old macro did.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
Acked-by: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
LKML-Reference: <49DC6751.30308@cn.fujitsu.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
(cherry picked from commit 8f7c2c3731)

* Add documentation.

* Adjust names in order to satisfy naming convention.

* Use __VA_ARGS__ instead a GNU extension as this works with more compilers.

Works with the newest versions of ICCAVR, GCC, CLANG and MSCV. C++11 may be required.

* Adjust to ETL folder structure.

* Change include guard to the one usual in ETL.

* Add definition guard for STRINGIFY.

---------

Co-authored-by: Linus Torvalds <torvalds@ppc970.osdl.org>
Co-authored-by: Zhaolei <zhaolei@cn.fujitsu.com>

* Updated stringify macro for variadic parameters

* Edited comments

* Added multi char type strcmp, strncmp, strcpy & strncpy

* Added support for self transitions

* Remove some unreachable code in bitset_new.h (#743)

Warning[Pe111]: statement is unreachable

* Re-enabled tests for etl::string_ext

* Changes require for non-virtual messages

* MutexMessageRouter example

* Allow C++03 delegate in etl::callback_timer

* #745 Assertion triggered in hash.h when compiling for MSP430

* Removed redundant #includes

* Removed unused variable

Minor format edit

* Added time_to_next() status function

* Added 'sentence case' versions of constants

* Added C++03 compatible etl::move or etl::move_backward

* Added c++03 zero parameter emplace member functions

* Fix issue of release of an object on empty an pool

* Remove used variable

* Remove used variable

* Change to require semicolons after debug count macros.

Added error assert for vector reserve greater than capacity.

* Added destructor to CMSIS_RTOS2 mutex

* Updated version and release notes

* Removed semicolon warnings

* Updated vesrion and release notes

* #753 [porting] IAR's overload resolution struggles with etl::span::span(etl::array&)

#754 ambiguous etl::bit_stream_reader::bit_stream_reader() for some etl::span

* #755 warning about constexpr in array.h

* Re-enabled tests

* const_reference at(size_t i) const is only constexpr from C++14

* Added conversion operators to std::span

* MacOS clang span issue

* MacOS clang span issue

* MacOS clang span issue

* MacOS clang span issue

* MacOS clang span issue

* Updated version and release notes

* Possible fix for #756

* Fixed: move assignment not working for non-empty containers (#759)

* Updated release notes

* Make "private" includes relativ (#761)

Avoid "include not found" when indirectly including etl-header

Co-authored-by: Andreas Pelczer <pelczer@stoerk-tronic.com>

* Make "private" includes relativ (#761)

Avoid "include not found" when indirectly including etl-header

Co-authored-by: Andreas Pelczer <pelczer@stoerk-tronic.com>

* Remove BOM from random.h (#762)

random.h is the only file with UTF8-BOM and IAR can not handle it.
For compatibility reasons it should be removed.

Co-authored-by: Andreas Pelczer <pelczer@stoerk-tronic.com>

* Enabled etl::overload for C++11 & C++14

* Added conversion operators to std::span

MacOS clang span issue

MacOS clang span issue

MacOS clang span issue

MacOS clang span issue

MacOS clang span issue

* Updated version and release notes

* Possible fix for #756

* Fixed: move assignment not working for non-empty containers (#759)

* Updated release notes

* Make "private" includes relativ (#761)

Avoid "include not found" when indirectly including etl-header

Co-authored-by: Andreas Pelczer <pelczer@stoerk-tronic.com>

* Remove BOM from random.h (#762)

random.h is the only file with UTF8-BOM and IAR can not handle it.
For compatibility reasons it should be removed.

Co-authored-by: Andreas Pelczer <pelczer@stoerk-tronic.com>

* Enabled etl::overload for C++11 & C++14

* Commets added

* #766 etl::unique_ptr::reset() can call deleter with nullptr argument

* Refactored storage for etl::optional

* Updated release notes

* Updated release notes

* Test name updates

* Fixed alignment functionality

* Updated versions

* Updated versions

* Initial code

* Final changes

* Fix maybe-uninitialized warning

* Fix maybe-uninitialized warning

* Added etl::string::fill()

* Added etl::string interface to etl::base64

* static_cast for string_view & array_view size() member functions

* Return raw pointer for `etl::multi_span::operator->()`. (#773)

- using `operator*()` in case getting the current element is changed one day
- using `&` to get the address

Simply returning `p_value` may break in case the internal type of `p_value` would change. This way it is more robust to changes.

* Added -> operator test

* #773 Return raw pointer for etl::multi_span::operator->()

* static_cast for string_view & array_view size() member functions

* Initial code

* Final changes

* Fix maybe-uninitialized warning

* Added etl::string::fill()

* Added etl::string interface to etl::base64

* Add missing semicolons (#776)

* #776 Add missing semicolons

* moved BUILD_TESTS check outside of root cmake check to allow building tests in submodules (#777)

added check for GNUC >= 11 for -Wstringop-overread ignore pragma since it isn't introduced until gcc/g++-11

Co-authored-by: Josh <joshua.martens@magna.com>

* Try to fix library error for Github Actions for Clang C++20

* Try to fix library error for Github Actions for Clang C++20

* Added pul-request branches to CI

* Removed temporary fix

* Refactored swap

* Added add_lvalue_reference for rvalue reference

* Revert "Added add_lvalue_reference for rvalue reference"

This reverts commit a09efffb8fadb0c25923fe5435b7b1f9bdd673f1.

* Updated  version and release notes

* Updated  version and release notes

* Try to fix library error for Github Actions for Clang C++20

(cherry picked from commit f208eb4073c98b3c190b17a73db3b61aa35fae25)

The line got lost in 95c7f44cc61d51406a70ccb9048724f9fbdd78f9.

* Add helper macro which enables function-like macro overload with 1 or 2 arguments.

Inspired by https://stackoverflow.com/a/11763277/

Warning: This requires support for variadic macros (`__VA_ARGS__`). These were officially added in C99 and C++11.

* Add unit test for static_assert.h

(cherry picked from commit 91e7f46effac2884de80a12ab0c7eda5ade5cabe)

* Add static assert overload for only 1 argument.

(cherry picked from commit ae8c19e8cba69ca209a13109a7225ce6281d945d)

* Add test case for static_assert with only 1 argument.

(cherry picked from commit 4d6e7a63094221feec22fe3addcccd903b2d81de)

* Spelling.

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
Co-authored-by: David Hebbeker <dhebbeker@users.noreply.github.com>
Co-authored-by: Linus Torvalds <torvalds@ppc970.osdl.org>
Co-authored-by: Zhaolei <zhaolei@cn.fujitsu.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.com>
Co-authored-by: Soma Veszelovszki <soma.veszelovszki@gmail.com>
Co-authored-by: Andreas <and1p@web.de>
Co-authored-by: Andreas Pelczer <pelczer@stoerk-tronic.com>
Co-authored-by: H1MSK <ksda47832338@Outlook.com>
Co-authored-by: jmartens-42 <jmartens@jayhawk.us>
Co-authored-by: Josh <joshua.martens@magna.com>
Co-authored-by: John Wellbelove <john.wellbelove@etlcpp.co.uk>
2024-02-22 19:11:38 +00:00

617 lines
16 KiB
C++

/******************************************************************************
The MIT License(MIT)
Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com
Copyright(c) 2021 John Wellbelove
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/
#ifndef ETL_CALLBACK_TIMER_ATOMIC_INCLUDED
#define ETL_CALLBACK_TIMER_ATOMIC_INCLUDED
#include "platform.h"
#include "algorithm.h"
#include "nullptr.h"
#include "function.h"
#include "static_assert.h"
#include "timer.h"
#include "error_handler.h"
#include "placement_new.h"
#include "delegate.h"
#include <stdint.h>
namespace etl
{
//***************************************************************************
/// Interface for callback timer
//***************************************************************************
template <typename TSemaphore>
class icallback_timer_atomic
{
public:
typedef etl::delegate<void(void)> callback_type;
//*******************************************
/// Register a timer.
//*******************************************
etl::timer::id::type register_timer(callback_type callback_,
uint32_t period_,
bool repeating_)
{
etl::timer::id::type id = etl::timer::id::NO_TIMER;
bool is_space = (number_of_registered_timers < MAX_TIMERS);
if (is_space)
{
// Search for the free space.
for (uint_least8_t i = 0U; i < MAX_TIMERS; ++i)
{
timer_data& timer = timer_array[i];
if (timer.id == etl::timer::id::NO_TIMER)
{
// Create in-place.
new (&timer) timer_data(i, callback_, period_, repeating_);
++number_of_registered_timers;
id = i;
break;
}
}
}
return id;
}
//*******************************************
/// Unregister a timer.
//*******************************************
bool unregister_timer(etl::timer::id::type id_)
{
bool result = false;
if (id_ != etl::timer::id::NO_TIMER)
{
timer_data& timer = timer_array[id_];
if (timer.id != etl::timer::id::NO_TIMER)
{
if (timer.is_active())
{
++process_semaphore;
active_list.remove(timer.id, false);
--process_semaphore;
}
// Reset in-place.
new (&timer) timer_data();
--number_of_registered_timers;
result = true;
}
}
return result;
}
//*******************************************
/// Enable/disable the timer.
//*******************************************
void enable(bool state_)
{
enabled = state_;
}
//*******************************************
/// Get the enable/disable state.
//*******************************************
bool is_running() const
{
return enabled;
}
//*******************************************
/// Clears the timer of data.
//*******************************************
void clear()
{
++process_semaphore;
active_list.clear();
--process_semaphore;
for (uint8_t i = 0U; i < MAX_TIMERS; ++i)
{
::new (&timer_array[i]) timer_data();
}
number_of_registered_timers = 0;
}
//*******************************************
// Called by the timer service to indicate the
// amount of time that has elapsed since the last successful call to 'tick'.
// Returns true if the tick was processed,
// false if not.
//*******************************************
bool tick(uint32_t count)
{
if (enabled)
{
if (process_semaphore == 0U)
{
// We have something to do?
bool has_active = !active_list.empty();
if (has_active)
{
while (has_active && (count >= active_list.front().delta))
{
timer_data& timer = active_list.front();
count -= timer.delta;
active_list.remove(timer.id, true);
if (timer.callback.is_valid())
{
// Call the delegate callback.
timer.callback();
}
if (timer.repeating)
{
// Reinsert the timer.
timer.delta = timer.period;
active_list.insert(timer.id);
}
has_active = !active_list.empty();
}
if (has_active)
{
// Subtract any remainder from the next due timeout.
active_list.front().delta -= count;
}
}
return true;
}
}
return false;
}
//*******************************************
/// Starts a timer.
//*******************************************
bool start(etl::timer::id::type id_, bool immediate_ = false)
{
bool result = false;
// Valid timer id?
if (id_ != etl::timer::id::NO_TIMER)
{
timer_data& timer = timer_array[id_];
// Registered timer?
if (timer.id != etl::timer::id::NO_TIMER)
{
// Has a valid period.
if (timer.period != etl::timer::state::Inactive)
{
++process_semaphore;
if (timer.is_active())
{
active_list.remove(timer.id, false);
}
timer.delta = immediate_ ? 0U : timer.period;
active_list.insert(timer.id);
--process_semaphore;
result = true;
}
}
}
return result;
}
//*******************************************
/// Stops a timer.
//*******************************************
bool stop(etl::timer::id::type id_)
{
bool result = false;
// Valid timer id?
if (id_ != etl::timer::id::NO_TIMER)
{
timer_data& timer = timer_array[id_];
// Registered timer?
if (timer.id != etl::timer::id::NO_TIMER)
{
if (timer.is_active())
{
++process_semaphore;
active_list.remove(timer.id, false);
--process_semaphore;
}
result = true;
}
}
return result;
}
//*******************************************
/// Sets a timer's period.
//*******************************************
bool set_period(etl::timer::id::type id_, uint32_t period_)
{
if (stop(id_))
{
timer_array[id_].period = period_;
return true;
}
return false;
}
//*******************************************
/// Sets a timer's mode.
//*******************************************
bool set_mode(etl::timer::id::type id_, bool repeating_)
{
if (stop(id_))
{
timer_array[id_].repeating = repeating_;
return true;
}
return false;
}
//*******************************************
/// Get the time to the next timer event.
//*******************************************
uint32_t time_to_next() const
{
++process_semaphore;
uint32_t delta = active_list.front().delta;
--process_semaphore;
return delta;
}
protected:
//*************************************************************************
/// The configuration of a timer.
struct timer_data
{
//*******************************************
timer_data()
: callback()
, period(0U)
, delta(etl::timer::state::Inactive)
, id(etl::timer::id::NO_TIMER)
, previous(etl::timer::id::NO_TIMER)
, next(etl::timer::id::NO_TIMER)
, repeating(true)
{
}
//*******************************************
/// ETL delegate callback
//*******************************************
timer_data(etl::timer::id::type id_,
callback_type callback_,
uint32_t period_,
bool repeating_)
: callback(callback_)
, period(period_)
, delta(etl::timer::state::Inactive)
, id(id_)
, previous(etl::timer::id::NO_TIMER)
, next(etl::timer::id::NO_TIMER)
, repeating(repeating_)
{
}
//*******************************************
/// Returns true if the timer is active.
//*******************************************
bool is_active() const
{
return delta != etl::timer::state::Inactive;
}
//*******************************************
/// Sets the timer to the inactive state.
//*******************************************
void set_inactive()
{
delta = etl::timer::state::Inactive;
}
callback_type callback;
uint32_t period;
uint32_t delta;
etl::timer::id::type id;
uint_least8_t previous;
uint_least8_t next;
bool repeating;
private:
// Disabled.
timer_data(const timer_data& other) ETL_DELETE;
timer_data& operator =(const timer_data& other) ETL_DELETE;
};
//*******************************************
/// Constructor.
//*******************************************
icallback_timer_atomic(timer_data* const timer_array_, const uint_least8_t MAX_TIMERS_)
: timer_array(timer_array_)
, active_list(timer_array_)
, enabled(false)
, process_semaphore(0U)
, number_of_registered_timers(0U)
, MAX_TIMERS(MAX_TIMERS_)
{
}
private:
//*************************************************************************
/// A specialised intrusive linked list for timer data.
//*************************************************************************
class timer_list
{
public:
//*******************************
timer_list(timer_data* ptimers_)
: head(etl::timer::id::NO_TIMER)
, tail(etl::timer::id::NO_TIMER)
, current(etl::timer::id::NO_TIMER)
, ptimers(ptimers_)
{
}
//*******************************
bool empty() const
{
return head == etl::timer::id::NO_TIMER;
}
//*******************************
// Inserts the timer at the correct delta position
//*******************************
void insert(etl::timer::id::type id_)
{
timer_data& timer = ptimers[id_];
if (head == etl::timer::id::NO_TIMER)
{
// No entries yet.
head = id_;
tail = id_;
timer.previous = etl::timer::id::NO_TIMER;
timer.next = etl::timer::id::NO_TIMER;
}
else
{
// We already have entries.
etl::timer::id::type test_id = begin();
while (test_id != etl::timer::id::NO_TIMER)
{
timer_data& test = ptimers[test_id];
// Find the correct place to insert.
if (timer.delta <= test.delta)
{
if (test.id == head)
{
head = timer.id;
}
// Insert before test.
timer.previous = test.previous;
test.previous = timer.id;
timer.next = test.id;
// Adjust the next delta to compensate.
test.delta -= timer.delta;
if (timer.previous != etl::timer::id::NO_TIMER)
{
ptimers[timer.previous].next = timer.id;
}
break;
}
else
{
timer.delta -= test.delta;
}
test_id = next(test_id);
}
// Reached the end?
if (test_id == etl::timer::id::NO_TIMER)
{
// Tag on to the tail.
ptimers[tail].next = timer.id;
timer.previous = tail;
timer.next = etl::timer::id::NO_TIMER;
tail = timer.id;
}
}
}
//*******************************
void remove(etl::timer::id::type id_, bool has_expired)
{
timer_data& timer = ptimers[id_];
if (head == id_)
{
head = timer.next;
}
else
{
ptimers[timer.previous].next = timer.next;
}
if (tail == id_)
{
tail = timer.previous;
}
else
{
ptimers[timer.next].previous = timer.previous;
}
if (!has_expired)
{
// Adjust the next delta.
if (timer.next != etl::timer::id::NO_TIMER)
{
ptimers[timer.next].delta += timer.delta;
}
}
timer.previous = etl::timer::id::NO_TIMER;
timer.next = etl::timer::id::NO_TIMER;
timer.delta = etl::timer::state::Inactive;
}
//*******************************
timer_data& front()
{
return ptimers[head];
}
//*******************************
const timer_data& front() const
{
return ptimers[head];
}
//*******************************
etl::timer::id::type begin()
{
current = head;
return current;
}
//*******************************
etl::timer::id::type previous(etl::timer::id::type last)
{
current = ptimers[last].previous;
return current;
}
//*******************************
etl::timer::id::type next(etl::timer::id::type last)
{
current = ptimers[last].next;
return current;
}
//*******************************
void clear()
{
etl::timer::id::type id = begin();
while (id != etl::timer::id::NO_TIMER)
{
timer_data& timer = ptimers[id];
id = next(id);
timer.next = etl::timer::id::NO_TIMER;
}
head = etl::timer::id::NO_TIMER;
tail = etl::timer::id::NO_TIMER;
current = etl::timer::id::NO_TIMER;
}
private:
etl::timer::id::type head;
etl::timer::id::type tail;
etl::timer::id::type current;
timer_data* const ptimers;
};
// The array of timer data structures.
timer_data* const timer_array;
// The list of active timers.
timer_list active_list;
bool enabled;
mutable TSemaphore process_semaphore;
uint_least8_t number_of_registered_timers;
public:
const uint_least8_t MAX_TIMERS;
};
//***************************************************************************
/// The callback timer
//***************************************************************************
template <uint_least8_t MAX_TIMERS_, typename TSemaphore>
class callback_timer_atomic : public etl::icallback_timer_atomic<TSemaphore>
{
public:
ETL_STATIC_ASSERT(MAX_TIMERS_ <= 254U, "No more than 254 timers are allowed");
//*******************************************
/// Constructor.
//*******************************************
callback_timer_atomic()
: icallback_timer_atomic<TSemaphore>(timer_array, MAX_TIMERS_)
{
}
private:
typename etl::icallback_timer_atomic<TSemaphore>::timer_data timer_array[MAX_TIMERS_];
};
}
#endif