{"id":14487,"date":"2024-08-26T17:29:50","date_gmt":"2024-08-26T09:29:50","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14487"},"modified":"2024-08-30T14:20:15","modified_gmt":"2024-08-30T06:20:15","slug":"ipcthreadstate-cpp","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14487","title":{"rendered":"IPCThreadState.cpp"},"content":{"rendered":"<p>\u6e90\u7801\u4f4d\u7f6e\uff1a<br \/>\n<code>frameworks\/native\/libs\/binder\/IPCThreadState.cpp<\/code><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/CrazyMo_\/article\/details\/129143626\">https:\/\/blog.csdn.net\/CrazyMo_\/article\/details\/129143626<\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_16 counter-hierarchy counter-decimal ez-toc-grey\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u5f55<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14487\/#%E6%96%B9%E6%B3%95\" title=\"\u65b9\u6cd5\">\u65b9\u6cd5<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14487\/#%E5%AE%9E%E4%BE%8B%E5%8C%96\" title=\"\u5b9e\u4f8b\u5316\">\u5b9e\u4f8b\u5316<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14487\/#talkWithDriver%EF%BC%88%E4%B8%8Ebinder%E9%A9%B1%E5%8A%A8%E4%BA%A4%E4%BA%92%E7%9A%84%E6%A0%B8%E5%BF%83%E6%96%B9%E6%B3%95%EF%BC%89\" title=\"talkWithDriver\uff08\u4e0ebinder\u9a71\u52a8\u4ea4\u4e92\u7684\u6838\u5fc3\u65b9\u6cd5\uff09\">talkWithDriver\uff08\u4e0ebinder\u9a71\u52a8\u4ea4\u4e92\u7684\u6838\u5fc3\u65b9\u6cd5\uff09<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14487\/#%E5%90%84%E7%A7%8DxxHandle%E6%96%B9%E6%B3%95\" title=\"\u5404\u79cdxxHandle()\u65b9\u6cd5\">\u5404\u79cdxxHandle()\u65b9\u6cd5<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%E6%96%B9%E6%B3%95\"><\/span>\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E4%BE%8B%E5%8C%96\"><\/span>\u5b9e\u4f8b\u5316<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6bcf\u4e2a\u7ebf\u7a0b\u90fd\u6709\u4e00\u4e2aIPCThreadState<\/p>\n<ul>\n<li>\n<p>\u6784\u9020\u51fd\u6570<\/p>\n<pre><code class=\"language-cpp\">802 IPCThreadState::IPCThreadState()\n803     : mProcess(ProcessState::self()),\n804       mWorkSource(kUnsetWorkSource),\n805       mPropagateWorkSource(false),\n806       mStrictModePolicy(0),\n807       mLastTransactionBinderFlags(0),\n808       mCallRestriction(mProcess-&gt;mCallRestriction)\n809 {\n810     pthread_setspecific(gTLS, this);\n811     clearCaller();\n812     mIn.setDataCapacity(256);\n813     mOut.setDataCapacity(256);\n814     mIPCThreadStateBase = IPCThreadStateBase::self();\n815 }<\/code><\/pre>\n<p><code>mProcess(ProcessState::self())<\/code>  \u5c06\u5f53\u524d\u7ebf\u7a0b\u7684\u8fdb\u7a0b\u6307\u9488\u8bb0\u5f55\u8d77\u6765<br \/>\n<code>pthread_setspecific(gTLS, this);<\/code>  \u5c06IPCThreadState\u5b9e\u4f8b\u4e0e\u7ebf\u7a0b\u7ed1\u5b9a<\/p>\n<\/li>\n<li>\n<p>self<\/p>\n<pre><code class=\"language-cpp\">281 static bool gHaveTLS = false;\n282 static pthread_key_t gTLS = 0;\n\n286 IPCThreadState* IPCThreadState::self()\n287 {\n288     if (gHaveTLS) {\n289 restart:\n290         const pthread_key_t k = gTLS;\n291         IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k);\n292         if (st) return st;\n293         return new IPCThreadState;\n294     }\n295 \n296     if (gShutdown) {\n297         ALOGW(&quot;Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\\n&quot;);\n298         return nullptr;\n299     }\n300 \n301     pthread_mutex_lock(&amp;gTLSMutex);\n302     if (!gHaveTLS) {\n303         int key_create_value = pthread_key_create(&amp;gTLS, threadDestructor);\n304         if (key_create_value != 0) {\n305             pthread_mutex_unlock(&amp;gTLSMutex);\n306             ALOGW(&quot;IPCThreadState::self() unable to create TLS key, expect a crash: %s\\n&quot;,\n307                     strerror(key_create_value));\n308             return nullptr;\n309         }\n310         gHaveTLS = true;\n311     }\n312     pthread_mutex_unlock(&amp;gTLSMutex);\n313     goto restart;\n314 }<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"talkWithDriver%EF%BC%88%E4%B8%8Ebinder%E9%A9%B1%E5%8A%A8%E4%BA%A4%E4%BA%92%E7%9A%84%E6%A0%B8%E5%BF%83%E6%96%B9%E6%B3%95%EF%BC%89\"><\/span>talkWithDriver\uff08\u4e0ebinder\u9a71\u52a8\u4ea4\u4e92\u7684\u6838\u5fc3\u65b9\u6cd5\uff09<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><code>ioctl(mProcess-&gt;mDriverFD, BINDER_WRITE_READ, &amp;bwr)<\/code><\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E5%90%84%E7%A7%8DxxHandle%E6%96%B9%E6%B3%95\"><\/span>\u5404\u79cdxxHandle()\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>incWeakHandle<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\"> 737 void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy)\n 738 {\n 739     LOG_REMOTEREFS(&quot;IPCThreadState::incWeakHandle(%d)\\n&quot;, handle);\n 740     mOut.writeInt32(BC_INCREFS);\n 741     mOut.writeInt32(handle);\n 742     \/\/ Create a temp reference until the driver has handled this command.\n 743     proxy-&gt;getWeakRefs()-&gt;incWeak(mProcess.get());\n 744     mPostWriteWeakDerefs.push(proxy-&gt;getWeakRefs());\n 745 }<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"14487\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u6e90\u7801\u4f4d\u7f6e\uff1a frameworks\/native\/libs\/binder\/IPCThreadState [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1113],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14487"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=14487"}],"version-history":[{"count":11,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14487\/revisions"}],"predecessor-version":[{"id":14539,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14487\/revisions\/14539"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14487"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}