{"id":49956,"date":"2025-07-30T20:42:40","date_gmt":"2025-07-30T12:42:40","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/49956.html"},"modified":"2025-07-30T20:42:40","modified_gmt":"2025-07-30T12:42:40","slug":"%e5%8d%8f%e7%a8%8b%e5%ba%93%ef%bc%88%e6%a8%a1%e5%9d%97%e7%9a%84%e8%af%a6%e8%a7%a3%e4%b8%8e%e4%bb%a3%e7%a0%81%e5%88%86%e6%9e%90%ef%bc%89ioscheduler%e7%b1%bbioscheduler%e7%9a%84%e7%bb%93%e5%90%88","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/49956.html","title":{"rendered":"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7"},"content":{"rendered":"<\/p>\n<h4>\u6587\u7ae0\u76ee\u5f55<\/h4>\n<ul>\n<li>\u4e3a\u4ec0\u4e48\u9700\u8981io&#043;\u534f\u7a0b&#xff1f;<\/li>\n<li>IO\u534f\u7a0b\u8c03\u5ea6\u662f\u4ec0\u4e48&#xff0c;\u6709\u4ec0\u4e48\u529f\u80fd&#xff1f;<\/li>\n<li>sylar\u7684IO\u534f\u7a0b\u8c03\u5ea6\u6a21\u5757\u57fa\u4e8e\u4ec0\u4e48&#xff1f;<\/li>\n<li>IO\u534f\u7a0b\u8c03\u5ea6\u4e3a\u4ec0\u4e48\u90fd\u5305\u542b\u4e00\u4e2a\u4e09\u5143\u7ec4\u4fe1\u606f&#xff1f;<\/li>\n<li>\n<ul>\n<li>Fdcontext\u90e8\u5206&#xff1a;<\/li>\n<li>Eventcontext\u90e8\u5206&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>IO\u534f\u7a0b\u8c03\u5ea6\u5668\u56e0\u4e3aTImer\u7c7b\u5bf9idle\u534f\u7a0b\u7684\u6539\u9020<\/li>\n<li>io&#043;\u534f\u7a0b\u548c\u534f\u7a0b\u8c03\u5ea6\u5668\u90e8\u5206\u6709\u4ec0\u4e48\u4e0d\u4e00\u6837&#xff1f;<\/li>\n<li>ioscheduler.h<\/li>\n<li>ioscheduler.cpp<\/li>\n<li>\n<ul>\n<li>IOManager::GetThis():<\/li>\n<li>\n<ul>\n<li>\u8865\u5145dynamic_cast\u548cstatic_cast&#xff1a;<\/li>\n<\/ul>\n<\/li>\n<li>IOManager:FdContext::getEventContext(Event event):<\/li>\n<li>IOManager::resetEventContext:<\/li>\n<li>IOManager::triggerEvent:<\/li>\n<li>IOManager\u7684\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570&#xff1a;<\/li>\n<li>\n<ul>\n<li>epoll<\/li>\n<li>Fcntl\u51fd\u6570<\/li>\n<\/ul>\n<\/li>\n<li>IOManager::ContextResize():<\/li>\n<li>\u91cd\u70b9\u7684IOManager::addevent\u51fd\u6570:<\/li>\n<li>IOManager::delEvent\u51fd\u6570&#xff1a;<\/li>\n<li>IOManager::cancelEvent\u51fd\u6570&#xff1a;<\/li>\n<li>IOManager::cancelAll():<\/li>\n<li>IOManager::tickle():<\/li>\n<li>IOManager::stopping():<\/li>\n<li>IOManager::idle():<\/li>\n<li>IOManager::onTimerInsertedAtFront():<\/li>\n<li>main.cpp<\/li>\n<li>\u6d4b\u8bd5\u7ed3\u679c<\/li>\n<li>\u51fd\u6570\u8c03\u7528\u56fe<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u9879\u76ee\u4ee3\u7801\u4ed3\u5e93<\/p>\n<h2>\u4e3a\u4ec0\u4e48\u9700\u8981io&#043;\u534f\u7a0b&#xff1f;<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124231-688a133746bfc.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h2>IO\u534f\u7a0b\u8c03\u5ea6\u662f\u4ec0\u4e48&#xff0c;\u6709\u4ec0\u4e48\u529f\u80fd&#xff1f;<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124231-688a1337cb162.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u4e8b\u5b9e\u4e0a\u5f88\u591a\u7684\u5e93\u90fd\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u5de5\u4f5c&#xff0c;\u6bd4\u5982libevent&#xff0c;libuv&#xff0c;libev\u7b49&#xff0c;\u8fd9\u4e9b\u5e93\u88ab\u79f0\u4e3a\u5f02\u6b65\u4e8b\u4ef6\u5e93\u6216IO\u5e93&#xff0c;\u4ece\u7f51\u7edc\u4e0a\u53ef\u4ee5\u641c\u7d22\u5230\u5f88\u591a\u8d44\u6599\u3002\u6df1\u5165\u6d45\u51fa\u7406\u89e3libevent\u2014\u20142\u4e07\u5b57\u603b\u7ed3_libev \u5806-CSDN\u535a\u5ba2\u7b49&#xff0c;\u6709\u7684\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406socketfd\u4e8b\u4ef6&#xff0c;\u8fd8\u53ef\u4ee5\u5904\u7406\u5b9a\u65f6\u5668\u4e8b\u4ef6\u548c\u4fe1\u53f7\u4e8b\u4ef6\u3002\u8fd9\u4e9b\u4e8b\u4ef6\u5e93\u7684\u5b9e\u73b0\u539f\u7406\u57fa\u672c\u7c7b\u4f3c&#xff0c;\u90fd\u662f\u5148\u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u72b6\u6001&#xff0c;\u7136\u540e\u5c06\u5957\u63a5\u5b57\u4e0e\u56de\u8c03\u51fd\u6570\u7ed1\u5b9a&#xff0c;\u63a5\u4e0b\u6765\u8fdb\u5165\u4e00\u4e2a\u57fa\u4e8eIO\u591a\u8def\u590d\u7528\u7684\u4e8b\u4ef6\u5faa\u73af&#xff0c;\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u7136\u540e\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<h2>sylar\u7684IO\u534f\u7a0b\u8c03\u5ea6\u6a21\u5757\u57fa\u4e8e\u4ec0\u4e48&#xff1f;<\/h2>\n<p>\u57fa\u4e8eepoll\u5b9e\u73b0&#xff0c;\u53ea\u652f\u6301linux\u5e73\u53f0\u3002\u5bf9\u6bcf\u4e2afd&#xff0c;sylar\u652f\u6301\u4e24\u7c7b\u4e8b\u4ef6&#xff0c;\u4e00\u7c7b\u662f\u53ef\u8bfb\u4e8b\u4ef6&#xff0c;\u5bf9\u5e94EPOLLIN&#xff0c;\u4e00\u7c7b\u662f\u53ef\u5199\u4e8b\u4ef6&#xff0c;\u5bf9\u5e94EPOLLOUT&#xff0c;sylar\u7684\u4e8b\u4ef6\u679a\u4e3e\u503c\u76f4\u63a5\u7ee7\u627f\u5b50epoll\u3002<\/p>\n<p>\u5f53\u7136epoll\u9664\u4e86\u672c\u8eab\u652f\u6301epollin\u548cepollout\u4e24\u7c7b\u4e8b\u4ef6\u5916&#xff0c;\u8fd8\u652f\u6301\u5176\u4ed6\u4e8b\u4ef6&#xff0c;\u6bd4\u5982epollrdhup&#xff08;\u5bf9\u7aef\u5173\u95ed&#xff09;&#xff0c;epollerr(\u9519\u8bef\u4e8b\u4ef6)&#xff0c;epollhup(\u6302\u8d77\u4e8b\u4ef6)\u3002\u5bf9\u4e8e\u8fd9\u4e9b\u4e8b\u4ef6&#xff0c;sylar\u7684\u505a\u6cd5\u662f\u5bf9\u5176\u8fdb\u884c\u5f52\u7c7b&#xff0c;\u5206\u522b\u5bf9\u5e94\u5230epollin\u548cepollout\u4e2d&#xff0c;\u4e5f\u5c31\u662f\u6240\u6709\u7684\u4e8b\u4ef6\u90fd\u53ef\u4ee5\u8868\u793a\u4e3a\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6&#xff0c;\u751a\u81f3\u6709\u7684\u4e8b\u4ef6\u8fd8\u53ef\u4ee5\u540c\u65f6\u8868\u793a\u53ef\u8bfb\u6216\u53ef\u5199\u3002\u6bd4\u5982epollerr\u4e8b\u4ef6\u53d1\u751f&#xff0c;fd\u540c\u65f6\u53ef\u8bfb\u6216\u53ef\u5199\u3002<\/p>\n<h2>IO\u534f\u7a0b\u8c03\u5ea6\u4e3a\u4ec0\u4e48\u90fd\u5305\u542b\u4e00\u4e2a\u4e09\u5143\u7ec4\u4fe1\u606f&#xff1f;<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124232-688a13386332e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5185\u90e8\u679a\u4e3e<\/span><br \/>\n    <span class=\"token comment\">\/\/ Event \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b&#xff0c;\u8868\u793a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6\u7c7b\u578b<\/span><br \/>\n    <span class=\"token keyword\">enum<\/span> <span class=\"token class-name\">Event<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8868\u793a\u6ca1\u6709\u4e8b\u4ef6<\/span><br \/>\n        NONE <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x0<\/span><span class=\"token punctuation\">,<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u8868\u793a\u8bfb\u4e8b\u4ef6&#xff0c;\u5bf9\u5e94\u4e8e epoll \u7684 EPOLLIN \u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token comment\">\/\/ READ &#061;&#061; EPOLLIN<\/span><br \/>\n        READ <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x1<\/span><span class=\"token punctuation\">,<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u8868\u793a\u5199\u4e8b\u4ef6&#xff0c;\u5bf9\u5e94\u4e8e epoll \u7684 EPOLLOUT \u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token comment\">\/\/ WRITE &#061;&#061; EPOLLOUT<\/span><br \/>\n        WRITE <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x4<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587<\/span><br \/>\n    <span class=\"token comment\">\/\/ FdContext \u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u3002\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u6709\u4e24\u4e2a\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff1a;read \u548c write&#xff0c;\u5206\u522b\u5bf9\u5e94\u8bfb\u4e8b\u4ef6\u548c\u5199\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">FdContext<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u63cf\u8ff0\u4e00\u4e2a\u5177\u4f53\u4e8b\u4ef6\u7684\u4e0a\u4e0b\u6587&#xff0c;\u5982\u8bfb\u4e8b\u4ef6\u6216\u5199\u4e8b\u4ef6 <\/span><br \/>\n        <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">EventContext<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ scheduler<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5173\u8054\u7684\u8c03\u5ea6\u5668 <\/span><br \/>\n            Scheduler<span class=\"token operator\">*<\/span> scheduler <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ callback fiber<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5173\u8054\u7684\u56de\u8c03\u7ebf\u7a0b&#xff08;\u534f\u7a0b&#xff09; <\/span><br \/>\n            std<span class=\"token operator\">::<\/span>shared_ptr<span class=\"token operator\">&lt;<\/span>Fiber<span class=\"token operator\">&gt;<\/span> fiber<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ callback function<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5173\u8054\u7684\u56de\u8c03\u51fd\u6570<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span> cb<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ read event context<\/span><br \/>\n        <span class=\"token comment\">\/\/ read \u548cwrite\u8868\u793a\u8bfb\u548c\u5199\u7684\u4e0a\u4e0b\u6587<\/span><br \/>\n        EventContext read<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ write event context<\/span><br \/>\n        EventContext write<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> fd <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ events registered<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5f53\u524d\u6ce8\u518c\u7684\u4e8b\u4ef6\u76ee\u524d\u662f\u6ca1\u6709\u4e8b\u4ef6&#xff0c;\u4f46\u53ef\u80fd\u53d8\u6210 READ\u3001WRITE \u6216\u4e8c\u8005\u7684\u7ec4\u5408\u3002<\/span><br \/>\n        Event events <span class=\"token operator\">&#061;<\/span> NONE<span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>mutex mutex<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u6839\u636e\u4e8b\u4ef6\u7c7b\u578b\u83b7\u53d6\u76f8\u5e94\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff08;\u5982\u8bfb\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u6216\u5199\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff09;<\/span><br \/>\n        EventContext<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">getEventContext<\/span><span class=\"token punctuation\">(<\/span>Event event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u91cd\u7f6e\u4e8b\u4ef6\u4e0a\u4e0b\u6587<\/span><br \/>\n        <span class=\"token keyword\">void<\/span> <span class=\"token function\">resetEventContext<\/span><span class=\"token punctuation\">(<\/span>EventContext<span class=\"token operator\">&amp;<\/span> ctx<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u89e6\u53d1\u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token keyword\">void<\/span> <span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>Event event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h3>Fdcontext\u90e8\u5206&#xff1a;<\/h3>\n<p>Fdcontext,\u9996\u5148\u8bb0\u5f55\u4e86fd\u548cevent(\u8bfb\u5199\u7ec4\u5408\u3001\u8bfb\u6216\u5199\u3001\u65e0\u4e8b\u4ef6)&#xff0c;\u5f88\u6b63\u5e38\u4ee5\u4e3a\u6211\u4eecio&#043;\u534f\u7a0b\u7684\u8bbe\u8ba1\u76ee\u7684\u5c31\u662f\u5229\u7528epoll\u7684IO\u591a\u8def\u590d\u7528&#xff0c;\u8ba9socket\u4e2d\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u7ed1\u5b9asylar\u4e2d\u89c4\u5b9a\u7684\u4e8b\u4ef6\u53ef\u8bfb\u6216\u53ef\u5199\u6216\u7ec4\u5408\u7684\u60c5\u51b5&#xff0c;\u6240\u4ee5\u5728\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u89c1Fdcontext\u6709fd&#xff0c;event\u7684\u6210\u5458\u53d8\u91cf&#xff0c;Fdcontext\u4e5f\u53eb\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u4e0b\u6587\u5177\u4f53\u7528\u6765\u5b58\u653efd\u548c\u76f8\u5e94\u7684\u8bfb\u5199\u4e8b\u4ef6<\/p>\n<h3>Eventcontext\u90e8\u5206&#xff1a;<\/h3>\n<p>Eventcontext\u662f\u4e3a\u4e86\u5728fd\u89e6\u53d1\u4e86event\u4e8b\u4ef6(\u8bfb\u6216\u5199\u6216\u8bfb\u5199\u7ec4\u5408)\u7684\u65f6\u5019\u5177\u4f53\u8ba9\u6267\u884c\u7684\u7cfb\u7edf\u8c03\u7528\u5982read(),write(),send()\u4f5c\u4e3a\u534f\u7a0b\u7684\u5165\u53e3\u51fd\u6570\u8fdb\u884c\u4efb\u52a1\u7684\u8c03\u5ea6&#xff0c;\u76ee\u7684\u662f\u63d0\u9ad8IO\u7b49\u5f85read()&#xff0c;write\u6570\u636e\u8f83\u591a\u7684\u65f6\u5019\u53ef\u4ee5\u5148\u89e3\u51b3\u5c0f\u4efb\u52a1&#xff0c;\u589e\u52a0\u7075\u6d3b\u6027\u548c\u63d0\u9ad8\u6548\u7387\u3002\u6240\u4ee5Eventcontext\u6709\u51fd\u6570\u5bf9\u8c61\u548c\u534f\u7a0b&#xff0c;\u534f\u7a0b\u8c03\u5ea6\u5668&#xff0c;\u76ee\u7684\u662f\u4e3a\u4e86\u5728\u89e6\u53d1\u4e8b\u4ef6\u540e\u627e\u5230\u5bf9\u5e94\u7684\u8c03\u5ea6\u5668\u5bf9\u8c61\u53bb\u8fd0\u884c\u534f\u7a0b\u6216\u8005\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u7684\u5165\u53e3\u51fd\u6570(read(),write())\u3002<\/p>\n<h2>IO\u534f\u7a0b\u8c03\u5ea6\u5668\u56e0\u4e3aTImer\u7c7b\u5bf9idle\u534f\u7a0b\u7684\u6539\u9020<\/h2>\n<p>IO\u534f\u7a0b\u8c03\u5ea6\u5668\u5728idle\u65f6\u4f1aepoll_wait\u6240\u6709\u6ce8\u518c\u7684fd&#xff0c;\u5982\u679c\u6709fd\u6ee1\u8db3\u6761\u4ef6&#xff0c;epoll_wait\u8fd4\u56de&#xff0c;\u4ece\u79c1\u6709\u6570\u636e\u4e2d\u62ff\u5230fd\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f&#xff0c;\u5e76\u4e14\u6267\u884c\u5176\u4e2d\u7684\u56de\u8c03\u51fd\u6570\u3002(\u5b9e\u9645\u662fidle\u534f\u7a0b\u53ea\u8d1f\u8d23\u6536\u96c6\u6240\u6709\u5df2\u89e6\u53d1\u7684fd\u7684\u56de\u8c03\u51fd\u6570\u5e76\u5c06\u5176\u52a0\u5165\u8c03\u5ea6\u5668\u7684\u4efb\u52a1\u961f\u5217&#xff0c;\u771f\u6b63\u7684\u6267\u884c\u65f6\u673a\u662fidle\u534f\u7a0b\u9000\u51fa\u540e&#xff0c;\u8c03\u5ea6\u5668\u5728\u4e0b\u4e00\u8f6e\u8c03\u5ea6\u65f6\u6267\u884c)\u3002<\/p>\n<h2>io&#043;\u534f\u7a0b\u548c\u534f\u7a0b\u8c03\u5ea6\u5668\u90e8\u5206\u6709\u4ec0\u4e48\u4e0d\u4e00\u6837&#xff1f;<\/h2>\n<p>IO\u534f\u7a0b\u8c03\u5ea6\u5668\u652f\u6301\u53d6\u6d88\u4e8b\u4ef6\u3002 \u53d6\u6d88\u4e8b\u4ef6\u8868\u793a\u4e0d\u5173\u5fc3\u67d0\u4e2afd\u7684\u67d0\u4e2a\u4e8b\u4ef6\u4e86&#xff0c;\u5982\u679c\u67d0\u4e2afd\u7684 \u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6\u90fd\u88ab\u53d6\u6d88\u4e86&#xff0c;\u90a3\u8fd9\u4e2afd\u4f1a\u4ece\u8c03\u5ea6\u5668\u7684epoll_wait\u4e2d\u5220\u9664\u3002 \u652f\u6301\u4f7f\u7528IO\u4e8b\u4ef6\u8c03\u5ea6&#xff0c;\u9488\u5bf9\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26&#xff0c;\u5c06\u63cf\u8ff0\u7b26\u6ce8\u518c\u53ef\u8bfb\u548c\u53ef\u5199\u4e8b\u4ef6\u7684\u56de\u5230\u51fd\u6570&#xff0c;\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\u6267\u884c\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002\u591a\u4e86\u4e00\u4e2a\u5916\u6302\u7684\u5b9a\u65f6\u5668\u5904\u7406\u5b9a\u65f6\u7684\u4efb\u52a1\u4e3b\u8981\u662f\u5904\u7406sleep\u3001usleep\u7b49\u3002<\/p>\n<h2>ioscheduler.h<\/h2>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">ifndef<\/span> <span class=\"token expression\">__SYLAR_IOMANAGER_H__<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">define<\/span> <span class=\"token macro-name\">__SYLAR_IOMANAGER_H__<\/span><\/span><\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;scheduler.h&#034;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;timer.h&#034;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">namespace<\/span> sylar <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token comment\">\/\/ work flow<\/span><br \/>\n<span class=\"token comment\">\/\/ 1 register one event -&gt; 2 wait for it to ready -&gt; 3 schedule the callback -&gt; 4 unregister the event -&gt; 5 run the callback<\/span><br \/>\n<span class=\"token comment\">\/\/ 1 \u6ce8\u518c\u4e8b\u4ef6 -&gt; 2 \u7b49\u5f85\u4e8b\u4ef6 -&gt; 3 \u4e8b\u4ef6\u89e6\u53d1\u8c03\u5ea6\u56de\u8c03 -&gt; 4 \u6ce8\u9500\u4e8b\u4ef6\u56de\u8c03\u540e\u4eceepoll\u6ce8\u9500 -&gt; 5 \u6267\u884c\u56de\u8c03\u8fdb\u5165\u8c03\u5ea6\u5668\u4e2d\u6267\u884c\u8c03\u5ea6\u3002<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4e3b\u8981\u7528\u4e8e\u7ba1\u7406\u5f02\u6b65 IO \u4e8b\u4ef6&#xff0c;\u5e76\u7ed3\u5408\u8c03\u5ea6\u5668\u548c\u5b9a\u65f6\u5668\u529f\u80fd\u8fdb\u884c\u9ad8\u6548\u7684\u4e8b\u4ef6\u5904\u7406\u3002\u8be5\u7c7b\u57fa\u4e8e\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u8bbe\u8ba1&#xff0c;\u9002\u7528\u4e8e\u9ad8\u6027\u80fd\u7684\u7f51\u7edc\u6216\u6587\u4ef6\u63cf\u8ff0\u7b26\u7ba1\u7406<\/span><br \/>\n<span class=\"token comment\">\/\/ IOManager \u7c7b\u7ee7\u627f\u81ea Scheduler \u548c TimerManager&#xff0c;\u56e0\u6b64\u5b83\u5177\u6709\u8c03\u5ea6\u4efb\u52a1\u548c\u7ba1\u7406\u5b9a\u65f6\u5668\u7684\u80fd\u529b\u3002<\/span><br \/>\n<span class=\"token keyword\">class<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">:<\/span> <span class=\"token base-clause\"><span class=\"token keyword\">public<\/span> <span class=\"token class-name\">Scheduler<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">public<\/span> <span class=\"token class-name\">TimerManager<\/span><\/span> <span class=\"token punctuation\">{<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5185\u90e8\u679a\u4e3e<\/span><br \/>\n    <span class=\"token comment\">\/\/ Event \u662f\u4e00\u4e2a\u679a\u4e3e\u7c7b\u578b&#xff0c;\u8868\u793a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u4e8b\u4ef6\u7c7b\u578b<\/span><br \/>\n    <span class=\"token keyword\">enum<\/span> <span class=\"token class-name\">Event<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8868\u793a\u6ca1\u6709\u4e8b\u4ef6<\/span><br \/>\n        NONE <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x0<\/span><span class=\"token punctuation\">,<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u8868\u793a\u8bfb\u4e8b\u4ef6&#xff0c;\u5bf9\u5e94\u4e8e epoll \u7684 EPOLLIN \u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token comment\">\/\/ READ &#061;&#061; EPOLLIN<\/span><br \/>\n        READ <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x1<\/span><span class=\"token punctuation\">,<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u8868\u793a\u5199\u4e8b\u4ef6&#xff0c;\u5bf9\u5e94\u4e8e epoll \u7684 EPOLLOUT \u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token comment\">\/\/ WRITE &#061;&#061; EPOLLOUT<\/span><br \/>\n        WRITE <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0x4<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u7528\u4e8e\u63cf\u8ff0\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587<\/span><br \/>\n    <span class=\"token comment\">\/\/ FdContext \u7ed3\u6784\u4f53\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u3002\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u53ef\u4ee5\u6709\u4e24\u4e2a\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff1a;read \u548c write&#xff0c;\u5206\u522b\u5bf9\u5e94\u8bfb\u4e8b\u4ef6\u548c\u5199\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">FdContext<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u63cf\u8ff0\u4e00\u4e2a\u5177\u4f53\u4e8b\u4ef6\u7684\u4e0a\u4e0b\u6587&#xff0c;\u5982\u8bfb\u4e8b\u4ef6\u6216\u5199\u4e8b\u4ef6 <\/span><br \/>\n        <span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">EventContext<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ scheduler<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5173\u8054\u7684\u8c03\u5ea6\u5668 \u7528\u4e8e\u5b89\u6392\u56de\u8c03\u7684\u6267\u884c <\/span><br \/>\n            Scheduler<span class=\"token operator\">*<\/span> scheduler <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ callback fiber<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u534f\u7a0b\u5bf9\u8c61&#xff0c;\u8868\u793a\u56de\u8c03\u51fd\u6570\u8fd0\u884c\u65f6\u7684\u4e0a\u4e0b\u6587\u3002<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>shared_ptr<span class=\"token operator\">&lt;<\/span>Fiber<span class=\"token operator\">&gt;<\/span> fiber<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ callback function<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5173\u8054\u7684\u56de\u8c03\u51fd\u6570 \u4e8b\u4ef6\u89e6\u53d1\u65f6\u4f1a\u6267\u884c\u8be5\u51fd\u6570\u3002<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span> cb<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ read event context<\/span><br \/>\n        <span class=\"token comment\">\/\/ read \u548cwrite\u8868\u793a\u8bfb\u548c\u5199\u7684\u4e0a\u4e0b\u6587<\/span><br \/>\n        EventContext read<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ write event context<\/span><br \/>\n        EventContext write<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> fd <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ events registered<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5f53\u524d\u6ce8\u518c\u7684\u4e8b\u4ef6&#xff0c;\u8868\u793a\u5f53\u524d\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u6ce8\u518c\u7684\u4e8b\u4ef6\u7c7b\u578b\u3002\u5b83\u7684\u503c\u53ef\u4ee5\u662f NONE\u3001READ\u3001WRITE \u6216\u8005 READ | WRITE&#xff08;\u7ec4\u5408\u4e8b\u4ef6&#xff09;\u3002\u8fd9\u4e2a\u53d8\u91cf\u7528\u4e8e\u6807\u8bc6\u54ea\u4e9b\u4e8b\u4ef6\u6b63\u5728\u88ab\u76d1\u89c6\u548c\u5904\u7406\u3002<\/span><br \/>\n        Event events <span class=\"token operator\">&#061;<\/span> NONE<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u7528\u4e8e\u4fdd\u62a4 FdContext \u6570\u636e\u7684\u4e92\u65a5\u9501\u3002\u7531\u4e8e IOManager \u53ef\u80fd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\u8fd0\u884c&#xff0c;mutex \u4fdd\u8bc1\u4e86\u5728\u5e76\u53d1\u73af\u5883\u4e2d\u5bf9\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u4e0b\u6587\u7684\u5b89\u5168\u8bbf\u95ee&#xff0c;\u907f\u514d\u7ade\u6001\u6761\u4ef6<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>mutex mutex<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u6839\u636e\u4e8b\u4ef6\u7c7b\u578b\u83b7\u53d6\u76f8\u5e94\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff08;\u5982\u8bfb\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u6216\u5199\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff09;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6839\u636e\u4e8b\u4ef6\u7c7b\u578b\u83b7\u53d6\u76f8\u5e94\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff08;read \u6216 write&#xff09;\u3002\u8fd9\u4e2a\u65b9\u6cd5\u6839\u636e\u4f20\u5165\u7684 event&#xff08;\u5982 READ \u6216 WRITE&#xff09;\u8fd4\u56de\u5bf9\u5e94\u7684 EventContext<\/span><br \/>\n        EventContext<span class=\"token operator\">&amp;<\/span> <span class=\"token function\">getEventContext<\/span><span class=\"token punctuation\">(<\/span>Event event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u91cd\u7f6e\u4e8b\u4ef6\u4e0a\u4e0b\u6587<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u91cd\u7f6e\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u7684\u72b6\u6001\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u5c06\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u4e2d\u7684\u6210\u5458\u53d8\u91cf&#xff08;\u5982 scheduler\u3001fiber \u548c cb&#xff09;\u91cd\u7f6e\u4e3a\u521d\u59cb\u72b6\u6001&#xff0c;\u901a\u5e38\u5728\u4e8b\u4ef6\u5b8c\u6210\u5904\u7406\u540e\u8c03\u7528&#xff0c;\u4ee5\u51c6\u5907\u597d\u4e0b\u4e00\u6b21\u4e8b\u4ef6\u7684\u6ce8\u518c\u3002<\/span><br \/>\n        <span class=\"token keyword\">void<\/span> <span class=\"token function\">resetEventContext<\/span><span class=\"token punctuation\">(<\/span>EventContext<span class=\"token operator\">&amp;<\/span> ctx<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u89e6\u53d1\u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u89e6\u53d1\u4e8b\u4ef6&#xff0c;\u6267\u884c\u4e0e\u4e8b\u4ef6\u76f8\u5173\u7684\u56de\u8c03\u3002\u8c03\u7528\u6b64\u65b9\u6cd5\u4f1a\u6839\u636e\u4e8b\u4ef6\u7c7b\u578b&#xff08;\u5982 READ \u6216 WRITE&#xff09;\u6267\u884c\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002<\/span><br \/>\n        <span class=\"token keyword\">void<\/span> <span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>Event event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">public<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5141\u8bb8\u8bbe\u7f6e\u7ebf\u7a0b\u6570\u91cf\u3001\u662f\u5426\u4f7f\u7528\u8c03\u7528\u8005\u7ebf\u7a0b\u4ee5\u53ca\u540d\u79f0\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ threads\u7ebf\u7a0b\u6570\u91cf&#xff0c;use_caller\u662f\u5426\u5c06\u4e3b\u7ebf\u7a0b\u6216\u8c03\u5ea6\u7ebf\u7a0b\u5305\u542b\u8fdb\u53bb&#xff0c;name\u8c03\u5ea6\u5668\u7684\u540d\u5b57<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5b9a\u662f\u5426\u4f7f\u7528\u8c03\u7528\u8005\u7ebf\u7a0b\u6765\u6267\u884c\u4e8b\u4ef6\u5904\u7406\u3002\u9ed8\u8ba4\u503c\u4e3a true&#xff0c;\u8868\u793a\u5728 IOManager \u4e2d&#xff0c;\u8c03\u7528\u8005\u7ebf\u7a0b\u4e5f\u4f1a\u88ab\u7528\u4e8e\u6267\u884c I\/O \u64cd\u4f5c<\/span><br \/>\n    <span class=\"token function\">IOManager<\/span><span class=\"token punctuation\">(<\/span>size_t threads <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">bool<\/span> use_caller <span class=\"token operator\">&#061;<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> std<span class=\"token operator\">::<\/span>string<span class=\"token operator\">&amp;<\/span> name <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;IOManager&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token operator\">~<\/span><span class=\"token function\">IOManager<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ add one event at a time<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4e8b\u4ef6\u7ba1\u7406\u65b9\u6cd5<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6dfb\u52a0\u4e00\u4e2a\u4e8b\u4ef6\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26 fd \u4e0a&#xff0c;\u5e76\u5173\u8054\u4e00\u4e2a\u56de\u8c03\u51fd\u6570 cb\u3002<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> <span class=\"token function\">addEvent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> Event event<span class=\"token punctuation\">,<\/span> std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span> cb <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete event<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5220\u9664\u6587\u4ef6\u63cf\u8ff0\u7b26fd\u4e0a\u7684\u67d0\u4e2a\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5220\u9664\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u7279\u5b9a\u4e8b\u4ef6\u3002\u53d6\u6d88\u8be5\u4e8b\u4ef6\u7684\u76d1\u89c6\u3002<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">delEvent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> Event event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete the event and trigger its callback<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u53d6\u6d88\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u67d0\u4e2a\u4e8b\u4ef6&#xff0c;\u5e76\u89e6\u53d1\u5176\u56de\u8c03\u51fd\u6570<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">cancelEvent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> Event event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete all events and trigger its callback<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u53d6\u6d88\u6587\u4ef6\u63cf\u8ff0\u7b26 fd \u4e0a\u7684\u6240\u6709\u4e8b\u4ef6&#xff0c;\u5e76\u89e6\u53d1\u6240\u6709\u56de\u8c03\u51fd\u6570\u3002<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">cancelAll<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u83b7\u53d6\u5f53\u524d\u7684 IOManager \u5b9e\u4f8b<\/span><br \/>\n    <span class=\"token keyword\">static<\/span> IOManager<span class=\"token operator\">*<\/span> <span class=\"token function\">GetThis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">protected<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/\u901a\u77e5\u8c03\u5ea6\u5668\u6709\u4efb\u52a1\u8c03\u5ea6<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5199pipe\u8ba9idle\u534f\u7a0b\u4eceepoll_wait\u9000\u51fa&#xff0c;\u5f85idle\u534f\u7a0byield\u4e4b\u540eScheduler::run\u5c31\u53ef\u4ee5\u8c03\u5ea6\u5176\u4ed6\u4efb\u52a1.<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">tickle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> override<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5224\u65ad\u8c03\u5ea6\u5668\u662f\u5426\u53ef\u4ee5\u505c\u6b62<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5224\u65ad\u6761\u4ef6\u662fScheduler::stopping()\u5916\u52a0IOManager\u7684m_pendingEventCount\u4e3a0&#xff0c;\u8868\u793a\u6ca1\u6709IO\u4e8b\u4ef6\u53ef\u8c03\u5ea6<\/span><br \/>\n    <span class=\"token keyword\">bool<\/span> <span class=\"token function\">stopping<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> override<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5b9e\u9645\u662fidle\u534f\u7a0b\u53ea\u8d1f\u8d23\u6536\u96c6\u6240\u6709\u5df2\u89e6\u53d1\u7684fd\u7684\u56de\u8c03\u51fd\u6570\u5e76\u5c06\u5176\u52a0\u2f0a\u8c03\u5ea6\u5668<\/span><br \/>\n    <span class=\"token comment\">\/\/\u7684\u4efb\u52a1\u961f\u5217&#xff0c;\u771f\u6b63\u7684\u6267\u2f8f\u65f6\u673a\u662fidle\u534f\u7a0b\u9000\u51fa\u540e&#xff0c;\u8c03\u5ea6\u5668\u5728\u4e0b\u2f00\u8f6e\u8c03\u5ea6\u65f6\u6267\u2f8f<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8fd9\u91cc\u4e5f\u662fscheduler\u7684\u91cd\u5199&#xff0c;\u5f53\u6ca1\u6709\u4e8b\u4ef6\u5904\u7406\u65f6&#xff0c;\u7ebf\u7a0b\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u65f6\u7684\u5904\u7406\u903b\u8f91\u3002<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">idle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> override<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u56e0\u4e3aTimer\u7c7b\u7684\u6210\u5458\u51fd\u6570\u91cd\u5199\u5f53\u6709\u65b0\u7684\u5b9a\u65f6\u5668\u63d2\u5165\u5230\u524d\u9762\u65f6\u7684\u5904\u7406\u903b\u8f91<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">onTimerInsertedAtFront<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> override<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8c03\u6574 IOManager \u5185\u90e8\u4e0a\u4e0b\u6587&#xff08;\u4f8b\u5982&#xff0c;\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u4e0b\u6587&#xff09;\u7684\u5927\u5c0f\u3002\u8fd9\u53ef\u80fd\u7528\u4e8e\u52a8\u6001\u589e\u52a0\u6216\u51cf\u5c11\u4e8b\u4ef6\u5904\u7406\u7684\u5bb9\u91cf<\/span><br \/>\n    <span class=\"token keyword\">void<\/span> <span class=\"token function\">contextResize<\/span><span class=\"token punctuation\">(<\/span>size_t size<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">private<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token comment\">\/\/\u7528\u4e8eepoll\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ fd[0] read&#xff0c;fd[1] write<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> m_epfd <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u7528\u4e8e\u7ebf\u7a0b\u95f4\u901a\u4fe1\u7684\u7ba1\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;fd[0] \u662f\u8bfb\u7aef&#xff0c;fd[1] \u662f\u5199\u7aef\u3002<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u539f\u5b50\u8ba1\u6570\u5668&#xff0c;\u7528\u4e8e\u8bb0\u5f55\u5f85\u5904\u7406\u7684\u4e8b\u4ef6\u6570\u91cf\u3002\u4f7f\u7528atomic\u7684\u597d\u5904\u662f\u8fd9\u4e2a\u53d8\u91cf\u518d\u8fdb\u884c\u52a0\u6216-\u90fd\u662f\u4e0d\u4f1a\u88ab\u591a\u7ebf\u7a0b\u5f71\u54cd<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u539f\u5b50\u53d8\u91cf&#xff0c;\u8868\u793a\u5f53\u524d\u6302\u8d77\u7684\u4e8b\u4ef6\u6570\u91cf\u3002\u4f7f\u7528 atomic \u7c7b\u578b\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u5e76\u53d1\u8bbf\u95ee\u4e0d\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>atomic<span class=\"token operator\">&lt;<\/span>size_t<span class=\"token operator\">&gt;<\/span> m_pendingEventCount <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8bfb\u5199\u9501<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>shared_mutex m_mutex<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ store fdcontexts for each fd<\/span><br \/>\n    <span class=\"token comment\">\/\/\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u4e0b\u6587\u6570\u7ec4&#xff0c;\u7528\u4e8e\u5b58\u50a8\u6bcf\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684 FdContext\u3002<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>FdContext<span class=\"token operator\">*<\/span><span class=\"token operator\">&gt;<\/span> m_fdContexts<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">endif<\/span><\/span><\/p>\n<h2>ioscheduler.cpp<\/h2>\n<p>\u4ee5\u4e0b\u90fd\u662f\u5173\u952e\u4ee3\u7801\u90e8\u5206&#xff1a;<\/p>\n<h3>IOManager::GetThis():<\/h3>\n<p>\u83b7\u53d6\u5f53\u524d\u7ebf\u7a0b\u7684\u8c03\u5ea6\u5668\u5bf9\u8c61&#xff0c;\u7136\u540e\u5c06\u5176\u52a8\u6001\u8f6c\u6362\u4e3aIOManager*\u7c7b\u578b&#xff0c;\u5982\u679c\u8f6c\u6362\u6210\u529f&#xff0c;\u8868\u793a\u5f53\u524d\u7ebf\u7a0b\u7684\u8c03\u5ea6\u5668\u5bf9\u8c61\u786e\u5b9e\u662f\u4e00\u4e2aIOManager\u5bf9\u8c61\u3002\u5426\u5219&#xff0c;\u5982\u679c\u662f\u8f6c\u5316\u7684\u662f\u6307\u9488\u7c7b\u578b\u8fd4\u56denullptr&#xff0c;\u5f15\u7528\u7c7b\u578b\u629b\u51fastd::bad_cast\u5f02\u5e38<\/p>\n<p>IOManager<span class=\"token operator\">*<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ dynamic_cast \u7528\u4e8e\u5c06\u57fa\u7c7b\u6307\u9488\u8f6c\u6362\u4e3a\u6d3e\u751f\u7c7b\u6307\u9488\u3002\u5728\u8fd9\u91cc&#xff0c;\u6211\u4eec\u5c06 Scheduler \u7c7b\u7684 GetThis() \u8fd4\u56de\u7684\u6307\u9488\u8f6c\u6362\u4e3a IOManager*<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u5f53\u524d\u8c03\u5ea6\u5668\u5b9e\u4f8b\u662f IOManager \u7c7b\u578b&#xff0c;\u8f6c\u6362\u5c06\u6210\u529f\u3002\u5982\u679c\u4e0d\u662f&#xff08;\u4f8b\u5982&#xff0c;\u57fa\u7c7b\u6307\u9488\u6307\u5411\u5176\u4ed6\u7c7b\u578b&#xff09;&#xff0c;dynamic_cast \u5c06\u8fd4\u56de nullptr&#xff0c;\u8fd9\u4fdd\u8bc1\u4e86\u7c7b\u578b\u5b89\u5168\u3002<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token keyword\">dynamic_cast<\/span><span class=\"token operator\">&lt;<\/span>IOManager<span class=\"token operator\">*<\/span><span class=\"token operator\">&gt;<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Scheduler<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h4>\u8865\u5145dynamic_cast\u548cstatic_cast&#xff1a;<\/h4>\n<p>dynamic_cast:\u6709\u8fd0\u884c\u65f6\u5019\u7684\u5b89\u5168\u68c0\u67e5&#xff0c;\u4e0d\u5b89\u5168\u5c31\u76f4\u63a5\u5982\u4e0a\u9762\u8bf4\u7684\u4e00\u6837\u8fd4\u56de&#xff0c;\u5fc5\u987b\u57fa\u7c7b\u9700\u8981\u6709\u865a\u51fd\u6570&#xff0c;\u786e\u4fdd\u7c7b\u578b\u5b89\u5168\u65f6\u4f7f\u7528\u6b64\u5173\u952e\u8bcd\u8f6c\u5316\u3002<\/p>\n<p>static_cast:\u65e0\u8fd0\u884c\u7684\u68c0\u67e5&#xff0c;\u6bd4\u5982\u57fa\u7c7b\u8f6c\u6362\u6210\u5b50\u7c7b\u5728dynamic_cast\u4e2d\u662f\u4e0d\u5141\u8bb8\u7684&#xff0c;\u4f46\u662fstatic_cast\u4e0d\u68c0\u67e5\u6240\u4ee5\u65e0\u6240\u8c13&#xff0c;\u4f46\u662f\u8f6c\u5316\u540e\u6709\u53ef\u80fd\u5bfc\u81f4\u67d0\u4e9b\u672a\u5b9a\u4e49\u7684\u884c\u4e3a&#xff0c;\u5982\u65e0\u6cd5\u8bbf\u95ee\u5b50\u7c7b\u7684\u90e8\u5206\u6210\u5458\u3002<\/p>\n<h3>IOManager:FdContext::getEventContext(Event event):<\/h3>\n<p>\u6839\u636e\u4f20\u5165\u7684\u4e8b\u4ef6event&#xff0c;\u8fd4\u56de\u5bf9\u5e94\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u7684\u5f15\u7528\u3002 assert\u662f\u4e00\u79cd\u8c03\u5f0f\u5de5\u5177&#xff0c;\u5b83\u4f1a\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u8fd9\u4e2a\u6761\u4ef6\u662f\u5426\u4e3a\u771f\u3002\u5982\u679c\u6761\u4ef6\u4e3a\u5047(\u5373event\u65e2\u4e0d\u662fREAD\u6216WRIE)&#xff0c;\u7a0b\u5e8f\u4e2d\u65ad\u6267\u884c&#xff0c;\u5e76\u62a5\u544a\u9519\u8bef\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u7528\u4e8e\u83b7\u53d6\u6307\u5b9a\u4e8b\u4ef6\u7c7b\u578b&#xff08;\u5982\u8bfb\u4e8b\u4ef6\u6216\u5199\u4e8b\u4ef6&#xff09;\u5bf9\u5e94\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff08;EventContext&#xff09;\u3002FdContext \u7c7b\u4e2d\u7ef4\u62a4\u4e86 read \u548c write \u4e24\u4e2a EventContext&#xff0c;\u8be5\u65b9\u6cd5\u6839\u636e\u4f20\u5165\u7684 event \u7c7b\u578b\u8fd4\u56de\u76f8\u5e94\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587<\/span><br \/>\nIOManager<span class=\"token operator\">::<\/span>FdContext<span class=\"token operator\">::<\/span>EventContext<span class=\"token operator\">&amp;<\/span> IOManager<span class=\"token operator\">::<\/span><span class=\"token class-name\">FdContext<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">getEventContext<\/span><span class=\"token punctuation\">(<\/span>Event event<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span>event <span class=\"token operator\">&#061;&#061;<\/span> READ <span class=\"token operator\">||<\/span> event <span class=\"token operator\">&#061;&#061;<\/span> WRITE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">switch<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ READ \u548c WRITE \u4e8b\u4ef6\u7c7b\u578b\u4f1a\u5206\u522b\u8fd4\u56de read \u6216 write \u5bf9\u5e94\u7684 EventContext\u3002<\/span><br \/>\n        <span class=\"token keyword\">case<\/span> READ<span class=\"token operator\">:<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> read<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">case<\/span> WRITE<span class=\"token operator\">:<\/span><br \/>\n            <span class=\"token keyword\">return<\/span> write<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u4f20\u5165\u7684 event \u65e2\u4e0d\u662f READ \u4e5f\u4e0d\u662f WRITE&#xff0c;\u5219\u629b\u51fa std::invalid_argument \u5f02\u5e38&#xff0c;\u8868\u793a\u4f20\u5165\u7684\u4e8b\u4ef6\u7c7b\u578b\u65e0\u6548\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8fd9\u884c\u4ee3\u7801\u53ef\u4ee5\u9632\u6b62\u4f20\u5165\u65e0\u6548\u4e8b\u4ef6\u7c7b\u578b\u65f6\u9020\u6210\u672a\u5b9a\u4e49\u884c\u4e3a&#xff0c;\u786e\u4fdd\u4ee3\u7801\u7684\u5065\u58ee\u6027\u3002<\/span><br \/>\n    <span class=\"token keyword\">throw<\/span> std<span class=\"token operator\">::<\/span><span class=\"token function\">invalid_argument<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;Unsupported event type&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p>\u8865\u5145&#xff1a; std::invalid_argument&#xff1a; \u5c5e\u4e8estd::exception\u7684\u6d3e\u751f\u7c7b\u5728&lt;stdexcept&gt;\u5934\u6587\u4ef6\u4e2d&#xff0c;\u7b80\u5355\u6765\u8bf4\u5c31\u662f\u4f20\u5165\u7684\u53c2\u6570\u4e0d\u7b26\u5408\u9884\u671f\u7684\u53c2\u6570\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<h3>IOManager::resetEventContext:<\/h3>\n<p>\u91cd\u7f6eEventContext\u4e8b\u4ef6\u7684\u4e0a\u4e0b\u6587&#xff0c;\u5c06\u5176\u6062\u590d\u5230\u521d\u59cb\u6216\u8005\u7a7a\u7684\u72b6\u6001\u3002\u4e3b\u8981\u4f5c\u7528\u662f\u6e05\u7406\u5e76\u91cd\u7f6e\u4f20\u5165\u7684 EventContext \u5bf9\u8c61&#xff0c;\u4f7f\u5176\u4e0d\u518d\u4e0e\u4efb\u4f55\u8c03\u5ea6\u5668\u3001\u7ebf\u7a0b\u6216\u56de\u8c03\u51fd\u6570\u76f8\u5173\u8054\u3002<\/p>\n<p><span class=\"token keyword\">void<\/span> IOManager<span class=\"token operator\">::<\/span><span class=\"token class-name\">FdContext<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">resetEventContext<\/span><span class=\"token punctuation\">(<\/span>EventContext<span class=\"token operator\">&amp;<\/span> ctx<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    ctx<span class=\"token punctuation\">.<\/span>scheduler <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    ctx<span class=\"token punctuation\">.<\/span>fiber<span class=\"token punctuation\">.<\/span><span class=\"token function\">reset<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    ctx<span class=\"token punctuation\">.<\/span>cb <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::triggerEvent:<\/h3>\n<p>\u51fd\u6570\u8d1f\u8d23\u5728\u6307\u5b9a\u7684 IO \u4e8b\u4ef6\u88ab\u89e6\u53d1\u65f6&#xff0c;\u6267\u884c\u76f8\u5e94\u7684\u56de\u8c03\u51fd\u6570\u6216\u7ebf\u7a0b&#xff0c;\u5e76\u4e14\u5728\u6267\u884c\u5b8c\u4e4b\u540e\u6e05\u7406\u76f8\u5173\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>\u5177\u4f53\u51fd\u6570\u6d41\u7a0b&#xff1a; \u901a\u8fc7\u5224\u65ad\u89e6\u53d1\u6307\u5b9a\u7684event\u4e8b\u4ef6\u5728Fdcontext\u4e2d\u7684events\u4e2d\u5b58\u5728\u5bf9\u5e94\u7684\u8bfb\u6216\u5199\u6216\u8bfb\u5199\u7ec4\u5408&#xff0c;\u6ca1\u6709assert\u5c31\u629b\u51fa\u5f02\u5e38\u4e86&#xff0c;\u6709\u5c31\u4ece\u53d6\u53cd\u4eceevents\u4e2d\u5220\u9664&#xff0c;\u7136\u540e\u83b7\u53d6\u76f8\u5e94\u7684EventContext\u5177\u4f53\u7684\u8bfb\u6216\u5199\u4e8b\u4ef6\u5bf9\u5e94\u7684\u4e0a\u4e0b\u6587&#xff0c;\u5c06fd\u7ed1\u5b9a\u7684\u5177\u4f53\u8bfb\u6216\u5199\u4efb\u52a1\u7684\u56de\u8c03\u534f\u7a0b\u6216\u56de\u8c03\u51fd\u6570&#xff0c;\u653e\u5165\u5230\u4efb\u52a1\u961f\u5217\u4e2d\u7b49\u5f85\u8c03\u5ea6\u5668\u8c03\u5ea6\u3002<\/p>\n<p><span class=\"token comment\">\/\/ no lock<\/span><br \/>\n<span class=\"token comment\">\/\/ \u7528\u4e8e\u89e6\u53d1\u5e76\u5904\u7406\u7279\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u4e0a\u5df2\u7ecf\u53d1\u751f\u7684\u4e8b\u4ef6\u3002<\/span><br \/>\n<span class=\"token keyword\">void<\/span> IOManager<span class=\"token operator\">::<\/span><span class=\"token class-name\">FdContext<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>IOManager<span class=\"token operator\">::<\/span>Event event<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u786e\u4fddevent\u662f\u4e2d\u6709\u6307\u5b9a\u7684\u4e8b\u4ef6&#xff0c;\u5426\u5219\u7a0b\u5e8f\u4e2d\u65ad\u3002<\/span><br \/>\n    <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span>events <span class=\"token operator\">&amp;<\/span> event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete event<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6e05\u7406\u8be5\u4e8b\u4ef6&#xff0c;\u8868\u793a\u4e0d\u518d\u5173\u6ce8&#xff0c;\u4e5f\u5c31\u662f\u8bf4&#xff0c;\u6ce8\u518cIO\u4e8b\u4ef6\u662f\u4e00\u6b21\u6027\u7684&#xff0c;<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5982\u679c\u60f3\u6301\u7eed\u5173\u6ce8\u67d0\u4e2aSocket fd\u7684\u8bfb\u5199\u4e8b\u4ef6&#xff0c;\u90a3\u4e48\u6bcf\u6b21\u89e6\u53d1\u4e8b\u4ef6\u540e\u90fd\u8981\u91cd\u65b0\u6dfb\u52a0<\/span><br \/>\n    <span class=\"token comment\">\/\/\u56e0\u4e3a\u4e0d\u662f\u4f7f\u7528\u4e86\u5341\u516d\u8fdb\u5236\u4f4d&#xff0c;\u6240\u4ee5\u5bf9\u6807\u5fd7\u4f4d\u53d6\u53cd\u5c31\u662f\u76f8\u5f53\u4e8e\u5c06event\u4eceevents\u4e2d\u5220\u9664<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u901a\u8fc7assert(events &amp; event)\u786e\u4fdd\u89e6\u53d1\u7684\u4e8b\u4ef6(event)\u7684\u786e\u662f\u4e4b\u524d\u6ce8\u518c\u8fc7\u5e76\u611f\u5174\u8da3\u7684&#xff0c;\u5426\u5219\u8bf4\u660e\u7a0b\u5e8f\u903b\u8f91\u5b58\u5728\u9519\u8bef&#xff0c;\u76f4\u63a5\u7ec8\u6b62\u7a0b\u5e8f\u6267\u884c\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ (events &amp; ~event)\u5219\u8868\u793a\u539f\u6765\u7684\u4e8b\u4ef6\u96c6\u4e2d\u79fb\u9664\u6307\u5b9a\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5047\u8bbeevents\u539f\u672c\u4e3a&#xff1a;0110&#xff08;\u5173\u6ce8\u4e8b\u4ef62\u30013&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5047\u8bbeevent\u4e3a&#xff1a;0010&#xff08;\u4e8b\u4ef62\u53d1\u751f&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u53d6\u53cd\u540e&#xff1a;~0010 \u2192 1101<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8fdb\u884c\u4e0e\u8fd0\u7b97\u540e&#xff1a;0110 &amp; 1101 \u2192 0100&#xff0c;\u6210\u529f\u79fb\u9664\u4e86\u4e8b\u4ef62&#xff0c;\u4ec5\u5269\u4e8b\u4ef63<\/span><br \/>\n    events <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>Event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span>events <span class=\"token operator\">&amp;<\/span> <span class=\"token operator\">~<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ trigger<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u83b7\u53d6\u5f53\u524d\u89e6\u53d1\u4e8b\u4ef6(event)\u6240\u5bf9\u5e94\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61(EventContext)<\/span><br \/>\n    EventContext<span class=\"token operator\">&amp;<\/span> ctx <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">getEventContext<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8fd9\u4e2a\u8fc7\u7a0b\u5c31\u76f8\u5f53\u4e8escheduler\u6587\u4ef6\u4e2d\u7684main.cpp\u6d4b\u8bd5\u4e00\u6837&#xff0c;\u628a\u771f\u6b63\u8981\u6267\u884c\u7684\u51fd\u6570\u653e\u5165\u5230\u4efb\u52a1\u961f\u5217\u4e2d\u7b49\u7ebf\u7a0b\u53d6\u51fa\u540e\u4efb\u52a1\u540e&#xff0c;\u534f\u7a0b\u6267\u884c&#xff0c;\u6267\u884c\u5b8c\u6210\u540e\u8fd4\u56de\u4e3b\u534f\u7a0b\u7ee7\u7eed&#xff0c;\u6267\u884crun\u65b9\u6cd5\u53d6\u4efb\u52a1\u6267\u884c\u4efb\u52a1(\u4e0d\u8fc7\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u7ebf\u7a0b\u7684\u534f\u7a0b\u6267\u884c\u4e86)\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5224\u65ad\u4e0a\u4e0b\u6587\u5bf9\u8c61\u5b58\u653e\u7684\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u8fd8\u662f\u4e00\u4e2a\u534f\u7a0b<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>ctx<span class=\"token punctuation\">.<\/span>cb<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ call ScheduleTask(std::function&lt;void()&gt;* f, int thr)<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u662f\u56de\u8c03\u51fd\u6570 (std::function&lt;void()&gt; ctx.cb)&#xff0c;\u5219\u5c06\u8be5\u56de\u8c03\u5c01\u88c5\u4e3a\u8c03\u5ea6\u4efb\u52a1&#xff0c;\u653e\u5165\u8c03\u5ea6\u5668\u7684\u4efb\u52a1\u961f\u5217\u3002<\/span><br \/>\n        ctx<span class=\"token punctuation\">.<\/span>scheduler<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">scheduleLock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>ctx<span class=\"token punctuation\">.<\/span>cb<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ call ScheduleTask(std::shared_ptr&lt;Fiber&gt;* f, int thr)<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u662f\u534f\u7a0b\u4efb\u52a1 (std::shared_ptr&lt;Fiber&gt; ctx.fiber)&#xff0c;\u5219\u76f4\u63a5\u5c06\u534f\u7a0b\u5bf9\u8c61\u4f5c\u4e3a\u4efb\u52a1\u52a0\u5165\u8c03\u5ea6\u961f\u5217\u3002<\/span><br \/>\n        ctx<span class=\"token punctuation\">.<\/span>scheduler<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">scheduleLock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">&amp;<\/span>ctx<span class=\"token punctuation\">.<\/span>fiber<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span>   <span class=\"token comment\">\/\/ scheduleLock \u662f\u8c03\u5ea6\u5668\u7684\u65b9\u6cd5&#xff0c;\u4f5c\u7528\u662f\u5c06\u4efb\u52a1\u5b89\u5168\u5730\u52a0\u5165\u5230\u8c03\u5ea6\u5668\u7ef4\u62a4\u7684\u4efb\u52a1\u961f\u5217\u4e2d&#xff0c;\u5e76\u5524\u9192\u7b49\u5f85\u53d6\u4efb\u52a1\u7684\u7ebf\u7a0b\u8fdb\u884c\u8c03\u5ea6\u6267\u884c<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ reset event context<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4e00\u65e6\u89e6\u53d1\u6267\u884c\u5b8c\u6bd5&#xff0c;\u5f53\u524d\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff08;ctx&#xff09;\u9700\u6062\u590d\u5230\u521d\u59cb\u72b6\u6001<\/span><br \/>\n    <span class=\"token function\">resetEventContext<\/span><span class=\"token punctuation\">(<\/span>ctx<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager\u7684\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570&#xff1a;<\/h3>\n<h4>epoll<\/h4>\n<p>\u9632\u6b62\u4f60\u6ca1\u5b66\u4e60\u8fc7epoll:\u56fe\u6587\u5e76\u8302\u8bb2\u89e3epoll\u539f\u7406&#xff0c;\u5f7b\u5e95\u5f04\u61c2epoll\u673a\u5236-CSDN\u535a\u5ba2<\/p>\n<p><span class=\"token function\">man<\/span> epoll_event<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124232-688a1338918e1.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">IOManager<\/span><span class=\"token punctuation\">(<\/span>size_t threads<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">bool<\/span> use_caller<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">const<\/span> std<span class=\"token operator\">::<\/span>string <span class=\"token operator\">&amp;<\/span>name<span class=\"token punctuation\">)<\/span><span class=\"token operator\">:<\/span><br \/>\n    <span class=\"token function\">Scheduler<\/span><span class=\"token punctuation\">(<\/span>threads<span class=\"token punctuation\">,<\/span> use_caller<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token function\">TimerManager<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ create epoll fd<\/span><br \/>\n        <span class=\"token comment\">\/\/ 5000&#xff0c;epoll_create \u7684\u53c2\u6570\u5b9e\u9645\u4e0a\u5728\u73b0\u4ee3 Linux \u5185\u6838\u4e2d\u5df2\u7ecf\u88ab\u5ffd\u7565&#xff0c;\u6700\u65e9\u7248\u672c\u7684 Linux \u4e2d&#xff0c;\u8fd9\u4e2a\u53c2\u6570\u7528\u4e8e\u6307\u5b9a epoll \u5185\u90e8\u4f7f\u7528\u7684\u4e8b\u4ef6\u8868\u7684\u5927\u5c0f\u3002<\/span><br \/>\n        m_epfd <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_create<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">5000<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u9519\u8bef\u5c31\u7ec8\u6b62\u7a0b\u5e8f<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6210\u529f\u8fd4\u56de\u65b0\u521b\u5efa\u7684epoll\u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u6b63\u6574\u6570&#xff09;,\u5931\u8d25\u8fd4\u56de-1<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span>m_epfd <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ create pipe<\/span><br \/>\n        <span class=\"token comment\">\/\/\u521b\u5efa\u7ba1\u9053\u7684\u51fd\u6570\u89c4\u5b9a\u4e86m_tickleFds[0]\u662f\u8bfb\u7aef&#xff0c;[1]\u662f\u5199\u7aef <\/span><br \/>\n        <span class=\"token comment\">\/\/ \u521b\u5efa\u4e00\u4e2a\u7ba1\u9053\u7528\u4e8e\u5524\u9192\u963b\u585e\u5728epoll_wait\u7684\u7ebf\u7a0b<\/span><br \/>\n        <span class=\"token comment\">\/\/ m_tickleFds[0]&#xff1a;\u8bfb\u7aef&#xff0c;\u7528\u4e8eepoll\u76d1\u63a7\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ m_tickleFds[1]&#xff1a;\u5199\u7aef&#xff0c;\u5f53\u6709\u65b0\u4efb\u52a1\u5230\u6765\u65f6\u5199\u5165\u8be5\u7aef&#xff0c;\u5524\u9192\u963b\u585e\u7684\u7ebf\u7a0b<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">pipe<\/span><span class=\"token punctuation\">(<\/span>m_tickleFds<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u6210\u529f\u8fd4\u56de0\u5931\u8d25\u8fd4\u56de-1<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>rt<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ add read event to epoll<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5c06\u7ba1\u9053\u7684\u76d1\u542c\u6ce8\u518c\u5230epoll\u4e0a<\/span><br \/>\n    <span class=\"token comment\">\/\/     struct epoll_event {<\/span><br \/>\n    <span class=\"token comment\">\/\/     uint32_t events; \/* Epoll events (\u4e8b\u4ef6\u7c7b\u578b) *\/<\/span><br \/>\n    <span class=\"token comment\">\/\/     epoll_data_t data; \/* User data variable (\u7528\u6237\u6570\u636e&#xff0c;\u4e00\u822c\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26) *\/<\/span><br \/>\n    <span class=\"token comment\">\/\/ };<\/span><br \/>\n        epoll_event event<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ Edge Triggered&#xff0c;\u8bbe\u7f6e\u6807\u5fd7\u4f4d&#xff0c;\u5e76\u4e14\u91c7\u7528\u8fb9\u7f18\u89e6\u53d1\u548c\u8bfb\u4e8b\u4ef6\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ EPOLLIN&#xff1a;\u76d1\u542c\u8bfb\u4e8b\u4ef6\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ EPOLLET (Edge Triggered)&#xff1a;\u8fb9\u6cbf\u89e6\u53d1\u6a21\u5f0f\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8fb9\u6cbf\u89e6\u53d1\u610f\u5473\u7740&#xff1a;\u4ec5\u5f53pipe\u4ece\u4e0d\u53ef\u8bfb\u53d8\u4e3a\u53ef\u8bfb\u65f6\u89e6\u53d1\u4e00\u6b21&#xff0c;\u82e5\u4e0d\u4e00\u6b21\u6027\u5904\u7406\u5b8c\u6bd5&#xff0c;\u5c06\u4e0d\u4f1a\u518d\u901a\u77e5\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u4ec5\u5f53\u6587\u4ef6\u63cf\u8ff0\u7b26\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\u901a\u77e5\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u72b6\u6001\u53d8\u5316\u7684\u542b\u4e49&#xff1a;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u4ece\u4e0d\u53ef\u8bfb\u53d8\u4e3a\u53ef\u8bfb&#xff0c;\u6216\u4ece\u4e0d\u53ef\u5199\u53d8\u4e3a\u53ef\u5199\u7684\u77ac\u95f4\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f\u4e0b&#xff0c;\u53ea\u6709\u5728\u72b6\u6001\u53d8\u5316\u77ac\u95f4&#xff08;\u4ece\u4e0d\u53ef\u8bfb \u2192 \u53ef\u8bfb&#xff0c;\u6216\u4ece\u4e0d\u53ef\u5199 \u2192 \u53ef\u5199&#xff09;\u65f6\u624d\u901a\u77e5\u4e00\u6b21\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u4e8b\u4ef6\u53d1\u751f\u540e\u4f60\u6ca1\u6709\u4e00\u6b21\u6027\u8bfb\u53d6\u5b8c\u7f13\u51b2\u533a\u4e2d\u7684\u6240\u6709\u6570\u636e&#xff0c;\u90a3\u4e48\u5373\u4f7f\u7f13\u51b2\u533a\u4e2d\u8fd8\u6709\u5269\u4f59\u6570\u636e&#xff0c;epoll\u4e5f\u4e0d\u4f1a\u518d\u901a\u77e5\u4f60\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8fb9\u6cbf\u89e6\u53d1\u5c31\u50cf\u201c\u95e8\u94c3\u201d&#xff0c;\u53ea\u5728\u6709\u4eba\u6309\u95e8\u94c3\u77ac\u95f4\u54cd\u4e00\u4e0b\u3002\u5982\u679c\u4f60\u5f00\u95e8\u65f6\u6ca1\u628a\u4eba\u63a5\u8fdb\u6765&#xff0c;\u540e\u9762\u518d\u53bb\u770b\u95e8\u94c3&#xff0c;\u5b83\u4e0d\u4f1a\u518d\u54cd\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u4e0e\u6c34\u5e73\u89e6\u53d1&#xff08;LT&#xff09;\u5bf9\u6bd4&#xff1a;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6c34\u5e73\u89e6\u53d1\u6a21\u5f0f&#xff08;\u9ed8\u8ba4\u6a21\u5f0f&#xff09;&#xff0c;\u53ea\u8981\u7f13\u51b2\u533a\u4e2d\u8fd8\u6709\u6570\u636e\u53ef\u8bfb&#xff0c;\u5c31\u4e00\u76f4\u901a\u77e5\u4f60&#xff08;\u4e0d\u65ad\u54cd\u94c3&#xff09;&#xff0c;\u76f4\u5230\u8bfb\u5b8c\u4e3a\u6b62\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ EPOLLIN&#xff1a;\u76d1\u542c\u8bfb\u4e8b\u4ef6&#xff08;\u53ef\u8bfb\u5373\u901a\u77e5&#xff09;\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ EPOLLET&#xff1a;\u4e8b\u4ef6\u8fb9\u6cbf\u89e6\u53d1\u6a21\u5f0f&#xff08;\u53d8\u5316\u77ac\u95f4\u901a\u77e5\u4e00\u6b21&#xff0c;\u4e0d\u4f1a\u6301\u7eed\u901a\u77e5&#xff09;\u3002<\/span><br \/>\n        event<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&#061;<\/span> EPOLLIN <span class=\"token operator\">|<\/span> EPOLLET<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u6307\u5b9aevent.data.fd\u4e3apipe\u7684\u8bfb\u7aef&#xff0c;\u786e\u4fddepoll\u76d1\u63a7pipe\u8bfb\u7aef\u7684\u53ef\u8bfb\u4e8b\u4ef6\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ epoll\u53ef\u4ee5\u76d1\u542c\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u5982socket\u3001pipe&#xff09;&#xff0c;\u8fd9\u91cc\u660e\u786e\u6307\u5b9a\u4e86\u8981\u76d1\u542c\u7684\u662f\u54ea\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ epoll\u76d1\u542c\u7684\u662f\u7ba1\u9053\u7684\u8bfb\u7aef&#xff0c;\u56e0\u4e3a\u53ea\u6709\u8bfb\u7aef\u6709\u6570\u636e\u53ef\u8bfb\u65f6&#xff0c;\u624d\u9700\u8981\u901a\u77e5\u7a0b\u5e8f\u5904\u7406\u6570\u636e<\/span><br \/>\n        event<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>fd <span class=\"token operator\">&#061;<\/span> m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ non-blocked<\/span><br \/>\n        <span class=\"token comment\">\/\/\u4fee\u6539\u7ba1\u9053\u6587\u4ef6\u63cf\u8ff0\u7b26\u4ee5\u975e\u963b\u585e\u7684\u65b9\u5f0f&#xff0c;\u914d\u5408\u8fb9\u7f18\u89e6\u53d1\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8bbe\u7f6epipe\u8bfb\u7aef\u4e3a\u975e\u963b\u585e\u6a21\u5f0f<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u9632\u6b62\u5728\u8bfb\u53d6pipe\u5185\u5bb9\u65f6\u7ebf\u7a0b\u963b\u585e\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u82e5\u65e0\u6570\u636e\u5219\u76f4\u63a5\u8fd4\u56de-1&#xff0c;\u800c\u975e\u7b49\u5f85\u6570\u636e\u3002<\/span><br \/>\n        rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">fcntl<\/span><span class=\"token punctuation\">(<\/span>m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> F_SETFL<span class=\"token punctuation\">,<\/span> O_NONBLOCK<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u6bcf\u6b21\u9700\u8981\u5224\u65adrt\u662f\u5426\u6210\u529f<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6210\u529f\u8fd4\u56de0\u5931\u8d25\u8fd4\u56de-1<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>rt<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u5c06 m_tickleFds[0];\u4f5c\u4e3a\u8bfb\u4e8b\u4ef6\u653e\u5165\u5230event\u76d1\u542c\u96c6\u5408\u4e2d<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8c03\u7528epoll_ctl\u51fd\u6570&#xff0c;\u5c06\u4e0a\u9762\u5b9a\u4e49\u597d\u7684\u4e8b\u4ef6&#xff08;event&#xff09;\u6dfb\u52a0\u5230epoll\u5b9e\u4f8b&#xff08;m_epfd&#xff09;\u4e2d&#xff0c;\u5f00\u59cb\u76d1\u63a7m_tickleFds[0]<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u6210\u529f\u8fd4\u56de0\u5931\u8d25\u8fd4\u56de-1<\/span><br \/>\n        rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_ctl<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> EPOLL_CTL_ADD<span class=\"token punctuation\">,<\/span> m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>rt<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u521d\u59cb\u5316\u4e86\u4e00\u4e2a\u5305\u542b 32 \u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u4e0b\u6587\u7684\u6570\u7ec4<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u4e8b\u4ef6\u4e0a\u4e0b\u6587\u6570\u7ec4\u5927\u5c0f\u521d\u59cb\u5316<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u9884\u5148\u5206\u914d\u5927\u5c0f\u4e3a32\u4e2aFD\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u5bf9\u8c61&#xff0c;\u51cf\u5c11\u8fd0\u884c\u65f6\u9891\u7e41\u5185\u5b58\u5206\u914d\u7684\u5f00\u9500\u3002<\/span><br \/>\n        <span class=\"token function\">contextResize<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">32<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u542f\u52a8 Scheduler&#xff0c;\u5f00\u542f\u7ebf\u7a0b\u6c60&#xff0c;\u51c6\u5907\u5904\u7406\u4efb\u52a1<\/span><br \/>\n        <span class=\"token function\">start<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token operator\">~<\/span><span class=\"token function\">IOManager<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5173\u95edscheduler\u7c7b\u4e2d\u7684\u7ebf\u7a0b\u6c60&#xff0c;\u8ba9\u4efb\u52a1\u5168\u90e8\u6267\u884c\u5b8c\u540e\u7ebf\u7a0b\u5b89\u5168\u9000\u51fa<\/span><br \/>\n    <span class=\"token function\">stop<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5173\u95edepoll\u7684\u53e5\u67c4<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5173\u95edepoll\u53e5\u67c4\u540e&#xff0c;\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u81ea\u52a8\u6e05\u7406epoll\u5b9e\u4f8b\u76f8\u5173\u8d44\u6e90&#xff0c;\u505c\u6b62\u76d1\u542c\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5173\u95ed\u7ba1\u9053\u8bfb\u7aef\u5199\u7aef<\/span><br \/>\n    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5c06fdcontext\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e00\u4e2a\u4e2a\u5173\u95ed<\/span><br \/>\n    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span>size_t i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">delete<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124233-688a13394952e.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124233-688a1339913e8.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124234-688a133a4c895.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">handleEvents<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">while<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5b9a\u4e49\u4e86\u4e00\u4e2a\u5927\u5c0f\u4e3a4096\u5b57\u8282\u7684\u7f13\u51b2\u533a&#xff0c;\u7528\u4e8e\u5b58\u50a8\u4ece\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bfb\u53d6\u5230\u7684\u6570\u636e\u3002<\/span><br \/>\n        <span class=\"token keyword\">char<\/span> buffer<span class=\"token punctuation\">[<\/span><span class=\"token number\">4096<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u4f7f\u7528recv()\u51fd\u6570\u4ece\u7ed9\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26 fd \u4e2d\u8bfb\u53d6\u6570\u636e\u3002<\/span><br \/>\n        ssize_t bytesRead <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">recv<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> buffer<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>buffer<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>bytesRead <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8868\u793a\u5f53\u524d\u5904\u4e8e\u975e\u963b\u585e\u6a21\u5f0f&#xff0c;\u6b64\u65f6\u7f13\u51b2\u533a\u6682\u65f6\u6ca1\u6709\u6570\u636e\u53ef\u8bfb&#xff08;\u975e\u963b\u585e\u6a21\u5f0f\u4e0b\u662f\u6b63\u5e38\u73b0\u8c61&#xff09;&#xff0c;\u53ef\u4ee5\u76f4\u63a5\u8df3\u51fa\u5faa\u73af&#xff0c;\u7a0d\u540e\u518d\u5c1d\u8bd5\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5728\u975e\u963b\u585e\u6a21\u5f0f\u4e0b&#xff0c;\u5982\u679c\u5f53\u524d\u6ca1\u6709\u6570\u636e\u53ef\u8bfb&#xff0c;\u7cfb\u7edf\u8c03\u7528\u4f1a\u7acb\u5373\u8fd4\u56de-1&#xff0c;\u4e14errno\u8bbe\u7f6e\u4e3aEAGAIN\u6216EWOULDBLOCK\u3002<\/span><br \/>\n            <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>errno <span class=\"token operator\">&#061;&#061;<\/span> EAGAIN <span class=\"token operator\">||<\/span> errno <span class=\"token operator\">&#061;&#061;<\/span> EWOULDBLOCK<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u6ca1\u6709\u66f4\u591a\u6570\u636e\u53ef\u8bfb<\/span><br \/>\n                <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5904\u7406\u5176\u4ed6\u9519\u8bef<\/span><br \/>\n                <span class=\"token function\">perror<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;recv&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>bytesRead <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5f53 recv() \u8fd4\u56de\u503c\u4e3a 0 \u65f6&#xff0c;\u610f\u5473\u7740\u5bf9\u7aef\u5df2\u7ecf\u5173\u95ed\u4e86\u8fde\u63a5&#xff0c;\u4e0d\u4f1a\u518d\u6709\u6570\u636e\u5230\u6765\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u8fde\u63a5\u5173\u95ed<\/span><br \/>\n            <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5904\u7406\u8bfb\u53d6\u7684\u6570\u636e<\/span><br \/>\n            <span class=\"token function\">processData<\/span><span class=\"token punctuation\">(<\/span>buffer<span class=\"token punctuation\">,<\/span> bytesRead<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124234-688a133ab2d8d.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124235-688a133b5b191.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h4>Fcntl\u51fd\u6570<\/h4>\n<p>fcntl() \u5168\u79f0 file control&#xff0c;\u7528\u4e8e\u63a7\u5236\u548c\u4fee\u6539\u5df2\u6253\u5f00\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u5c5e\u6027\u3002 \u5b83\u53ef\u4ee5\u7528\u4e8e\u8bbe\u7f6e\u6587\u4ef6\u63cf\u8ff0\u7b26\u72b6\u6001\u6807\u5fd7&#xff08;\u4f8b\u5982\u963b\u585e\/\u975e\u963b\u585e\u6a21\u5f0f&#xff09;\u3001\u6587\u4ef6\u72b6\u6001\u6807\u5fd7\u3001\u6587\u4ef6\u9501\u7b49<\/p>\n<p>\u51fd\u6570\u539f\u578b&#xff1a;<\/p>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;fcntl.h&gt;<\/span><\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">fcntl<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">int<\/span> cmd<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span> <span class=\"token comment\">\/* arg *\/<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124235-688a133bec591.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124236-688a133c0db82.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> \u8bbe\u7f6e\u975e\u963b\u585e\u6a21\u5f0f&#xff08;O_NONBLOCK&#xff09; \u6700\u5e38\u89c1\u7684\u7528\u6cd5\u4e4b\u4e00&#xff0c;\u5373\u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e&#xff1a;<\/p>\n<p><span class=\"token keyword\">int<\/span> flags <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">fcntl<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> F_GETFL<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u83b7\u53d6\u5f53\u524d\u6807\u5fd7<\/span><br \/>\n<span class=\"token function\">fcntl<\/span><span class=\"token punctuation\">(<\/span>fd<span class=\"token punctuation\">,<\/span> F_SETFL<span class=\"token punctuation\">,<\/span> flags <span class=\"token operator\">|<\/span> O_NONBLOCK<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u4e3a\u975e\u963b\u585e<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124236-688a133cbe642.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124236-688a133cec2a4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>IOManager::ContextResize():<\/h3>\n<p>\u4e3b\u8981\u4f5c\u7528\u662f\u8c03\u6574m_fdContexts\u6570\u7ec4\u7684\u5927\u5c0f&#xff0c;\u5e76\u4e3a\u65b0\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26(fd)\u521b\u5efa\u5e76\u521d\u59cb\u5316\u76f8\u5e94\u7684Fdcontext\u5bf9\u8c61\u3002<\/p>\n<p><span class=\"token comment\">\/\/ no lock<\/span><br \/>\n<span class=\"token comment\">\/\/ \u51fd\u6570\u7684\u7528\u9014\u662f\u8c03\u6574\u7c7b\u5185\u7ef4\u62a4\u7684FdContext\u6570\u7ec4&#xff08;\u6216\u5bb9\u5668&#xff09;\u5927\u5c0f\u5230\u6307\u5b9a\u7684size\u3002<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">contextResize<\/span><span class=\"token punctuation\">(<\/span>size_t size<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>size <span class=\"token operator\">&lt;<\/span> m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">for<\/span> <span class=\"token punctuation\">(<\/span>size_t i <span class=\"token operator\">&#061;<\/span> size<span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">delete<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">\/\/ \u91ca\u653e\u591a\u4f59\u7684FdContext<\/span><br \/>\n            m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8c03\u6574m_fdContexts\u7684\u5927\u5c0f<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u904d\u5386 m_fdContexts \u5411\u91cf&#xff0c;\u521d\u59cb\u5316\u5c1a\u672a\u521d\u59cb\u5316\u7684 FdContext \u5bf9\u8c61<\/span><br \/>\n    m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">resize<\/span><span class=\"token punctuation\">(<\/span>size<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span>size_t i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">new<\/span> <span class=\"token function\">FdContext<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ \u5c06\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u7f16\u53f7\u8d4b\u503c\u7ed9 fd<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u8bbe\u7f6eFdContext\u7684\u6210\u5458fd\u4e3a\u5f53\u524d\u7684\u7d22\u5f15i<\/span><br \/>\n            m_fdContexts<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token operator\">-&gt;<\/span>fd <span class=\"token operator\">&#061;<\/span> i<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u91cd\u70b9\u7684IOManager::addevent\u51fd\u6570:<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124237-688a133d18138.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token comment\">\/\/ addEvent\u65b9\u6cd5\u7528\u4e8e\u5411IO\u7ba1\u7406\u5668\u4e2d\u6ce8\u518c\u4e00\u4e2a\u4e8b\u4ef6&#xff08;\u5982\u8bfb\u6216\u5199\u4e8b\u4ef6&#xff09;<\/span><br \/>\n<span class=\"token comment\">\/\/ fd&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;\u901a\u5e38\u662fsocket&#xff09;\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ event&#xff1a;\u4e8b\u4ef6\u7c7b\u578b&#xff08;\u5982\u8bfb\u4e8b\u4ef6\u6216\u5199\u4e8b\u4ef6&#xff09;\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ cb&#xff1a;\u5f53\u4e8b\u4ef6\u89e6\u53d1\u65f6\u6267\u884c\u7684\u56de\u8c03\u51fd\u6570\u3002<\/span><br \/>\n<span class=\"token keyword\">int<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">addEvent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> Event event<span class=\"token punctuation\">,<\/span> std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;<\/span> cb<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ attemp to find FdContext <\/span><br \/>\n    FdContext<span class=\"token operator\">*<\/span> fd_ctx <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>shared_lock<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>shared_mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">read_lock<\/span><span class=\"token punctuation\">(<\/span>m_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5982\u679c\u5bb9\u5668m_fdContexts\u8db3\u591f\u5927&#xff0c;\u5feb\u901f\u83b7\u53d6\u5bf9\u5e94fd\u7684\u4e0a\u4e0b\u6587\u5bf9\u8c61<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span>m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> fd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        fd_ctx <span class=\"token operator\">&#061;<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>fd<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u5927\u5c0f\u4e0d\u8db3&#xff0c;\u91ca\u653e\u8bfb\u9501&#xff0c;\u7533\u8bf7\u72ec\u5360\u5199\u9501&#xff0c;\u8c03\u7528contextResize(fd * 1.5)\u6269\u5c55\u5bb9\u5668&#xff0c;\u4fdd\u8bc1\u5bb9\u5668\u80fd\u591f\u5bb9\u7eb3\u65b0\u7684fd\u3002<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>unique_lock<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>shared_mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">write_lock<\/span><span class=\"token punctuation\">(<\/span>m_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">contextResize<\/span><span class=\"token punctuation\">(<\/span>fd <span class=\"token operator\">*<\/span> <span class=\"token number\">1.5<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        fd_ctx <span class=\"token operator\">&#061;<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>fd<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u9501\u5b9afd_ctx\u5e76\u68c0\u67e5\u662f\u5426\u5df2\u6709\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token comment\">\/\/ fd_ctx-&gt;mutex\u662f\u4fdd\u62a4FdContext\u81ea\u8eab\u72b6\u6001\u7684\u4e92\u65a5\u9501&#xff0c;\u786e\u4fdd\u591a\u4e2a\u7ebf\u7a0b\u4e0d\u4f1a\u540c\u65f6\u4fee\u6539\u3002<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>lock_guard<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ the event has already been added<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u68c0\u67e5fd_ctx-&gt;events\u662f\u5426\u5df2\u7ecf\u5305\u542b\u5f53\u524d\u7684\u4e8b\u4ef6&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u5df2\u7ecf\u5305\u542b&#xff0c;\u5219\u8868\u793a\u91cd\u590d\u6dfb\u52a0\u4e8b\u4ef6&#xff0c;\u76f4\u63a5\u8fd4\u56de-1&#xff0c;\u8868\u793a\u5931\u8d25\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u4e0d\u5305\u542b&#xff0c;\u7ee7\u7eed\u5f80\u4e0b\u6267\u884c\u3002<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> event<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ add new event<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6784\u5efaepoll\u4e8b\u4ef6\u5e76\u8c03\u7528epoll_ctl<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u786e\u5b9a\u8c03\u7528epoll_ctl\u7684\u64cd\u4f5c\u7c7b\u578b&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u82e5fd_ctx\u5df2\u6709\u4e8b\u4ef6&#xff0c;\u5219\u4f7f\u7528EPOLL_CTL_MOD&#xff08;\u4fee\u6539&#xff09;\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u82e5\u8fd8\u6ca1\u6709\u6ce8\u518c\u4efb\u4f55\u4e8b\u4ef6&#xff0c;\u5219\u4f7f\u7528EPOLL_CTL_ADD&#xff08;\u65b0\u589e&#xff09;\u3002<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> op <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">?<\/span> EPOLL_CTL_MOD<span class=\"token operator\">:<\/span> EPOLL_CTL_ADD<span class=\"token punctuation\">;<\/span><br \/>\n    epoll_event epevent<span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&#061;<\/span> EPOLLET <span class=\"token operator\">|<\/span> fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">|<\/span> event<span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>ptr <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u6210\u529f\u8fd4\u56de0\u5931\u8d25\u8fd4\u56de-1<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_ctl<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> op<span class=\"token punctuation\">,<\/span> fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>epevent<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>rt<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;addEvent::epoll_ctl failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token operator\">&#043;&#043;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ update fdcontext<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u66f4\u65b0fd_ctx\u5185\u90e8\u7684\u4e8b\u4ef6\u72b6\u6001\u548c\u56de\u8c03<\/span><br \/>\n    fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>Event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">|<\/span> event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ update event context<\/span><br \/>\n    <span class=\"token comment\">\/\/ getEventContext(event) \u4f1a\u8fd4\u56de\u5bf9\u5e94\u4e8b\u4ef6\u7684\u5f15\u7528&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6bd4\u5982\u4e8b\u4ef6\u4e3aREAD\u6216WRITE&#xff0c;\u5206\u522b\u8fd4\u56de\u5bf9\u5e94\u7684EventContext\u7ed3\u6784<\/span><br \/>\n    FdContext<span class=\"token operator\">::<\/span>EventContext<span class=\"token operator\">&amp;<\/span> event_ctx <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">getEventContext<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u786e\u4fdd\u4e8b\u4ef6\u4e0a\u4e0b\u6587\u672a\u88ab\u5360\u7528&#xff08;\u9632\u5fa1\u6027\u7f16\u7a0b&#xff09;<\/span><br \/>\n    <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>event_ctx<span class=\"token punctuation\">.<\/span>scheduler <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token operator\">!<\/span>event_ctx<span class=\"token punctuation\">.<\/span>fiber <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token operator\">!<\/span>event_ctx<span class=\"token punctuation\">.<\/span>cb<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5f53\u524d\u7684\u4e8b\u4ef6\u88ab\u6ce8\u518c\u5230\u7279\u5b9a\u7684\u8c03\u5ea6\u5668&#xff08;\u5f53\u524d\u7ebf\u7a0b\u7ed1\u5b9a\u7684\u8c03\u5ea6\u5668&#xff09;<\/span><br \/>\n    event_ctx<span class=\"token punctuation\">.<\/span>scheduler <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Scheduler<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u7ed1\u5b9a\u56de\u8c03\u51fd\u6570&#xff08;\u56de\u8c03\u6a21\u5f0f&#xff09;\u6216\u7ed1\u5b9a\u534f\u7a0b&#xff08;\u534f\u7a0b\u6a21\u5f0f&#xff09;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>cb<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u4f20\u5165\u7684\u53c2\u6570 cb \u4e0d\u4e3a\u7a7a&#xff0c;\u8bf4\u660e\u91c7\u7528\u56de\u8c03\u6a21\u5f0f<\/span><br \/>\n        event_ctx<span class=\"token punctuation\">.<\/span>cb<span class=\"token punctuation\">.<\/span><span class=\"token function\">swap<\/span><span class=\"token punctuation\">(<\/span>cb<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u672a\u63d0\u4f9b\u56de\u8c03\u51fd\u6570&#xff0c;\u9ed8\u8ba4\u4f7f\u7528\u534f\u7a0b\u6a21\u5f0f&#xff1a;<\/span><br \/>\n        event_ctx<span class=\"token punctuation\">.<\/span>fiber <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">Fiber<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u65ad\u8a00\u534f\u7a0b\u72b6\u6001\u4e3a Fiber::RUNNING&#xff0c;\u8bf4\u660e\u5f53\u524d\u4e00\u5b9a\u5904\u4e8e\u534f\u7a0b\u8fd0\u884c\u72b6\u6001\u4e0b\u8c03\u7528\u6b64\u51fd\u6570\u3002<\/span><br \/>\n        <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span>event_ctx<span class=\"token punctuation\">.<\/span>fiber<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">getState<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> Fiber<span class=\"token operator\">::<\/span>RUNNING<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::delEvent\u51fd\u6570&#xff1a;<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124237-688a133d50022.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token comment\">\/\/ delEvent \u51fd\u6570\u7684\u4f5c\u7528\u662f\u4ece\u4e8b\u4ef6\u7ba1\u7406\u5668\u4e2d\u79fb\u9664\u4e00\u4e2a\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u7684\u7279\u5b9a\u4e8b\u4ef6&#xff08;Event&#xff09;\u3002\u901a\u5e38\u7528\u4e8e\u53d6\u6d88\u5bf9\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u7684\u8bfb\u5199\u6216\u5f02\u5e38\u4e8b\u4ef6\u76d1\u542c\u3002<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">delEvent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> Event event<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ attemp to find FdContext <\/span><br \/>\n    <span class=\"token comment\">\/\/\u8fd9\u91cc\u7684\u6b65\u9aa4\u548c\u4e0a\u9762\u7684addevent\u6dfb\u52a0\u4e8b\u4ef6\u7c7b\u4f3c<\/span><br \/>\n    FdContext<span class=\"token operator\">*<\/span> fd_ctx <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8fd9\u91cc\u4f7f\u7528\u8bfb\u9501&#xff08;\u5171\u4eab\u9501&#xff09;\u5b89\u5168\u8bbf\u95eem_fdContexts\u5bb9\u5668&#xff08;\u901a\u5e38\u662f\u6570\u7ec4\u6216vector&#xff09;&#xff0c;\u68c0\u67e5\u6307\u5b9a\u7684fd\u662f\u5426\u5b58\u5728\u5bf9\u5e94\u7684\u4e0a\u4e0b\u6587FdContext\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u82e5\u627e\u5230&#xff0c;\u5219\u4fdd\u5b58\u5230fd_ctx\u6307\u9488\u4e2d&#xff1b;\u5426\u5219\u76f4\u63a5\u8fd4\u56defalse&#xff08;\u8868\u793a\u65e0\u6cd5\u5220\u9664&#xff09;<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>shared_lock<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>shared_mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">read_lock<\/span><span class=\"token punctuation\">(<\/span>m_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span>m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> fd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        fd_ctx <span class=\"token operator\">&#061;<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>fd<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token comment\">\/\/\u5982\u679c\u6ca1\u67e5\u627e\u5230\u4ee3\u8868\u6570\u7ec4\u4e2d\u6ca1\u8fd9\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u76f4\u63a5&#xff0c;\u8fd4\u56defalse&#xff1b;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u627e\u5230\u540e\u6dfb\u52a0\u4e92\u65a5\u9501<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>lock_guard<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ the event doesn&#039;t exist<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u68c0\u67e5\u5f85\u5220\u9664\u4e8b\u4ef6\u662f\u5426\u5b58\u5728<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete the event<\/span><br \/>\n    <span class=\"token comment\">\/\/\u56e0\u4e3a\u8fd9\u91cc\u8981\u5220\u9664\u4e8b\u4ef6&#xff0c;\u5bf9\u539f\u6709\u7684\u4e8b\u4ef6\u72b6\u6001\u53d6\u53cd\u5c31\u662f\u5220\u9664\u539f\u6709\u7684\u72b6\u6001\u6bd4\u5982\u8bf4\u4f20\u5165\u53c2\u6570\u662f\u8bfb\u4e8b\u4ef6&#xff0c;\u6211\u4eec\u53d6\u53cd\u5c31\u662f\u5220\u9664\u4e86\u8fd9\u4e2a\u8bfb\u4e8b\u4ef6\u4f46\u53ef\u80fd\u8fd8\u8981\u5199\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4f7f\u7528\u4f4d\u8fd0\u7b97\u4ece\u539f\u672c\u7684\u4e8b\u4ef6\u63a9\u7801\u4e2d\u79fb\u9664\u6307\u5b9a\u7684event<\/span><br \/>\n    Event new_events <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>Event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> <span class=\"token operator\">~<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5982\u679c\u5220\u9664\u540e\u4e8b\u4ef6\u8fd8\u5b58\u5728\u5176\u4ed6\u76d1\u542c\u4e8b\u4ef6&#xff0c;\u5219\u4fee\u6539&#xff08;EPOLL_CTL_MOD&#xff09;\u76d1\u542c\u7684\u4e8b\u4ef6\u96c6&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5426\u5219&#xff0c;\u6ca1\u6709\u4efb\u4f55\u4e8b\u4ef6\u76d1\u542c\u4e86&#xff0c;\u5219\u4eceepoll\u76d1\u542c\u96c6\u4e2d\u5220\u9664&#xff08;EPOLL_CTL_DEL&#xff09;\u6b64fd\u3002<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> op <span class=\"token operator\">&#061;<\/span> new_events <span class=\"token operator\">?<\/span> EPOLL_CTL_MOD<span class=\"token operator\">:<\/span> EPOLL_CTL_DEL<span class=\"token punctuation\">;<\/span><\/p>\n<p>    epoll_event epevent<span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&#061;<\/span> EPOLLET <span class=\"token operator\">|<\/span> new_events<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u8fd9\u4e00\u6b65\u662f\u4e3a\u4e86\u5728 epoll \u4e8b\u4ef6\u89e6\u53d1\u65f6\u80fd\u591f\u5feb\u901f\u627e\u5230\u4e0e\u8be5\u4e8b\u4ef6\u76f8\u5173\u8054\u7684 FdContext \u5bf9\u8c61\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5e76\u5c06fd_ctx\u4fdd\u5b58\u81f3data.ptr&#xff0c;\u4fbf\u4e8eepoll_wait\u65f6\u83b7\u53d6\u4e0a\u4e0b\u6587<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>ptr <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8c03\u7528epoll_ctl\u66f4\u65b0epoll\u4e8b\u4ef6\u76d1\u542c\u72b6\u6001<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_ctl<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> op<span class=\"token punctuation\">,<\/span> fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>epevent<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>rt<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;delEvent::epoll_ctl failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u51cf\u5c11\u4e86\u5f85\u5904\u7406\u7684\u4e8b\u4ef6<\/span><br \/>\n    <span class=\"token operator\">&#8212;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ update fdcontext<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u66f4\u65b0FdContext\u7684\u4e8b\u4ef6\u63a9\u7801<\/span><br \/>\n    fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&#061;<\/span> new_events<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ update event context<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u91cd\u7f6e\u4e8b\u4ef6\u4e0a\u4e0b\u6587EventContext<\/span><br \/>\n    FdContext<span class=\"token operator\">::<\/span>EventContext<span class=\"token operator\">&amp;<\/span> event_ctx <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">getEventContext<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5c06\u539f\u672c\u5b58\u50a8\u5728FdContext\u5185\u7684\u4e8b\u4ef6\u4e0a\u4e0b\u6587&#xff08;\u901a\u5e38\u5305\u542b\u56de\u8c03\u3001\u534f\u7a0b\u4fe1\u606f\u7b49&#xff09;\u6e05\u7406\u91cd\u7f6e&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u6216\u8bef\u64cd\u4f5c\u3002<\/span><br \/>\n    fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">resetEventContext<\/span><span class=\"token punctuation\">(<\/span>event_ctx<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::cancelEvent\u51fd\u6570&#xff1a;<\/h3>\n<p>\u51fd\u6570\u7528\u4e8e\u53d6\u6d88\u7279\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u7684\u6307\u5b9a\u4e8b\u4ef6(\u5982\u8bfb\u4e8b\u4ef6\u6216\u5199\u4e8b\u4ef6)&#xff0c;\u5e76\u89e6\u53d1\u8be5\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\u5177\u4f53\u7684\u64cd\u4f5c\u597d\u50cf\u8fd9\u51e0\u4e2aevent\u4e8b\u4ef6\u51fd\u6570\u505a\u7684\u90fd\u5dee\u4e0d\u591a&#xff0c;\u4ee3\u7801\u4e5f\u5dee\u4e0d\u591a\u5b9e\u9645\u4e0a\u786e\u5b9e\u5dee\u4e0d\u591a\u3002<\/p>\n<p>\u8fd9\u91cc\u76f8\u6bd4delEvent\u4e0d\u540c\u5728\u4e8e\u5220\u9664\u4e8b\u4ef6\u540e&#xff0c;\u8fd8\u9700\u8981\u5c06\u5220\u9664\u7684\u4e8b\u4ef6\u76f4\u63a5\u4ea4\u7ed9trigger\u51fd\u6570\u653e\u5165\u5230\u534f\u7a0b\u8c03\u5ea6\u5668\u4e2d\u8fdb\u884c\u89e6\u53d1\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u53d6\u6d88\u5e76\u7acb\u5373\u89e6\u53d1\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u4e0a\u7684\u7279\u5b9a\u4e8b\u4ef6\u3002\u4e0edelEvent\u4e0d\u540c\u7684\u662f&#xff0c;\u5b83\u4f1a\u5728\u53d6\u6d88\u4e8b\u4ef6\u7684\u540c\u65f6&#xff0c;\u4e3b\u52a8\u8c03\u7528\u4e0e\u8be5\u4e8b\u4ef6\u5173\u8054\u7684\u56de\u8c03\u51fd\u6570\u6216\u534f\u7a0b&#xff0c;\u4f7f\u4e8b\u4ef6\u5904\u7406\u903b\u8f91\u7acb\u5373\u6267\u884c&#xff0c;\u800c\u4e0d\u662f\u7b49\u5f85\u4e8b\u4ef6\u5b9e\u9645\u89e6\u53d1\u3002<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">cancelEvent<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">,<\/span> Event event<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ attemp to find FdContext <\/span><br \/>\n    FdContext<span class=\"token operator\">*<\/span> fd_ctx <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>shared_lock<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>shared_mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">read_lock<\/span><span class=\"token punctuation\">(<\/span>m_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span>m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> fd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        fd_ctx <span class=\"token operator\">&#061;<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>fd<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>lock_guard<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ the event doesn&#039;t exist<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete the event<\/span><br \/>\n    Event new_events <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>Event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> <span class=\"token operator\">~<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> op <span class=\"token operator\">&#061;<\/span> new_events <span class=\"token operator\">?<\/span> EPOLL_CTL_MOD<span class=\"token operator\">:<\/span> EPOLL_CTL_DEL<span class=\"token punctuation\">;<\/span><br \/>\n    epoll_event epevent<span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&#061;<\/span> EPOLLET <span class=\"token operator\">|<\/span> new_events<span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>ptr <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_ctl<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> op<span class=\"token punctuation\">,<\/span> fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>epevent<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>rt<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;cancelEvent::epoll_ctl failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token operator\">&#8212;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ update fdcontext, event context and trigger<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8fd9\u4e2a\u4ee3\u7801\u548c\u4e0a\u9762\u90a3\u4e2adelEvent\u4e00\u81f4\u597d\u50cf\u5c31\u662f\u6700\u540e\u7684\u5904\u7406\u4e0d\u540c\u4e00\u4e2a\u662f\u91cd\u7f6e&#xff0c;\u4e00\u4e2a\u662f\u8c03\u7528\u4e8b\u4ef6\u7684\u56de\u8c03\u51fd\u6570<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u7acb\u5373\u89e6\u53d1\u4e8b\u4ef6\u56de\u8c03<\/span><br \/>\n    fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::cancelAll():<\/h3>\n<p>\u4e3b\u8981\u529f\u80fd\u662f\u53d6\u6d88\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26(fd)\u4e0a\u7684\u6240\u6709\u4e8b\u4ef6&#xff0c;\u5e76\u4e14\u89e6\u53d1\u8fd9\u4e9b\u4e8b\u4ef6\u7684\u56de\u8c03\u3002 \u548ccancle\u7684\u4e0d\u540c&#xff0c;\u5b8c\u5168\u5c06fd\u4e0a\u7684\u4e8b\u4ef6\u4eceepoll\u4e2d\u79fb\u9664&#xff0c;\u5e76\u4e14\u6328\u4e2a\u7684\u89e6\u53d1\u4e86\u54cd\u5e94\u7684\u8bfb\u4e8b\u4ef6\u6216\u5199\u4e8b\u4ef6\u786e\u4fdd&#xff0c;\u4e8b\u4ef6\u90fd\u80fd\u88ab\u6267\u884c\u3002\u4e0d\u7136\u548b\u52a0\u4e86\u4e00\u4e2aall\u5462&#xff1f;<\/p>\n<p><span class=\"token comment\">\/\/ \u7528\u4e8e\u53d6\u6d88\u6307\u5b9a\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;\u4e0a\u6240\u6709\u5df2\u6ce8\u518c\u7684\u4e8b\u4ef6\u76d1\u542c&#xff0c;\u5e76\u4e14\u7acb\u5373\u4e3b\u52a8\u89e6\u53d1\u6240\u6709\u5df2\u6ce8\u518c\u4e8b\u4ef6\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u6216\u534f\u7a0b\u903b\u8f91\u3002<\/span><br \/>\n<span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">cancelAll<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> fd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ attemp to find FdContext <\/span><br \/>\n    FdContext<span class=\"token operator\">*<\/span> fd_ctx <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">nullptr<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>shared_lock<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>shared_mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">read_lock<\/span><span class=\"token punctuation\">(<\/span>m_mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span>m_fdContexts<span class=\"token punctuation\">.<\/span><span class=\"token function\">size<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> fd<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        fd_ctx <span class=\"token operator\">&#061;<\/span> m_fdContexts<span class=\"token punctuation\">[<\/span>fd<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        read_lock<span class=\"token punctuation\">.<\/span><span class=\"token function\">unlock<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>lock_guard<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ none of events exist<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ delete all events<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> op <span class=\"token operator\">&#061;<\/span> EPOLL_CTL_DEL<span class=\"token punctuation\">;<\/span><br \/>\n    epoll_event epevent<span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    epevent<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>ptr <span class=\"token operator\">&#061;<\/span> fd_ctx<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u6240\u6709\u4e8b\u4ef6\u6e05\u7a7a<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_ctl<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> op<span class=\"token punctuation\">,<\/span> fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>epevent<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>rt<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;IOManager::epoll_ctl failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">false<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ update fdcontext, event context and trigger<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u9010\u4e2a\u68c0\u67e5\u5e76\u89e6\u53d1\u5df2\u6ce8\u518c\u4e8b\u4ef6\u7684\u56de\u8c03<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u68c0\u6d4b\u5e76\u4e3b\u52a8\u89e6\u53d1\u6240\u6709\u5df2\u6ce8\u518c\u4e8b\u4ef6&#xff08;\u5982\u8bfb\u4e8b\u4ef6\u3001\u5199\u4e8b\u4ef6&#xff09;\u7684\u56de\u8c03\u51fd\u6570\u6216\u534f\u7a0b\u4efb\u52a1\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6bcf\u89e6\u53d1\u4e00\u4e2a\u4e8b\u4ef6\u7684\u56de\u8c03&#xff0c;\u90fd\u9700\u8981\u51cf\u5c11\u5168\u5c40\u5f85\u5904\u7406\u4e8b\u4ef6\u8ba1\u6570\u5668&#xff08;m_pendingEventCount&#xff09;\u3002<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> READ<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>READ<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token operator\">&#8212;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> WRITE<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>WRITE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token operator\">&#8212;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u786e\u8ba4\u6700\u7ec8fd_ctx-&gt;events\u63a9\u7801\u4e2d\u4e8b\u4ef6\u5df2\u88ab\u5168\u90e8\u6e05\u9664\u3002<\/span><br \/>\n    <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> <span class=\"token boolean\">true<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::tickle():<\/h3>\n<p>\u91cd\u5199\u4e86scheduler\u7684tickle(); \u4f5c\u7528\u662f\u68c0\u6d4b\u5230\u6709\u7a7a\u95f2\u7ebf\u7a0b\u65f6&#xff0c;\u901a\u8fc7\u5199\u5165\u4e00\u4e2a\u5b57\u7b26\u5230\u7ba1(m_tickleFds[1]) \u4e2d&#xff0c;\u5524\u9192\u90a3\u4e9b\u7b49\u5f85\u4efb\u52a1\u7684\u7ebf\u7a0b\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u7528\u4e8e\u5524\u9192\u5f53\u524dIO\u7ba1\u7406\u5668\u4e2d\u5904\u4e8e\u7a7a\u95f2\u7b49\u5f85&#xff08;idle&#xff09;\u72b6\u6001\u7684\u7ebf\u7a0b\u3002<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">tickle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ no idle threads<\/span><br \/>\n    <span class=\"token comment\">\/\/\u8fd9\u4e2a\u51fd\u6570\u5728scheduler\u68c0\u67e5\u5f53\u524d\u662f\u5426\u6709\u7ebf\u7a0b\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\u3002\u5982\u679c\u6ca1\u6709\u7a7a\u95f2\u7ebf\u7a0b&#xff0c;\u51fd\u6570\u76f4\u63a5\u8fd4\u56de&#xff0c;\u4e0d\u6267\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u82e5\u5f53\u524d\u6ca1\u6709\u4efb\u4f55\u7ebf\u7a0b\u5904\u4e8e\u7a7a\u95f2&#xff08;\u7b49\u5f85\u4e8b\u4ef6&#xff09;\u7684\u72b6\u6001&#xff0c;\u5219\u65e0\u9700\u5524\u9192\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u63d0\u524d\u8fd4\u56de&#xff0c;\u907f\u514d\u65e0\u610f\u4e49\u7684\u5524\u9192\u8c03\u7528\u3002<\/span><br \/>\n    <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span><span class=\"token function\">hasIdleThreads<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">return<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/\u5982\u679c\u6709\u7a7a\u95f2\u7ebf\u7a0b&#xff0c;\u51fd\u6570\u4f1a\u5411\u7ba1\u9053 m_tickleFds[1] \u5199\u5165\u4e00\u4e2a\u5b57\u7b26 &#034;T&#034;\u3002\u8fd9\u4e2a\u5199\u64cd\u4f5c\u7684\u76ee\u7684\u662f\u5411\u7b49\u5f85\u5728 m_tickleFds[0]&#xff08;\u7ba1\u9053\u7684\u53e6\u4e00\u7aef&#xff09;\u7684\u7ebf\u7a0b\u53d1\u9001\u4e00\u4e2a\u4fe1\u53f7&#xff0c;\u901a\u77e5\u5b83\u6709\u65b0\u4efb\u52a1\u53ef\u4ee5\u5904\u7406\u4e86\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u7528\u4e8e\u5524\u9192\u5904\u4e8e\u963b\u585e\u7b49\u5f85\u72b6\u6001&#xff08;\u4f8b\u5982\u8c03\u7528epoll_wait()&#xff09;\u7684\u7ebf\u7a0b<\/span><br \/>\n    <span class=\"token comment\">\/\/ ssize_t write(int fd, const void *buf, size_t count);<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5c06\u7f13\u51b2\u533abuf\u4e2d\u6700\u591acount\u5b57\u8282\u7684\u6570\u636e\u5199\u5165\u5230\u6587\u4ef6\u63cf\u8ff0\u7b26fd\u4e2d\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6210\u529f\u65f6\u8fd4\u56de\u5b9e\u9645\u5199\u5165\u7684\u5b57\u8282\u6570&#xff08;\u5e94\u4e0ecount\u76f8\u540c&#xff09;&#xff0c;\u5931\u8d25\u5219\u8fd4\u56de-1\u3002<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">write<\/span><span class=\"token punctuation\">(<\/span>m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;T&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token function\">assert<\/span><span class=\"token punctuation\">(<\/span>rt <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::stopping():<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124237-688a133d7d8b3.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token keyword\">bool<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">stopping<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token keyword\">uint64_t<\/span> timeout <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">getNextTimer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ no timers left and no pending events left with the Scheduler::stopping()<\/span><br \/>\n    <span class=\"token keyword\">return<\/span> timeout <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token operator\">~<\/span><span class=\"token number\">0ull<\/span> <span class=\"token operator\">&amp;&amp;<\/span> m_pendingEventCount <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span> <span class=\"token operator\">&amp;&amp;<\/span> <span class=\"token class-name\">Scheduler<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">stopping<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::idle():<\/h3>\n<p>\u8fd9\u91cc\u662fScheduler\u7684idle&#xff0c;IOmanager\u5bf9idle\u7684\u4e00\u4e2a\u91cd\u5199;<\/p>\n<p>\u901a\u5e38\u5728\u6ca1\u6709\u4efb\u52a1\u5904\u7406\u65f6\u8fd0\u884c(\u6216\u8005\u5373\u4f7f\u5f53\u524d\u6ca1\u6709\u4efb\u52a1\u5904\u7406&#xff0c;\u7ebf\u7a0b\u4e5f\u4f1a\u5728 idle() \u4e2d\u6301\u7eed\u4f11\u7720\u5e76\u7b49\u5f85\u65b0\u7684\u4efb\u52a1\u3002\u4fdd\u8bc1\u4e86\u5728\u6240\u6709\u4efb\u52a1\u5b8c\u6210\u4e4b\u524d&#xff0c;\u8c03\u5ea6\u5668\u4e0d\u4f1a\u9000\u51fa)&#xff0c;\u7b49\u5f85\u548c\u5904\u7406 I\/O \u4e8b\u4ef6\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124237-688a133d9fd1b.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124238-688a133e45cef.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124238-688a133e8b9f0.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124238-688a133ee8424.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><span class=\"token comment\">\/\/ \u672c\u8d28\u662f\u4e00\u4e2a\u8fd0\u884c\u4e8eFiber&#xff08;\u534f\u7a0b&#xff09;\u6216\u72ec\u7acb\u7ebf\u7a0b\u4e0a\u7684\u4e8b\u4ef6\u5faa\u73af\u51fd\u6570&#xff0c;\u8d1f\u8d23\u76d1\u89c6\u5e76\u5904\u7406IO\u4e8b\u4ef6\u4e0e\u5b9a\u65f6\u4efb\u52a1\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ \u8be5\u51fd\u6570\u5229\u7528\u4e86Linux\u7684\u9ad8\u6548I\/O\u590d\u7528\u673a\u5236&#xff08;epoll&#xff09;&#xff0c;\u5e76\u7ed3\u5408\u8d85\u65f6\u673a\u5236\u4e0e\u534f\u7a0b\u8c03\u5ea6&#xff0c;\u6784\u5efa\u4e00\u4e2a\u5f02\u6b65\u9ad8\u6548\u7684\u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u3002<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">idle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/\u5b9a\u4e49\u4e86 epoll_wait \u80fd\u540c\u65f6\u5904\u7406\u7684\u6700\u5927\u4e8b\u4ef6\u6570\u3002<\/span><br \/>\n    <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">uint64_t<\/span> MAX_EVENTS <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">256<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ &lt;epoll_event[]&gt;&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8868\u793a\u7ba1\u7406\u7684\u662f\u4e00\u4e2a\u52a8\u6001\u6570\u7ec4&#xff0c;\u800c\u4e0d\u662f\u5355\u4e2a\u5bf9\u8c61\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u56e0\u6b64&#xff0c;unique_ptr\u4f1a\u8c03\u7528delete[]\u91ca\u653e\u6570\u7ec4\u5185\u5b58<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4f7f\u7528 std::unique_ptr \u52a8\u6001\u5206\u914d\u4e86\u4e00\u4e2a\u5927\u5c0f\u4e3a MAX_EVENTS \u7684 epoll_event \u6570\u7ec4&#xff0c;\u7528\u4e8e\u5b58\u50a8\u4ece epoll_wait \u83b7\u53d6\u7684\u4e8b\u4ef6<\/span><br \/>\n    std<span class=\"token operator\">::<\/span>unique_ptr<span class=\"token operator\">&lt;<\/span>epoll_event<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token operator\">&gt;<\/span> <span class=\"token function\">events<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">new<\/span> epoll_event<span class=\"token punctuation\">[<\/span>MAX_EVENTS<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>debug<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;IOManager::idle(),run in thread: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token class-name\">Thread<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThreadId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5982\u679cIOManager\u51c6\u5907\u505c\u6b62&#xff08;stopping()\u8fd4\u56detrue&#xff09;&#xff0c;\u5219\u9000\u51fa\u5faa\u73af\u5e76\u7ed3\u675fidle()\u8fd0\u884c<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">stopping<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>debug<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;name &#061; &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">getName<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034; idle exits in thread: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token class-name\">Thread<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThreadId<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ blocked at epoll_wait<\/span><br \/>\n        <span class=\"token comment\">\/\/ int epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout);<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u76d1\u63a7\u4e00\u4e2a\u6216\u591a\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26(fd)\u4e0a\u7684\u4e8b\u4ef6\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679c\u6307\u5b9a\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u53d1\u751f\u4e86\u4e8b\u4ef6&#xff08;\u5982\u8bfb\u3001\u5199\u6216\u9519\u8bef&#xff09;&#xff0c;\u5b83\u4f1a\u8fd4\u56de\u5e76\u586b\u5145events\u6570\u7ec4\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u82e5\u65e0\u4e8b\u4ef6\u53d1\u751f&#xff0c;\u5219\u5728timeout\u6beb\u79d2\u540e\u8fd4\u56de\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5982\u679ctimeout\u8bbe\u4e3a-1&#xff0c;\u4f1a\u65e0\u9650\u671f\u963b\u585e\u76f4\u5230\u6709\u4e8b\u4ef6\u53d1\u751f\u3002<\/span><br \/>\n        <span class=\"token comment\">\/*<br \/>\n        epfd&#xff1a;<br \/>\n        epoll \u5b9e\u4f8b\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u7531 epoll_create \u6216 epoll_create1 \u521b\u5efa\u3002<br \/>\n        \u6b64\u5904\u4e3a\u53d8\u91cf m_epfd\u3002<br \/>\n        events&#xff1a;<br \/>\n        \u6307\u5411\u7528\u6237\u5206\u914d\u7684\u6570\u7ec4&#xff0c;\u5185\u5b58\u7528\u4e8e\u5b58\u50a8\u8fd4\u56de\u7684\u4e8b\u4ef6\u3002<br \/>\n        \u6b64\u5904\u4e3a\u667a\u80fd\u6307\u9488 events.get()&#xff0c;\u6307\u5411 epoll_event \u7c7b\u578b\u6570\u7ec4\u3002<br \/>\n        maxevents&#xff1a;<br \/>\n        \u5141\u8bb8\u8fd4\u56de\u7684\u6700\u5927\u4e8b\u4ef6\u6570\u91cf&#xff0c;\u8868\u793a\u6570\u7ec4\u6700\u5927\u957f\u5ea6\u3002<br \/>\n        \u6b64\u5904\u4e3a MAX_EVENTS (\u6bd4\u5982256)\u3002<br \/>\n        timeout&#xff1a;<br \/>\n        \u7b49\u5f85\u8d85\u65f6\u65f6\u95f4&#xff0c;\u4ee5\u6beb\u79d2\u4e3a\u5355\u4f4d\u3002<br \/>\n        \u5982\u679c timeout &#061; -1&#xff0c;\u8868\u793a\u4e00\u76f4\u963b\u585e\u76f4\u5230\u6709\u4e8b\u4ef6\u53d1\u751f\u3002<br \/>\n        \u5982\u679c timeout &#061; 0&#xff0c;\u8868\u793a\u7acb\u5373\u8fd4\u56de&#xff08;\u975e\u963b\u585e&#xff09;\u3002<br \/>\n        \u5982\u679c timeout &gt; 0&#xff0c;\u8868\u793a\u6700\u591a\u963b\u585e\u8fd9\u4e48\u591a\u6beb\u79d2\u3002*\/<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u8fd4\u56de\u503c\u542b\u4e49&#xff1a;<\/span><br \/>\n        <span class=\"token comment\">\/\/ &gt; 0 \u8868\u793a\u89e6\u53d1\u4e8b\u4ef6\u7684\u6570\u91cf\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ &#061; 0 \u8868\u793a\u8d85\u65f6&#xff0c;\u672a\u6709\u4e8b\u4ef6\u53d1\u751f\u3002<\/span><br \/>\n        <span class=\"token comment\">\/\/ &lt; 0 \u51fa\u9519&#xff0c;\u9700\u68c0\u67e5errno\u786e\u5b9a\u9519\u8bef\u539f\u56e0\u3002<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u5b58\u653eepoll_wait\u8c03\u7528\u7684\u8fd4\u56de\u503c&#xff08;\u89e6\u53d1\u4e8b\u4ef6\u7684\u6570\u91cf\u6216\u9519\u8bef\u7801&#xff09;<\/span><br \/>\n        <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ \u65e0\u9650\u5faa\u73af\u76f4\u81f3epoll_wait\u6210\u529f\u8fd4\u56de\u6216\u53d1\u751f\u975e\u4fe1\u53f7\u4e2d\u65ad\u9519\u8bef<\/span><br \/>\n        <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token boolean\">true<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5c06next_timeout\u9650\u5236\u5728\u6700\u59275000ms&#xff08;5\u79d2&#xff09;\u5185<\/span><br \/>\n            <span class=\"token keyword\">static<\/span> <span class=\"token keyword\">const<\/span> <span class=\"token keyword\">uint64_t<\/span> MAX_TIMEOUT <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">5000<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/\u83b7\u53d6\u4e0b\u4e00\u4e2a\u8d85\u65f6\u7684\u5b9a\u65f6\u5668<\/span><br \/>\n            <span class=\"token keyword\">uint64_t<\/span> next_timeout <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">getNextTimer<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/\u83b7\u53d6\u4e0b\u4e00\u4e2a\u5b9a\u65f6\u5668\u7684\u8d85\u65f6\u65f6\u95f4&#xff0c;\u5e76\u5c06\u5176\u4e0e MAX_TIMEOUT \u53d6\u8f83\u5c0f\u503c&#xff0c;\u907f\u514d\u7b49\u5f85\u65f6\u95f4\u8fc7\u957f\u3002<\/span><br \/>\n            next_timeout <span class=\"token operator\">&#061;<\/span> std<span class=\"token operator\">::<\/span><span class=\"token function\">min<\/span><span class=\"token punctuation\">(<\/span>next_timeout<span class=\"token punctuation\">,<\/span> MAX_TIMEOUT<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ std::unique_ptr\u901a\u8fc7get()\u65b9\u6cd5\u8fd4\u56de\u5176\u7ba1\u7406\u7684\u539f\u59cb\u6307\u9488&#xff08;\u88f8\u6307\u9488&#xff09;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u6ce8\u610f&#xff1a;\u6b64\u5904\u5fc5\u987b\u63d0\u4f9bC\u98ce\u683c\u88f8\u6307\u9488\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ C&#043;&#043;\u667a\u80fd\u6307\u9488\u65e0\u6cd5\u76f4\u63a5\u9690\u5f0f\u8f6c\u6362\u4e3a\u539f\u59cb\u6307\u9488&#xff0c;\u56e0\u6b64\u5fc5\u987b\u663e\u5f0f\u8c03\u7528get()<\/span><br \/>\n            <span class=\"token comment\">\/\/epoll_wait\u9677\u5165\u963b\u585e&#xff0c;\u7b49\u5f85tickle\u4fe1\u53f7\u7684\u5524\u9192&#xff0c;<\/span><br \/>\n            <span class=\"token comment\">\/\/\u5e76\u4e14\u4f7f\u7528\u4e86\u5b9a\u65f6\u5668\u5806\u4e2d\u6700\u65e9\u8d85\u65f6\u7684\u5b9a\u65f6\u5668\u4f5c\u4e3aepoll_wait\u8d85\u65f6\u65f6\u95f4\u3002<\/span><br \/>\n            rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_wait<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> events<span class=\"token punctuation\">.<\/span><span class=\"token function\">get<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> MAX_EVENTS<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span><span class=\"token punctuation\">)<\/span>next_timeout<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ EINTR -&gt; retry<\/span><br \/>\n            <span class=\"token comment\">\/\/ EINTR\u5168\u79f0\u4e3a &#034;Interrupted system call&#034;&#xff08;\u88ab\u4e2d\u65ad\u7684\u7cfb\u7edf\u8c03\u7528&#xff09;&#xff0c;\u662f\u5728Linux\/Unix\u73af\u5883\u4e2d\u975e\u5e38\u5e38\u89c1\u7684\u4e00\u79cd\u9519\u8bef\u8fd4\u56de\u503c\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5f53\u7a0b\u5e8f\u8c03\u7528\u67d0\u4e9b\u7cfb\u7edf\u51fd\u6570&#xff08;\u4f8b\u5982epoll_wait()\u3001select()\u3001read()\u7b49&#xff09;\u65f6&#xff0c;\u82e5\u8c03\u7528\u8fc7\u7a0b\u4e2d\u6536\u5230\u4e86\u4fe1\u53f7&#xff08;\u4f8b\u5982SIGALRM\u3001SIGINT\u3001SIGTERM\u7b49&#xff09;&#xff0c;\u7cfb\u7edf\u8c03\u7528\u53ef\u80fd\u4f1a\u88ab\u4e2d\u65ad&#xff0c;\u5e76\u7acb\u5373\u8fd4\u56de-1&#xff0c;\u540c\u65f6\u8bbe\u7f6eerrno &#061; EINTR\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u6ce8\u610f&#xff1a;EINTR\u5e76\u975e\u771f\u6b63\u7684\u9519\u8bef&#xff0c;\u800c\u662f\u4e00\u79cd\u4e2d\u65ad&#xff0c;\u63d0\u793a\u7a0b\u5e8f\u201c\u5f53\u524d\u8c03\u7528\u672a\u5b8c\u6210&#xff0c;\u9700\u8981\u91cd\u65b0\u5c1d\u8bd5\u201d\u3002<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>rt <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token operator\">&amp;&amp;<\/span> errno <span class=\"token operator\">&#061;&#061;<\/span> EINTR<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">continue<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">break<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ collect all timers overdue<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5904\u7406\u5230\u671f\u7684\u5b9a\u65f6\u4efb\u52a1<\/span><br \/>\n        <span class=\"token comment\">\/\/\u7528\u4e8e\u5b58\u50a8\u8d85\u65f6\u7684\u56de\u8c03\u51fd\u6570\u3002<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>vector<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>function<span class=\"token operator\">&lt;<\/span><span class=\"token keyword\">void<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&gt;&gt;<\/span> cbs<span class=\"token punctuation\">;<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u7528\u6765\u83b7\u53d6\u6240\u6709\u8d85\u65f6\u7684\u5b9a\u65f6\u5668\u56de\u8c03&#xff0c;\u5e76\u5c06\u5b83\u4eec\u6dfb\u52a0\u5230 cbs \u5411\u91cf\u4e2d<\/span><br \/>\n        <span class=\"token function\">listExpiredCb<\/span><span class=\"token punctuation\">(<\/span>cbs<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">!<\/span>cbs<span class=\"token punctuation\">.<\/span><span class=\"token function\">empty<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">const<\/span> <span class=\"token keyword\">auto<\/span><span class=\"token operator\">&amp;<\/span> cb<span class=\"token operator\">:<\/span> cbs<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5c06\u5b9a\u65f6\u5668\u56de\u8c03\u8c03\u5ea6\u5230\u534f\u7a0b\/\u4efb\u52a1\u961f\u5217\u4e2d\u5f02\u6b65\u6267\u884c\u3002<\/span><br \/>\n                <span class=\"token function\">scheduleLock<\/span><span class=\"token punctuation\">(<\/span>cb<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n            cbs<span class=\"token punctuation\">.<\/span><span class=\"token function\">clear<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/ collect all events ready<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5904\u7406epoll_wait\u8fd4\u56de\u7684\u6240\u6709I\/O\u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token comment\">\/\/ \u5faa\u73af\u5904\u7406\u6b64\u6b21\u8c03\u7528epoll_wait\u8fd4\u56de\u7684rt\u4e2a\u4e8b\u4ef6<\/span><br \/>\n        <span class=\"token keyword\">for<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> i <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span> i <span class=\"token operator\">&lt;<\/span> rt<span class=\"token punctuation\">;<\/span> <span class=\"token operator\">&#043;&#043;<\/span>i<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u83b7\u53d6\u7b2c i \u4e2a epoll_event&#xff0c;\u7528\u4e8e\u5904\u7406\u8be5\u4e8b\u4ef6\u3002<\/span><br \/>\n            epoll_event<span class=\"token operator\">&amp;<\/span> event <span class=\"token operator\">&#061;<\/span> events<span class=\"token punctuation\">[<\/span>i<span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ tickle event<\/span><br \/>\n            <span class=\"token comment\">\/\/\u68c0\u67e5\u5f53\u524d\u4e8b\u4ef6\u662f\u5426\u662f tickle \u4e8b\u4ef6&#xff08;\u5373\u7528\u4e8e\u5524\u9192\u7a7a\u95f2\u7ebf\u7a0b\u7684\u4e8b\u4ef6&#xff09;\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5f53\u5176\u4ed6\u7ebf\u7a0b\u6dfb\u52a0\u4e86\u65b0\u4efb\u52a1\u6216\u65b0\u76d1\u542c\u4e8b\u4ef6\u65f6&#xff0c;\u9700\u8981\u7acb\u523b\u901a\u77e5&#xff08;\u5524\u9192&#xff09;\u963b\u585e\u7ebf\u7a0b\u91cd\u65b0\u5904\u7406<\/span><br \/>\n            <span class=\"token comment\">\/\/ tickle\u4e8b\u4ef6&#xff08;\u901a\u5e38\u662f\u4e00\u4e2a\u7ba1\u9053\u6216eventfd&#xff09;\u7528\u4e8e\u5524\u9192\u963b\u585e\u5728epoll_wait\u7684\u7ebf\u7a0b\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u91c7\u7528\u8fb9\u7f18\u89e6\u53d1\u6a21\u5f0f&#xff08;EPOLLET&#xff09;&#xff0c;\u5fc5\u987b\u5c06\u7ba1\u9053\u5185\u6570\u636e\u5168\u90e8\u8bfb\u51fa&#xff0c;\u5426\u5219\u4e0b\u6b21\u4e0d\u4f1a\u518d\u901a\u77e5\u3002<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>fd <span class=\"token operator\">&#061;&#061;<\/span> m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">uint8_t<\/span> dummy<span class=\"token punctuation\">[<\/span><span class=\"token number\">256<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>                <span class=\"token comment\">\/\/ edge triggered -&gt; exhaust<\/span><br \/>\n                <span class=\"token comment\">\/\/ epoll_wait\u4fa6\u6d4b\u5230\u53ef\u8bfb\u4e8b\u4ef6\u540e\u8fd4\u56de&#xff0c;\u5982\u679c\u4e0d\u53ca\u65f6\u5c06\u7ba1\u9053\u5185\u7684\u6570\u636e\u8bfb\u8d70&#xff0c;\u4e0b\u6b21\u8c03\u7528epoll_wait\u8fd8\u4f1a\u6301\u7eed\u8fd4\u56de&#xff08;\u56e0\u4e3a\u7ba1\u9053\u5185\u8fd8\u6709\u6570\u636e&#xff09;&#xff0c;\u9020\u6210\u65e0\u6548\u5524\u9192&#xff08;busy loop&#xff09;\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u56e0\u6b64\u5fc5\u987b\u5728\u63a5\u6536\u5230\u4e8b\u4ef6\u901a\u77e5\u540e\u4e00\u6b21\u6027\u5c06\u7ba1\u9053\u5185\u6570\u636e\u5168\u90e8\u8bfb\u53d6\u5b8c\u6bd5\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5bf9\u4e8e\u4e00\u4e2a\u7ba1\u9053&#xff0c;\u5982\u679c\u7ba1\u9053\u91cc\u6709\u591a\u4e2a\u5b57\u8282\u6570\u636e&#xff08;\u53ef\u80fd\u662f\u591a\u6b21\u5199\u5165&#xff09;&#xff0c;epoll_wait\u53ea\u4f1a\u5728\u7ba1\u9053\u4e2d\u6709\u6570\u636e\u53ef\u8bfb\u65f6\u89e6\u53d1\u4e00\u6b21\u4e8b\u4ef6\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u4e3a\u4e86\u786e\u4fdd\u6bcf\u6b21\u4e8b\u4ef6\u89e6\u53d1\u90fd\u80fd\u201c\u6d88\u8d39\u6389\u201d\u7ba1\u9053\u4e2d\u7684\u6240\u6709\u6570\u636e&#xff0c;\u5fc5\u987b\u5c06\u7ba1\u9053\u4e2d\u79ef\u7d2f\u7684\u6570\u636e\u4e00\u6b21\u6027\u8bfb\u5b8c\u3002\u5426\u5219&#xff0c;\u4e0b\u6b21epoll_wait\u88ab\u8c03\u7528\u65f6&#xff0c;\u53ef\u80fd\u65e0\u6cd5\u518d\u6b21\u89e6\u53d1tickle\u4e8b\u4ef6\u3002<\/span><\/p>\n<p>                <span class=\"token comment\">\/\/ ssize_t read(int fd, void *buf, size_t count);<\/span><br \/>\n                <span class=\"token comment\">\/\/ fd&#xff1a;\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;file descriptor&#xff09;&#xff0c;\u8868\u793a\u8981\u8bfb\u53d6\u7684\u6587\u4ef6\u3001\u7ba1\u9053\u3001socket \u7b49\u7684\u6807\u8bc6\u7b26\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ buf&#xff1a;\u6307\u5411\u7f13\u51b2\u533a\u7684\u6307\u9488&#xff0c;\u5b58\u50a8\u8bfb\u53d6\u7684\u6570\u636e\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ count&#xff1a;\u671f\u671b\u8bfb\u53d6\u7684\u5b57\u8282\u6570&#xff08;buffer\u5927\u5c0f&#xff09;\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u8fd4\u56de\u503c&#xff08;ssize_t&#xff0c;\u6709\u7b26\u53f7\u6574\u578b&#xff09;&#xff1a;<\/span><br \/>\n                <span class=\"token comment\">\/\/ &gt; 0&#xff1a;\u6210\u529f\u8bfb\u53d6\u7684\u5b57\u8282\u6570\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ &#061; 0&#xff1a;\u5df2\u5230\u8fbe\u6587\u4ef6\u672b\u5c3e&#xff08;EOF&#xff09;\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ &lt; 0&#xff1a;\u8bfb\u53d6\u5931\u8d25&#xff0c;\u51fa\u9519\u4fe1\u606f\u5b58\u50a8\u5728errno\u4e2d<\/span><br \/>\n                <span class=\"token keyword\">while<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">read<\/span><span class=\"token punctuation\">(<\/span>m_tickleFds<span class=\"token punctuation\">[<\/span><span class=\"token number\">0<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">,<\/span> dummy<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>dummy<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token keyword\">continue<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ other events<\/span><br \/>\n            <span class=\"token comment\">\/\/\u901a\u8fc7 event.data.ptr \u83b7\u53d6\u4e0e\u5f53\u524d\u4e8b\u4ef6\u5173\u8054\u7684 FdContext \u6307\u9488 fd_ctx&#xff0c;\u8be5\u6307\u9488\u5305\u542b\u4e86\u4e0e\u6587\u4ef6\u63cf\u8ff0\u7b26\u76f8\u5173\u7684\u4e0a\u4e0b\u6587\u4fe1\u606f\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u666e\u901a\u4e8b\u4ef6\u5904\u7406\u903b\u8f91&#xff1a;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u83b7\u53d6 fd \u7684\u4e0a\u4e0b\u6587&#xff0c;\u5e76\u52a0\u9501&#xff1a;<\/span><br \/>\n            FdContext<span class=\"token operator\">*<\/span> fd_ctx <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>FdContext<span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>event<span class=\"token punctuation\">.<\/span>data<span class=\"token punctuation\">.<\/span>ptr<span class=\"token punctuation\">;<\/span><br \/>\n            std<span class=\"token operator\">::<\/span>lock_guard<span class=\"token operator\">&lt;<\/span>std<span class=\"token operator\">::<\/span>mutex<span class=\"token operator\">&gt;<\/span> <span class=\"token function\">lock<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>mutex<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ convert EPOLLERR or EPOLLHUP to -&gt; read or write event<\/span><br \/>\n            <span class=\"token comment\">\/\/\u5982\u679c\u5f53\u524d\u4e8b\u4ef6\u662f\u9519\u8bef\u6216\u6302\u8d77&#xff08;EPOLLERR \u6216 EPOLLHUP&#xff09;&#xff0c;\u5219\u5c06\u5176\u8f6c\u6362\u4e3a\u53ef\u8bfb\u6216\u53ef\u5199\u4e8b\u4ef6&#xff08;EPOLLIN \u6216 EPOLLOUT&#xff09;&#xff0c;\u4ee5\u4fbf\u540e\u7eed\u5904\u7406\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ EPOLLERR&#xff1a;\u8868\u793a fd \u4e0a\u53d1\u751f\u4e86\u9519\u8bef&#xff08;\u4f8b\u5982 socket \u51fa\u9519&#xff09;\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ EPOLLHUP&#xff1a;\u8868\u793a\u5bf9\u7aef\u5173\u95ed\u8fde\u63a5&#xff08;\u4f8b\u5982 socket \u88ab\u5bf9\u65b9\u5173\u95ed&#xff09;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&amp;<\/span> <span class=\"token punctuation\">(<\/span>EPOLLERR <span class=\"token operator\">|<\/span> EPOLLHUP<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ fd_ctx-&gt;events&#xff1a;\u8be5 fd \u5f53\u524d\u7528\u6237\u6ce8\u518c\u76d1\u542c\u7684\u4e8b\u4ef6\u96c6\u5408&#xff08;READ\/WRITE&#xff09;\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ EPOLLIN | EPOLLOUT&#xff1a;\u8868\u793a \u201c\u53ef\u8bfb\u6216\u53ef\u5199\u201d \u4e24\u79cd\u4e8b\u4ef6\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u53ea\u8f6c\u5316 \u7528\u6237\u5173\u5fc3\u7684\u4e8b\u4ef6&#xff0c;\u6240\u4ee5\u8981\u4e0e fd_ctx-&gt;events \u505a\u4e00\u4e2a \u6309\u4f4d\u4e0e\u8fd0\u7b97<\/span><br \/>\n                event<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">|&#061;<\/span> <span class=\"token punctuation\">(<\/span>EPOLLIN <span class=\"token operator\">|<\/span> EPOLLOUT<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&amp;<\/span> fd_ctx<span class=\"token operator\">-&gt;<\/span>events<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ events happening during this turn of epoll_wait<\/span><br \/>\n            <span class=\"token comment\">\/\/\u786e\u5b9a\u5b9e\u9645\u53d1\u751f\u7684\u4e8b\u4ef6\u7c7b\u578b&#xff08;\u8bfb\u53d6\u3001\u5199\u5165\u6216\u4e24\u8005&#xff09;\u3002<\/span><br \/>\n            <span class=\"token keyword\">int<\/span> real_events <span class=\"token operator\">&#061;<\/span> NONE<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ EPOLLIN \u548c EPOLLOUT \u662f Linux epoll \u4e8b\u4ef6\u673a\u5236\u4e2d\u7684\u4e8b\u4ef6\u6807\u5fd7&#xff0c;\u7528\u4e8e\u8868\u793a\u4f60\u60f3\u8981\u201c\u76d1\u542c\u4ec0\u4e48\u7c7b\u578b\u7684\u4e8b\u4ef6\u201d&#xff0c;\u6216\u8005\u201c\u67d0\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u4ef6\u201d\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/ EPOLLIN \u8868\u793a&#xff1a;\u8fd9\u4e2a fd \u4e0a\u73b0\u5728\u201c\u53ef\u8bfb\u201d&#xff0c;\u4e5f\u5c31\u662f&#xff1a;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u6709\u6570\u636e\u53ef\u8bfb&#xff08;\u5bf9\u4e8e socket\/\u7ba1\u9053&#xff09;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u6587\u4ef6\u53ef\u8bfb<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5ba2\u6237\u7aef\u8fde\u63a5\u5df2\u5c31\u7eea&#xff08;\u5bf9\u4e8e\u76d1\u542c socket&#xff09;<\/span><br \/>\n            <span class=\"token comment\">\/\/ EPOLLOUT \u8868\u793a&#xff1a;\u8fd9\u4e2a fd \u4e0a\u73b0\u5728\u201c\u53ef\u5199\u201d&#xff0c;\u4e5f\u5c31\u662f&#xff1a;<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u53d1\u9001\u7f13\u51b2\u533a\u6709\u8db3\u591f\u7a7a\u95f4\u53ef\u4ee5\u5199\u5165\u6570\u636e<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5224\u65ad epoll \u662f\u5426\u8fd4\u56de\u4e86\u8bfb\u4e8b\u4ef6<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&amp;<\/span> EPOLLIN<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u5982\u679c epoll_wait() \u8fd4\u56de\u7684 event \u5305\u542b EPOLLIN&#xff08;\u53ef\u8bfb&#xff09;&#xff0c;\u8bf4\u660e\u8fd9\u4e2a fd \u5f53\u524d\u53ef\u4ee5\u8bfb\u4e86\u3002<\/span><br \/>\n                <span class=\"token comment\">\/\/ \u628a real_events \u6807\u8bb0\u4e0a READ&#xff08;\u6846\u67b6\u5185\u90e8\u81ea\u5b9a\u4e49\u7684\u679a\u4e3e&#xff09;\u3002<\/span><br \/>\n                real_events <span class=\"token operator\">|&#061;<\/span> READ<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ \u5224\u65ad epoll \u662f\u5426\u8fd4\u56de\u4e86\u5199\u4e8b\u4ef6<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>event<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&amp;<\/span> EPOLLOUT<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                real_events <span class=\"token operator\">|&#061;<\/span> WRITE<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ \u68c0\u67e5\u8fd9\u6b21\u8fd4\u56de\u7684\u4e8b\u4ef6\u662f\u5426\u662f\u6211\u4eec\u6ce8\u518c\u76d1\u542c\u7684<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5982\u679c real_events \u4e2d\u7684\u4e8b\u4ef6\u6211\u4eec\u5e76\u6ca1\u6709\u76d1\u542c\u8fc7&#xff0c;\u5c31\u8df3\u8fc7&#xff0c;\u4e0d\u5904\u7406&#xff08;\u4f8b\u5982\u7cfb\u7edf\u8fd4\u56de\u4e86\u5199\u4e8b\u4ef6&#xff0c;\u4f46\u6211\u4eec\u6ca1\u76d1\u542c\u5199&#xff09;\u3002<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> real_events<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;&#061;<\/span> NONE<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                <span class=\"token keyword\">continue<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ delete the events that have already happened<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u5220\u9664\u5df2\u7ecf\u89e6\u53d1\u7684\u4e8b\u4ef6&#xff0c;\u66f4\u65b0 epoll \u76d1\u542c\u72b6\u6001<\/span><br \/>\n            <span class=\"token comment\">\/\/ \u4f8b\u5982\u6211\u4eec\u76d1\u542c\u4e86 READ &#043; WRITE&#xff0c;\u4f46\u73b0\u5728\u53ea\u89e6\u53d1\u4e86 READ&#xff0c;\u90a3\u4e48 left_events &#061; WRITE\u3002<\/span><br \/>\n            <span class=\"token comment\">\/\/\u8fd9\u91cc\u8fdb\u884c\u53d6\u53cd\u5c31\u662f\u8ba1\u7b97\u5269\u4f59\u672a\u53d1\u9001\u7684\u7684\u4e8b\u4ef6<\/span><br \/>\n            <span class=\"token keyword\">int<\/span> left_events <span class=\"token operator\">&#061;<\/span> <span class=\"token punctuation\">(<\/span>fd_ctx<span class=\"token operator\">-&gt;<\/span>events <span class=\"token operator\">&amp;<\/span> <span class=\"token operator\">~<\/span>real_events<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">int<\/span> op <span class=\"token operator\">&#061;<\/span> left_events <span class=\"token operator\">?<\/span> EPOLL_CTL_MOD<span class=\"token operator\">:<\/span> EPOLL_CTL_DEL<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/\u5982\u679cleft_event\u6ca1\u6709\u4e8b\u4ef6\u4e86\u90a3\u4e48\u5c31\u53ea\u5269\u4e0b\u8fb9\u7f18\u89e6\u53d1\u4e86events\u8bbe\u7f6e\u4e86<\/span><br \/>\n            event<span class=\"token punctuation\">.<\/span>events <span class=\"token operator\">&#061;<\/span> EPOLLET <span class=\"token operator\">|<\/span> left_events<span class=\"token punctuation\">;<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ \u6784\u9020\u65b0\u7684\u4e8b\u4ef6\u8bbe\u7f6e&#xff0c;\u5e76\u63d0\u4ea4\u66f4\u65b0<\/span><br \/>\n            <span class=\"token comment\">\/\/\u6839\u636e\u4e4b\u524d\u8ba1\u7b97\u7684\u64cd\u4f5c&#xff08;op&#xff09;&#xff0c;\u8c03\u7528 epoll_ctl \u66f4\u65b0\u6216\u5220\u9664 epoll \u76d1\u542c&#xff0c;\u5982\u679c\u5931\u8d25&#xff0c;\u6253\u5370\u9519\u8bef\u5e76\u7ee7\u7eed\u5904\u7406\u4e0b\u4e00\u4e2a\u4e8b\u4ef6\u3002<\/span><br \/>\n            <span class=\"token keyword\">int<\/span> rt2 <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">epoll_ctl<\/span><span class=\"token punctuation\">(<\/span>m_epfd<span class=\"token punctuation\">,<\/span> op<span class=\"token punctuation\">,<\/span> fd_ctx<span class=\"token operator\">-&gt;<\/span>fd<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>event<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>rt2<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;idle::epoll_ctl failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token keyword\">continue<\/span><span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token comment\">\/\/ schedule callback and update fdcontext and event context<\/span><br \/>\n            <span class=\"token comment\">\/\/\u89e6\u53d1\u4e8b\u4ef6&#xff0c;\u4e8b\u4ef6\u7684\u6267\u884c<\/span><br \/>\n            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>real_events <span class=\"token operator\">&amp;<\/span> READ<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>READ<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token operator\">&#8212;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><\/p>\n<p>            <span class=\"token keyword\">if<\/span><span class=\"token punctuation\">(<\/span>real_events <span class=\"token operator\">&amp;<\/span> WRITE<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n                fd_ctx<span class=\"token operator\">-&gt;<\/span><span class=\"token function\">triggerEvent<\/span><span class=\"token punctuation\">(<\/span>WRITE<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n                <span class=\"token operator\">&#8212;<\/span>m_pendingEventCount<span class=\"token punctuation\">;<\/span><br \/>\n            <span class=\"token punctuation\">}<\/span><br \/>\n        <span class=\"token punctuation\">}<\/span><\/p>\n<p>        <span class=\"token comment\">\/\/\u5f53\u524d\u7ebf\u7a0b\u7684\u534f\u7a0b\u4e3b\u52a8\u8ba9\u51fa\u63a7\u5236\u6743&#xff0c;\u8c03\u5ea6\u5668\u53ef\u4ee5\u9009\u62e9\u6267\u884c\u5176\u4ed6\u4efb\u52a1\u6216\u518d\u6b21\u8fdb\u5165 idle \u72b6\u6001\u3002<\/span><br \/>\n        <span class=\"token class-name\">Fiber<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">GetThis<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">-&gt;<\/span><span class=\"token function\">yield<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>IOManager::onTimerInsertedAtFront():<\/h3>\n<p>\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5728\u5b9a\u65f6\u5668\u88ab\u63d2\u5165\u5230\u6700\u524d\u9762\u65f6&#xff0c;\u89e6\u53d1tickle\u4e8b\u4ef6&#xff0c;\u5524\u9192\u963b\u585e\u7684epoll_wait\u56de\u6536\u8d85\u65f6\u7684\u5b9a\u65f6\u4efb\u52a1(\u56de\u8c03cb\u548c\u534f\u7a0b)\u653e\u5165\u534f\u7a0b\u8c03\u5ea6\u5668\u4e2d\u7b49\u5f85\u8c03\u5ea6\u3002<\/p>\n<p><span class=\"token comment\">\/\/ \u5f53\u4e00\u4e2a\u5b9a\u65f6\u5668\u88ab\u63d2\u5165\u5230\u5b9a\u65f6\u5668\u961f\u5217\u7684\u6700\u524d\u9762\u65f6&#xff0c;\u901a\u77e5&#xff08;\u5524\u9192&#xff09;IOManager \u7684 epoll \u7ebf\u7a0b&#xff0c;\u91cd\u65b0\u8bc4\u4f30\u7b49\u5f85\u65f6\u95f4\u3002<\/span><br \/>\n<span class=\"token comment\">\/\/ onTimerInsertedAtFront() \u662f\u4e00\u4e2a\u94a9\u5b50&#xff0c;\u7528\u4e8e\u5728\u63d2\u5165\u6700\u65e9\u5b9a\u65f6\u5668\u65f6\u7acb\u5373\u5524\u9192 epoll&#xff0c;\u4f7f\u5f97\u5b9a\u65f6\u5668\u7cbe\u786e\u89e6\u53d1\u3002<\/span><br \/>\n<span class=\"token keyword\">void<\/span> <span class=\"token class-name\">IOManager<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">onTimerInsertedAtFront<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5524\u9192\u53ef\u80fd\u88ab\u963b\u585e\u7684 epoll_wait \u8c03\u7528<\/span><br \/>\n    <span class=\"token function\">tickle<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>main.cpp<\/h3>\n<p><span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&#034;ioscheduler.h&#034;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;iostream&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/socket.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;netinet\/in.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;arpa\/inet.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;fcntl.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;unistd.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;sys\/epoll.h&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cstring&gt;<\/span><\/span><br \/>\n<span class=\"token macro property\"><span class=\"token directive-hash\">#<\/span><span class=\"token directive keyword\">include<\/span> <span class=\"token string\">&lt;cerrno&gt;<\/span><\/span><\/p>\n<p><span class=\"token comment\">\/\/ 1. \u6784\u9020\u670d\u52a1\u5668\u5730\u5740 159.75.118.34:80<\/span><br \/>\n<span class=\"token comment\">\/\/ 2. \u521b\u5efa TCP socket&#xff08;\u975e\u963b\u585e&#xff09;<\/span><br \/>\n<span class=\"token comment\">\/\/ 3. \u8c03\u7528 connect() \u53d1\u8d77\u8fde\u63a5<\/span><br \/>\n<span class=\"token comment\">\/\/ 4. epoll \u6ce8\u518c&#xff1a;<\/span><br \/>\n<span class=\"token comment\">\/\/    \u251c\u2500 WRITE \u2192 func2()&#xff1a;\u8fde\u63a5\u5efa\u7acb\u65f6\u53d1\u8bf7\u6c42<\/span><br \/>\n<span class=\"token comment\">\/\/    \u2514\u2500 READ  \u2192 func() &#xff1a;\u6536\u5230\u54cd\u5e94\u65f6\u8bfb\u53d6\u6570\u636e<\/span><br \/>\n<span class=\"token comment\">\/\/ 5. IOManager&#xff08;\u57fa\u4e8e epoll&#xff09;\u5f02\u6b65\u8c03\u5ea6<\/span><br \/>\n<span class=\"token comment\">\/\/ 6. \u7f51\u7edc\u901a\u4fe1\u5b8c\u6210<\/span><\/p>\n<p><span class=\"token keyword\">using<\/span> <span class=\"token keyword\">namespace<\/span> sylar<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u63a5\u6536\u7f13\u51b2\u533a<\/span><br \/>\n<span class=\"token keyword\">char<\/span> recv_data<span class=\"token punctuation\">[<\/span><span class=\"token number\">4096<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">\/\/ \u662f\u4e00\u4e2a HTTP \u8bf7\u6c42\u62a5\u6587&#xff0c;\u8868\u793a\u5411\u670d\u52a1\u5668\u8bf7\u6c42\u4e3b\u9875 <\/span><br \/>\n<span class=\"token comment\">\/\/ GET \/ HTTP\/1.0\\\\r\\\\n     \u2190 \u8bf7\u6c42\u884c&#xff1a;GET\u65b9\u6cd5&#xff0c;\u8bf7\u6c42&#034;\/&#034;&#xff0c;\u534f\u8baeHTTP\/1.0<\/span><br \/>\n<span class=\"token comment\">\/\/ \\\\r\\\\n                   \u2190 \u7a7a\u884c&#xff1a;\u8868\u793a\u6ca1\u6709\u989d\u5916\u8bf7\u6c42\u5934&#xff0c;\u8bf7\u6c42\u7ed3\u675f<\/span><br \/>\n<span class=\"token keyword\">const<\/span> <span class=\"token keyword\">char<\/span> data<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#034;GET \/ HTTP\/1.0\\\\r\\\\n\\\\r\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span> <\/p>\n<p><span class=\"token comment\">\/\/ \u5168\u5c40 socket fd<\/span><br \/>\n<span class=\"token keyword\">int<\/span> sock<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">func<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ ssize_t recv(int sockfd, void *buf, size_t len, int flags);<\/span><br \/>\n    <span class=\"token comment\">\/\/ sockfd: socket \u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;int&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ buf: \u63a5\u6536\u7f13\u51b2\u533a&#xff08;char \u6570\u7ec4&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5e0c\u671b\u8bfb\u53d6\u7684\u6700\u5927\u5b57\u8282\u6570&#xff08;\u6700\u591a\u8bfb 4096 \u5b57\u8282&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ flags \u53c2\u6570&#xff0c;\u8bbe\u7f6e\u4e3a 0 \u8868\u793a\u9ed8\u8ba4\u884c\u4e3a&#xff08;\u963b\u585e\u8bfb\u3001\u65e0\u7279\u6b8a\u6807\u5fd7&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8fd4\u56de\u503c&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ &gt;0 : \u6210\u529f\u8bfb\u53d6\u7684\u5b57\u8282\u6570&#xff08;\u53ef\u80fd\u5c11\u4e8e 4096&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ &#061;0 : \u5bf9\u7aef\u5df2\u5173\u95ed\u8fde\u63a5&#xff08;FIN&#xff09;&#xff0c;\u8868\u793a\u8fde\u63a5\u7ed3\u675f<\/span><br \/>\n    <span class=\"token comment\">\/\/ &lt;0 : \u53d1\u751f\u9519\u8bef&#xff0c;\u9700\u901a\u8fc7 errno \u5224\u65ad\u539f\u56e0<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4ece sock \u8868\u793a\u7684 TCP \u8fde\u63a5\u4e2d\u5c1d\u8bd5\u8bfb\u53d6\u6700\u591a 4096 \u5b57\u8282\u7684\u6570\u636e<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8bfb\u53d6\u5230\u7684\u6570\u636e\u4f1a\u88ab\u5b58\u5165 recv_data \u6307\u5411\u7684\u7f13\u51b2\u533a<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8c03\u7528\u4f1a\u963b\u585e\u5f53\u524d\u7ebf\u7a0b\/\u534f\u7a0b&#xff0c;\u76f4\u5230\u81f3\u5c11\u6709\u4e00\u4e2a\u5b57\u8282\u53ef\u4ee5\u8bfb&#xff08;\u524d\u63d0\u662f socket \u662f\u963b\u585e\u6a21\u5f0f&#xff09;&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c socket \u662f\u975e\u963b\u585e&#xff0c;\u53ef\u80fd\u4f1a\u7acb\u5373\u8fd4\u56de -1 \u5e76\u8bbe\u7f6e errno &#061; EAGAIN \u6216 EWOULDBLOCK<\/span><br \/>\n    ssize_t n <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">recv<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> recv_data<span class=\"token punctuation\">,<\/span> <span class=\"token number\">4096<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>n <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        recv_data<span class=\"token punctuation\">[<\/span>n<span class=\"token punctuation\">]<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\\\\0&#039;<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u6dfb\u52a0\u7ec8\u6b62\u7b26<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Received:\\\\n&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> recv_data <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>n <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Connection closed by peer\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;recv failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">void<\/span> <span class=\"token function\">func2<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    <span class=\"token comment\">\/\/ ssize_t send(int sockfd, const void *buf, size_t len, int flags);<\/span><br \/>\n    <span class=\"token comment\">\/\/ sockfd: \u76ee\u6807 socket \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff0c;\u8868\u793a\u4e00\u4e2a\u5df2\u8fde\u63a5\u7684 TCP \u8fde\u63a5<\/span><br \/>\n    <span class=\"token comment\">\/\/ buf: \u6307\u5411\u8981\u53d1\u9001\u7684\u6570\u636e\u7f13\u51b2\u533a\u7684\u6307\u9488<\/span><br \/>\n    <span class=\"token comment\">\/\/ len: \u8981\u53d1\u9001\u7684\u5b57\u8282\u6570&#xff0c;\u8fd9\u91cc\u662f sizeof(&#034;GET \/ HTTP\/1.0\\\\r\\\\n\\\\r\\\\n&#034;)<\/span><br \/>\n    <span class=\"token comment\">\/\/ flags: flags \u53c2\u6570&#xff0c;\u8bbe\u4e3a 0 \u8868\u793a\u9ed8\u8ba4\u53d1\u9001\u884c\u4e3a&#xff08;\u963b\u585e\u53d1\u9001\u3001\u65e0\u7279\u6b8a\u9009\u9879&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8fd4\u56de\u503c&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5b9e\u9645\u6210\u529f\u5199\u5165\u5230 socket \u7f13\u51b2\u533a\u7684\u5b57\u8282\u6570\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6210\u529f\u65f6&#xff1a;&gt; 0&#xff0c;\u8868\u793a\u53d1\u9001\u4e86\u591a\u5c11\u5b57\u8282&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5931\u8d25\u65f6&#xff1a;&lt; 0&#xff0c;\u8868\u793a\u51fa\u9519&#xff0c;\u9700\u8981\u68c0\u67e5 errno<\/span><br \/>\n    ssize_t n <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">send<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> data<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>data<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>n <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;send failed: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Sent &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> n <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034; bytes&#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token keyword\">int<\/span> <span class=\"token function\">main<\/span><span class=\"token punctuation\">(<\/span><span class=\"token keyword\">int<\/span> argc<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">char<\/span> <span class=\"token keyword\">const<\/span><span class=\"token operator\">*<\/span> argv<span class=\"token punctuation\">[<\/span><span class=\"token punctuation\">]<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n    IOManager <span class=\"token function\">manager<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u521b\u5efa socket<\/span><br \/>\n    <span class=\"token comment\">\/\/ int socket(int domain, int type, int protocol);<\/span><br \/>\n    <span class=\"token comment\">\/\/ AF_INET \u2014\u2014 \u5730\u5740\u65cf&#xff08;domain&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8868\u793a\u4f7f\u7528 IPv4 \u534f\u8bae&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u4f60\u60f3\u7528 IPv6&#xff0c;\u53ef\u4ee5\u5199 AF_INET6&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5bf9\u4e8e\u672c\u5730\u901a\u4fe1&#xff08;\u5982\u8fdb\u7a0b\u95f4\u901a\u4fe1&#xff09;&#xff0c;\u53ef\u4ee5\u4f7f\u7528 AF_UNIX<\/span><br \/>\n    <span class=\"token comment\">\/\/ SOCK_STREAM \u2014\u2014 \u5957\u63a5\u5b57\u7c7b\u578b&#xff08;type&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8868\u793a\u4f7f\u7528 \u9762\u5411\u8fde\u63a5\u7684 TCP \u534f\u8bae<\/span><br \/>\n    <span class=\"token comment\">\/\/ 0 \u2014\u2014 \u534f\u8bae&#xff08;protocol&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u4e00\u822c\u8bbe\u7f6e\u4e3a 0&#xff0c;\u8ba9\u7cfb\u7edf\u81ea\u52a8\u9009\u62e9\u9ed8\u8ba4\u534f\u8bae&#xff1a;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u4f60\u9009\u4e86 SOCK_STREAM&#xff0c;\u5c31\u9ed8\u8ba4\u662f TCP&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5982\u679c\u4f60\u9009\u4e86 SOCK_DGRAM&#xff0c;\u5c31\u9ed8\u8ba4\u662f UDP<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u8fd4\u56de\u503c&#xff1a;\u4e00\u4e2a\u6574\u6570&#xff0c;\u8868\u793a socket \u7684\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff08;fd&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6210\u529f&#xff1a;\u8fd4\u56de\u4e00\u4e2a\u6b63\u6574\u6570&#xff08;\u6587\u4ef6\u63cf\u8ff0\u7b26&#xff09;&#xff0c;\u7528\u4e8e\u540e\u7eed\u7684&#xff1a;connect()\u3001send()\u3001recv() \u7b49\u64cd\u4f5c&#xff1b;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u5931\u8d25&#xff1a;\u8fd4\u56de -1&#xff0c;\u5e76\u8bbe\u7f6e errno \u6765\u6307\u793a\u9519\u8bef\u539f\u56e0&#xff08;\u5982\u6587\u4ef6\u63cf\u8ff0\u7b26\u8017\u5c3d\u3001\u6743\u9650\u95ee\u9898\u7b49&#xff09;<\/span><br \/>\n    sock <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">socket<\/span><span class=\"token punctuation\">(<\/span>AF_INET<span class=\"token punctuation\">,<\/span> SOCK_STREAM<span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ sockaddr_in&#xff1a;IPv4 \u4e13\u7528\u7684 socket \u5730\u5740\u7ed3\u6784<\/span><br \/>\n    sockaddr_in server<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ server.sin_family &#061; AF_INET;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6307\u5b9a\u5730\u5740\u65cf\u4e3a IPv4<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>sin_family <span class=\"token operator\">&#061;<\/span> AF_INET<span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8bbe\u7f6e\u76ee\u6807\u7aef\u53e3\u4e3a 80&#xff08;HTTP \u9ed8\u8ba4\u7aef\u53e3&#xff09;\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ htons&#xff1a;\u5c06\u4e3b\u673a\u5b57\u8282\u5e8f\u8f6c\u6362\u4e3a\u7f51\u7edc\u5b57\u8282\u5e8f&#xff08;\u5927\u7aef&#xff09;<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>sin_port <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htons<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">8080<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u5c06 IP \u5730\u5740\u5b57\u7b26\u4e32 &#034;159.75.118.34&#034; \u8f6c\u6362\u4e3a\u6574\u6570&#xff0c;\u5e76\u8d4b\u503c\u7ed9 s_addr \u5b57\u6bb5\u3002<\/span><br \/>\n    <span class=\"token comment\">\/\/ inet_addr \u8fd4\u56de\u7684\u662f\u7f51\u7edc\u5b57\u8282\u5e8f\u7684 IPv4 \u5730\u5740\u3002<\/span><br \/>\n    server<span class=\"token punctuation\">.<\/span>sin_addr<span class=\"token punctuation\">.<\/span>s_addr <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">inet_addr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token comment\">\/\/ server.sin_addr.s_addr &#061; inet_addr(&#034;10.1.12.15&#034;);<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u8bbe\u7f6e socket \u4e3a\u975e\u963b\u585e\u6a21\u5f0f<\/span><br \/>\n    <span class=\"token function\">fcntl<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> F_SETFL<span class=\"token punctuation\">,<\/span> O_NONBLOCK<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ \u53d1\u8d77\u8fde\u63a5&#xff08;\u975e\u963b\u585e connect&#xff09;<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u56e0\u4e3a socket \u662f\u975e\u963b\u585e\u7684&#xff0c;\u8fd9\u901a\u5e38\u4f1a\u7acb\u5373\u8fd4\u56de -1&#xff0c;errno &#061; EINPROGRESS&#xff0c;\u8868\u793a\u201c\u8fde\u63a5\u6b63\u5728\u5efa\u7acb\u4e2d\u201d<\/span><br \/>\n    <span class=\"token keyword\">int<\/span> rt <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">connect<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">struct<\/span> <span class=\"token class-name\">sockaddr<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span><span class=\"token operator\">&amp;<\/span>server<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">sizeof<\/span><span class=\"token punctuation\">(<\/span>server<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>rt <span class=\"token operator\">&#061;&#061;<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Connect immediately succeeded (rare)\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">func2<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u5982\u679c\u7acb\u5373\u8fde\u63a5\u6210\u529f&#xff0c;\u76f4\u63a5\u8c03\u7528\u5199\u56de\u8c03<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token keyword\">if<\/span> <span class=\"token punctuation\">(<\/span>rt <span class=\"token operator\">&lt;<\/span> <span class=\"token number\">0<\/span> <span class=\"token operator\">&amp;&amp;<\/span> errno <span class=\"token operator\">&#061;&#061;<\/span> EINPROGRESS<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;Connecting&#8230;\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        manager<span class=\"token punctuation\">.<\/span><span class=\"token function\">addEvent<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> IOManager<span class=\"token operator\">::<\/span>WRITE<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>func2<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">\/\/ \u7b49\u8fde\u63a5\u5b8c\u6210\u518d\u5199<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span> <span class=\"token keyword\">else<\/span> <span class=\"token punctuation\">{<\/span><br \/>\n        std<span class=\"token operator\">::<\/span>cerr <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;connect error: &#034;<\/span> <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token function\">strerror<\/span><span class=\"token punctuation\">(<\/span>errno<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;&lt;<\/span> std<span class=\"token operator\">::<\/span>endl<span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token function\">close<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n        <span class=\"token keyword\">return<\/span> <span class=\"token operator\">&#8211;<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n    <span class=\"token punctuation\">}<\/span><\/p>\n<p>    <span class=\"token comment\">\/\/ manager.addEvent(sock, IOManager::WRITE, &amp;func2);<\/span><br \/>\n    <span class=\"token comment\">\/\/ \u6ce8\u518c\u8bfb\u4e8b\u4ef6&#xff08;\u65e0\u8bba\u5199\u662f\u5426\u6210\u529f&#xff09;<\/span><br \/>\n    manager<span class=\"token punctuation\">.<\/span><span class=\"token function\">addEvent<\/span><span class=\"token punctuation\">(<\/span>sock<span class=\"token punctuation\">,<\/span> IOManager<span class=\"token operator\">::<\/span>READ<span class=\"token punctuation\">,<\/span> <span class=\"token operator\">&amp;<\/span>func<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>    std<span class=\"token operator\">::<\/span>cout <span class=\"token operator\">&lt;&lt;<\/span> <span class=\"token string\">&#034;event has been posted\\\\n\\\\n&#034;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">return<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token punctuation\">}<\/span><\/p>\n<h3>\u6d4b\u8bd5\u7ed3\u679c<\/h3>\n<p>\u5982\u679c\u4f60\u6ca1\u6709\u8fd0\u884c\u4efb\u4f55 HTTP \u670d\u52a1\u5668&#xff0c;\u4f60\u53ef\u4ee5\u4e34\u65f6\u8d77\u4e00\u4e2a&#xff1a;<\/p>\n<p>python3 -m http.server <span class=\"token number\">8080<\/span><\/p>\n<p>\u7136\u540e\u518d\u6b21\u8fd0\u884c\u4f60\u7684\u7a0b\u5e8f&#xff0c;\u5c31\u80fd\u6536\u5230 HTTP \u54cd\u5e94&#xff0c;\u4e0d\u518d\u662f Connection refused<\/p>\n<p>\u8fd9\u4e2a\u547d\u4ee4\u7684\u4f5c\u7528\u662f&#xff1a;\u5728\u672c\u5730 \u542f\u52a8\u4e00\u4e2a HTTP \u670d\u52a1\u5668&#xff0c;\u5b83\u4f1a\u76d1\u542c\u672c\u5730\u7684 8080 \u7aef\u53e3&#xff0c;\u5e76\u51c6\u5907\u63a5\u6536\u5ba2\u6237\u7aef\u7684\u8fde\u63a5\u8bf7\u6c42\u3002<\/p>\n<p>server<span class=\"token punctuation\">.<\/span>sin_addr<span class=\"token punctuation\">.<\/span>s_addr <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">inet_addr<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;127.0.0.1&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\nserver<span class=\"token punctuation\">.<\/span>sin_port <span class=\"token operator\">&#061;<\/span> <span class=\"token function\">htons<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">8080<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8868\u793a&#xff1a;\u5ba2\u6237\u7aef\u8981\u8fde\u63a5\u672c\u673a\u7684 127.0.0.1:8080<\/p>\n<p>\u5982\u679c\u8fd9\u65f6\u672c\u5730\u6839\u672c\u6ca1\u6709\u4efb\u4f55\u7a0b\u5e8f\u76d1\u542c\u8fd9\u4e2a\u7aef\u53e3&#xff08;8080&#xff09;&#xff0c;\u90a3\u4e48&#xff1a;<\/p>\n<p>\u64cd\u4f5c\u7cfb\u7edf\u4e00\u770b\u5230\u5ba2\u6237\u7aef\u6765\u8fde\u63a5 127.0.0.1:8080&#xff0c;<\/p>\n<p>\u5c31\u53d1\u73b0&#xff1a;\u6ca1\u4eba\u76d1\u542c\u8fd9\u4e2a\u7aef\u53e3\u554a&#xff01;<\/p>\n<p>\u6240\u4ee5\u5c31\u7acb\u523b\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef&#xff1a;<\/p>\n<p>connect error: Connection refused<\/p>\n<p>\u8868\u793a\u8fde\u63a5\u8bf7\u6c42\u88ab\u201c\u7cfb\u7edf\u201d\u62d2\u7edd\u4e86\u3002 <img decoding=\"async\" src=\"2025-07-30dmdshtmr0lz.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><img decoding=\"async\" src=\"2025-07-302phzj0rxhxa.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"2025-07-30s4pfvgm4uhf.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>\u51fd\u6570\u8c03\u7528\u56fe<\/h3>\n<p><img decoding=\"async\" src=\"2025-07-302ixmbei0eda.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"2025-07-30m5p2vvzebv3.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"2025-07-303bi4xq53qbv.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"2025-07-30pxkxywhffz4.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"2025-07-30eitcirntydu.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/> <img decoding=\"async\" src=\"2025-07-30qgb0dulq3bh.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<p><font color=\"#FF5B22\" size=\"3\" face=\"\u6977\u4f53\">\u4e4b\u540e\u6211\u4f1a\u6301\u7eed\u66f4\u65b0&#xff0c;\u5982\u679c\u559c\u6b22\u6211\u7684\u6587\u7ae0&#xff0c;\u8bf7\u8bb0\u5f97\u4e00\u952e\u4e09\u8fde\u54e6&#xff0c;\u70b9\u8d5e\u5173\u6ce8\u6536\u85cf&#xff0c;\u4f60\u7684\u6bcf\u4e00\u4e2a\u8d5e\u6bcf\u4e00\u4efd\u5173\u6ce8\u6bcf\u4e00\u6b21\u6536\u85cf\u90fd\u5c06\u662f\u6211\u524d\u8fdb\u8def\u4e0a\u7684\u65e0\u9650\u52a8\u529b &#xff01;&#xff01;&#xff01;\u2196(\u2594\u25bd\u2594)\u2197\u611f\u8c22\u652f\u6301&#xff01;<\/font><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb829\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf6\u6b21\u3002\u4e8b\u5b9e\u4e0a\u5f88\u591a\u7684\u5e93\u90fd\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u5de5\u4f5c\uff0c\u6bd4\u5982libevent\uff0clibuv\uff0clibev\u7b49\uff0c\u8fd9\u4e9b\u5e93\u88ab\u79f0\u4e3a\u5f02\u6b65\u4e8b\u4ef6\u5e93\u6216IO\u5e93\uff0c\u4ece\u7f51\u7edc\u4e0a\u53ef\u4ee5\u641c\u7d22\u5230\u5f88\u591a\u8d44\u6599\u3002\u6df1\u5165\u6d45\u51fa\u7406\u89e3libevent\u2014\u20142\u4e07\u5b57\u603b\u7ed3_libev \u5806-CSDN\u535a\u5ba2\u7b49\uff0c\u6709\u7684\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406socketfd\u4e8b\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5b9a\u65f6\u5668\u4e8b\u4ef6\u548c\u4fe1\u53f7\u4e8b\u4ef6\u3002\u8fd9\u4e9b\u4e8b\u4ef6\u5e93\u7684\u5b9e\u73b0\u539f\u7406\u57fa\u672c\u7c7b\u4f3c\uff0c\u90fd\u662f\u5148\u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u72b6\u6001\uff0c\u7136\u540e\u5c06\u5957\u63a5\u5b57\u4e0e\u56de\u8c03\u51fd\u6570\u7ed1\u5b9a\uff0c\u63a5\u4e0b\u6765\u8fdb\u5165\u4e00\u4e2a\u57fa\u4e8eIO\u591a\u8def\u590d\u7528\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\uff0c\u7136\u540e\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002\u57fa\u4e8eepoll\u5b9e\u73b0\uff0c\u53ea\u652f\u6301linux\u5e73\u53f0\u3002\u5bf9\u6bcf\u4e2af<\/p>\n","protected":false},"author":2,"featured_media":49936,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,4857,190,124],"topic":[],"class_list":["post-49956","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-4857","tag-190","tag-124"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/49956.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb829\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf6\u6b21\u3002\u4e8b\u5b9e\u4e0a\u5f88\u591a\u7684\u5e93\u90fd\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u5de5\u4f5c\uff0c\u6bd4\u5982libevent\uff0clibuv\uff0clibev\u7b49\uff0c\u8fd9\u4e9b\u5e93\u88ab\u79f0\u4e3a\u5f02\u6b65\u4e8b\u4ef6\u5e93\u6216IO\u5e93\uff0c\u4ece\u7f51\u7edc\u4e0a\u53ef\u4ee5\u641c\u7d22\u5230\u5f88\u591a\u8d44\u6599\u3002\u6df1\u5165\u6d45\u51fa\u7406\u89e3libevent\u2014\u20142\u4e07\u5b57\u603b\u7ed3_libev \u5806-CSDN\u535a\u5ba2\u7b49\uff0c\u6709\u7684\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406socketfd\u4e8b\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5b9a\u65f6\u5668\u4e8b\u4ef6\u548c\u4fe1\u53f7\u4e8b\u4ef6\u3002\u8fd9\u4e9b\u4e8b\u4ef6\u5e93\u7684\u5b9e\u73b0\u539f\u7406\u57fa\u672c\u7c7b\u4f3c\uff0c\u90fd\u662f\u5148\u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u72b6\u6001\uff0c\u7136\u540e\u5c06\u5957\u63a5\u5b57\u4e0e\u56de\u8c03\u51fd\u6570\u7ed1\u5b9a\uff0c\u63a5\u4e0b\u6765\u8fdb\u5165\u4e00\u4e2a\u57fa\u4e8eIO\u591a\u8def\u590d\u7528\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\uff0c\u7136\u540e\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002\u57fa\u4e8eepoll\u5b9e\u73b0\uff0c\u53ea\u652f\u6301linux\u5e73\u53f0\u3002\u5bf9\u6bcf\u4e2af\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/49956.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T12:42:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124231-688a133746bfc.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"22 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49956.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/49956.html\",\"name\":\"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T12:42:40+00:00\",\"dateModified\":\"2025-07-30T12:42:40+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/49956.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/49956.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/49956.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/49956.html","og_locale":"zh_CN","og_type":"article","og_title":"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb829\u6b21\uff0c\u70b9\u8d5e14\u6b21\uff0c\u6536\u85cf6\u6b21\u3002\u4e8b\u5b9e\u4e0a\u5f88\u591a\u7684\u5e93\u90fd\u53ef\u4ee5\u5b9e\u73b0\u7c7b\u4f3c\u7684\u5de5\u4f5c\uff0c\u6bd4\u5982libevent\uff0clibuv\uff0clibev\u7b49\uff0c\u8fd9\u4e9b\u5e93\u88ab\u79f0\u4e3a\u5f02\u6b65\u4e8b\u4ef6\u5e93\u6216IO\u5e93\uff0c\u4ece\u7f51\u7edc\u4e0a\u53ef\u4ee5\u641c\u7d22\u5230\u5f88\u591a\u8d44\u6599\u3002\u6df1\u5165\u6d45\u51fa\u7406\u89e3libevent\u2014\u20142\u4e07\u5b57\u603b\u7ed3_libev \u5806-CSDN\u535a\u5ba2\u7b49\uff0c\u6709\u7684\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406socketfd\u4e8b\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u5b9a\u65f6\u5668\u4e8b\u4ef6\u548c\u4fe1\u53f7\u4e8b\u4ef6\u3002\u8fd9\u4e9b\u4e8b\u4ef6\u5e93\u7684\u5b9e\u73b0\u539f\u7406\u57fa\u672c\u7c7b\u4f3c\uff0c\u90fd\u662f\u5148\u5c06\u5957\u63a5\u5b57\u8bbe\u7f6e\u4e3a\u975e\u963b\u585e\u72b6\u6001\uff0c\u7136\u540e\u5c06\u5957\u63a5\u5b57\u4e0e\u56de\u8c03\u51fd\u6570\u7ed1\u5b9a\uff0c\u63a5\u4e0b\u6765\u8fdb\u5165\u4e00\u4e2a\u57fa\u4e8eIO\u591a\u8def\u590d\u7528\u7684\u4e8b\u4ef6\u5faa\u73af\uff0c\u7b49\u5f85\u4e8b\u4ef6\u53d1\u751f\uff0c\u7136\u540e\u8c03\u7528\u5bf9\u5e94\u7684\u56de\u8c03\u51fd\u6570\u3002\u57fa\u4e8eepoll\u5b9e\u73b0\uff0c\u53ea\u652f\u6301linux\u5e73\u53f0\u3002\u5bf9\u6bcf\u4e2af","og_url":"https:\/\/www.wsisp.com\/helps\/49956.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T12:42:40+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/07\/20250730124231-688a133746bfc.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"22 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/49956.html","url":"https:\/\/www.wsisp.com\/helps\/49956.html","name":"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T12:42:40+00:00","dateModified":"2025-07-30T12:42:40+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/49956.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/49956.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/49956.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u534f\u7a0b\u5e93\uff08\u6a21\u5757\u7684\u8be6\u89e3\u4e0e\u4ee3\u7801\u5206\u6790\uff09ioscheduler\u7c7b(io+scheduler\u7684\u7ed3\u5408)\u7bc7"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=49956"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/49956\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/49936"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=49956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=49956"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=49956"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=49956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}