timer.c 21.7 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10 11 12
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-12     Bernard      first version
 * 2006-04-29     Bernard      implement thread timer
 * 2006-06-04     Bernard      implement rt_timer_control
 * 2006-08-10     Bernard      fix the periodic timer bug
 * 2006-09-03     Bernard      implement rt_timer_detach
13
 * 2009-11-11     LiJin        add soft timer
14
 * 2010-05-12     Bernard      fix the timer check bug.
15
 * 2010-11-02     Charlie      re-implement tick overflow issue
16
 * 2012-12-15     Bernard      fix the next timeout issue in soft timer
17
 * 2014-07-12     Bernard      does not lock scheduler when invoking soft-timer
18
 *                             timeout function.
Thomas_Fly's avatar
Thomas_Fly 已提交
19
 * 2021-08-15     supperthomas add the comment
20 21 22 23 24
 */

#include <rtthread.h>
#include <rthw.h>

25
/* hard timer list */
26
static rt_list_t rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
27

28
#ifdef RT_USING_TIMER_SOFT
B
Bernard Xiong 已提交
29 30 31 32

#define RT_SOFT_TIMER_IDLE              1
#define RT_SOFT_TIMER_BUSY              0

33
#ifndef RT_TIMER_THREAD_STACK_SIZE
D
dzzxzz@gmail.com 已提交
34
#define RT_TIMER_THREAD_STACK_SIZE     512
35
#endif /* RT_TIMER_THREAD_STACK_SIZE */
36 37

#ifndef RT_TIMER_THREAD_PRIO
D
dzzxzz@gmail.com 已提交
38
#define RT_TIMER_THREAD_PRIO           0
39
#endif /* RT_TIMER_THREAD_PRIO */
40

B
Bernard Xiong 已提交
41 42
/* soft timer status */
static rt_uint8_t soft_timer_status = RT_SOFT_TIMER_IDLE;
43
/* soft timer list */
44
static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
45 46 47
static struct rt_thread timer_thread;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
48
#endif /* RT_USING_TIMER_SOFT */
49

50
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
51 52
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
53 54
static void (*rt_timer_enter_hook)(struct rt_timer *timer);
static void (*rt_timer_exit_hook)(struct rt_timer *timer);
55 56 57 58

/**
 * @addtogroup Hook
 */
D
dzzxzz 已提交
59

D
dogandog 已提交
60
/**@{*/
61 62

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
63
 * @brief This function will set a hook function on timer,
Thomas_Fly's avatar
Thomas_Fly 已提交
64
 * which will be invoked when enter timer timeout callback function.
Thomas_Fly's avatar
Thomas_Fly 已提交
65
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
66
 * @param hook the function point of timer
67
 */
68
void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
69
{
70 71 72 73
    rt_timer_enter_hook = hook;
}

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
74
 * @brief This function will set a hook function, which will be
Thomas_Fly's avatar
Thomas_Fly 已提交
75
 * invoked when exit * timer timeout callback function.
Thomas_Fly's avatar
Thomas_Fly 已提交
76
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
77
 * @param hook the function point of timer
78 79 80 81
 */
void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
{
    rt_timer_exit_hook = hook;
82 83
}

D
dogandog 已提交
84
/**@}*/
85
#endif /* RT_USING_HOOK */
86

Thomas_Fly's avatar
Thomas_Fly 已提交
87 88 89

/**
 * @brief [internal] the init funtion of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
90
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
91
 * the internal called function of rt_timer_init
Thomas_Fly's avatar
Thomas_Fly 已提交
92
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
93
 * @see rt_timer_init
Thomas_Fly's avatar
Thomas_Fly 已提交
94
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
95 96 97 98 99 100
 * @param timer the static timer object
 * @param timeout the timeout function
 * @param parameter the parameter of timeout function
 * @param time the tick of timer
 * @param flag the flag of timer
 */
