ptimer.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #ifndef __PTIMER_H__
  2. #define __PTIMER_H__
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdarg.h>
  9. #include <error.h>
  10. #include <errno.h>
  11. #include <time.h>
  12. #include <signal.h>
  13. #include <pthread.h>
  14. #include <sys/time.h> //struct timeval
  15. #include "tc_system.h"
  16. // CLOCK_REALTIME :Systemwide realtime clock.
  17. // CLOCK_MONOTONIC:Represents monotonic time. Cannot be set.
  18. // CLOCK_PROCESS_CPUTIME_ID :High resolution per-process timer.
  19. // CLOCK_THREAD_CPUTIME_ID :Thread-specific timer.
  20. // CLOCK_REALTIME_HR :High resolution version of CLOCK_REALTIME.
  21. // CLOCK_MONOTONIC_HR :High resolution version of CLOCK_MONOTONIC.
  22. // union sigval
  23. // {
  24. // int sival_int; //integer value
  25. // void *sival_ptr; //pointer value
  26. // }
  27. // struct sigevent
  28. // {
  29. // int sigev_notify; //notification type
  30. // int sigev_signo; //signal number
  31. // union sigval sigev_value; //signal value
  32. // void (*sigev_notify_function)(union sigval);
  33. // pthread_attr_t *sigev_notify_attributes;
  34. // }
  35. // 通过将evp->sigev_notify设定为如下值来定制定时器到期后的行为:
  36. // SIGEV_NONE:什么都不做,只提供通过timer_gettime和timer_getoverrun查询超时信息。
  37. // SIGEV_SIGNAL: 当定时器到期,内核会将sigev_signo所指定的信号传送给进程。在信号处理程序中,si_value会被设定会sigev_value。
  38. // SIGEV_THREAD: 当定时器到期,内核会(在此进程内)以sigev_notification_attributes为线程属性创建一个线程,并且让它执行sigev_notify_function,传入sigev_value作为为一个参数。
  39. // sigev_signo的值是SIGALRM以及sigev_value的值是定时器的标识符
  40. // struct timespec{
  41. // time_t tv_sec;
  42. // long tv_nsec;
  43. // };
  44. // struct itimespec{
  45. // struct timespec it_interval;
  46. // struct timespec it_value;
  47. // };
  48. // int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
  49. // int timer_settime(timer_t timerid, int flags, const struct itimerspec *value, struct itimerspec *ovalue);
  50. // int timer_gettime(timer_t timerid, struct itimerspec *value);
  51. // int timer_getoverrun(timer_t timerid);
  52. // int timer_delete (timer_t timerid);
  53. typedef struct ptimer
  54. {
  55. timer_t timerid;
  56. struct sigevent evp;
  57. struct itimerspec it;
  58. struct itimerspec save;
  59. void *priv;
  60. void (*timeout_callback)(void* priv);
  61. }ptimer_t;
  62. void com_ptimer_init(ptimer_t *ptimer, const char* type, void (*timeout_callback)(void* priv), void *priv);
  63. void com_ptimer_reset(ptimer_t *ptimer, int start_ms, int interval_ms);
  64. void com_ptimer_exit(ptimer_t *ptimer);
  65. #endif//__PTIMER_H__