sp_tmr.c 4.9 KB

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