{"id":14446,"date":"2024-08-23T10:39:47","date_gmt":"2024-08-23T02:39:47","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14446"},"modified":"2024-08-30T14:43:11","modified_gmt":"2024-08-30T06:43:11","slug":"processstate-cpp","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14446","title":{"rendered":"ProcessState.cpp"},"content":{"rendered":"<p>\u76f8\u5173\u6e90\u7801\u4f4d\u7f6e\uff1a<br \/>\n<code>frameworks\/native\/libs\/binder\/ProcessState.cpp<\/code><br \/>\n<code>frameworks\/native\/libs\/binder\/include\/binder\/ProcessState.h<\/code><\/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-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14446\/#%E7%BB%A7%E6%89%BF%E5%85%B3%E7%B3%BB\" title=\"\u7ee7\u627f\u5173\u7cfb\">\u7ee7\u627f\u5173\u7cfb<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14446\/#%E6%96%B9%E6%B3%95\" title=\"\u65b9\u6cd5\">\u65b9\u6cd5<\/a><ul class=\"ez-toc-list-level-2\"><li class=\"ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14446\/#%E5%AE%9E%E4%BE%8B%E5%8C%96%E6%96%B9%E6%B3%95\" title=\"\u5b9e\u4f8b\u5316\u65b9\u6cd5\">\u5b9e\u4f8b\u5316\u65b9\u6cd5<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14446\/#%E5%85%B6%E5%AE%83%E6%96%B9%E6%B3%95\" title=\"\u5176\u5b83\u65b9\u6cd5\">\u5176\u5b83\u65b9\u6cd5<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E7%BB%A7%E6%89%BF%E5%85%B3%E7%B3%BB\"><\/span>\u7ee7\u627f\u5173\u7cfb<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><code>class ProcessState : public virtual RefBase<\/code><\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E6%96%B9%E6%B3%95\"><\/span>\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"%E5%AE%9E%E4%BE%8B%E5%8C%96%E6%96%B9%E6%B3%95\"><\/span>\u5b9e\u4f8b\u5316\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>\n<p>\u6784\u9020\u65b9\u6cd5<\/p>\n<pre><code class=\"language-cpp\">424 ProcessState::ProcessState(const char *driver)\n425     : mDriverName(String8(driver))\n426     , mDriverFD(open_driver(driver))\n427     , mVMStart(MAP_FAILED)\n428     , mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)\n429     , mThreadCountDecrement(PTHREAD_COND_INITIALIZER)\n430     , mExecutingThreadsCount(0)\n431     , mMaxThreads(DEFAULT_MAX_BINDER_THREADS)\n432     , mStarvationStartTimeMs(0)\n433     , mManagesContexts(false)\n434     , mBinderContextCheckFunc(nullptr)\n435     , mBinderContextUserData(nullptr)\n436     , mThreadPoolStarted(false)\n437     , mThreadPoolSeq(1)\n438     , mCallRestriction(CallRestriction::NONE)\n439 {\n440     if (mDriverFD &gt;= 0) {\n441         \/\/ mmap the binder, providing a chunk of virtual address space to receive transactions.\n442         mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, mDriverFD, 0);\n443         if (mVMStart == MAP_FAILED) {\n444             \/\/ *sigh*\n445             ALOGE(&quot;Using %s failed: unable to mmap transaction memory.\\n&quot;, mDriverName.c_str());\n446             close(mDriverFD);\n447             mDriverFD = -1;\n448             mDriverName.clear();\n449         }\n450     }\n451 \n452     LOG_ALWAYS_FATAL_IF(mDriverFD &lt; 0, &quot;Binder driver could not be opened.  Terminating.&quot;);\n453 }<\/code><\/pre>\n<p>\u5728\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316open_driver(driver)\uff0cdriver\u5bf9\u4e8eservicemanager\u8fdb\u7a0b\u6765\u8bf4\u5373\u662f<code>\/dev\/binder<\/code><\/p>\n<pre><code class=\"language-cpp\">46 #ifdef __ANDROID_VNDK__\n47 const char* kDefaultDriver = &quot;\/dev\/vndbinder&quot;;\n48 #else\n49 const char* kDefaultDriver = &quot;\/dev\/binder&quot;;\n50 #endif<\/code><\/pre>\n<\/li>\n<li>\n<p>self<\/p>\n<pre><code class=\"language-cpp\">74 sp&lt;ProcessState&gt; ProcessState::self()\n75 {\n76     Mutex::Autolock _l(gProcessMutex);\n77     if (gProcess != nullptr) {\n78         return gProcess;\n79     }\n80     gProcess = new ProcessState(kDefaultDriver);\n81     return gProcess;\n82 }<\/code><\/pre>\n<p>gProcess\u5b9a\u4e49\u5728<br \/>\n<code>frameworks\/native\/libs\/binder\/include\/private\/binder\/Static.h:36:extern sp&lt;ProcessState&gt; gProcess;<\/code><\/p>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"%E5%85%B6%E5%AE%83%E6%96%B9%E6%B3%95\"><\/span>\u5176\u5b83\u65b9\u6cd5<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>\n<p>getContextObject<\/p>\n<pre><code class=\"language-cpp\">116 sp&lt;IBinder&gt; ProcessState::getContextObject(const sp&lt;IBinder&gt;&amp; \/*caller*\/)\n117 {\n118     return getStrongProxyForHandle(0);\n119 }<\/code><\/pre>\n<\/li>\n<li>\n<p>getStrongProxyForHandle<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">259 sp&lt;IBinder&gt; ProcessState::getStrongProxyForHandle(int32_t handle)\n260 {\n261     sp&lt;IBinder&gt; result;\n262 \n263     AutoMutex _l(mLock);\n264 \n265     handle_entry* e = lookupHandleLocked(handle);\n266 \n267     if (e != nullptr) {\n268         \/\/ We need to create a new BpBinder if there isn&#039;t currently one, OR we\n269         \/\/ are unable to acquire a weak reference on this current one.  See comment\n270         \/\/ in getWeakProxyForHandle() for more info about this.\n271         IBinder* b = e-&gt;binder;\n272         if (b == nullptr || !e-&gt;refs-&gt;attemptIncWeak(this)) {\n273             if (handle == 0) {\n274                 \/\/ Special case for context manager...\n275                 \/\/ The context manager is the only object for which we create\n276                 \/\/ a BpBinder proxy without already holding a reference.\n277                 \/\/ Perform a dummy transaction to ensure the context manager\n278                 \/\/ is registered before we create the first local reference\n279                 \/\/ to it (which will occur when creating the BpBinder).\n280                 \/\/ If a local reference is created for the BpBinder when the\n281                 \/\/ context manager is not present, the driver will fail to\n282                 \/\/ provide a reference to the context manager, but the\n283                 \/\/ driver API does not return status.\n284                 \/\/\n285                 \/\/ Note that this is not race-free if the context manager\n286                 \/\/ dies while this code runs.\n287                 \/\/\n288                 \/\/ TODO: add a driver API to wait for context manager, or\n289                 \/\/ stop special casing handle 0 for context manager and add\n290                 \/\/ a driver API to get a handle to the context manager with\n291                 \/\/ proper reference counting.\n292 \n293                 Parcel data;\n294                 status_t status = IPCThreadState::self()-&gt;transact(\n295                         0, IBinder::PING_TRANSACTION, data, nullptr, 0);\n296                 if (status == DEAD_OBJECT)\n297                    return nullptr;\n298             }\n299 \n300             b = BpBinder::create(handle);\n301             e-&gt;binder = b;\n302             if (b) e-&gt;refs = b-&gt;getWeakRefs();\n303             result = b;\n304         } else {\n305             \/\/ This little bit of nastyness is to allow us to add a primary\n306             \/\/ reference to the remote proxy when this team doesn&#039;t have one\n307             \/\/ but another team is sending the handle to us.\n308             result.force_set(b);\n309             e-&gt;refs-&gt;decWeak(this);\n310         }\n311     }\n312 \n313     return result;\n314 }<\/code><\/pre>\n<p>\u2192lookupHandleLocked<br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/944067088827\">https:\/\/www.jianshu.com\/p\/944067088827<\/a><\/p>\n<pre><code class=\"language-cpp\">246 ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle)\n247 {\n248     const size_t N=mHandleToObject.size();\n249     if (N &lt;= (size_t)handle) {\n250         handle_entry e;\n251         e.binder = nullptr;\n252         e.refs = nullptr;\n253         status_t err = mHandleToObject.insertAt(e, N, handle+1-N);\n254         if (err &lt; NO_ERROR) return nullptr;\n255     }\n256     return &amp;mHandleToObject.editItemAt(handle);\n257 }<\/code><\/pre>\n<p>\u2192BpBinder.create<br \/>\n<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14481\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14481<\/a><\/p>\n<button class=\"simplefavorite-button\" data-postid=\"14446\" 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>\u76f8\u5173\u6e90\u7801\u4f4d\u7f6e\uff1a frameworks\/native\/libs\/binder\/ProcessState [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1104],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14446"}],"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=14446"}],"version-history":[{"count":15,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14446\/revisions"}],"predecessor-version":[{"id":14544,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14446\/revisions\/14544"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}