101
static void _rt_timer_init(rt_timer_t timer,
D
dzzxzz@gmail.com 已提交
102 103 104 105
                           void (*timeout)(void *parameter),
                           void      *parameter,
                           rt_tick_t  time,
                           rt_uint8_t flag)
106
{
107 108
    int i;

109 110
    /* set flag */
    timer->parent.flag  = flag;
111

112 113
    /* set deactivated */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
114

115 116
    timer->timeout_func = timeout;
    timer->parameter    = parameter;
117

118 119
    timer->timeout_tick = 0;
    timer->init_tick    = time;
120

121
    /* initialize timer list */
122 123 124 125
    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        rt_list_init(&(timer->row[i]));
    }
126 127
}

Thomas_Fly's avatar
Thomas_Fly 已提交
128 129
/**
 * @brief  find the next emtpy timer
Thomas_Fly's avatar
Thomas_Fly 已提交
130
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
131
 * @param timer_list the timer of the next timeout
Thomas_Fly's avatar
Thomas_Fly 已提交
132
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
133 134
 * @return rt_tick_t the point of timer
 */
135
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
136
{
137
    struct rt_timer *timer;
138 139
    register rt_base_t level;
    rt_tick_t timeout_tick = RT_TICK_MAX;
D
dzzxzz@gmail.com 已提交
140

141 142
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
B
Bernard Xiong 已提交
143

144 145 146 147 148 149 150 151 152
    if (!rt_list_isempty(&timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
    {
        timer = rt_list_entry(timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                              struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
        timeout_tick = timer->timeout_tick;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);
D
dzzxzz@gmail.com 已提交
153

154
    return timeout_tick;
155 156
}

Thomas_Fly's avatar
Thomas_Fly 已提交
157
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
158 159
 * @brief remove the timer
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
160 161
 * @param timer the point of timer
 */
162 163 164 165 166 167 168 169 170 171
rt_inline void _rt_timer_remove(rt_timer_t timer)
{
    int i;

    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        rt_list_remove(&timer->row[i]);
    }
}

G
Grissiom 已提交
172
#if RT_DEBUG_TIMER
Thomas_Fly's avatar
Thomas_Fly 已提交
173 174
/**
 * @brief the number of timer
Thomas_Fly's avatar
Thomas_Fly 已提交
175 176
 *
 * @param timer
Thomas_Fly's avatar
Thomas_Fly 已提交
177 178
 * @return int the count
 */
179 180 181 182 183 184 185 186 187 188 189
static int rt_timer_count_height(struct rt_timer *timer)
{
    int i, cnt = 0;

    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        if (!rt_list_isempty(&timer->row[i]))
            cnt++;
    }
    return cnt;
}
Thomas_Fly's avatar
Thomas_Fly 已提交
190 191
/**
 * @brief dump the all timer information
Thomas_Fly's avatar
Thomas_Fly 已提交
192
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
193 194
 * @param timer_heads the head of timer
 */
195 196 197 198
void rt_timer_dump(rt_list_t timer_heads[])
{
    rt_list_t *list;

199 200
    for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
         list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
201 202 203 204
         list = list->next)
    {
        struct rt_timer *timer = rt_list_entry(list,
                                               struct rt_timer,
205
                                               row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
206 207 208 209
        rt_kprintf("%d", rt_timer_count_height(timer));
    }
    rt_kprintf("\n");
}
210
#endif /* RT_DEBUG_TIMER */
211

212 213 214
/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
215

D
dogandog 已提交
216
/**@{*/
217 218

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
219 220
 * @brief This function will initialize a timer
 *        normally this function is used to initialize a static timer object.
221 222 223 224 225 226 227
 * @param timer the static timer object
 * @param name the name of timer
 * @param timeout the timeout function
 * @param parameter the parameter of timeout function
 * @param time the tick of timer
 * @param flag the flag of timer
 */
D
dzzxzz@gmail.com 已提交
228
void rt_timer_init(rt_timer_t  timer,
229
                   const char *name,
D
dzzxzz@gmail.com 已提交
230 231 232 233
                   void (*timeout)(void *parameter),
                   void       *parameter,
                   rt_tick_t   time,
                   rt_uint8_t  flag)
234
{
235 236
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
237

238
    /* timer object initialization */
239
    rt_object_init(&(timer->parent), RT_Object_Class_Timer, name);
240

241
    _rt_timer_init(timer, timeout, parameter, time, flag);
242
}
243
RTM_EXPORT(rt_timer_init);
244

245
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
246
 * @brief This function will detach a timer from timer management.
Thomas_Fly's avatar
Thomas_Fly 已提交
247
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
248 249
 * @param timer the timer to be detached
 * @return rt_err_t RT_EOK
250 251 252
 */
rt_err_t rt_timer_detach(rt_timer_t timer)
{
253
    register rt_base_t level;
254

255 256
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
257 258
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent));
259

