static void sleep_tick_adjust(uint32_t ms) { uint32_t diff; diff = rt_tick_from_millisecond(ms); rt_tick_set(rt_tick_get() + diff); { struct rt_thread *thread; /* check time slice */ thread = rt_thread_self(); if (thread->remaining_tick <= diff) { /* change to initialized tick */ thread->remaining_tick = thread->init_tick; /* yield */ rt_thread_yield(); } else { thread->remaining_tick -= diff; } /* check timer */ rt_timer_check(); } }
/// Pass control to next thread that is in state READY osStatus osThreadYield(void) { rt_err_t result; result = rt_thread_yield(); if (result == RT_EOK) return osOK; else return osErrorOS; }
/* 线程2入口 */ static void thread2_entry(void* parameter) { rt_uint32_t count = 0; while (1) { /* 打印线程2的输出 */ rt_kprintf("thread2: count = %d\n", count ++); /* 执行yield后应该切换到thread1执行 */ rt_thread_yield(); } }
/** * This function will notify kernel there is one tick passed. Normally, * this function is invoked by clock ISR. */ void rt_tick_increase(void) { struct rt_thread *thread; /* increase the global tick */ ++ rt_tick; /* check time slice */ thread = rt_thread_self(); -- thread->remaining_tick; if (thread->remaining_tick == 0) { /* change to initialized tick */ thread->remaining_tick = thread->init_tick; /* yield */ rt_thread_yield(); } /* check timer */ rt_timer_check(); }
int sched_yield(void) { rt_thread_yield(); return 0; }