test_timer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (C) 2009 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #ifndef _TEST_TIMER_H_
  23. #define _TEST_TIMER_H_
  24. typedef struct test_timer_s {
  25. tsk_timer_id_t id;
  26. uint64_t timeout;
  27. const char *arg;
  28. }
  29. test_timer_t;
  30. test_timer_t timers[] = {
  31. {0, 2000, "3"},
  32. {1, 2500, "4"},
  33. {2, 500, "1"},
  34. {3, 1000, "2"},
  35. {4, 1000, "2"},
  36. {5, 0, "0"},
  37. {6, 10000, "6"},
  38. {7, 3000, "5"},
  39. {8, 2500, "4"},
  40. };
  41. static int test_timer_callback(const void* arg, tsk_timer_id_t timer_id)
  42. {
  43. // Do quick job
  44. printf("test_timer - id=%llu and arg=%s//\n", timer_id, arg);
  45. return 0;
  46. }
  47. void test_global_timer()
  48. {
  49. size_t i;
  50. tsk_timer_mgr_global_ref();
  51. // for test: start it two times
  52. tsk_timer_mgr_global_start();
  53. tsk_timer_mgr_global_start();
  54. for(i=0; i<sizeof(timers)/sizeof(test_timer_t); ++i) {
  55. timers[i].id = tsk_timer_mgr_global_schedule(timers[i].timeout, test_timer_callback, timers[i].arg);
  56. }
  57. tsk_timer_mgr_global_cancel(timers[6].id);
  58. tsk_timer_mgr_global_cancel(timers[2].id);
  59. tsk_thread_sleep(4000);
  60. // for test: stop it only one time
  61. tsk_timer_mgr_global_stop();
  62. tsk_timer_mgr_global_stop();
  63. tsk_timer_mgr_global_unref();
  64. }
  65. void test_single_timer()
  66. {
  67. size_t i;
  68. tsk_timer_manager_handle_t *handle = tsk_timer_manager_create();
  69. printf("test_timer//\n");
  70. tsk_timer_manager_start(handle);
  71. for(i=0; i<sizeof(timers)/sizeof(test_timer_t); ++i) {
  72. timers[i].id = tsk_timer_manager_schedule(handle, timers[i].timeout, test_timer_callback, timers[i].arg);
  73. }
  74. tsk_timer_manager_cancel(handle, timers[6].id);
  75. tsk_timer_manager_cancel(handle, timers[2].id);
  76. tsk_thread_sleep(4000);
  77. /* Stops and frees the timer manager */
  78. TSK_OBJECT_SAFE_FREE(handle);
  79. }
  80. void test_timer()
  81. {
  82. //test_single_timer();
  83. test_global_timer();
  84. }
  85. #endif /* _TEST_TIMER_H_ */