260 261
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
262

263
    _rt_timer_remove(timer);
B
Bernard Xiong 已提交
264 265
    /* stop timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
266

267 268
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
269

270
    rt_object_detach(&(timer->parent));
271

272
    return RT_EOK;
273
}
274
RTM_EXPORT(rt_timer_detach);
275 276 277

#ifdef RT_USING_HEAP
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
278
 * @brief This function will create a timer
279 280 281 282 283 284 285 286 287
 *
 * @param name the name of timer
 * @param timeout the timeout function
 * @param parameter the parameter of timeout function
 * @param time the tick of timer
 * @param flag the flag of timer
 *
 * @return the created timer object
 */
D
dzzxzz@gmail.com 已提交
288 289 290 291 292
rt_timer_t rt_timer_create(const char *name,
                           void (*timeout)(void *parameter),
                           void       *parameter,
                           rt_tick_t   time,
                           rt_uint8_t  flag)
293
{
294
    struct rt_timer *timer;
295

296 297 298 299 300 301
    /* allocate a object */
    timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
    if (timer == RT_NULL)
    {
        return RT_NULL;
    }
302

303
    _rt_timer_init(timer, timeout, parameter, time, flag);
304

305
    return timer;
306
}
307
RTM_EXPORT(rt_timer_create);
308 309

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
310
 * @brief This function will delete a timer and release timer memory
311 312 313 314 315 316 317
 *
 * @param timer the timer to be deleted
 *
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_delete(rt_timer_t timer)
{
318
    register rt_base_t level;
319

320 321
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
322 323
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
324

325 326
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
327

328
    _rt_timer_remove(timer);
B
Bernard Xiong 已提交
329 330
    /* stop timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
331

332 333
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
334

335
    rt_object_delete(&(timer->parent));
336

337
    return RT_EOK;
338
}
339
RTM_EXPORT(rt_timer_delete);
340
#endif /* RT_USING_HEAP */
341 342

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
343
 * @brief This function will start the timer
344 345 346
 *
 * @param timer the timer to be started
 *
B
bernard.xiong@gmail.com 已提交
347
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
348 349 350
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
351
    unsigned int row_lvl;
352
    rt_list_t *timer_list;
353
    register rt_base_t level;
354 355 356
    rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
    unsigned int tst_nr;
    static unsigned int random_nr;
357

358 359
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
360
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
361

362 363 364
    /* stop timer firstly */
    level = rt_hw_interrupt_disable();
    /* remove timer from list */
365 366 367
    _rt_timer_remove(timer);
    /* change status of timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
368

369
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
370

D
dzzxzz@gmail.com 已提交
371 372 373 374 375
    /*
     * get timeout tick,
     * the max timeout tick shall not great than RT_TICK_MAX/2
     */
    RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
376
    timer->timeout_tick = rt_tick_get() + timer->init_tick;
377

378
#ifdef RT_USING_TIMER_SOFT
379 380 381
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* insert timer to soft timer list */
382
        timer_list = rt_soft_timer_list;
383 384
    }
    else
