timer.c 20.6 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
B
Bernard Xiong 已提交
28 29 30 31

#define RT_SOFT_TIMER_IDLE              1
#define RT_SOFT_TIMER_BUSY              0

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

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

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

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

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

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

/**
62 63
 * This function will set a hook function, which will be invoked when enter
 * timer timeout callback function.
64
 *
65 66
 * @param hook the hook function
 */
67
void rt_timer_enter_sethook(void (*hook)(struct rt_timer *timer))
68
{
69 70 71 72 73 74 75 76 77 78 79 80
    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;
81 82
}

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

86
static void _rt_timer_init(rt_timer_t timer,
D
dzzxzz@gmail.com 已提交
87 88 89 90
                           void (*timeout)(void *parameter),
                           void      *parameter,
                           rt_tick_t  time,
                           rt_uint8_t flag)
91
{
92 93
    int i;

94 95
    /* set flag */
    timer->parent.flag  = flag;
96

97 98
    /* set deactivated */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
99

100 101
    timer->timeout_func = timeout;
    timer->parameter    = parameter;
102

103 104
    timer->timeout_tick = 0;
    timer->init_tick    = time;
105

106
    /* initialize timer list */
107 108 109 110
    for (i = 0; i < RT_TIMER_SKIP_LIST_LEVEL; i++)
    {
        rt_list_init(&(timer->row[i]));
    }
111 112
}

113 114
/* the fist timer always in the last row */
static rt_tick_t rt_timer_list_next_timeout(rt_list_t timer_list[])
115
{
116
    struct rt_timer *timer;
117 118
    register rt_base_t level;
    rt_tick_t timeout_tick = RT_TICK_MAX;
D
dzzxzz@gmail.com 已提交
119

120 121
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
B
Bernard Xiong 已提交
122

123 124 125 126 127 128 129 130 131
    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 已提交
132

133
    return timeout_tick;
134 135
}

136 137 138 139 140 141 142 143 144 145
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 已提交
146
#if RT_DEBUG_TIMER
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
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;

163 164
    for (list = timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1].next;
         list != &timer_heads[RT_TIMER_SKIP_LIST_LEVEL - 1];
165 166 167 168
         list = list->next)
    {
        struct rt_timer *timer = rt_list_entry(list,
                                               struct rt_timer,
169
                                               row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
170 171 172 173
        rt_kprintf("%d", rt_timer_count_height(timer));
    }
    rt_kprintf("\n");
}
174
#endif
175

176 177 178
/**
 * @addtogroup Clock
 */
D
dzzxzz 已提交
179

D
dogandog 已提交
180
/**@{*/
181 182

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

203 204
    /* timer object initialization */
    rt_object_init((rt_object_t)timer, RT_Object_Class_Timer, name);
205

206
    _rt_timer_init(timer, timeout, parameter, time, flag);
207
}
208
RTM_EXPORT(rt_timer_init);
209

210
/**
211 212 213
 * This function will detach a timer from timer management.
 *
 * @param timer the static timer object
214
 *
215 216 217 218
 * @return the operation status, RT_EOK on OK; RT_ERROR on error
 */
rt_err_t rt_timer_detach(rt_timer_t timer)
{
219
    register rt_base_t level;
220

221 222
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
223 224
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent));
225

226 227
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
228

229
    _rt_timer_remove(timer);
B
Bernard Xiong 已提交
230 231
    /* stop timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
232

233 234
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
235

236
    rt_object_detach((rt_object_t)timer);
237

238
    return RT_EOK;
239
}
240
RTM_EXPORT(rt_timer_detach);
241 242 243 244 245 246 247 248 249 250 251 252 253

#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 已提交
254 255 256 257 258
rt_timer_t rt_timer_create(const char *name,
                           void (*timeout)(void *parameter),
                           void       *parameter,
                           rt_tick_t   time,
                           rt_uint8_t  flag)
259
{
260
    struct rt_timer *timer;
261

262 263 264 265 266 267
    /* allocate a object */
    timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
    if (timer == RT_NULL)
    {
        return RT_NULL;
    }
