sp_tmr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include "precompile.h"
  2. #include "sp_tmr.h"
  3. #include "sp_def.h"
  4. #include "sp_svc.h"
  5. #include "sp_rsn.h"
  6. #include "list.h"
  7. #include "array.h"
  8. #include "spinlock.h"
  9. #include "refcnt.h"
  10. struct sp_tmr_t
  11. {
  12. struct list_head entry;
  13. timer_entry tm_entry;
  14. sp_tmr_callback cb;
  15. void *tag;
  16. strand_t *strand;
  17. int tm_error;
  18. sp_tmr_mgr_t *mgr;
  19. int in_schedule;
  20. sp_rsn_context_t rsn_ctx;
  21. spinlock_t lock;
  22. DECLARE_REF_COUNT_MEMBER(ref_cnt);
  23. };
  24. DECLARE_REF_COUNT_STATIC(sp_tmr, sp_tmr_t)
  25. struct sp_tmr_mgr_t
  26. {
  27. struct list_head tm_list;
  28. CRITICAL_SECTION lock;
  29. int tmr_cnt;
  30. int stop;
  31. sp_svc_t *svc;
  32. };
  33. static void tmr_lock(sp_tmr_t *timer)
  34. {
  35. spinlock_enter(&timer->lock, -1);
  36. }
  37. static void tmr_unlock(sp_tmr_t *timer)
  38. {
  39. spinlock_leave(&timer->lock);
  40. }
  41. static void mgr_lock(sp_tmr_mgr_t *mgr)
  42. {
  43. EnterCriticalSection(&mgr->lock);
  44. }
  45. static void mgr_unlock(sp_tmr_mgr_t *mgr)
  46. {
  47. LeaveCriticalSection(&mgr->lock);
  48. }
  49. int sp_tmr_create(sp_tmr_mgr_t *mgr, strand_t *strand, sp_tmr_callback *cb, sp_tmr_t **p_timer)
  50. {
  51. sp_tmr_t *timer;
  52. if (mgr->stop)
  53. return Error_Unexpect;
  54. timer = ZALLOC_T(sp_tmr_t);
  55. timer->mgr = mgr;
  56. timer->in_schedule = 0;
  57. timer->tm_error = 0;
  58. timer->strand = strand;
  59. if (strand)
  60. strand_inc_ref(strand);
  61. memcpy(&timer->cb, cb, sizeof(sp_tmr_callback));
  62. REF_COUNT_INIT(&timer->ref_cnt);
  63. spinlock_init(&timer->lock);
  64. mgr_lock(mgr);
  65. list_add_tail(&timer->entry, &mgr->tm_list);
  66. mgr->tmr_cnt ++;
  67. mgr_unlock(mgr);
  68. *p_timer = timer;
  69. return 0;
  70. }
  71. void sp_tmr_destroy(sp_tmr_t *timer)
  72. {
  73. sp_tmr_dec_ref(timer);
  74. }
  75. static void __sp_tmr_destroy(sp_tmr_t *timer)
  76. {
  77. if (timer->cb.on_destroy)
  78. timer->cb.on_destroy(timer, timer->cb.user_data);
  79. mgr_lock(timer->mgr);
  80. list_del(&timer->entry);
  81. timer->mgr->tmr_cnt--;
  82. mgr_unlock(timer->mgr);
  83. if (timer->strand)
  84. strand_dec_ref(timer->strand);
  85. free(timer);
  86. }
  87. IMPLEMENT_REF_COUNT_MT_STATIC(sp_tmr, sp_tmr_t, ref_cnt, __sp_tmr_destroy)
  88. static void on_timer(threadpool_t *threadpool, void *arg)
  89. {
  90. sp_tmr_t *timer = (sp_tmr_t *)arg;
  91. sp_tmr_mgr_t *timer_mgr = timer->mgr;
  92. sp_uid_t rsn = sp_svc_new_runserial(timer_mgr->svc);
  93. sp_rsn_context_init_original(rsn, SP_ORIGINAL_T_TIMER, &timer->rsn_ctx);
  94. sp_svc_push_runserial_context(timer_mgr->svc, &timer->rsn_ctx);
  95. tmr_lock(timer);
  96. if (timer->in_schedule) {
  97. timer->in_schedule = 0;
  98. if (timer->cb.on_timer)
  99. timer->cb.on_timer(timer, timer->tm_error, timer->cb.user_data);
  100. }
  101. tmr_unlock(timer);
  102. sp_tmr_dec_ref(timer); //@
  103. sp_svc_pop_runserial_context(timer_mgr->svc);
  104. }
  105. static void __on_timer(timer_queue_t *q, timer_entry *tm_entry, int err)
  106. {
  107. sp_tmr_t *timer = (sp_tmr_t *)tm_entry->user_data;
  108. threadpool_queue_workitem(sp_svc_get_threadpool(timer->mgr->svc), timer->strand, &on_timer, timer);
  109. }
  110. int sp_tmr_schedule(sp_tmr_t *timer, unsigned int delay)
  111. {
  112. int rc = 0;
  113. if (timer->mgr->stop)
  114. return Error_Unexpect;
  115. tmr_lock(timer);
  116. if (!timer->in_schedule) {
  117. timer->in_schedule = 1;
  118. timer->tm_entry.user_data = timer;
  119. timer->tm_entry.cb = &__on_timer;
  120. timer->tm_error = 0;
  121. sp_tmr_inc_ref(timer); //@
  122. rc = sp_iom_schedule_timer(sp_svc_get_iom(timer->mgr->svc), &timer->tm_entry, delay);
  123. if (rc != 0) {
  124. sp_tmr_dec_ref(timer); //@
  125. timer->in_schedule = 0;
  126. }
  127. } else {
  128. rc = Error_Unexpect;
  129. }
  130. tmr_unlock(timer);
  131. return rc;
  132. }
  133. int sp_tmr_cancel(sp_tmr_t *timer)
  134. {
  135. int rc = Error_Unexpect;
  136. tmr_lock(timer);
  137. if (timer->in_schedule) {
  138. timer->in_schedule = 0;
  139. rc = sp_iom_cancel_timer(sp_svc_get_iom(timer->mgr->svc),&timer->tm_entry, 0);
  140. if (rc == 0) {
  141. timer->tm_error = Error_Cancel;
  142. threadpool_queue_workitem(sp_svc_get_threadpool(timer->mgr->svc), timer->strand, &on_timer, timer); //@
  143. }
  144. }
  145. tmr_unlock(timer);
  146. return rc;
  147. }
  148. void sp_tmr_set_tag(sp_tmr_t *timer, void *tag)
  149. {
  150. timer->tag = tag;
  151. }
  152. void *sp_tmr_get_tag(sp_tmr_t *timer)
  153. {
  154. return timer->tag;
  155. }
  156. const sp_rsn_context_t *sp_tmr_get_rsn_context(sp_tmr_t *timer)
  157. {
  158. return &timer->rsn_ctx;
  159. }
  160. int sp_tmr_mgr_create(sp_svc_t *svc, sp_tmr_mgr_t **p_mgr)
  161. {
  162. sp_tmr_mgr_t *mgr = ZALLOC_T(sp_tmr_mgr_t);
  163. mgr->svc = svc;
  164. InitializeCriticalSection(&mgr->lock);
  165. mgr->stop = 0;
  166. mgr->tmr_cnt = 0;
  167. INIT_LIST_HEAD(&mgr->tm_list);
  168. *p_mgr = mgr;
  169. return 0;
  170. }
  171. void sp_tmr_mgr_destroy(sp_tmr_mgr_t *mgr)
  172. {
  173. assert(mgr->tmr_cnt == 0);
  174. DeleteCriticalSection(&mgr->lock);
  175. free(mgr);
  176. }
  177. int sp_tmr_mgr_cancel_all_tmr(sp_tmr_mgr_t *mgr)
  178. {
  179. array_header_t *arr = array_make(mgr->tmr_cnt, sizeof(sp_tmr_t*));
  180. sp_tmr_t *pos;
  181. int i;
  182. mgr->stop = 1;
  183. mgr_lock(mgr);
  184. list_for_each_entry(pos, &mgr->tm_list, sp_tmr_t, entry) {
  185. sp_tmr_inc_ref(pos);
  186. ARRAY_PUSH(arr, sp_tmr_t*) = pos;
  187. }
  188. mgr_unlock(mgr);
  189. for (i = 0; i < arr->nelts; ++i) {
  190. pos = ARRAY_IDX(arr, i, sp_tmr_t*);
  191. sp_tmr_cancel(pos);
  192. sp_tmr_dec_ref(pos);
  193. }
  194. array_free(arr);
  195. return 0;
  196. }
  197. int sp_tmr_mgr_tmr_cnt(sp_tmr_mgr_t *mgr)
  198. {
  199. return mgr->tmr_cnt;
  200. }