timer.c 19.0 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, 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.
19 20 21 22 23
 */

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

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

27
#ifdef RT_USING_TIMER_SOFT
28
#ifndef RT_TIMER_THREAD_STACK_SIZE
D
dzzxzz@gmail.com 已提交
29
#define RT_TIMER_THREAD_STACK_SIZE     512
30 31 32
#endif

#ifndef RT_TIMER_THREAD_PRIO
D
dzzxzz@gmail.com 已提交
33
#define RT_TIMER_THREAD_PRIO           0
34 35
#endif

36
/* soft timer list */
37
static rt_list_t rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL];
38 39 40
static struct rt_thread timer_thread;
ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t timer_thread_stack[RT_TIMER_THREAD_STACK_SIZE];
41
#endif
42

43
#ifdef RT_USING_HOOK
D
dzzxzz 已提交
44 45
extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object *object);
46 47
static void (*rt_timer_enter_hook)(struct rt_timer *timer);
static void (*rt_timer_exit_hook)(struct rt_timer *timer);
48 49 50 51

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

D
dogandog 已提交
53
/**@{*/
54 55

/**
56 57
 * This function will set a hook function, which will be invoked when enter
 * timer timeout callback function.
58
 *
59 60
 * @param hook the hook function
 */
61
void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
62
{
63 64 65 66 67 68 69 70 71 72 73 74
    rt_timer_enter_hook = hook;
}

/**
 * This function will set a hook function, which will be invoked when exit
 * timer timeout callback function.
 *
 * @param hook the hook function
 */
void rt_timer_exit_sethook(void (*hook)(struct rt_timer *timer))
{
    rt_timer_exit_hook = hook;
75 76
}

D
dogandog 已提交
77
/**@}*/
78 79
#endif

80
static void _rt_timer_init(rt_timer_t timer,
D
dzzxzz@gmail.com 已提交
81 82 83 84
                           void (*timeout)(void *parameter),
                           void      *parameter,
                           rt_tick_t  time,
                           rt_uint8_t flag)
85
{
86 87
    int i;

88 89
    /* set flag */
    timer->parent.flag  = flag;
90

91 92
    /* set deactivated */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
93

94 95
    timer->timeout_func = timeout;
    timer->parameter    = parameter;
96

97 98
    timer->timeout_tick = 0;
    timer->init_tick    = time;
99

100
    /* initialize timer list */
101 102 103 104
    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        rt_list_init(&(timer->row[i]));
    }
105 106
}

107 108
/* the fist timer always in the last row */
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
109
{
110
    struct rt_timer *timer;
111 112
    register rt_base_t level;
    rt_tick_t timeout_tick = RT_TICK_MAX;
D
dzzxzz@gmail.com 已提交
113

114 115
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
B
Bernard Xiong 已提交
116

117 118 119 120 121 122 123 124 125
    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 已提交
126

127
    return timeout_tick;
128 129
}

130 131 132 133 134 135 136 137 138 139
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 已提交
140
#if RT_DEBUG_TIMER
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
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;
}

void rt_timer_dump(rt_list_t timer_heads[])
{
    rt_list_t *list;

157 158
    for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
         list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
159 160 161 162
         list = list->next)
    {
        struct rt_timer *timer = rt_list_entry(list,
                                               struct rt_timer,
163
                                               row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
164 165 166 167
        rt_kprintf("%d", rt_timer_count_height(timer));
    }
    rt_kprintf("\n");
}
168
#endif
169

170 171 172
/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
173

D
dogandog 已提交
174
/**@{*/
175 176

/**
177 178
 * This function will initialize a timer, normally this function is used to
 * initialize a static timer object.
179 180 181 182 183 184 185 186
 *
 * @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 已提交
187
void rt_timer_init(rt_timer_t  timer,
188
                   const char *name,
D
dzzxzz@gmail.com 已提交
189 190 191 192
                   void (*timeout)(void *parameter),
                   void       *parameter,
                   rt_tick_t   time,
                   rt_uint8_t  flag)
193
{
194 195
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
196

197 198
    /* timer object initialization */
    rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
199

200
    _rt_timer_init(timer, timeout, parameter, time, flag);
201
}
202
RTM_EXPORT(rt_timer_init);
203

204
/**
205 206 207
 * This function will detach a timer from timer management.
 *
 * @param timer the static timer object
208
 *
209 210 211 212
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_detach(rt_timer_t timer)
{
213
    register rt_base_t level;
214

215 216
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
217 218
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent));
219

220 221
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
222

223
    _rt_timer_remove(timer);
224

225 226
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
227

228
    rt_object_detach((rt_object_t)timer);
229

230
    return RT_EOK;
231
}
232
RTM_EXPORT(rt_timer_detach);
233 234 235 236 237 238 239 240 241 242 243 244 245

#ifdef RT_USING_HEAP
/**
 * This function will create a timer
 *
 * @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 已提交
246 247 248 249 250
rt_timer_t rt_timer_create(const char *name,
                           void (*timeout)(void *parameter),
                           void       *parameter,
                           rt_tick_t   time,
                           rt_uint8_t  flag)
251
{
252
    struct rt_timer *timer;
253

254 255 256 257 258 259
    /* allocate a object */
    timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
    if (timer == RT_NULL)
    {
        return RT_NULL;
    }
