You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.1KB

  1. #include "sip-timer.h"
  2. #include "aio-timeout.h"
  3. #include "sys/system.h"
  4. #include "sys/thread.h"
  5. #include <stdlib.h>
  6. static pthread_t s_threads[2];
  7. static int s_running;
  8. static int STDCALL sip_timer_run(void* param)
  9. {
  10. volatile int* running = (int*)param;
  11. while (*running)
  12. {
  13. aio_timeout_process();
  14. system_sleep(5);
  15. }
  16. return 0;
  17. }
  18. void sip_timer_init(void)
  19. {
  20. int i;
  21. s_running = 1;
  22. for(i = 0; i < sizeof(s_threads)/sizeof(s_threads[0]); i++)
  23. {
  24. thread_create(s_threads+i, sip_timer_run, &s_running);
  25. }
  26. }
  27. void sip_timer_cleanup(void)
  28. {
  29. int i;
  30. s_running = 0;
  31. for (i = 0; i < sizeof(s_threads) / sizeof(s_threads[0]); i++)
  32. {
  33. thread_destroy(s_threads[i]);
  34. }
  35. }
  36. sip_timer_t sip_timer_start(int timeout, sip_timer_handle handler, void* usrptr)
  37. {
  38. struct aio_timeout_t* t;
  39. t = calloc(1, sizeof(struct aio_timeout_t));
  40. if (0 == aio_timeout_start(t, timeout, handler, usrptr))
  41. return t;
  42. free(t);
  43. return NULL;
  44. }
  45. int sip_timer_stop(sip_timer_t* id)
  46. {
  47. int r;
  48. struct aio_timeout_t* t;
  49. if (NULL == id || NULL == *id)
  50. return -1;
  51. t = (struct aio_timeout_t*)*id;
  52. r = aio_timeout_stop(t);
  53. free(t);
  54. *id = NULL;
  55. return r;
  56. }