Linux核心同步機制之completion

來源:互聯網
上載者:User

核心編程中常見的一種模式是,在當前線程之外初始化某個活動,然後等待該活動的結束。這個活動可能是,建立一個新的核心線程或者新的使用者空間進程、對一個 已有進程的某個請求,或者某種類型的硬體動作,等等。在這種情況下,我們可以使用訊號量來同步這兩個

 

核心編程中常見的一種模式是,在當前線程之外初始化某個活動,然後等待該活動的結束。這個活動可能是,建立一個新的核心線程或者新的使用者空間進程、對一個 已有進程的某個請求,或者某種類型的硬體動作,等等。在這種情況下,我們可以使用訊號量來同步這兩個任務。然而,核心中提供了另外一種機制 --completion介面。Completion是一種輕量級的機制,他允許一個線程告訴另一個線程某個工作已經完成。
   
    結構與初始化
   
    Completion在核心中的實現基於等待隊列(關於等待隊列理論知識在前面的文章中有介紹),completion結構很簡單:
   
    struct completion {
        unsigned int done;/*用於同步的原子量*/
        wait_queue_head_t wait;/*等待事件隊列*/
    };
   
    和訊號量一樣,初始化分為靜態初始化和動態初始化兩種情況:
   
    靜態初始化:
    #define COMPLETION_INITIALIZER(work) \
            { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work)。wait) }
   
    #define DECLARE_COMPLETION(work) \
            struct completion work = COMPLETION_INITIALIZER(work)
   
    動態初始化:
    static inline void init_completion(struct completion *x)
    {
         x->done = 0;
        init_waitqueue_head(&x->wait);
    }
   
    可見,兩種初始化都將用於同步的done原子量置位了0,後面我們會看到,該變數在wait相關函數中減一,在complete系列函數中加一。
   
    實現
    同步函數一般都成對出現,completion也不例外,我們看看最基本的兩個complete和wait_for_completion函數的實現。
    wait_for_completion最終由下面函數實現:
    static inline long __sched
    do_wait_for_common(struct completion *x, long timeout, int state)
    {
        if (!x->done){
        DECLARE_WAITQUEUE(wait, current);
        wait.flags |= WQ_FLAG_EXCLUSIVE;
        __add_wait_queue_tail(&x->wait, &wait);
        do {
            if (signal_pending_state(state, current)) {
                timeout = -ERESTARTSYS;
                break;
                }
   
            __set_current_state(state);
            spin_unlock_irq(&x->wait.lock);
            timeout = schedule_timeout(timeout);
            spin_lock_irq(&x->wait.lock);
            } while (!x->done && timeout);
   
        __remove_wait_queue(&x->wait, &wait);
   
        if (!x->done)
            return timeout;
        }
   
        x->done--;
   
        return timeout ?: 1;
   }
   
    而complete實現如下:
    void complete(struct completion *x)
    {
        unsigned long flags;
        spin_lock_irqsave(&x->wait.lock, flags);
        x->done++;
        __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
        spin_unlock_irqrestore(&x->wait.lock, flags);
    }
   
    不看核心實現的原始碼我們也能想到他的實現,不外乎在wait函數中迴圈等待done變為可用(正),而另一邊的complete函數為喚醒函數,當然是將done加一,喚醒待處理的函數。是的,從上面的代碼看到,和我們想的一樣。核心也是這樣做的。
   
    運用
    運用LDD3中的例子:
   
    #include <linux/module.h>
    #include <linux/init.h>
    #include <linux/sched.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <linux/types.h>
    #include <linux/completion.h>

   

    MODULE_LICENSE(“GPL”);
   
    static int complete_major=250;
    DECLARE_COMPLETION(comp);
   
    ssize_t complete_read(struct file *filp,char __user *buf,size_t count,loff_t *pos)
    {
        printk(KERN_ERR “process %i (%s) going to sleep\n”,current->pid,current->comm);
        wait_for_completion(&comp);
        printk(KERN_ERR “awoken %i (%s)\n”,current->pid,current->comm);
        return 0;
    }
   
    ssize_t complete_write(struct file *filp,const char __user *buf,size_t count,loff_t *pos)
    {
        printk(KERN_ERR “process %i (%s) awakening the readers…\n”,current->pid,current->comm);
        complete(&comp);
        return count;
    }
   
    struct file_operations complete_fops={
        .owner=THIS_MODULE,
         .read=complete_read,
         .write=complete_write,
    };
   
    int complete_init(void)
    {
        int result;
        result=register_chrdev(complete_major,“complete”,&complete_fops);
        if(result<0)
            return result;
        if(complete_major==0)
            complete_major=result;
        return 0;
    }
   
    void complete_cleanup(void)
    {
        unregister_chrdev(complete_major,“complete”);
    }
   
    module_init(complete_init);
    module_exit(complete_cleanup);
   
    測試步驟:
   
    1、mknod /dev/complete建立complete節點,在linux上驅動程式需要手動建立檔案節點。
    2、insmod complete.ko 插入驅動模組,這裡要注意的是,因為我們的代碼中是手動分配的裝置號,很可能被系統已經使用了,所以如果出現這種情況,查看/proc/devices檔案。找一個沒有被使用的裝置號。
    3、cat /dev/complete 用於讀該裝置,調用裝置的讀函數
    4、開啟另一個終端輸入 echo “hello” > /dev/complete 該命令用於寫入該裝置。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.