__utils.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. inline void throw_system_error(int err, const char *what_msg)
  17. {
  18. #ifdef RT_USING_CPP_EXCEPTIONS
  19. throw std::system_error(std::error_code(err, std::system_category()), what_msg);
  20. #else
  21. (void)err;
  22. (void)what_msg;
  23. ::abort();
  24. #endif
  25. }
  26. class tick_clock
  27. {
  28. public:
  29. typedef clock_t rep;
  30. typedef std::ratio<1, RT_TICK_PER_SECOND> period;
  31. typedef std::chrono::duration<tick_clock::rep, tick_clock::period> duration;
  32. typedef std::chrono::time_point<tick_clock> time_point;
  33. constexpr static bool is_ready = true;
  34. static time_point now();
  35. };
  36. class real_time_clock
  37. {
  38. public:
  39. typedef std::chrono::nanoseconds duration;
  40. typedef duration::rep rep;
  41. typedef duration::period period;
  42. typedef std::chrono::time_point<real_time_clock, duration> time_point;
  43. static constexpr bool is_steady = true;
  44. static time_point
  45. now() noexcept;
  46. };