Module: kamailio
Branch: master
Commit: 0e8a67b83a3ccb1113107402d9dda11b9829eaaa
URL: 
https://github.com/kamailio/kamailio/commit/0e8a67b83a3ccb1113107402d9dda11b9829eaaa

Author: Daniel-Constantin Mierla <[email protected]>
Committer: Daniel-Constantin Mierla <[email protected]>
Date: 2016-01-26T00:35:57+01:00

core: secondary timer implemented as classic wheel timer (wtimer)

---

Modified: timer_proc.c

---

Diff:  
https://github.com/kamailio/kamailio/commit/0e8a67b83a3ccb1113107402d9dda11b9829eaaa.diff
Patch: 
https://github.com/kamailio/kamailio/commit/0e8a67b83a3ccb1113107402d9dda11b9829eaaa.patch

---

diff --git a/timer_proc.c b/timer_proc.c
index ba87e7e..975c1fb 100644
--- a/timer_proc.c
+++ b/timer_proc.c
@@ -284,8 +284,9 @@ int fork_sync_utimer(int child_id, char* desc, int 
make_sock,
 typedef struct sr_wtimer_node {
        struct sr_wtimer_node *next;
        uint32_t interval;  /* frequency of execution (secs) */
-       uint32_t steps;     /* interval = loops * SR_WTIMER_SIZE + steps */
+       uint32_t steps;     /* init: interval = loops * SR_WTIMER_SIZE + steps 
*/
        uint32_t loops;
+       uint32_t eloop;
        timer_function* f;
        void* param;
 } sr_wtimer_node_t;
@@ -337,6 +338,7 @@ int sr_wtimer_add(timer_function* f, void* param, int 
interval)
        wt->interval = interval;
        wt->steps = interval % SR_WTIMER_SIZE;
        wt->loops = interval / SR_WTIMER_SIZE;
+       wt->eloop = wt->loops;
        wt->next = _sr_wtimer->wlist[wt->steps];
        _sr_wtimer->wlist[wt->steps] = wt;
 
@@ -346,11 +348,28 @@ int sr_wtimer_add(timer_function* f, void* param, int 
interval)
 /**
  *
  */
+int sr_wtimer_reinsert(uint32_t cs, sr_wtimer_node_t *wt)
+{
+       uint32_t ts;
+
+       ts = (cs + wt->interval) % SR_WTIMER_SIZE;
+       wt->eloop = wt->interval / SR_WTIMER_SIZE;
+       wt->next = _sr_wtimer->wlist[ts];
+       _sr_wtimer->wlist[ts] = wt;
+
+       return 0;
+}
+
+/**
+ *
+ */
 void sr_wtimer_exec(unsigned int ticks, void *param)
 {
        sr_wtimer_node_t *wt;
-       uint32_t i;
-       uint32_t c;
+       sr_wtimer_node_t *wn;
+       sr_wtimer_node_t *wp;
+       uint32_t cs;
+       uint32_t cl;
 
        if(_sr_wtimer==NULL) {
                LM_ERR("wtimer not intialized\n");
@@ -358,17 +377,29 @@ void sr_wtimer_exec(unsigned int ticks, void *param)
        }
 
        _sr_wtimer->itimer++;
-       c = _sr_wtimer->itimer / SR_WTIMER_SIZE;
-
-       for(i=1; i<=SR_WTIMER_SIZE; i++) {
-               if(_sr_wtimer->itimer % i == 0) {
-                       for(wt=_sr_wtimer->wlist[i % SR_WTIMER_SIZE];
-                                       wt!=NULL; wt = wt->next) {
-                               if(wt->loops==0 || (c % wt->loops==0)) {
-                                       wt->f(ticks, wt->param);
-                               }
+       cs = _sr_wtimer->itimer % SR_WTIMER_SIZE;
+       cl = _sr_wtimer->itimer / SR_WTIMER_SIZE;
+       LM_DBG("wtimer - loop: %u - slot: %u\n", cl, cs);
+
+       wp = NULL;
+       wt=_sr_wtimer->wlist[cs];
+       while(wt) {
+               wn = wt->next;
+               if(wt->eloop==0) {
+                       /* execute timer callback function */
+                       wt->f(ticks, wt->param);
+                       /* extract and reinsert timer item */
+                       if(wp==NULL) {
+                               _sr_wtimer->wlist[cs] = wn;
+                       } else {
+                               wp->next = wn;
                        }
+                       sr_wtimer_reinsert(cs, wt);
+               } else {
+                       wt->eloop--;
+                       wp = wt;
                }
+               wt = wn;
        }
 }
 


_______________________________________________
sr-dev mailing list
[email protected]
http://lists.sip-router.org/cgi-bin/mailman/listinfo/sr-dev

Reply via email to