260

261
    _rt_timer_init(timer, timeout, parameter, time, flag);
262

263
    return timer;
264
}
265
RTM_EXPORT(rt_timer_create);
266 267 268 269 270 271 272 273 274 275

/**
 * This function will delete a timer and release timer memory
 *
 * @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)
{
276
    register rt_base_t level;
277

278 279
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
280 281
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
282

283 284
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
285

286
    _rt_timer_remove(timer);
287

288 289
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
290

291
    rt_object_delete((rt_object_t)timer);
292

293
    return RT_EOK;
294
}
295
RTM_EXPORT(rt_timer_delete);
296 297 298 299 300 301 302
#endif

/**
 * This function will start the timer
 *
 * @param timer the timer to be started
 *
B
bernard.xiong@gmail.com 已提交
303
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
304 305 306
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
307
    unsigned int row_lvl;
308
    rt_list_t *timer_list;
309
    register rt_base_t level;
310 311 312
    rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
    unsigned int tst_nr;
    static unsigned int random_nr;
313

314 315
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
316
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
317

318 319 320
    /* stop timer firstly */
    level = rt_hw_interrupt_disable();
    /* remove timer from list */
321 322 323 324
    _rt_timer_remove(timer);
    /* change status of timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
    rt_hw_interrupt_enable(level);
325

326
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
327

D
dzzxzz@gmail.com 已提交
328 329 330 331 332
    /*
     * get timeout tick,
     * the max timeout tick shall not great than RT_TICK_MAX/2
     */
    RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
333
    timer->timeout_tick = rt_tick_get() + timer->init_tick;
334

335 336
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
337

338
#ifdef RT_USING_TIMER_SOFT
339 340 341
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* insert timer to soft timer list */
342
        timer_list = rt_soft_timer_list;
343 344
    }
    else
345
#endif
346 347
    {
        /* insert timer to system timer list */
348
        timer_list = rt_timer_list;
349 350
    }

351 352
    row_head[0]  = &timer_list[0];
    for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
353
    {
354
        for (; row_head[row_lvl] != timer_list[row_lvl].prev;
355
             row_head[row_lvl]  = row_head[row_lvl]->next)
356
        {
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
            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;
            }
376
        }
377
        if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
378
            row_head[row_lvl + 1] = row_head[row_lvl] + 1;
379
    }
380 381 382 383 384 385 386 387

    /* 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;

388 389
    rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
                         &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
390
    for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
391
    {
392 393 394 395 396 397 398
        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. */
399
        tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
400 401 402 403 404 405
    }

    timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

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

407
#ifdef RT_USING_TIMER_SOFT
408 409 410
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* check whether timer thread is ready */
411
        if ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND)
412 413 414 415 416 417
        {
            /* resume timer thread to check soft timer */
            rt_thread_resume(&timer_thread);
            rt_schedule();
        }
    }
418 419
#endif

420
    return RT_EOK;
421
}
422
RTM_EXPORT(rt_timer_start);
423 424 425 426 427 428

/**
 * This function will stop the timer
 *
 * @param timer the timer to be stopped
 *
B
bernard.xiong@gmail.com 已提交
429
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
430 431 432
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
433
    register rt_base_t level;
434

435 436
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
437 438
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);

439 440
    if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
        return -RT_ERROR;
441

442
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
443

444 445
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
446

447
    _rt_timer_remove(timer);
448

449 450
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
451

452 453
    /* change stat */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
454

455
    return RT_EOK;
456
}
457
RTM_EXPORT(rt_timer_stop);
458 459 460 461 462 463 464 465

/**
 * This function will get or set some options of the timer
 *
 * @param timer the timer to be get or set
 * @param cmd the control command
 * @param arg the argument
 *
B
bernard.xiong@gmail.com 已提交
466
 * @return RT_EOK
467
 */
B
bernard 已提交
468
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
469
{
470 471
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
472
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
473

474 475 476 477 478
    switch (cmd)
    {
    case RT_TIMER_CTRL_GET_TIME:
        *(rt_tick_t *)arg = timer->init_tick;
        break;
479

480 481 482
    case RT_TIMER_CTRL_SET_TIME:
        timer->init_tick = *(rt_tick_t *)arg;
        break;
483

484 485 486
    case RT_TIMER_CTRL_SET_ONESHOT:
        timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
        break;
487

488 489 490 491
    case RT_TIMER_CTRL_SET_PERIODIC:
        timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
        break;
    }
492

493
    return RT_EOK;
494
}
495
RTM_EXPORT(rt_timer_control);
496 497