385
#endif /* RT_USING_TIMER_SOFT */
386 387
    {
        /* insert timer to system timer list */
388
        timer_list = rt_timer_list;
389 390
    }

391 392
    row_head[0]  = &timer_list[0];
    for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
393
    {
394
        for (; row_head[row_lvl] != timer_list[row_lvl].prev;
395
             row_head[row_lvl]  = row_head[row_lvl]->next)
396
        {
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
            struct rt_timer *t;
            rt_list_t *p = row_head[row_lvl]->next;

            /* fix up the entry pointer */
            t = rt_list_entry(p, struct rt_timer, row[row_lvl]);

            /* If we have two timers that timeout at the same time, it's
             * preferred that the timer inserted early get called early.
             * So insert the new timer to the end the the some-timeout timer
             * list.
             */
            if ((t->timeout_tick - timer->timeout_tick) == 0)
            {
                continue;
            }
            else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2)
            {
                break;
            }
416
        }
417
        if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
418
            row_head[row_lvl + 1] = row_head[row_lvl] + 1;
419
    }
420 421 422 423 424 425 426 427

    /* Interestingly, this super simple timer insert counter works very very
     * well on distributing the list height uniformly. By means of "very very
     * well", I mean it beats the randomness of timer->timeout_tick very easily
     * (actually, the timeout_tick is not random and easy to be attacked). */
    random_nr++;
    tst_nr = random_nr;

428 429
    rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
                         &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
430
    for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
431
    {
432 433 434 435 436 437 438
        if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK))
            rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - row_lvl],
                                 &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - row_lvl]));
        else
            break;
        /* Shift over the bits we have tested. Works well with 1 bit and 2
         * bits. */
439
        tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
440 441 442 443 444 445
    }

    timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

    /* enable interrupt */
    rt_hw_interrupt_enable(level);
446

447
#ifdef RT_USING_TIMER_SOFT
448 449 450
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* check whether timer thread is ready */
B
Bernard Xiong 已提交
451 452
        if ((soft_timer_status == RT_SOFT_TIMER_IDLE) &&
           ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
453 454 455 456 457 458
        {
            /* resume timer thread to check soft timer */
            rt_thread_resume(&timer_thread);
            rt_schedule();
        }
    }
459
#endif /* RT_USING_TIMER_SOFT */
460

461
    return RT_EOK;
462
}
463
RTM_EXPORT(rt_timer_start);
464 465

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
466
 * @brief This function will stop the timer
467 468 469
 *
 * @param timer the timer to be stopped
 *
B
bernard.xiong@gmail.com 已提交
470
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
471 472 473
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
474
    register rt_base_t level;
475

476 477
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
478 479
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);

480 481
    if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
        return -RT_ERROR;
482

483
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
484

485 486
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
487

488
    _rt_timer_remove(timer);
B
Bernard Xiong 已提交
489 490
    /* change status */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
491

492 493
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
494

495
    return RT_EOK;
496
}
497
RTM_EXPORT(rt_timer_stop);
498 499

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
500
 * @brief This function will get or set some options of the timer
501 502 503 504 505
 *
 * @param timer the timer to be get or set
 * @param cmd the control command
 * @param arg the argument
 *
B
bernard.xiong@gmail.com 已提交
506
 * @return RT_EOK
507
 */
