summaryrefslogtreecommitdiffstats
path: root/src/stw_timer.cpp
blob: 3146c9a51e68d8d5d3a43a48de7ea930569d21f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stw_timer.h"

/* todo :

1. add jitter support 
2. in case ticks take too much time add a quata and keep the time in the current bucket then speed up 

hhaim
*/


void CTimerObj::Dump(FILE *fd){

    fprintf(fd,"m_rotation_count        :%lu \n", (ulong)m_rotation_count);
    fprintf(fd,"m_last_update_tick      :%lu \n", (ulong)m_last_update_tick);
    fprintf(fd,"m_aging_ticks           :%lu \n", (ulong)m_aging_ticks);
}


void  CTimerWheelBucket::dump_link_list(void *userdata,tw_on_tick_cb_t cb,FILE *fd){


    CTimerWheelLink  *bucket, *next;
    CTimerObj *tmr;


    bucket = m_active_bucket;

    tmr = (CTimerObj *)bucket->stw_next;
    bool found=false;
    if ((CTimerWheelLink *)tmr != bucket) {
        fprintf(fd,"[%lu,\n",(ulong)m_bucket_index);
        found=true;
    }

    while( (CTimerWheelLink *)tmr != bucket) {

        next = (CTimerWheelLink *)tmr->m_links.stw_next;

        tmr->Dump(fd);
        cb(userdata,tmr);

        tmr = (CTimerObj *)next;
    }
    if (found){
        fprintf(fd,"]\n");
    }
}


bool CTimerWheelBucket::do_tick(void *userdata,
                                tw_on_tick_cb_t cb,
                                int32_t limit){

    
    CTimerObj * tmr;
    int cnt=0;
    while (  true ) {
        tmr = timer_tick_get_next();
        if (!tmr) {
            break;
        }
        cb(userdata,tmr);
        cnt++;
        if (cnt>limit && (limit>0)) {
            return(false);
        }
    }
    timer_tick();
    return(true);
}


void CTimerWheelBucket::timer_stats_dump(FILE *fd){
    fprintf(fd,"wheel_size         :%lu \n", (ulong)m_wheel_size);
    fprintf(fd,"ticks              :%lu \n", (ulong)m_ticks);
    fprintf(fd,"bucket_index       :%lu \n", (ulong)m_bucket_index);
    fprintf(fd,"timer_active       :%lu \n", (ulong)m_timer_active);
    fprintf(fd,"timer_expired      :%lu \n", (ulong)m_timer_expired);
    fprintf(fd,"timer_hiwater_mark :%lu \n", (ulong)m_timer_hiwater_mark);
    fprintf(fd,"timer_starts       :%lu \n", (ulong)m_timer_starts);
    fprintf(fd,"timer_cancelled    :%lu \n", (ulong)m_timer_cancelled);
    fprintf(fd,"m_timer_restart    :%lu \n", (ulong)m_timer_restart);
    
}


RC_STW_t CTimerWheelBucket::timer_stop (CTimerObj *tmr)
{
    CTimerWheelLink *next, *prev;

#ifdef TW_DEBUG 

    if (this == 0) {
        return (RC_STW_NULL_WHEEL);
    }

    if (tmr == 0) {
        return (RC_STW_NULL_TMR);
    }

    if (m_magic_tag != MAGIC_TAG ) {
        return (RC_STW_INVALID_WHEEL);
    }

#endif

    next = tmr->m_links.stw_next;
    if (next) {
        prev = tmr->m_links.stw_prev;
        next->stw_prev = prev;
        prev->stw_next = next;
        tmr->m_links.stw_next = 0;    /* 0 == tmr is free */
        tmr->m_links.stw_prev = 0;

        /*
         * stats bookkeeping
         */
        m_timer_active--;
        m_timer_cancelled++;
    }
    return (RC_STW_OK);
}

RC_STW_t CTimerWheelBucket::Delete() {
    uint32_t  j;
    CTimerWheelLink *spoke;

    CTimerObj *tmr;
    if (this == 0) {
        return (RC_STW_NULL_WHEEL);
    }

    if (m_magic_tag != MAGIC_TAG ) {
        return (RC_STW_INVALID_WHEEL);
    }

    for (j = 0; j < m_wheel_size; j++) {
        spoke = &m_buckets[j];

        tmr = (CTimerObj *)spoke->stw_next;

        while ( (CTimerWheelLink *)tmr != spoke) {
            timer_stop(tmr);
            tmr = (CTimerObj *)spoke->stw_next;
        } /* end while */

    } /* end for */

    /*
     * clear the magic so we do not mistakenly access this wheel
     */
    m_magic_tag = 0;

    /*
     * now free the wheel structures
     */
    free(m_buckets);
    m_buckets=0;

    return (RC_STW_OK);
}

RC_STW_t CTimerWheelBucket::Create(uint32_t wheel_size){
    uint32_t j;
    CTimerWheelLink *bucket;

    if (wheel_size < STW_MIN_WHEEL_SIZE || wheel_size > STW_MAX_WHEEL_SIZE) {
        return (RC_STW_INVALID_WHEEL_SIZE);
    }

    m_buckets = (CTimerWheelLink *)malloc(wheel_size * sizeof(CTimerWheelLink));
    if (m_buckets == 0) {
        return (RC_STW_NO_RESOURCES);
    }

    m_magic_tag = MAGIC_TAG;
    m_ticks = 0;
    m_bucket_index = 0;
    m_wheel_size  = wheel_size;

    m_timer_hiwater_mark  = 0;
    m_timer_active = 0;
    m_timer_cancelled=0;
    m_timer_expired=0;
    m_timer_starts=0;
    m_timer_restart=0;

    bucket = &m_buckets[0];
	m_active_bucket=bucket;
    m_active_tick_timer = m_active_bucket;
    /* link list point to itself */
    for (j = 0; j < wheel_size; j++) {
        bucket->stw_next = bucket;    
        bucket->stw_prev = bucket;
        bucket++;
    }
    return (RC_STW_OK);
}