/**
498
 * This function will check timer list, if a timeout event happens, the
499 500
 * corresponding timeout function will be invoked.
 *
B
bernard.xiong@gmail.com 已提交
501
 * @note this function shall be invoked in operating system timer interrupt.
502
 */
503
void rt_timer_check(void)
504
{
505 506 507 508 509 510 511 512 513 514 515
    struct rt_timer *t;
    rt_tick_t current_tick;
    register rt_base_t level;

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

    current_tick = rt_tick_get();

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

516
    while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
517
    {
518 519
        t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                          struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
520 521 522 523 524

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
525
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
526
        {
527
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
528 529

            /* remove timer from timer list firstly */
530
            _rt_timer_remove(t);
531 532 533 534 535 536 537

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

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

538
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
539 540 541
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
542
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
            else
            {
                /* stop timer */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
        }
        else
            break;
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
562 563
}

564 565 566 567 568 569
/**
 * This function will return the next timeout tick in the system.
 *
 * @return the next timeout tick in the system
 */
rt_tick_t rt_timer_next_timeout_tick(void)
570
{
571
    return rt_timer_list_next_timeout(rt_timer_list);
572 573
}

574
#ifdef RT_USING_TIMER_SOFT
575 576 577 578
/**
 * This function will check timer list, if a timeout event happens, the
 * corresponding timeout function will be invoked.
 */
D
dzzxzz 已提交
579
void rt_soft_timer_check(void)
580
{
581 582 583 584 585 586 587 588
    rt_tick_t current_tick;
    rt_list_t *n;
    struct rt_timer *t;

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

    current_tick = rt_tick_get();

589 590
    /* lock scheduler */
    rt_enter_critical();
591

592 593
    for (n = rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
         n != &(rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]);)
594
    {
595
        t = rt_list_entry(n, struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
596 597 598 599 600

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
D
dzzxzz@gmail.com 已提交
601
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
602
        {
603
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
604 605 606 607 608

            /* move node to the next */
            n = n->next;

            /* remove timer from timer list firstly */
609
            _rt_timer_remove(t);
610

611 612
            /* not lock scheduler when performing timeout function */
            rt_exit_critical();
613 614 615 616 617 618
            /* call timeout function */
            t->timeout_func(t->parameter);

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

619
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
620 621
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

622 623
            /* lock scheduler */
            rt_enter_critical();
624

625
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
626
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
627 628 629 630 631 632 633 634 635 636 637 638 639 640
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
            else
            {
                /* stop timer */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
            }
        }
        else break; /* not check anymore */
    }

641 642
    /* unlock scheduler */
    rt_exit_critical();
643

644
    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
645 646
}

647
/* system timer thread entry */
D
dzzxzz 已提交
648
static void rt_thread_timer_entry(void *parameter)
649
{
650
    rt_tick_t next_timeout;
B
Bernard Xiong 已提交
651

652 653 654
    while (1)
    {
        /* get the next timeout tick */
655
        next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
656 657 658 659 660 661 662 663 664 665 666 667 668
        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();

669
            if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
670 671 672 673 674 675 676 677 678 679
            {
                /* get the delta timeout tick */
                next_timeout = next_timeout - current_tick;
                rt_thread_delay(next_timeout);
            }
        }

        /* check software timer */
        rt_soft_timer_check();
    }
680
}
681
#endif
682

683 684 685
/**
 * @ingroup SystemInit
 *
686
 * This function will initialize system timer
687
 */
D
dzzxzz 已提交
688
void rt_system_timer_init(void)
689
{
690 691
    int i;

692
    for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
693
    {
694
        rt_list_init(rt_timer_list + i);
695
    }
B
bernard.xiong 已提交
696
}
697

B
bernard.xiong 已提交
698 699 700
/**
 * @ingroup SystemInit
 *
701
 * This function will initialize system timer thread
B
bernard.xiong 已提交
702
 */
D
dzzxzz 已提交
703
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
704 705
{
#ifdef RT_USING_TIMER_SOFT
706 707 708
    int i;

    for (i = 0;
709
         i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
710 711
         i++)
    {
712
        rt_list_init(rt_soft_timer_list + i);
713
    }
714

715 716 717
    /* start software timer thread */
    rt_thread_init(&timer_thread,
                   "timer",
D
dzzxzz@gmail.com 已提交
718 719 720 721 722 723
                   rt_thread_timer_entry,
                   RT_NULL,
                   &timer_thread_stack[0],
                   sizeof(timer_thread_stack),
                   RT_TIMER_THREAD_PRIO,
                   10);
724

725 726
    /* startup */
    rt_thread_startup(&timer_thread);
727
#endif
728
}
729

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