* @brief This function will start the timer * * @param timer the timer to be started * * @return the operation status, RT_EOK on OK, -RT_ERROR on error */
| 407 | * @return the operation status, RT_EOK on OK, -RT_ERROR on error |
| 408 | */ |
| 409 | static rt_err_t _timer_start(rt_list_t *timer_list, rt_timer_t timer) |
| 410 | { |
| 411 | unsigned int row_lvl; |
| 412 | rt_list_t *row_head[RT_TIMER_SKIP_LIST_LEVEL]; |
| 413 | unsigned int tst_nr; |
| 414 | static unsigned int random_nr; |
| 415 | |
| 416 | /* remove timer from list */ |
| 417 | _timer_remove(timer); |
| 418 | /* change status of timer */ |
| 419 | timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; |
| 420 | |
| 421 | RT_OBJECT_HOOK_CALL(rt_object_take_hook, (&(timer->parent))); |
| 422 | |
| 423 | timer->timeout_tick = rt_tick_get() + timer->init_tick; |
| 424 | |
| 425 | row_head[0] = &timer_list[0]; |
| 426 | for (row_lvl = 0; row_lvl < RT_TIMER_SKIP_LIST_LEVEL; row_lvl++) |
| 427 | { |
| 428 | for (; row_head[row_lvl] != timer_list[row_lvl].prev; |
| 429 | row_head[row_lvl] = row_head[row_lvl]->next) |
| 430 | { |
| 431 | struct rt_timer *t; |
| 432 | rt_list_t *p = row_head[row_lvl]->next; |
| 433 | |
| 434 | /* fix up the entry pointer */ |
| 435 | t = rt_list_entry(p, struct rt_timer, row[row_lvl]); |
| 436 | |
| 437 | /* If we have two timers that timeout at the same time, it's |
| 438 | * preferred that the timer inserted early get called early. |
| 439 | * So insert the new timer to the end the the some-timeout timer |
| 440 | * list. |
| 441 | */ |
| 442 | if ((t->timeout_tick - timer->timeout_tick) == 0) |
| 443 | { |
| 444 | continue; |
| 445 | } |
| 446 | else if ((t->timeout_tick - timer->timeout_tick) < RT_TICK_MAX / 2) |
| 447 | { |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | if (row_lvl != RT_TIMER_SKIP_LIST_LEVEL - 1) |
| 452 | row_head[row_lvl + 1] = row_head[row_lvl] + 1; |
| 453 | } |
| 454 | |
| 455 | /* Interestingly, this super simple timer insert counter works very very |
| 456 | * well on distributing the list height uniformly. By means of "very very |
| 457 | * well", I mean it beats the randomness of timer->timeout_tick very easily |
| 458 | * (actually, the timeout_tick is not random and easy to be attacked). */ |
| 459 | random_nr++; |
| 460 | tst_nr = random_nr; |
| 461 | |
| 462 | rt_list_insert_after(row_head[RT_TIMER_SKIP_LIST_LEVEL - 1], |
| 463 | &(timer->row[RT_TIMER_SKIP_LIST_LEVEL - 1])); |
| 464 | for (row_lvl = 2; row_lvl <= RT_TIMER_SKIP_LIST_LEVEL; row_lvl++) |
| 465 | { |
| 466 | if (!(tst_nr & RT_TIMER_SKIP_LIST_MASK)) |
no test coverage detected