sp_tmr.c 4.8 KB

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