268

269
    _rt_timer_init(timer, timeout, parameter, time, flag);
270

271
    return timer;
272
}
273
RTM_EXPORT(rt_timer_create);
274 275 276 277 278 279 280 281 282 283

/**
 * 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)
{
284
    register rt_base_t level;
285

286 287
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
288 289
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
    RT_ASSERT(rt_object_is_systemobject(&timer->parent) == RT_FALSE);
290

291 292
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
293

294
    _rt_timer_remove(timer);
B
Bernard Xiong 已提交
295 296
    /* stop timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
297

298 299
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
300

301
    rt_object_delete((rt_object_t)timer);
302

303
    return RT_EOK;
304
}
305
RTM_EXPORT(rt_timer_delete);
306 307 308 309 310 311 312
#endif

/**
 * This function will start the timer
 *
 * @param timer the timer to be started
 *
B
bernard.xiong@gmail.com 已提交
313
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
314 315 316
 */
rt_err_t rt_timer_start(rt_timer_t timer)
{
317
    unsigned int row_lvl;
318
    rt_list_t *timer_list;
319
    register rt_base_t level;
320 321 322
    rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL];
    unsigned int tst_nr;
    static unsigned int random_nr;
323

324 325
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
326
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
327

328 329 330
    /* stop timer firstly */
    level = rt_hw_interrupt_disable();
    /* remove timer from list */
331 332 333 334
    _rt_timer_remove(timer);
    /* change status of timer */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
    rt_hw_interrupt_enable(level);
335

336
    RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent)));
337

D
dzzxzz@gmail.com 已提交
338 339 340 341 342
    /*
     * get timeout tick,
     * the max timeout tick shall not great than RT_TICK_MAX/2
     */
    RT_ASSERT(timer->init_tick < RT_TICK_MAX / 2);
343
    timer->timeout_tick = rt_tick_get() + timer->init_tick;
344

345 346
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
347

348
#ifdef RT_USING_TIMER_SOFT
349 350 351
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* insert timer to soft timer list */
352
        timer_list = rt_soft_timer_list;
353 354
    }
    else
355
#endif
356 357
    {
        /* insert timer to system timer list */
358
        timer_list = rt_timer_list;
359 360
    }

361 362
    row_head[0]  = &timer_list[0];
    for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
363
    {
364
        for (; row_head[row_lvl] != timer_list[row_lvl].prev;
365
             row_head[row_lvl]  = row_head[row_lvl]->next)
366
        {
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
            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;
            }
386
        }
387
        if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1)
388
            row_head[row_lvl + 1] = row_head[row_lvl] + 1;
389
    }
390 391 392 393 394 395 396 397

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

398 399
    rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1],
                         &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1]));
400
    for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++)
401
    {
402 403 404 405 406 407 408
        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. */
409
        tst_nr >>= (RT_TIMER_SKIP_LIST_MASK + 1) >> 1;
410 411 412 413 414 415
    }

    timer->parent.flag |= RT_TIMER_FLAG_ACTIVATED;

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

417
#ifdef RT_USING_TIMER_SOFT
418 419 420
    if (timer->parent.flag & RT_TIMER_FLAG_SOFT_TIMER)
    {
        /* check whether timer thread is ready */
B
Bernard Xiong 已提交
421 422
        if ((soft_timer_status == RT_SOFT_TIMER_IDLE) &&
           ((timer_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_SUSPEND))
423 424 425 426 427 428
        {
            /* resume timer thread to check soft timer */
            rt_thread_resume(&timer_thread);
            rt_schedule();
        }
    }
429 430
#endif

431
    return RT_EOK;
432
}
433
RTM_EXPORT(rt_timer_start);
434 435 436 437 438 439

/**
 * This function will stop the timer
 *
 * @param timer the timer to be stopped
 *
B
bernard.xiong@gmail.com 已提交
440
 * @return the operation status, RT_EOK on OK, -RT_ERROR on error
441 442 443
 */
rt_err_t rt_timer_stop(rt_timer_t timer)
{
444
    register rt_base_t level;
445

446 447
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
448 449
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);

