test_runnable.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_RUNNABLE_H_
  23. #define _TEST_RUNNABLE_H_
  24. typedef struct test_runnable_timer_s {
  25. tsk_timer_id_t id;
  26. uint64_t timeout;
  27. const char *arg;
  28. }
  29. test_runnable_timer_t;
  30. test_runnable_timer_t runnable_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. typedef struct tsk_obj_s {
  42. TSK_DECLARE_OBJECT;
  43. tsk_timer_id_t timer_id;
  44. }
  45. tsk_obj_t;
  46. static void* tsk_obj_ctor(void * self, va_list * app)
  47. {
  48. tsk_obj_t *obj = self;
  49. if(obj) {
  50. obj->timer_id = va_arg(*app, tsk_timer_id_t);
  51. }
  52. return self;
  53. }
  54. static void* tsk_obj_dtor(void * self)
  55. {
  56. return self;
  57. }
  58. static int tsk_obj_cmp(const void *obj1, const void *obj2)
  59. {
  60. return 0;
  61. }
  62. static const tsk_object_def_t tsk_obj_def_s = {
  63. sizeof(tsk_obj_t),
  64. tsk_obj_ctor,
  65. tsk_obj_dtor,
  66. tsk_obj_cmp,
  67. };
  68. const tsk_object_def_t *tsk_obj_def_t = &tsk_obj_def_s;
  69. void *run(void* self)
  70. {
  71. int i = 0;
  72. tsk_list_item_t *curr;
  73. TSK_RUNNABLE_RUN_BEGIN(self);
  74. if(curr = TSK_RUNNABLE_POP_FIRST(self)) {
  75. const tsk_obj_t *obj = (const tsk_obj_t*)curr->data;
  76. printf("\n\nRunnable event-id===>[%llu]\n\n", obj->timer_id);
  77. tsk_object_unref(curr);
  78. }
  79. TSK_RUNNABLE_RUN_END(self);
  80. return 0;
  81. }
  82. static int test_runnable_timer_callback(const void* arg, tsk_timer_id_t timer_id)
  83. {
  84. const tsk_runnable_t* runnable = arg;
  85. if(runnable) {
  86. TSK_RUNNABLE_ENQUEUE(runnable, timer_id);
  87. return 0;
  88. }
  89. return -1;
  90. }
  91. void test_runnable()
  92. {
  93. size_t i;
  94. tsk_timer_manager_handle_t *timer_mgr = tsk_timer_manager_create();
  95. tsk_runnable_t* runnable = tsk_runnable_create();
  96. runnable->run = run;
  97. printf("test_runnable//\n");
  98. tsk_timer_manager_start(timer_mgr);
  99. tsk_runnable_start(runnable, tsk_obj_def_t);
  100. for(i=0; i<sizeof(runnable_timers)/sizeof(test_runnable_timer_t); ++i) {
  101. runnable_timers[i].id = tsk_timer_manager_schedule(timer_mgr, runnable_timers[i].timeout, test_runnable_timer_callback, runnable);
  102. }
  103. tsk_thread_sleep(4000);
  104. /* Stops and frees both timer manager and runnable object */
  105. TSK_OBJECT_SAFE_FREE(runnable);
  106. TSK_OBJECT_SAFE_FREE(timer_mgr);
  107. }
  108. #endif /* _TEST_RUNNABLE_H_ */