timer.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <common/glib.h>
  3. #include <driver/timers/HPET/HPET.h>
  4. #include <driver/timers/rtc/rtc.h>
  5. // 定义LONG_MAX为最大超时时间 - 允许负数
  6. #define MAX_TIMEOUT (int64_t)((1ul << 63) - 1)
  7. uint64_t volatile timer_jiffies = 0; // 系统时钟计数
  8. // 计算接下来n毫秒对应的系统时间片
  9. #define cal_next_n_ms_jiffies(expire_ms) (timer_jiffies + 1000 * (expire_ms))
  10. // 计算接下来n微秒对应的系统时间片
  11. #define cal_next_n_us_jiffies(expire_us) (timer_jiffies + (expire_us))
  12. void timer_init();
  13. void do_timer_softirq(void *data);
  14. /**
  15. * @brief 定时功能队列
  16. *
  17. */
  18. struct timer_func_list_t
  19. {
  20. struct List list;
  21. uint64_t expire_jiffies;
  22. void (*func)(void *data);
  23. void *data;
  24. };
  25. extern struct timer_func_list_t timer_func_head;
  26. /**
  27. * @brief 初始化定时功能
  28. *
  29. * @param timer_func 队列结构体
  30. * @param func 定时功能处理函数
  31. * @param data 传输的数据
  32. * @param expire_ms 定时时长(单位:ms)
  33. */
  34. void timer_func_init(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_ms);
  35. /**
  36. * @brief 初始化定时功能
  37. *
  38. * @param timer_func 队列结构体
  39. * @param func 定时功能处理函数
  40. * @param data 传输的数据
  41. * @param expire_us 定时时长(单位:us)
  42. */
  43. void timer_func_init_us(struct timer_func_list_t *timer_func, void (*func)(void *data), void *data, uint64_t expire_us);
  44. /**
  45. * @brief 将定时功能添加到列表中
  46. *
  47. * @param timer_func 待添加的定时功能
  48. */
  49. void timer_func_add(struct timer_func_list_t *timer_func);
  50. /**
  51. * @brief 将定时功能从列表中删除
  52. *
  53. * @param timer_func
  54. */
  55. void timer_func_del(struct timer_func_list_t *timer_func);
  56. uint64_t clock();
  57. /**
  58. * @brief 睡眠timeout的时间之后唤醒进程/线程
  59. *
  60. * @param timeout
  61. * @return long
  62. */
  63. long schedule_timeout_ms(long timeout);