450 451
    if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED))
        return -RT_ERROR;
452

453
    RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
454

455 456
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
457

458
    _rt_timer_remove(timer);
B
Bernard Xiong 已提交
459 460
    /* change status */
    timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
461

462 463
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
464

465
    return RT_EOK;
466
}
467
RTM_EXPORT(rt_timer_stop);
468 469 470 471 472 473 474 475

/**
 * 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 已提交
476
 * @return RT_EOK
477
 */
B
bernard 已提交
478
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
479
{
B
Bernard Xiong 已提交
480 481
    register rt_base_t level;

482 483
    /* timer check */
    RT_ASSERT(timer != RT_NULL);
484
    RT_ASSERT(rt_object_get_type(&timer->parent) == RT_Object_Class_Timer);
485

B
Bernard Xiong 已提交
486
    level = rt_hw_interrupt_disable();
487 488 489 490 491
    switch (cmd)
    {
    case RT_TIMER_CTRL_GET_TIME:
        *(rt_tick_t *)arg = timer->init_tick;
        break;
492

493 494 495
    case RT_TIMER_CTRL_SET_TIME:
        timer->init_tick = *(rt_tick_t *)arg;
        break;
496

497 498 499
    case RT_TIMER_CTRL_SET_ONESHOT:
        timer->parent.flag &= ~RT_TIMER_FLAG_PERIODIC;
        break;
500

501 502 503
    case RT_TIMER_CTRL_SET_PERIODIC:
        timer->parent.flag |= RT_TIMER_FLAG_PERIODIC;
        break;
B
Bernard Xiong 已提交
504

505 506 507 508 509 510 511 512 513 514 515
    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 已提交
516 517 518 519
        break;

    default:
        break;
520
    }
B
Bernard Xiong 已提交
521
    rt_hw_interrupt_enable(level);
522

523
    return RT_EOK;
524
}
525
RTM_EXPORT(rt_timer_control);
526 527

/**
528
 * This function will check timer list, if a timeout event happens, the
529 530
 * corresponding timeout function will be invoked.
 *
B
bernard.xiong@gmail.com 已提交
531
 * @note this function shall be invoked in operating system timer interrupt.
532
 */
533
void rt_timer_check(void)
534
{
535 536 537
    struct rt_timer *t;
    rt_tick_t current_tick;
    register rt_base_t level;
B
Bernard Xiong 已提交
538
    rt_list_t list = RT_LIST_OBJECT_INIT(list);
539 540 541 542 543 544 545 546

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

    current_tick = rt_tick_get();

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

547
    while (!rt_list_isempty(&rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
548
    {
549 550
        t = rt_list_entry(rt_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1].next,
                          struct rt_timer, row[RT_TIMER_SKIP_LIST_LEVEL - 1]);
551 552 553 554 555

        /*
         * It supposes that the new tick shall less than the half duration of
         * tick max.
         */
556
        if ((current_tick - t->timeout_tick) < RT_TICK_MAX / 2)
557
        {
558
            RT_OBJECT_HOOK_CALL(rt_timer_enter_hook, (t));
559 560

            /* remove timer from timer list firstly */
561
            _rt_timer_remove(t);
B
Bernard Xiong 已提交
562 563 564 565 566 567
            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]));
568 569 570 571 572 573
            /* call timeout function */
            t->timeout_func(t->parameter);

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

574
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
575 576
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

B
Bernard Xiong 已提交
577 578 579 580 581 582
            /* Check whether the timer object is detached or started again */
            if (rt_list_isempty(&list))
            {
                continue;
            }

583
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
584
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
585 586 587 588 589 590
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
B
Bernard Xiong 已提交
591
        else break;
592 593 594 595 596 597
    }

    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
598 599
}

600 601 602 603 604 605
/**
 * 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)
606
{
607
    return rt_timer_list_next_timeout(rt_timer_list);
608 609
}

610
#ifdef RT_USING_TIMER_SOFT
611
/**
B
Bernard Xiong 已提交
612
 * This function will check software-timer list, if a timeout event happens, the
613 614
 * corresponding timeout function will be invoked.
 */