B
bernard 已提交
508
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
509
{
B
Bernard Xiong 已提交
510 511
    register rt_base_t level;

512 513
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
514
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
515

B
Bernard Xiong 已提交
516
    level = rt_hw_interrupt_disable();
517 518 519 520 521
    switch (cmd)
    {
    case RT_TIMER_CTRL_GET_TIME:
        *(rt_tick_t *)arg = timer->init_tick;
        break;
522

523 524 525
    case RT_TIMER_CTRL_SET_TIME:
        timer->init_tick = *(rt_tick_t *)arg;
        break;
526

527 528 529
    case RT_TIMER_CTRL_SET_ONESHOT:
        timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
        break;
530

531 532 533
    case RT_TIMER_CTRL_SET_PERIODIC:
        timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
        break;
B
Bernard Xiong 已提交
534

535 536 537 538 539 540 541 542 543 544 545
    case RT_TIMER_CTRL_GET_STATE:
        if(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)
        {
            /*timer is start and run*/
            *(rt_tick_t *)arg = RT_TIMER_FLAG_ACTIVATED;
        }
        else
        {
            /*timer is stop*/
            *(rt_tick_t *)arg = RT_TIMER_FLAG_DEACTIVATED;
        }
C
chenchaoqun 已提交
546 547 548 549
        break;

    default:
        break;
550
    }
B
Bernard Xiong 已提交
551
    rt_hw_interrupt_enable(level);
552

553
    return RT_EOK;
554
}
555
RTM_EXPORT(rt_timer_control);
556 557

/**
Thomas_Fly's avatar
Thomas_Fly 已提交
558
 * @brief This function will check timer list, if a timeout event happens,
Thomas_Fly's avatar
Thomas_Fly 已提交
559
 *        the corresponding timeout function will be invoked.
560
 *
B
bernard.xiong@gmail.com 已提交
561
 * @note this function shall be invoked in operating system timer interrupt.
562
 */
563
void rt_timer_check(void)
564
{
565 566 567
    struct rt_timer *t;
    rt_tick_t current_tick;
    register rt_base_t level;
Nameless-Y's avatar
Nameless-Y 已提交
568 569 570
    rt_list_t list;

    rt_list_init(&list);
571 572 573 574 575 576 577 578

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));

    current_tick = rt_tick_get();

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

579
    while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
