日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux 使用spinlock的配对关系问题

發(fā)布時間:2025/3/21 linux 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 使用spinlock的配对关系问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

大家使用spinlock的時候,一般是這么配對:

spin_lock---------------------spin_unlock------------------最輕 spin_lock_bh----------------spin_unlock_bh-----------------較輕 spin_lock_irq----------------spin_unlock_irq---------------較重 spin_lock_irqsave---------spin_lock_irqrestore--------------最重

那么,假設我使用?spin_lock_irqsave 來關中斷和關搶占,并保存之前的中斷狀態(tài),那么,我能不能使用spin_unlock來解鎖呢?

static inline int __mod_timer(struct timer_list *timer, unsigned long expires,bool pending_only, int pinned) {struct tvec_base *base, *new_base;unsigned long flags;int ret = 0 , cpu;timer_stats_timer_set_start_info(timer);BUG_ON(!timer->function);base = lock_timer_base(timer, &flags);ret = detach_if_pending(timer, base, false);if (!ret && pending_only)goto out_unlock;debug_activate(timer, expires);cpu = smp_processor_id();#if defined(CONFIG_NO_HZ_COMMON) && defined(CONFIG_SMP)if (!pinned && get_sysctl_timer_migration())cpu = get_nohz_timer_target(); #endifnew_base = per_cpu(tvec_bases, cpu);if (base != new_base) {/** We are trying to schedule the timer on the local CPU.* However we can't change timer's base while it is running,* otherwise del_timer_sync() can't detect that the timer's* handler yet has not finished. This also guarantees that* the timer is serialized wrt itself.*/if (likely(base->running_timer != timer)) {/* See the comment in lock_timer_base() */timer_set_base(timer, NULL);spin_unlock(&base->lock);base = new_base;spin_lock(&base->lock);timer_set_base(timer, base);}}timer->expires = expires;internal_add_timer(base, timer);out_unlock:spin_unlock_irqrestore(&base->lock, flags);-----------收尾很重要return ret; }

答案就在這個函數中,看完這個函數,你就不會這么淺顯地理解配對了,可能會更深地去理解spinlock的使用。

其實spinlock這種忙等的方式,使用場景一般要求,臨界區(qū)不能太大,且多路徑訪問的概率不那么高。這里的多路徑,就是指進程上下文和中斷上下文,其中中斷上下文又分為硬中斷和軟中斷。

進程上下文包括:用戶進程,內核線程,從調度角度來說,都歸屬進程上下文,可以睡眠。

中斷上下文包括:HW interrupt context(中斷handler)、軟中斷上下文(soft irq,當然由于各種原因,該softirq被推遲到softirqd的內核線程中執(zhí)行的時候就不屬于這個場景了,屬于進程上下文那個分類了)、timer的callback函數(本質上也是softirq)、tasklet(本質上也是softirq)。

不同的控制路徑,使用不同的spinlock,比如只有進程上下文訪問臨界區(qū),則使用spin_lock就夠了,如果有軟中斷和進程上下文訪問臨界區(qū),則需要用spin_lock_bh,如果有硬中斷訪問和進程上下文訪問,則用spin_lock_irq。

假設只有軟中斷訪問臨界區(qū),則只要關軟中斷就行,甚至都不需要spin-lock,

假設只有硬中斷訪問臨界區(qū),則只要關硬中斷就行,也不需要spin-lock

?如果有興趣完全弄懂,可以參考?蝸窩科技? http://www.wowotech.net/kernel_synchronization/spinlock.html

轉載于:https://www.cnblogs.com/10087622blog/p/9560979.html

總結

以上是生活随笔為你收集整理的linux 使用spinlock的配对关系问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。