D
dzzxzz 已提交
615
void rt_soft_timer_check(void)
616
{
617 618
    rt_tick_t current_tick;
    struct rt_timer *t;
B
Bernard Xiong 已提交
619 620
    register rt_base_t level;
    rt_list_t list = RT_LIST_OBJECT_INIT(list);
621 622 623

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

B
Bernard Xiong 已提交
624 625
    /* disable interrupt */
    level = rt_hw_interrupt_disable();
626

627
    while (!rt_list_isempty(&rt_soft_timer_list[RT_TIMER_SKIP_LIST_LEVEL - 1]))
628
    {
629 630 631 632
        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();
633 634 635 636 637

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

            /* remove timer from timer list firstly */
643
            _rt_timer_remove(t);
B
Bernard Xiong 已提交
644 645 646 647 648 649 650 651 652 653
            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);
654 655 656 657

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

658
            RT_OBJECT_HOOK_CALL(rt_timer_exit_hook, (t));
659 660
            RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));

B
Bernard Xiong 已提交
661 662 663 664 665 666 667 668 669
            /* 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;
            }
670

671
            if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
D
dzzxzz@gmail.com 已提交
672
                (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
673 674 675 676 677 678 679 680
            {
                /* start it */
                t->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED;
                rt_timer_start(t);
            }
        }
        else break; /* not check anymore */
    }
B
Bernard Xiong 已提交
681 682
    /* enable interrupt */
    rt_hw_interrupt_enable(level);
683

684
    RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
685 686
}

687
/* system timer thread entry */
D
dzzxzz 已提交
688
static void rt_thread_timer_entry(void *parameter)
689
{
690
    rt_tick_t next_timeout;
B
Bernard Xiong 已提交
691

692 693 694
    while (1)
    {
        /* get the next timeout tick */
695
        next_timeout = rt_timer_list_next_timeout(rt_soft_timer_list);
696 697 698 699 700 701 702 703 704 705 706 707 708
        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();

709
            if ((next_timeout - current_tick) < RT_TICK_MAX / 2)
710 711 712 713 714 715 716 717 718 719
            {
                /* get the delta timeout tick */
                next_timeout = next_timeout - current_tick;
                rt_thread_delay(next_timeout);
            }
        }

        /* check software timer */
        rt_soft_timer_check();
    }
720
}
721
#endif
722

723 724 725
/**
 * @ingroup SystemInit
 *
726
 * This function will initialize system timer
727
 */
D
dzzxzz 已提交
728
void rt_system_timer_init(void)
729
{
730 731
    int i;

732
    for (i = 0; i < sizeof(rt_timer_list) / sizeof(rt_timer_list[0]); i++)
733
    {
734
        rt_list_init(rt_timer_list + i);
735
    }
B
bernard.xiong 已提交
736
}
737

B
bernard.xiong 已提交
738 739 740
/**
 * @ingroup SystemInit
 *
741
 * This function will initialize system timer thread
B
bernard.xiong 已提交
742
 */
D
dzzxzz 已提交
743
void rt_system_timer_thread_init(void)
B
bernard.xiong 已提交
744 745
{
#ifdef RT_USING_TIMER_SOFT
746 747 748
    int i;

    for (i = 0;
749
         i < sizeof(rt_soft_timer_list) / sizeof(rt_soft_timer_list[0]);
750 751
         i++)
    {
752
        rt_list_init(rt_soft_timer_list + i);
753
    }
754

755 756 757
    /* start software timer thread */
    rt_thread_init(&timer_thread,
                   "timer",
D
dzzxzz@gmail.com 已提交
758 759 760 761 762 763
                   rt_thread_timer_entry,
                   RT_NULL,
                   &timer_thread_stack[0],
                   sizeof(timer_thread_stack),
                   RT_TIMER_THREAD_PRIO,
                   10);
764

765 766
    /* startup */
    rt_thread_startup(&timer_thread);
767
#endif
768
}
769

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