580
    {
581 582
        t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                          struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
583 584 585 586 587

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
588
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
589
        {
590
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
591 592

            /* remove timer from timer list firstly */
593
            _rt_timer_remove(t);
B
Bernard Xiong 已提交
594 595 596 597 598 599
            if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
            {
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
            /* add timer to temporary list  */
            rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
600 601 602 603 604 605
            /* call timeout function */
            t->timeout_func(t->parameter);

            /* re-get tick */
            current_tick = rt_tick_get();

606
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
607 608
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

B
Bernard Xiong 已提交
609 610 611 612 613
            /* Check whether the timer object is detached or started again */
            if (rt_list_isempty(&list))
            {
                continue;
            }
614
            rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
615
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
616
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
617 618 619 620 621 622
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
B
Bernard Xiong 已提交
623
        else break;
624 625 626 627 628 629
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
630 631
}

632
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
633
 * @brief This function will return the next timeout tick in the system.
634 635 636 637
 *
 * @return the next timeout tick in the system
 */
rt_tick_t rt_timer_next_timeout_tick(void)
638
{
639
    return rt_timer_list_next_timeout(rt_timer_list);
640 641
}

642
#ifdef RT_USING_TIMER_SOFT
643
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
644
 * @brief This function will check software-timer list, if a timeout event happens, the
645 646
 * corresponding timeout function will be invoked.
 */
D
dzzxzz 已提交
647
void rt_soft_timer_check(void)
648
{
649 650
    rt_tick_t current_tick;
    struct rt_timer *t;
B
Bernard Xiong 已提交
651
    register rt_base_t level;
Nameless-Y's avatar
Nameless-Y 已提交
652 653 654
    rt_list_t list;

    rt_list_init(&list);
655 656 657

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));

B
Bernard Xiong 已提交
658 659
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
660

661
    while (!rt_list_isempty(&rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
662
    {
663 664 665 666
        t = rt_list_entry(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                            struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);

        current_tick = rt_tick_get();
667 668 669 670 671

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
D
dzzxzz@gmail.com 已提交
672
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
673
        {
674
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
675 676

            /* remove timer from timer list firstly */
677
            _rt_timer_remove(t);
B
Bernard Xiong 已提交
678 679 680 681 682 683 684 685 686 687
            if (!(t->parent.flag & RT_TIMER_FLAG_PERIODIC))
            {
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
            /* add timer to temporary list  */
            rt_list_insert_after(&list, &(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));

            soft_timer_status = RT_SOFT_TIMER_BUSY;
            /* enable interrupt */
            rt_hw_interrupt_enable(level);
688 689 690 691

            /* call timeout function */
            t->timeout_func(t->parameter);

692
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
693 694
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

B
Bernard Xiong 已提交
695 696 697 698 699 700 701 702 703
            /* disable interrupt */
            level = rt_hw_interrupt_disable();

            soft_timer_status = RT_SOFT_TIMER_IDLE;
            /* Check whether the timer object is detached or started again */
            if (rt_list_isempty(&list))
            {
                continue;
            }
704
            rt_list_remove(&(t->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
705
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
706
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
707 708 709 710 711 712 713 714
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
        else break; /* not check anymore */
    }
B
Bernard Xiong 已提交
715 716
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
717

718
    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
719 720
}

Thomas_Fly's avatar
Thomas_Fly 已提交
721
/**
Thomas_Fly's avatar
Thomas_Fly 已提交
722 723 724
 * @brief system timer thread entry
 *
 * @param parameter
Thomas_Fly's avatar
Thomas_Fly 已提交
725
 */
D
dzzxzz 已提交
726
static void rt_thread_timer_entry(void *parameter)
727
{
728
    rt_tick_t next_timeout;
B
Bernard Xiong 已提交
729

730 731 732
    while (1)
    {
        /* get the next timeout tick */
733
        next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
734 735 736 737 738 739 740 741 742 743 744 745 746
        if (next_timeout == RT_TICK_MAX)
        {
            /* no software timer exist, suspend self. */
            rt_thread_suspend(rt_thread_self());
            rt_schedule();
        }
        else
        {
            rt_tick_t current_tick;

            /* get current tick */
            current_tick = rt_tick_get();

747
            if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
748 749 750 751 752 753 754 755 756 757
            {
                /* get the delta timeout tick */
                next_timeout = next_timeout - current_tick;
                rt_thread_delay(next_timeout);
            }
        }

        /* check software timer */
        rt_soft_timer_check();
    }
758
}
759
#endif /* RT_USING_TIMER_SOFT */
760

761 762 763
/**
 * @ingroup SystemInit
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
764
 * @brief This function will initialize system timer
765
 */
D
dzzxzz 已提交
766
void rt_system_timer_init(void)
767
{
768 769
    int i;

770
    for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
771
    {
772
        rt_list_init(rt_timer_list + i);
773
    }
B
bernard.xiong 已提交
774
}
775

B
bernard.xiong 已提交
776 777 778
/**
 * @ingroup SystemInit
 *
Thomas_Fly's avatar
Thomas_Fly 已提交
779
 * @brief This function will initialize system timer thread
B
bernard.xiong 已提交
780
 */
D
dzzxzz 已提交
781
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
782 783
{
#ifdef RT_USING_TIMER_SOFT
784 785 786
    int i;

    for (i = 0;
787
         i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
788 789
         i++)
    {
790
        rt_list_init(rt_soft_timer_list + i);
791
    }
792

793 794 795
    /* start software timer thread */
    rt_thread_init(&timer_thread,
                   "timer",
D
dzzxzz@gmail.com 已提交
796 797 798 799 800 801
                   rt_thread_timer_entry,
                   RT_NULL,
                   &timer_thread_stack[0],
                   sizeof(timer_thread_stack),
                   RT_TIMER_THREAD_PRIO,
                   10);
802

803 804
    /* startup */
    rt_thread_startup(&timer_thread);
805
#endif /* RT_USING_TIMER_SOFT */
806
}
807

D
dogandog 已提交
808
/**@}*/