123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #include <winpr/synch.h>
- #include "precompile.h"
- #include "sp_tmr.h"
- #include "sp_def.h"
- #include "sp_svc.h"
- #include "sp_rsn.h"
- #include "list.h"
- #include "array.h"
- #include "spinlock.h"
- #include "refcnt.h"
- #include "memutil.h"
- #include "dbgutil.h"
- #define TAG SPBASE_TAG("sp_tmr")
- struct sp_tmr_t
- {
- struct list_head entry;
- timer_entry tm_entry;
- sp_tmr_callback cb;
- void *tag;
- strand_t *strand;
- int tm_error;
- sp_tmr_mgr_t *mgr;
- int in_schedule;
- sp_rsn_context_t rsn_ctx;
- spinlock_t lock;
- DECLARE_REF_COUNT_MEMBER(ref_cnt);
- };
- DECLARE_REF_COUNT_STATIC(sp_tmr, sp_tmr_t)
- struct sp_tmr_mgr_t
- {
- struct list_head tm_list;
- CRITICAL_SECTION lock;
- int tmr_cnt;
- int stop;
- sp_svc_t *svc;
- };
- static void tmr_lock(sp_tmr_t *timer)
- {
- spinlock_enter(&timer->lock, -1);
- }
- static void tmr_unlock(sp_tmr_t *timer)
- {
- spinlock_leave(&timer->lock);
- }
- static void mgr_lock(sp_tmr_mgr_t *mgr)
- {
- EnterCriticalSection(&mgr->lock);
- }
- static void mgr_unlock(sp_tmr_mgr_t *mgr)
- {
- LeaveCriticalSection(&mgr->lock);
- }
- int sp_tmr_create(sp_tmr_mgr_t *mgr, strand_t *strand, sp_tmr_callback *cb, sp_tmr_t **p_timer)
- {
- sp_tmr_t *timer;
- if (mgr->stop)
- return Error_Unexpect;
- timer = ZALLOC_T(sp_tmr_t);
- timer->mgr = mgr;
- timer->in_schedule = 0;
- timer->tm_error = 0;
- timer->strand = strand;
- if (strand)
- strand_inc_ref(strand);
- memcpy(&timer->cb, cb, sizeof(sp_tmr_callback));
- REF_COUNT_INIT(&timer->ref_cnt);
- spinlock_init(&timer->lock);
- mgr_lock(mgr);
- list_add_tail(&timer->entry, &mgr->tm_list);
- mgr->tmr_cnt ++;
- mgr_unlock(mgr);
- *p_timer = timer;
- WLog_DBG(TAG, "tmr object created!");
- return 0;
- }
- void sp_tmr_destroy(sp_tmr_t *timer)
- {
- sp_tmr_dec_ref(timer);
- }
- static void __sp_tmr_destroy(sp_tmr_t *timer)
- {
- if (timer->cb.on_destroy)
- timer->cb.on_destroy(timer, timer->cb.user_data);
- mgr_lock(timer->mgr);
- list_del(&timer->entry);
- timer->mgr->tmr_cnt--;
- mgr_unlock(timer->mgr);
- if (timer->strand)
- strand_dec_ref(timer->strand);
-
- free(timer);
- }
- IMPLEMENT_REF_COUNT_MT_STATIC(sp_tmr, sp_tmr_t, ref_cnt, __sp_tmr_destroy)
- static void on_timer(threadpool_t *threadpool, void *arg)
- {
- sp_tmr_t *timer = (sp_tmr_t *)arg;
- sp_tmr_mgr_t *timer_mgr = timer->mgr;
- sp_uid_t rsn = sp_svc_new_runserial(timer_mgr->svc);
- sp_rsn_context_init_original(rsn, SP_ORIGINAL_T_TIMER, &timer->rsn_ctx);
- sp_svc_push_runserial_context(timer_mgr->svc, &timer->rsn_ctx);
- tmr_lock(timer);
- if (timer->in_schedule) {
- timer->in_schedule = 0;
- if (timer->cb.on_timer)
- timer->cb.on_timer(timer, timer->tm_error, timer->cb.user_data);
- }
- tmr_unlock(timer);
- sp_tmr_dec_ref(timer); //@
- sp_svc_pop_runserial_context(timer_mgr->svc);
- }
- static void __on_timer(timer_queue_t *q, timer_entry *tm_entry, int err)
- {
- sp_tmr_t *timer = (sp_tmr_t *)tm_entry->user_data;
- threadpool_queue_workitem(sp_svc_get_threadpool(timer->mgr->svc), timer->strand, &on_timer, timer);
- }
- int sp_tmr_schedule(sp_tmr_t *timer, unsigned int delay)
- {
- int rc = 0;
- if (timer->mgr->stop)
- return Error_Unexpect;
- tmr_lock(timer);
- if (!timer->in_schedule) {
- timer->in_schedule = 1;
- timer->tm_entry.user_data = timer;
- timer->tm_entry.cb = &__on_timer;
- timer->tm_error = 0;
- sp_tmr_inc_ref(timer); //@
- rc = sp_iom_schedule_timer(sp_svc_get_iom(timer->mgr->svc), &timer->tm_entry, delay);
- if (rc != 0) {
- sp_tmr_dec_ref(timer); //@
- timer->in_schedule = 0;
- }
- } else {
- rc = Error_Unexpect;
- }
- tmr_unlock(timer);
- return rc;
- }
- int sp_tmr_cancel(sp_tmr_t *timer)
- {
- int rc = Error_Unexpect;
- tmr_lock(timer);
- if (timer->in_schedule) {
- timer->in_schedule = 0;
- rc = sp_iom_cancel_timer(sp_svc_get_iom(timer->mgr->svc),&timer->tm_entry, 0);
- if (rc == 0) {
- timer->tm_error = Error_Cancel;
- threadpool_queue_workitem(sp_svc_get_threadpool(timer->mgr->svc), timer->strand, &on_timer, timer); //@
- }
- }
- tmr_unlock(timer);
- return rc;
- }
- void sp_tmr_set_tag(sp_tmr_t *timer, void *tag)
- {
- timer->tag = tag;
- }
- void *sp_tmr_get_tag(sp_tmr_t *timer)
- {
- return timer->tag;
- }
- const sp_rsn_context_t *sp_tmr_get_rsn_context(sp_tmr_t *timer)
- {
- return &timer->rsn_ctx;
- }
- int sp_tmr_mgr_create(sp_svc_t *svc, sp_tmr_mgr_t **p_mgr)
- {
- sp_tmr_mgr_t *mgr = ZALLOC_T(sp_tmr_mgr_t);
- mgr->svc = svc;
- InitializeCriticalSection(&mgr->lock);
- mgr->stop = 0;
- mgr->tmr_cnt = 0;
- INIT_LIST_HEAD(&mgr->tm_list);
- *p_mgr = mgr;
- return 0;
- }
- void sp_tmr_mgr_destroy(sp_tmr_mgr_t *mgr)
- {
- TOOLKIT_ASSERT(mgr->tmr_cnt == 0);
- DeleteCriticalSection(&mgr->lock);
- free(mgr);
- }
- int sp_tmr_mgr_cancel_all_tmr(sp_tmr_mgr_t *mgr)
- {
- array_header_t *arr = array_make(mgr->tmr_cnt, sizeof(sp_tmr_t*));
- sp_tmr_t *pos;
- int i;
- mgr->stop = 1;
- mgr_lock(mgr);
- list_for_each_entry(pos, &mgr->tm_list, sp_tmr_t, entry) {
- sp_tmr_inc_ref(pos);
- ARRAY_PUSH(arr, sp_tmr_t*) = pos;
- }
- mgr_unlock(mgr);
- for (i = 0; i < arr->nelts; ++i) {
- pos = ARRAY_IDX(arr, i, sp_tmr_t*);
- sp_tmr_cancel(pos);
- sp_tmr_dec_ref(pos);
- }
- array_free(arr);
- return 0;
- }
- int sp_tmr_mgr_tmr_cnt(sp_tmr_mgr_t *mgr)
- {
- return mgr->tmr_cnt;
- }
|