skynet采用二級隊列的方式調(diào)度任務(wù)斥铺。
struct global_queue {
struct message_queue *head;
struct message_queue *tail;
struct spinlock lock;
};
global_queue為第一級顽染,可見隊列內(nèi)容是第二級隊列message_queue 旭斥,隊列操作使用自旋鎖嵌洼,避免頻繁的線程上下文切換。
每次調(diào)度message_queue ,會根據(jù)message_queue 長度一次性的pop多個任務(wù)來執(zhí)行
else if (i==0 && weight >= 0) {
n = skynet_mq_length(q);
n >>= weight;
}
weight為制定的線程權(quán)重,前四個是負值彪杉。
如果message_queue 為空,則標記in_global=0牵咙,不再放入global_queue中派近,否則重新入隊。
如果global_queue為空(實際應(yīng)用中不會的)洁桌,message_queue 也不會重新入隊渴丸,而是直接再次調(diào)度,不用再入隊出隊战坤。
如果global_queue沒有任何message_queue 可調(diào)度曙强,則
pthread_mutex_lock
pthread_cond_wait
pthread_mutex_unlock
等待喚醒残拐。
加鎖
(pthread_cond_wait前要先加鎖)
pthread_cond_wait內(nèi)部會解鎖途茫,然后等待條件變量被其它線程激活
(pthread_cond_wait被激活后會再自動加鎖)
解鎖