{"id":1854,"date":"2010-10-26T10:05:47","date_gmt":"2010-10-26T02:05:47","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=1854"},"modified":"2010-10-28T10:46:58","modified_gmt":"2010-10-28T02:46:58","slug":"%e7%ad%89%e5%be%85%e9%98%9f%e5%88%97%e6%ba%90%e7%a0%81%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/1854","title":{"rendered":"\u7b49\u5f85\u961f\u5217\u6e90\u7801\u5206\u6790"},"content":{"rendered":"<p>\u6b63\u5982list_head\u7ed3\u6784\u90a3\u6837\uff0c\u7b49\u5f85\u961f\u5217(wait queue)\u4f5c\u4e3alinux\u5185\u6838\u4e2d\u7684\u57fa\u7840\u6570\u636e\u7ed3\u6784\uff0c\u4e0e\u8fdb\u7a0b\u8c03\u5ea6\u7d27\u5bc6\u7ed3\u5408\u5728\u4e00\u8d77\uff1b\u5728\u9a71\u52a8\u7a0b\u5e8f\u4e2d\uff0c\u5e38\u5e38\u4f7f\u7528\u7b49\u5f85\u961f\u5217\u6765\u5b9e\u73b0\u8fdb\u7a0b\u7684\u963b\u585e\u548c\u8fdb\u7a0b\u7684\u5524\u9192\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5f88\u6709\u5fc5\u8981\u5bf9\u5b83\u7684\u5185\u90e8\u5b9e\u73b0\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<p><strong>0.\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u4e00\u822c\u6211\u4eec\u7684\u94fe\u5f0f\u7ebf\u6027\u8868\u90fd\u4f1a\u6709\u4e00\u4e2a\u5934\u7ed3\u70b9\uff0c\u4ee5\u4f7f\u6211\u4eec\u8fc5\u901f\u627e\u5230\u8fd9\u4e2a\u7ebf\u6027\u94fe\u8868\u7684\u201c\u9886\u5bfc\u201d\u3002\u5728\u7b49\u5f85\u961f\u5217\u4e2d\uff0c\u540c\u6837\u6709\u961f\u5217\u5934\uff0c\u53ea\u4e0d\u8fc7\u7b49\u5f85\u961f\u5217\u5934\u548c\u666e\u901a\u7684\u7b49\u5f85\u961f\u5217\u7ed3\u70b9\u5b9a\u4e49\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<pre class=\"brush:c\">  \r\n \/\/yoursource\/include\/linux\/wait.h\r\n 50struct __wait_queue_head {\r\n  51        spinlock_t lock;\r\n  52        struct list_head task_list;\r\n  53};\r\n  54typedef struct __wait_queue_head wait_queue_head_t;<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u7b49\u5f85\u961f\u5217\u5934\u7ed3\u6784\u4e2d\u5c01\u88c5\u4e86list_head\u7ed3\u6784\u3002\u8fd9\u4e48\u505a\u662f\u53ef\u4ee5\u60f3\u8c61\u5230\u7684\uff0c\u56e0\u4e3a\u961f\u5217\u548c\u6808\u672c\u8d28\u4e0a\u8fd8\u662f\u53cc\u8054\u8868\u3002\u5f53\u6211\u4eec\u9002\u5f53\u9650\u5236\u53cc\u8054\u8868\u7684\u67d0\u4e9b\u64cd\u4f5c\u65f6\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u6837\u7684\u529f\u80fd\u3002\u53e6\u5916\uff0c\u7b49\u5f85\u961f\u5217\u5934\u7ed3\u6784\u4e2d\u8fd8\u6709\u4e00\u4e2a\u81ea\u65cb\u9501\u7ed3\u6784\u7684\u53d8\u91cflock\uff0c\u5b83\u8d77\u5230\u4e86\u5bf9\u7b49\u5f85\u961f\u5217\u8fdb\u884c\u4e92\u65a5\u64cd\u4f5c\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u5728\u7b49\u5f85\u961f\u5217\u7ed3\u6784\u4e2d\uff0c\u9664\u4e86\u4f7f\u7528list_head\u7ed3\u6784\u5916\uff0c\u8fd8\u6709\u4ee5\u4e0b\u51e0\u4e2a\u5b57\u6bb5\uff1a<\/p>\n<pre class=\"brush:c\">  28typedef struct __wait_queue wait_queue_t;\r\n  32struct __wait_queue {\r\n  33        unsigned int flags;\r\n  34#define WQ_FLAG_EXCLUSIVE       0x01\r\n  35        void *private;\r\n  36        wait_queue_func_t func;\r\n  37        struct list_head task_list;\r\n  38};<\/pre>\n<p>flag\uff1a\u6307\u660e\u8be5\u7b49\u5f85\u7684\u8fdb\u7a0b\u662f\u4e92\u65a5\u8fd8\u662f\u975e\u4e92\u65a5\uff0c\u4e3a0\u65f6\u975e\u4e92\u65a5\uff0c\u4e3a1\u65f6\u4e92\u65a5\uff1b<br \/>\nWQ_FLAG_EXCLUSIVE \uff1a\u4ece\u53d8\u91cf\u53ef\u4ee5\u770b\u51fa\uff0c\u6b64\u5b8f\u4ee3\u8868\u8fdb\u7a0b\u662f\u4e92\u65a5\u7684\uff1b<br \/>\nprivate\uff1avoid\u578b\u6307\u9488\u53d8\u91cf\u529f\u80fd\u5f3a\u5927\uff0c\u4f60\u53ef\u4ee5\u8d4b\u7ed9\u5b83\u4f60\u9700\u8981\u7684\u7ed3\u6784\u4f53\u6307\u9488\u3002\u4e00\u822c\u8d4b\u503c\u4e3atask_struct\u7c7b\u578b\u7684\u6307\u9488\uff0c\u4e5f\u5c31\u662f\u8bf4\u6307\u5411\u4e00\u4e2a\u8fdb\u7a0b\uff1b<br \/>\nfunc\uff1a\u51fd\u6570\u6307\u9488\uff0c\u6307\u5411\u8be5\u7b49\u5f85\u961f\u5217\u9879\u7684\u5524\u9192\u51fd\u6570\uff1b<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8ff0\u56fe\u6765\u8be6\u7ec6\u4e86\u89e3\u7b49\u5f85\u961f\u5217\u5934\u548c\u7b49\u5f85\u961f\u5217\u7684\u7ed3\u6784\u5173\u7cfb\uff1a<\/p>\n<p><a rel=\"attachment wp-att-1855\" href=\"http:\/\/edsionte.com\/techblog\/archives\/1854\/5850668_1\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-1855\" title=\"5850668_1\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/10\/5850668_1-300x78.png\" alt=\"\" width=\"300\" height=\"78\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/10\/5850668_1-300x78.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2010\/10\/5850668_1.png 600w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><strong>1.\u5b9a\u4e49\u53ca\u521d\u59cb\u5316<\/strong><\/p>\n<p>\u5185\u6838\u4e2d\u4f7f\u7528init_waitqueue_head\u5b8f\u6765\u521d\u59cb\u5316\u7b49\u5f85\u961f\u5217\u5934\u3002<\/p>\n<pre class=\"brush:c\">\/\/yourLinuxSourceDir\/include\/linux\/wait.h\r\n  82#define init_waitqueue_head(q)                          \\\r\n  83        do {                                            \\\r\n  84                static struct lock_class_key __key;     \\\r\n  85                                                        \\\r\n  86                __init_waitqueue_head((q), &amp;__key);     \\\r\n  87        } while (0)<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u5b8f\u4f7f\u7528\u4e86do-while\u578b\u5faa\u73af\u8bed\u53e5\uff0c\u91cc\u9762\u5305\u542b\u4e24\u6761\u8bed\u53e5\u3002\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u53d8\u91cf__key\uff0c\u7136\u540e\u518d\u8c03\u7528init_waitqueue_head\u51fd\u6570\u3002<\/p>\n<p>\u4e8b\u5b9e\u4e0a\uff0c\u8fd9\u4e2ado-while\u5faa\u73af\u8bed\u53e5\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u90a3\u4e48\uff0c\u4e3a\u4ec0\u4e48\u8981\u9009\u62e9\u4f7f\u7528\u8fd9\u4e2a\u5faa\u73af\u8bed\u53e5\uff1f\u5728\u5b9a\u4e49\u5b8f\u7684\u65f6\u5019\u5c06\u4e0a\u8ff0\u8bed\u53e5\u5d4c\u5957\u5728\u4e00\u4e2a\u5927\u62ec\u53f7\u91cc\u4e5f\u53ef\u4ee5\u554a\uff01\u53ef\u80fd\u6211\u4eec\u5982\u4e0b\u90a3\u6837\u4f7f\u7528\u4e00\u4e2a\u5b8f\uff1a<\/p>\n<pre class=\"brush:c\"> 10         if(conditon)\r\n 11                 init_waitqueue_head(q);\r\n 12         else\r\n 13                 do_somthing_else();<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u53bb\u9664do-while\uff0c\u90a3\u4e48\u66ff\u6362\u540e\u4f1a\u7f16\u8bd1\u9519\u8bef\u3002\u56e0\u4e3a\u5b8f\u672b\u5c3e\u7684\u5206\u53f7\u4f7f\u5f97else\u8bed\u53e5\u6210\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u53e5\u5b50\u3002\u4f60\u4e5f\u8bb8\u4f1a\u8bf4\uff0c\u90a3\u6211\u8fd9\u6837\u4f7f\u7528\uff1ainit_waitqueue_head(q)\u5c31\u53ef\u4ee5\u907f\u514d\u8fd9\u4e2a\u9519\u8bef\u4e86\u3002\u8fd9\u6837\u662f\u53ef\u4ee5\uff0c\u4f46\u662f\u5bf9\u4e8e\u90a3\u4e9b\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u5f88\u96be\u907f\u514d\u4ed6\u4eec\u8981\u52a0\u4e0a\u4e00\u4e2a;\u3002\u5e76\u4e14\uff0c\u5728\u832b\u832b\u4ee3\u7801\u4e2d\uff0c\u5b64\u96f6\u96f6\u7684\u51fa\u73b0\u4e00\u4e2a\u6ca1\u6709;\u7684\u8bed\u53e5\uff0c\u672a\u514d\u663e\u5f97\u6709\u4e9b\u5947\u602a\u3002<\/p>\n<p>\u8a00\u5f52\u6b63\u4f20\uff0c\u5728init_waitqueue_head\u5b8f\u4e2d\u8c03\u7528\u7684__init_waitqueue_head\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\"> 13void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *key)\r\n  14{\r\n  15        spin_lock_init(&amp;q-&gt;lock);\r\n  16        lockdep_set_class(&amp;q-&gt;lock, key);\r\n  17        INIT_LIST_HEAD(&amp;q-&gt;task_list);\r\n  18}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u9996\u5148\u5229\u7528\u81ea\u65cb\u9501\u521d\u59cb\u5316\u51fd\u6570\u521d\u59cb\u5316\u8fd9\u4e2a\u81ea\u65cb\u9501\uff1b\u5728\u4e0a\u8ff0\u7b49\u5f85\u961f\u5217\u5934\u7684\u5b9a\u4e49\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230task_list\u5b57\u6bb5\u662f\u4e00\u4e2alist_head\u7ed3\u6784\u7c7b\u578b\uff0c\u56e0\u6b64\u6211\u4eec\u4f7f\u7528INIT_LIST_HEAD\u5bf9\u8fd9\u4e2a\u94fe\u8868\u8fdb\u884c\u521d\u59cb\u5316\u3002\u8fd9\u4e9b\u8fc7\u7a0b\u90fd\u662f\u6211\u4eec\u6240\u719f\u6089\u7684\u3002<\/p>\n<p>\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u4f7f\u7528\u521d\u59cb\u5316\u5b8fDECLARE_WAIT_QUEUE_HEAD\u4e00\u6b65\u7684\u8fdb\u884c\u5b9a\u4e49\u548c\u521d\u59cb\u5316\u3002<\/p>\n<pre class=\"brush:c\">  70#define __WAIT_QUEUE_HEAD_INITIALIZER(name) {                           \\\r\n  71        .lock           = __SPIN_LOCK_UNLOCKED(name.lock),              \\\r\n  72        .task_list      = { &amp;(name).task_list, &amp;(name).task_list } }\r\n  73\r\n  74#define DECLARE_WAIT_QUEUE_HEAD(name) \\\r\n  75        wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)<\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5bf9\u4e8e\u6709C\u57fa\u7840\u7684\u4eba\u7406\u89e3\u8d77\u6765\u5e76\u4e0d\u56f0\u96be\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u5bf9task_list\u8fdb\u884c\u8d4b\u503c\u540e\uff0c\u8fd9\u4e2a\u7ed3\u70b9\u7684\u524d\u540e\u6307\u9488\u90fd\u4f1a\u6307\u5411\u81ea\u5df1\u3002<\/p>\n<p>\u540c\u6837\uff0c\u5bf9\u4e8e\u7b49\u5f85\u961f\u5217\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528DECLARE_WAITQUEUE\u5b8f\u6765\u5b9a\u4e49\u5e76\u521d\u59cb\u5316\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u9879\u3002<\/p>\n<pre class=\"brush:c\">  30int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);\r\n  62#define __WAITQUEUE_INITIALIZER(name, tsk) {                            \\\r\n  63        .private        = tsk,                                          \\\r\n  64        .func           = default_wake_function,                        \\\r\n  65        .task_list      = { NULL, NULL } }\r\n  66\r\n  67#define DECLARE_WAITQUEUE(name, tsk)                                    \\\r\n  68        wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)<\/pre>\n<p>name\u8981\u5b9a\u4e49\u7684\u7b49\u5f85\u961f\u5217\u9879\u7684\u540d\u79f0\uff1btsk\u662ftask_struct\u7c7b\u578b\u7684\u6307\u9488\u53d8\u91cf\uff0c\u5b83\u6307\u5411\u8fd9\u4e2a\u7b49\u5f85\u961f\u5217\u9879\u6240\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002<\/p>\n<p><strong>3.\u6dfb\u52a0\/\u79fb\u9664\u7b49\u5f85\u961f\u5217<\/strong><\/p>\n<p>add_wait_queue\u6dfb\u52a0\u51fd\u6570\u5c06\u7b49\u5f85\u961f\u5217wait\u6dfb\u52a0\u5230\u4ee5q\u4e3a\u7b49\u5f85\u961f\u5217\u5934\u7684\u90a3\u4e2a\u7b49\u5f85\u961f\u5217\u94fe\u8868\u4e2d\u3002<\/p>\n<pre class=\"brush:c\">22void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)\r\n23{\r\n24        unsigned long flags;\r\n25\r\n26        wait-&gt;flags &amp;= ~WQ_FLAG_EXCLUSIVE;\r\n27        spin_lock_irqsave(&amp;q-&gt;lock, flags);\r\n28        __add_wait_queue(q, wait);\r\n29        spin_unlock_irqrestore(&amp;q-&gt;lock, flags);\r\n30}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u770b\u5230flags\u7684\u7ed3\u679c\u5fc5\u7136\u662f0\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e2a\u51fd\u6570\u662f\u5c06\u975e\u4e92\u65a5\u8fdb\u7a0b\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u5f53\u4e2d\u3002\u800c\u4e14\u5728\u8c03\u7528\u5177\u4f53\u7684\u6dfb\u52a0\u51fd\u6570\u65f6\u5019\uff0c\u4f7f\u7528\u5173\u4e2d\u65ad\u5e76\u4fdd\u62a4\u73b0\u573a\u7684\u81ea\u65cb\u9501\u65b9\u5f0f\u4f7f\u5f97\u6dfb\u52a0\u64cd\u4f5c\u6bcf\u6b21\u53ea\u88ab\u4e00\u4e2a\u8fdb\u7a0b\u8bbf\u95ee\u3002<\/p>\n<p>\u5177\u4f53\u7684\u6dfb\u52a0\u8fc7\u7a0b\u662f\u5c06\u5f53\u524d\u8fdb\u7a0b\u6240\u5bf9\u5e94\u7684\u7b49\u5f85\u961f\u5217\u7ed3\u70b9wait\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u5934\u7ed3\u70b9q\u4e4b\u540e\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5c31\u662f\u5c06new-&gt;task_list\u7ed3\u70b9\u6dfb\u52a0\u5230\u4ee5head-&gt;task_list\u4e3a\u5934\u6307\u9488\u7684\u53cc\u94fe\u8868\u5f53\u4e2d\u3002\u53e6\u5916\uff0c\u901a\u8fc7add_wait_queue_exclusive\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u4e92\u65a5\u8fdb\u7a0b\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u5f53\u4e2d\u3002\u4ece\u6dfb\u52a0\u8fc7\u7a0b\u53ef\u4ee5\u53d1\u73b0\uff0cadd_wait_queue\u51fd\u6570\u4f1a\u5c06\u975e\u4e92\u65a5\u8fdb\u7a0b\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u7684\u524d\u90e8\u3002<\/p>\n<pre class=\"brush:c\"> 122static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)\r\n 123{\r\n 124        list_add(&amp;new-&gt;task_list, &amp;head-&gt;task_list);\r\n 125}<\/pre>\n<p>\u53e6\u5916\uff0c add_wait_queue_exclusive\u6dfb\u52a0\u51fd\u6570\u5219\u4f1a\u5c06\u4e92\u65a5\u8fdb\u7a0b\u6dfb\u52a0\u5230\u7b49\u5f85\u961f\u5217\u7684\u672b\u5c3e\u3002<\/p>\n<pre class=\"brush:c\">  33void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)\r\n  34{\r\n  35        unsigned long flags;\r\n  36\r\n  37        wait-&gt;flags |= WQ_FLAG_EXCLUSIVE;\r\n  38        spin_lock_irqsave(&amp;q-&gt;lock, flags);\r\n  39        __add_wait_queue_tail(q, wait);\r\n  40        spin_unlock_irqrestore(&amp;q-&gt;lock, flags);\r\n  41}<\/pre>\n<p>remove_wait_queue\u51fd\u6570\u7528\u4e8e\u5c06\u7b49\u5f85\u961f\u5217\u9879wait\u4ece\u4ee5q\u4e3a\u7b49\u5f85\u961f\u5217\u5934\u7684\u7b49\u5f85\u961f\u5217\u4e2d\u79fb\u9664\uff0c\u6e90\u7801\u5982\u4e0b\u3002<\/p>\n<pre class=\"brush:c\">  44void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)\r\n  45{\r\n  46        unsigned long flags;\r\n  47\r\n  48        spin_lock_irqsave(&amp;q-&gt;lock, flags);\r\n  49        __remove_wait_queue(q, wait);\r\n  50        spin_unlock_irqrestore(&amp;q-&gt;lock, flags);\r\n  51}\r\n 150static inline void __remove_wait_queue(wait_queue_head_t *head,\r\n 151                                                        wait_queue_t *old)\r\n 152{\r\n 153        list_del(&amp;old-&gt;task_list);\r\n 154}<\/pre>\n<p>\u6709\u4e86\u4e0a\u8ff0\u7684\u57fa\u7840\uff0c\u90a3\u4e48\u79fb\u9664\u51fd\u6570\u5c31\u7b80\u5355\u4e86\u8bb8\u591a\u3002<\/p>\n<p><strong>4.\u5728\u7b49\u5f85\u961f\u5217\u4e0a\u7761\u7720<\/strong><\/p>\n<p><strong> <\/strong>\u5982\u4f55\u5b9e\u73b0\u8fdb\u7a0b\u7684\u963b\u585e\uff1f\u5927\u81f4\u8fc7\u7a0b\u5c31\u662f\u5c06\u5f53\u524d\u8fdb\u7a0b\u7684\u72b6\u6001\u8bbe\u7f6e\u6210\u7761\u7720\u72b6\u6001\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u8fdb\u7a0b\u52a0\u5165\u5230\u7b49\u5f85\u961f\u5217\u5373\u53ef\u3002\u5728linux\u5185\u6838\u4e2d\u6709\u4e00\u7ec4\u51fd\u6570\u63a5\u53e3\u6765\u5b9e\u73b0\u8fd9\u4e2a\u529f\u80fd\u3002<\/p>\n<pre class=\"brush:c\">4313void __sched interruptible_sleep_on(wait_queue_head_t *q)\r\n4314{\r\n4315        sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);\r\n4316}\r\n4317EXPORT_SYMBOL(interruptible_sleep_on);\r\n4318\r\n4319long __sched\r\n4320interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)\r\n4321{\r\n4322        return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);\r\n4323}\r\n4324EXPORT_SYMBOL(interruptible_sleep_on_timeout);\r\n4325\r\n4326void __sched sleep_on(wait_queue_head_t *q)\r\n4327{\r\n4328        sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);\r\n4329}\r\n4330EXPORT_SYMBOL(sleep_on);\r\n4331\r\n4332long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)\r\n4333{\r\n4334        return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);\r\n4335}\r\n4336EXPORT_SYMBOL(sleep_on_timeout);<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u6e90\u7801\uff0c\u4f60\u53ef\u4ee5\u53d1\u73b0\u8fd9\u4e9b\u51fd\u6570\u5728\u5185\u90e8\u90fd\u8c03\u7528\u4e86sleep_on_common\u51fd\u6570\uff0c\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u503c\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u529f\u80fd\u3002\u800c\u8fd9\u4e2a\u901a\u7528\u51fd\u6570\u7684\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u5173\u6ce8\u7684\u662f\uff1a\u8fdb\u7a0b\u8981\u52a0\u5165\u5230\u90a3\u4e2a\u7b49\u5f85\u961f\u5217\uff1f\u8fdb\u7a0b\u662f\u90a3\u79cd\u7761\u7720\u72b6\u6001\uff08TASK_UNINTERRUPTIBLE\u8fd8\u662fTASK_INTERRUPTIBLE\uff09\uff1f\u8fdb\u7a0b\u7761\u7720\u7684\u65f6\u95f4\uff1f<\/p>\n<pre class=\"brush:c\">4292static long __sched\r\n4293sleep_on_common(wait_queue_head_t *q, int state, long timeout)\r\n4294{\r\n4295        unsigned long flags;\r\n4296        wait_queue_t wait;\r\n4297\r\n4298        init_waitqueue_entry(&amp;wait, current);\r\n4299\r\n4300        __set_current_state(state);\r\n4301\r\n4302        spin_lock_irqsave(&amp;q-&gt;lock, flags);\r\n4303        __add_wait_queue(q, &amp;wait);\r\n4304        spin_unlock(&amp;q-&gt;lock);\r\n4305        timeout = schedule_timeout(timeout);\r\n4306        spin_lock_irq(&amp;q-&gt;lock);\r\n4307        __remove_wait_queue(q, &amp;wait);\r\n4308        spin_unlock_irqrestore(&amp;q-&gt;lock, flags);\r\n4309\r\n4310        return timeout;\r\n4311}<\/pre>\n<p>\u5728\u6b64\u51fd\u6570\u4e2d\uff0c\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u7b49\u5f85\u961f\u5217\u9879\u7ed3\u70b9\uff0c\u901a\u8fc7 init_waitqueue_entry\u51fd\u6570\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\u3002\u53ef\u4ee5\u4ece\u4e0b\u8ff0\u521d\u59cb\u5316\u6e90\u7801\u4e2d\u770b\u5230\uff0c\u6b64\u65f6\u8be5\u7b49\u5f85\u961f\u5217\u9879\u6307\u5411\u5f53\u524d\u5f53\u524d\u8fdb\u7a0b\u3002\u800c\u5524\u9192\u51fd\u6570\u6307\u9488func\u5219\u6307\u5411\u5185\u6838\u81ea\u5b9a\u4e49\u7684\u4e00\u4e2a\u9ed8\u8ba4\u5524\u9192\u51fd\u6570default_wake_function\u3002<\/p>\n<pre class=\"brush:c\">  98static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)\r\n  99{\r\n 100        q-&gt;flags = 0;\r\n 101        q-&gt;private = p;\r\n 102        q-&gt;func = default_wake_function;\r\n 103}<\/pre>\n<p>\u521d\u59cb\u5316\u5b8c\u6bd5\u540e\uff0c\u901a\u8fc7__set_current_state\u51fd\u6570\u5c06\u5f53\u524d\u8fdb\u7a0b\u7684\u72b6\u6001\u8bbe\u7f6e\u6210state\u3002\u63a5\u7740\uff0c\u5728\u81ea\u65cb\u9501\u7684\u4fdd\u62a4\u4e0b\uff0c\u5c06\u5f53\u524d\u8fdb\u7a0b\u5bf9\u5e94\u7684\u7b49\u5f85\u961f\u5217\u7ed3\u70b9\u63d2\u5165\u5230\u7b49\u5f85\u961f\u5217\u94fe\u8868\u5f53\u4e2d\u3002\u66f4\u91cd\u8981\u7684\u662f\uff0c\u5728schedule_timeout\u51fd\u6570\u4e2d\u4e0d\u4ec5\u8981\u8bbe\u7f6e\u8fdb\u7a0b\u7684\u7761\u7720\u65f6\u95f4\uff08\u4ee5jiffies\u4e3a\u5355\u4f4d\u7684\uff09\uff0c\u8fd8\u8981\u4f7f\u7528schedule\u51fd\u6570\u8fdb\u884c\u91cd\u65b0\u8c03\u5ea6\u3002\u4e00\u65e6\u4f7f\u7528\u4e86schedule\u51fd\u6570\u540e\uff0c\u4e5f\u5c31\u610f\u5473\u8fd9\u5f53\u524d\u8fd9\u4e2a\u8fdb\u7a0b\u771f\u6b63\u7684\u7761\u7720\u4e86\uff0c\u90a3\u4e48\u63a5\u4e0b\u6765\u7684\u4ee3\u7801\u4f1a\u5728\u5b83\u5524\u9192\u540e\u6267\u884c\u3002<span style=\"font-size: 13.3333px;\">\u5f53\u8be5\u8fdb\u7a0b\u88ab\u5524\u9192\u540e\uff08\u8d44\u6e90\u53ef\u7528\u65f6\uff09\uff0c\u4f1a\u4ece\u7b49\u5f85\u961f\u5217\u4e2d\u5c06\u81ea\u5df1\u5bf9\u5e94\u7684\u90a3\u4e2a\u7b49\u5f85\u961f\u5217\u7ed3\u70b9wait\u79fb\u9664\u3002<\/span><\/p>\n<p>\u4e0a\u8ff0\u8fc7\u7a0b\u90fd\u662f\u5728\u81ea\u65cb\u9501\u4fdd\u62a4\u4e0b\u8fdb\u884c\u7684\uff0c\u5e76\u4e14\u5728\u6574\u4e2a\u6267\u884c\u8fc7\u7a0b\u4e2d\u4e0d\u53ef\u88ab\u5176\u4ed6\u4e2d\u65ad\u6240\u6253\u65ad\u3002\u73b0\u5728\u518d\u56de\u8fc7\u5934\u53bb\u770b\u4e00\u5f00\u59cb\u7684\u90a3\u56db\u4e2a\u7761\u7720\u51fd\u6570\u63a5\u53e3\uff0c\u4f60\u5c31\u660e\u767d\u4e86\u5b83\u4eec\u5404\u81ea\u7684\u4e0d\u540c\u4e4b\u5904\u4e86\u3002<\/p>\n<p><strong>5.\u5524\u9192\u51fd\u6570<\/strong><\/p>\n<p>\u5524\u9192\u51fd\u6570\u4f1a\u5524\u9192\u4ee5x\u4e3a\u5934\u7ed3\u70b9\u7684\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u7b49\u5f85\u961f\u5217\u9879\u6240\u5bf9\u5e94\u7684\u8fdb\u7a0b\u3002\u4e0e\u7761\u7720\u51fd\u6570\u7c7b\u4f3c\uff0c\u5185\u6838\u4e2d\u4e5f\u6709\u4e00\u7ec4\u51fd\u6570\u53ef\u4ee5\u5bf9\u963b\u585e\u7684\u8fdb\u7a0b\u8fdb\u884c\u5524\u9192\u3002<\/p>\n<pre class=\"brush:c\"> 170#define wake_up(x)                      __wake_up(x, TASK_NORMAL, 1, NULL)\r\n 171#define wake_up_nr(x, nr)               __wake_up(x, TASK_NORMAL, nr, NULL)\r\n 172#define wake_up_all(x)                  __wake_up(x, TASK_NORMAL, 0, NULL)\r\n\r\n 175#define wake_up_interruptible(x)        __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)\r\n 176#define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)\r\n 177#define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u8fd9\u4e9b\u5524\u9192\u51fd\u6570\u5747\u8c03\u7528\u4e86__wake_up\u51fd\u6570\u3002__wake_up\u51fd\u6570\u7684\u56db\u4e2a\u53c2\u6570\u5206\u522b\u6307\uff1a\u5934\u7ed3\u70b9\u6307\u9488\u3001\u5524\u9192\u8fdb\u7a0b\u7684\u7c7b\u578b\u3001\u5524\u9192\u8fdb\u7a0b\u7684\u6570\u91cf\u548c\u4e00\u4e2a\u9644\u52a0\u7684void\u578b\u6307\u9488\u53d8\u91cf\u3002<\/p>\n<pre class=\"brush:c\">3999void __wake_up(wait_queue_head_t *q, unsigned int mode,\r\n4000                        int nr_exclusive, void *key)\r\n4001{\r\n4002        unsigned long flags;\r\n4003\r\n4004        spin_lock_irqsave(&amp;q-&gt;lock, flags);\r\n4005        __wake_up_common(q, mode, nr_exclusive, 0, key);\r\n4006        spin_unlock_irqrestore(&amp;q-&gt;lock, flags);\r\n4007}<\/pre>\n<p>\u5728__wake_up\u51fd\u6570\u53c8\u901a\u8fc7\u4f20\u9012\u4e0d\u540c\u7684\u53c2\u6570\u8c03\u7528__wake_up_common\u51fd\u6570\u6765\u5b9e\u73b0\u4e0d\u540c\u7684\u5524\u9192\u529f\u80fd\u3002<\/p>\n<pre class=\"brush:c\">3975static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,\r\n3976                        int nr_exclusive, int wake_flags, void *key)\r\n3977{\r\n3978        wait_queue_t *curr, *next;\r\n3979\r\n3980        list_for_each_entry_safe(curr, next, &amp;q-&gt;task_list, task_list) {\r\n3981                unsigned flags = curr-&gt;flags;\r\n3982\r\n3983                if (curr-&gt;func(curr, mode, wake_flags, key) &amp;&amp;\r\n3984                                (flags &amp; WQ_FLAG_EXCLUSIVE) &amp;&amp; !--nr_exclusive)\r\n3985                        break;\r\n3986        }\r\n3987}<\/pre>\n<p>list_for_each_entry_safe\u51fd\u6570\u5c06\u904d\u5386\u6574\u4e2a\u7b49\u5f85\u961f\u5217\u4e2d\u7684\u94fe\u8868\uff0c\u901a\u8fc7\u6bcf\u6b21\u7684\u903b\u8f91\u5224\u65ad\u6765\u5524\u9192\u76f8\u5e94\u7684\u8fdb\u7a0b\u3002\u8fd9\u4e2aif\u8bed\u53e5\u521d\u770b\u8d77\u6765\u6709\u70b9\u9ebb\u70e6\uff0c\u4e0d\u8fc7\u6211\u4eec\u4e00\u70b9\u4e00\u70b9\u7684\u5c06\u5b83\u62c6\u89e3\u3002<\/p>\n<p>curr-&gt;func(curr, mode, sync, key)\uff1a\u5373\u6267\u884c\u9ed8\u8ba4\u7684\u5524\u9192\u51fd\u6570\uff0c\u5c06\u6307\u5b9a\u7684\u8fdb\u7a0bcurr\u4ee5mode\u65b9\u5f0f\u5524\u9192\u3002\u6210\u529f\u5524\u9192\u8fd4\u56de1\uff1b\u5426\u5219\uff0c\u8fd4\u56de0\uff1b<br \/>\n(flags &amp; WQ_FLAG_EXCLUSIVE)\uff1a\u5224\u65ad\u5f53\u524d\u8fdb\u7a0b\u662f\u5426\u4ee5\u4e92\u65a5\u5f62\u5f0f\u5524\u9192\u3002\u662f\u4e92\u65a5\u5f62\u5f0f\u5219\u8fd4\u56de1\uff1b\u5426\u5219\u8fd4\u56de0\uff1b<br \/>\n!&#8211;nr_exclusive\uff1anr_exclusive\u4e3a\u9700\u8981\u5524\u9192\u7684\u4e92\u65a5\u8fdb\u7a0b\u7684\u6570\u91cf\u3002<\/p>\n<p>\u8fd9\u4e09\u4e2a\u90e8\u5206\u662f\u901a\u8fc7\u903b\u8f91\u4e0e\u8fde\u63a5\u8d77\u6765\u7684\u3002\u6839\u636e\u903b\u8f91\u4e0e\u7684\u8fd0\u7b97\u89c4\u5219\uff0c\u53ea\u6709\u5f53\u7b2c\u4e00\u90e8\u5206\u4e3a\u771f\u65f6\u624d\u4f1a\u5224\u65ad\u7b2c\u4e8c\u90e8\u5206\u7684\u503c\uff0c\u4f9d\u6b21\u518d\u5224\u65ad\u7b2c\u4e09\u90e8\u5206\u7684\u503c\u3002<\/p>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u7684\u7b49\u5f85\u961f\u5217\u7684\u6dfb\u52a0\u8fc7\u7a0b\u6211\u4eec\u77e5\u9053\uff0c\u7b49\u5f85\u961f\u5217\u4e2d\u524d\u9762\u5747\u662f\u975e\u4e92\u65a5\u8fdb\u7a0b\uff0c\u540e\u9762\u624d\u662f\u4e92\u65a5\u8fdb\u7a0b\u3002\u56e0\u6b64\uff0c\u5524\u9192\u51fd\u6570\u603b\u5148\u5524\u9192\u5168\u90e8\u7684\u975e\u4e92\u65a5\u8fdb\u7a0b\u3002\u56e0\u4e3a\u5f53__wake_up_commom\u51fd\u6570\u6bcf\u4e00\u6b21\u53bb\u5224\u65adif\u8bed\u65f6\uff0c\u603b\u4f1a\u201c\u4e0d\u81ea\u89c9\u201d\u53bb\u6267\u884c\u9ed8\u8ba4\u7684\u5524\u9192\u51fd\u6570\uff08\u9664\u975e\u5524\u9192\u5931\u8d25\uff0c\u90a3\u4e48\u4f1a\u9000\u51fa\u904d\u5386\u5b8f\uff09\uff1b\u5f53\u5168\u90e8\u7684\u975e\u4e92\u65a5\u8fdb\u7a0b\u88ab\u5524\u9192\u540e\uff0c\u7b2c\u4e8c\u4e2a\u5224\u65ad\u6761\u4ef6\u4e5f\u5c31\u6210\u7acb\u4e86\u3002\u56e0\u6b64__wake_up_commom\u51fd\u6570\u4f1a\u4f9d\u6b21\u5524\u9192nr_exclusive\u4e2a\u4e92\u65a5\u8fdb\u7a0b\uff1b\u5f53&#8211;nr_exclusive\u4e3a0\u65f6\uff08!&#8211;nr_exclusive\u4e5f\u5c31\u4e3a\u771f\uff09\uff0c\u6574\u4e2a\u904d\u5386\u8fc7\u7a0b\u4e5f\u6070\u597d\u7ed3\u675f\uff0c\u800c\u6b64\u65f6if\u8bed\u53e5\u7684\u4e09\u4e2a\u5224\u65ad\u6761\u4ef6\u624d\u521a\u597d\u6ee1\u8db3\uff08<strong>\u8fd9\u6bb5\u4ee3\u7801\u592a\u5f3a\u5927\u4e86!!!<\/strong>\uff09\u3002<\/p>\n<p><strong>6.\u6709\u6761\u4ef6\u7684\u7761\u7720<\/strong><\/p>\n<p>\u4e0e\u7761\u7720\u51fd\u6570\u4e0d\u540c\uff0c\u6761\u4ef6\u7761\u7720\u662f\u6307\u5f53\u67d0\u4e9b\u6761\u4ef6\u53d1\u751f\u65f6\uff0c\u8fd9\u4e2a\u8fdb\u7a0b\u624d\u4f1a\u52a0\u5165\u5230\u7b49\u5f85\u961f\u5217\u5f53\u4e2d\u3002\u5173\u4e8e\u6761\u4ef6\u7761\u7720\u6709\u4e0b\u5217\u7684\u5b8f\uff1a<\/p>\n<pre class=\"brush:c\">wait_event(wq, condition)\r\nwait_event_timeout(wq, condition, timeout)\r\nwait_event_interruptible(wq, condition)\r\nwait_event_interruptible_timeout(wq, condition, timeout)<\/pre>\n<p>\u5173\u4e8e\u6761\u4ef6\u7761\u7720\uff0c\u867d\u7136\u51fd\u6570\u5b9e\u73b0\u4e0e\u7761\u7720\u51fd\u6570\u4e0d\u540c\uff0c\u4f46\u662f\u57fa\u672c\u601d\u60f3\u662f\u76f8\u4f3c\u7684\uff0c\u5404\u4f4d\u53ef\u4ee5\u67e5\u627e\u76f8\u5e94\u7684\u6e90\u7801\u8fdb\u884c\u5206\u6790\u3002Try\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6b63\u5982list_head\u7ed3\u6784\u90a3\u6837\uff0c\u7b49\u5f85\u961f\u5217(wait queue)\u4f5c\u4e3alinux\u5185\u6838\u4e2d\u7684\u57fa\u7840\u6570\u636e\u7ed3\u6784\uff0c\u4e0e\u8fdb\u7a0b\u8c03\u5ea6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[110],"tags":[134,171],"class_list":["post-1854","post","type-post","status-publish","format-standard","hentry","category-linux","tag-kernel","tag-171"],"views":30568,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=1854"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/1854\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=1854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=1854"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=1854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}