1. spa
#define cond_resched() ({ \ ___might_sleep(__FILE__, __LINE__, 0); \ _cond_resched(); \ }) int __sched _cond_resched(void) { if (should_resched(0)) { preempt_schedule_common(); return 1; } return 0; } static __always_inline bool should_resched(int preempt_offset) { return unlikely(preempt_count() == preempt_offset && tif_need_resched()); } #define tif_need_resched() test_thread_flag(TIF_NEED_RESCHED) 一個A進程, 在用戶態時, 時間滴答若是選出新的高優先級B進程的話, 會當即進程切換, 若是A進程處於 內核態時,滴答不能當即切換B進程, 只是在A進程的thread_info.flag標誌 TIF_NEED_RESCHED , 而後A進程 從內核態返回用戶態時, 系統判斷若是有這個TIF_NEED_RESCHED標誌, 就立馬切換到B進程,因此內核態的程序 (驅動)若是while(1); 除了中斷會獲得執行, 其餘代碼都沒機會運行, 所以一個驅動處理完事情儘快返回用戶態, 或者主動調用sleep(), 先休眠, 若是既想貪心執行, 又臉皮薄怕其餘進程等過久, 能夠經過cond_resched() 判斷 一下自身有沒有TIF_NEED_RESCHED, 有的話說明有更急的進程須要執行, 那就切換, 不然就問心無愧的跑(不是我不讓, 是大家沒叫我讓)
2.線程
cd->health_thread = kthread_run(genwqe_health_thread, cd, GENWQE_DEVNAME "%d_health", cd->card_idx); kthread_stop(cd->health_thread); static int genwqe_health_thread(void *data) { while (!kthread_should_stop()) { rc = wait_event_interruptible_timeout(cd->health_waitq, (genwqe_health_check_cond(cd, &gfir) || (should_stop = kthread_should_stop())), genwqe_health_check_interval * HZ); } } 若是一個線程能夠一直作某事就直接while(1) 不然就增長一個判斷條件
3.code
#include <stdio.h> void test(int a) { switch(a) { case 1: printf("1\n"); break; default: printf("default\n"); break; case 0: printf("0\n"); break; } } void main() { test(5); printf("==============\n"); test(0); } default ============== 0