__utils.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-27 flybreak the first version.
  9. */
  10. #pragma once
  11. #include <cstdlib>
  12. #include <system_error>
  13. #include <chrono>
  14. #include <ratio>
  15. #include <rtthread.h>
  16. #define RT_USING_CPP_EXCEPTION
  17. inline void throw_system_error(int err, const char *what_msg)
  18. {
  19. #ifdef RT_USING_CPP_EXCEPTION
  20. throw std::system_error(std::error_code(err, std::system_category()), what_msg);
  21. #else
  22. (void)err;
  23. (void)what_msg;
  24. ::abort();
  25. #endif
  26. }
  27. class tick_clock
  28. {
  29. public:
  30. typedef clock_t rep;
  31. typedef std::ratio<1, RT_TICK_PER_SECOND> period;
  32. typedef std::chrono::duration<tick_clock::rep, tick_clock::period> duration;
  33. typedef std::chrono::time_point<tick_clock> time_point;
  34. constexpr static bool is_ready = true;
  35. static time_point now();
  36. };
  37. class real_time_clock
  38. {
  39. public:
  40. typedef std::chrono::nanoseconds duration;
  41. typedef duration::rep rep;
  42. typedef duration::period period;
  43. typedef std::chrono::time_point<real_time_clock, duration> time_point;
  44. static constexpr bool is_steady = true;
  45. static time_point
  46. now() noexcept;
  47. };