MOD_INC_USE_COUNT和MOD_DEC_USE_COUNT)

來源:互聯網
上載者:User
2.4核心中,模組自身通過 MOD_INC_USE_COUNT, MOD_DEC_USE_COUNT宏來管理自己被使用的計數。
   2.6核心提供了更健壯、靈活的模組計數管理介面 try_module_get(&module), module_put(&module)取代2.4中的模組使用計數管理宏;模組的使用計數不必由自身管理,而且在管理模組使用計數時考慮到 SMP與PREEMPT機制的影響。

   int try_module_get(struct module *module); 用於增加模組使用計數;若返回為0,表示調用失敗,希望使用的模組沒有被載入或正在被卸載中。
   void module_put(struct module *module); 減少模組使用計數。

   try_module_get與module_put 的引入與使用與2.6核心下的裝置模型密切相關。模組是用來管理硬體裝置的,2.6核心為不同類型的裝置定義了struct module *owner 域,用來指向管理此裝置的模組。如字元裝置的定義:
struct cdev
{
    struct kobject kobj;
    struct module *owner;
    struct file_operations *ops;
    struct list_head list;
    dev_t dev;
    unsigned int count;
};

    從裝置使用的角度出發,當需要開啟、開始使用某個裝置時,使用 try_module_get(dev->owner)去增加管理此裝置的 owner模組的使用計數;當關閉、不再使用此裝置時,使用module_put(dev->owner)減少對管理此裝置的owner模組的使用 計數。這樣,當裝置在使用時,管理此裝置的模組就不能被卸載;只有裝置不再使用時模組才能被卸載。
   2.6核心下,對於為具體裝置寫驅動的開發人員而言,基本無需使用 try_module_get與module_put,因為此時開發人員所寫的驅動通常為支援某具體裝置的owner模組,對此裝置owner模組的計數管理由核心裡更底層的代碼如匯流排驅動或是此類裝置共用的核心模組來實現,從而簡化了裝置驅動開發。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.