{"id":14506,"date":"2024-08-29T17:11:14","date_gmt":"2024-08-29T09:11:14","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14506"},"modified":"2024-09-06T11:27:52","modified_gmt":"2024-09-06T03:27:52","slug":"pthread%e6%ba%90%e7%a0%81%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=14506","title":{"rendered":"pthread\u6e90\u7801\u4ecb\u7ecd"},"content":{"rendered":"<p>\u5934\u6587\u4ef6\u4f4d\u7f6e\uff1a<code>bionic\/libc\/include\/<\/code><br \/>\n\u6e90\u7801\u5b9e\u73b0\u4f4d\u7f6e\uff1a<code>bionic\/libc\/bionic\/<\/code><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/weixin_46935110\/article\/details\/130792477\">https:\/\/blog.csdn.net\/weixin_46935110\/article\/details\/130792477<\/a><\/p>\n<p><a href=\"https:\/\/blog.csdn.net\/Lid_23\/article\/details\/132036106\">https:\/\/blog.csdn.net\/Lid_23\/article\/details\/132036106<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/qq_34888036\/article\/details\/133960234\">https:\/\/blog.csdn.net\/qq_34888036\/article\/details\/133960234<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/swartz_lubel\/article\/details\/77150949\">https:\/\/blog.csdn.net\/swartz_lubel\/article\/details\/77150949<\/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-1\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14506\/#%E7%89%B9%E6%80%A7%E8%AF%B4%E6%98%8E%EF%BC%88%EF%BC%89\" title=\"\u7279\u6027\u8bf4\u660e\uff08****\uff09\">\u7279\u6027\u8bf4\u660e\uff08****\uff09<\/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=14506\/#%E5%85%B7%E4%BD%93%E7%B1%BB%E8%AE%B2%E8%A7%A3\" title=\"\u5177\u4f53\u7c7b\u8bb2\u89e3\">\u5177\u4f53\u7c7b\u8bb2\u89e3<\/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=14506\/#pthread_typesh\" title=\"pthread_types.h\">pthread_types.h<\/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=14506\/#pthread_keycpp\" title=\"pthread_key.cpp\">pthread_key.cpp<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14506\/#pthread_setname_npcpp\" title=\"pthread_setname_np.cpp\">pthread_setname_np.cpp<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14506\/#pthread_selfcpp\" title=\"pthread_self.cpp\">pthread_self.cpp<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-1\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14506\/#ndk_cruftcpp\" title=\"ndk_cruft.cpp\">ndk_cruft.cpp<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%E7%89%B9%E6%80%A7%E8%AF%B4%E6%98%8E%EF%BC%88%EF%BC%89\"><\/span>\u7279\u6027\u8bf4\u660e\uff08****\uff09<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>\u4e0d\u8bba\u54ea\u4e2a\u7ebf\u7a0b\u8c03\u7528\u4e86 pthread_key_create()\uff0c\u6240\u521b\u5efa\u7684 key \u90fd\u662f\u6240\u6709\u7ebf\u7a0b\u53ef\u4ee5\u8bbf\u95ee\u7684\uff0c\u4f46\u5404\u4e2a\u7ebf\u7a0b\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u8981\u5f80 key \u4e2d\u586b\u5165\u4e0d\u540c\u7684\u503c\uff0c\u76f8\u5f53\u4e8e\u63d0\u4f9b\u4e86\u4e00\u4e2a\u540c\u540d\u800c\u4e0d\u540c\u503c\u7684\u5168\u5c40\u53d8\u91cf(\u8fd9\u4e2a\u5168\u5c40\u53d8\u91cf\u76f8\u5bf9\u4e8e\u62e5\u6709\u8fd9\u4e2a\u53d8\u91cf\u7684\u7ebf\u7a0b\u6765\u8bf4)\u3002<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%E5%85%B7%E4%BD%93%E7%B1%BB%E8%AE%B2%E8%A7%A3\"><\/span>\u5177\u4f53\u7c7b\u8bb2\u89e3<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"pthread_typesh\"><\/span>pthread_types.h<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-cpp\">typedef int pthread_key_t;\ntypedef long pthread_t<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"pthread_keycpp\"><\/span>pthread_key.cpp<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>\n<p>pthread_key_create\u65b9\u6cd5<\/p>\n<pre><code class=\"language-cpp\">124 int pthread_key_create(pthread_key_t* key, void (*key_destructor)(void*)) {\n125   for (size_t i = 0; i &lt; BIONIC_PTHREAD_KEY_COUNT; ++i) {\n126     uintptr_t seq = atomic_load_explicit(&amp;key_map[i].seq, memory_order_relaxed);\n127     while (!SeqOfKeyInUse(seq)) {\n128       if (atomic_compare_exchange_weak(&amp;key_map[i].seq, &amp;seq, seq + SEQ_INCREMENT_STEP)) {\n129         atomic_store(&amp;key_map[i].key_destructor, reinterpret_cast&lt;uintptr_t&gt;(key_destructor));\n130         *key = i | KEY_VALID_FLAG;\n131         return 0;\n132       }\n133     }\n134   }\n135   return EAGAIN;\n136 }<\/code><\/pre>\n<\/li>\n<li>\n<p>pthread_setspecific\u65b9\u6cd5<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"language-cpp\">178 int pthread_setspecific(pthread_key_t key, const void* ptr) {\n\/\/KeyInValidRange\u5224\u65adkey\u503c\u6709\u6ca1\u6709\u8d85\u51fa\u8303\u56f4\n179   if (__predict_false(!KeyInValidRange(key))) {\n180     return EINVAL;\n181   }\n182   key &amp;= ~KEY_VALID_FLAG;\n183   uintptr_t seq = atomic_load_explicit(&amp;key_map[key].seq, memory_order_relaxed);\n184   if (__predict_true(SeqOfKeyInUse(seq))) {\n185     pthread_key_data_t* data = &amp;get_thread_key_data()[key];\n186     data-&gt;seq = seq;\n187     data-&gt;data = const_cast&lt;void*&gt;(ptr);\n188     return 0;\n189   }\n190   return EINVAL;\n191 }<\/code><\/pre>\n<p>pthread_key_t \u5b9e\u9645\u4e3aint\u7c7b\u578b<\/p>\n<p>\u2192key_map\u6570\u7ec4<\/p>\n<pre><code class=\"language-cpp\"> 50 struct pthread_key_internal_t {\n 51   atomic_uintptr_t seq;\n 52   atomic_uintptr_t key_destructor;\n 53 };\n 54 \n 55 static pthread_key_internal_t key_map[BIONIC_PTHREAD_KEY_COUNT];<\/code><\/pre>\n<p>\u6839\u636e<a href=\"http:\/\/xinyiworld.top\/wordpress_it\/?p=14519\">http:\/\/xinyiworld.top\/wordpress_it\/?p=14519<\/a>\u53ef\u4ee5\u77e5\u9053atomic_uintptr_t\u4e3a\u7c7b\u578b\u4e3a<code>unsigned long<\/code>\u7c7b\u578b\u7684\u539f\u5b50\u53d8\u91cf<\/p>\n<p>\/\/todo<\/p>\n<h2><span class=\"ez-toc-section\" id=\"pthread_setname_npcpp\"><\/span>pthread_setname_np.cpp<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>pthread_setname_np(\u8bbe\u7f6e\u7ebf\u7a0b\u540d\u79f0)\n<pre><code class=\"language-cpp\">83 int pthread_setname_np(pthread_t t, const char* thread_name) {\n84   ErrnoRestorer errno_restorer;\n85 \n86   size_t thread_name_len = strlen(thread_name);\n87   if (thread_name_len &gt;= MAX_TASK_COMM_LEN) return ERANGE;\n88 \n89   \/\/ Setting our own name is an easy special case.\n90   if (t == pthread_self()) {\n91     return prctl(PR_SET_NAME, thread_name) ? errno : 0;\n92   }\n93 \n94   \/\/ We have to set another thread&#039;s name.\n95   int fd = __open_task_comm_fd(t, O_WRONLY, &quot;pthread_setname_np&quot;);\n96   if (fd == -1) return errno;\n97 \n98   ssize_t n = TEMP_FAILURE_RETRY(write(fd, thread_name, thread_name_len));\n99   close(fd);\n100 \n101   if (n == -1) return errno;\n102   if (n != static_cast&lt;ssize_t&gt;(thread_name_len)) return EIO;\n103   return 0;\n104 }<\/code><\/pre>\n<p>\u5411<code>\/proc\/\u8fdb\u7a0bid\/task\/\u7ebf\u7a0bid\/comm<\/code>\u6587\u4ef6\u4e2d\u5199\u5165\u7ebf\u7a0b\u540d\u79f0<\/p>\n<\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"pthread_selfcpp\"><\/span>pthread_self.cpp<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<pre><code class=\"language-cpp\"> 29 #include &quot;pthread_internal.h&quot;\n 30 \n 31 pthread_t pthread_self() {\n 32   return reinterpret_cast&lt;pthread_t&gt;(__get_thread());\n 33 }<\/code><\/pre>\n<h1><span class=\"ez-toc-section\" id=\"ndk_cruftcpp\"><\/span>ndk_cruft.cpp<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<ul>\n<li>__get_thread\n<pre><code class=\"language-cpp\">366 \/\/ Various third-party apps contain a backport of our pthread_rwlock implementation that uses this.\n367 pthread_internal_t* __get_thread() {\n368   return __real_get_thread();\n369 }<\/code><\/pre>\n<p>__real_get_thread\u5b9e\u73b0\u4f4d\u7f6e \uff1f<\/p>\n<\/li>\n<\/ul>\n<button class=\"simplefavorite-button\" data-postid=\"14506\" 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>\u5934\u6587\u4ef6\u4f4d\u7f6e\uff1abionic\/libc\/include\/ \u6e90\u7801\u5b9e\u73b0\u4f4d\u7f6e\uff1abionic\/libc\/bion [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1118],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14506"}],"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=14506"}],"version-history":[{"count":24,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14506\/revisions"}],"predecessor-version":[{"id":14720,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/14506\/revisions\/14720"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=14506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=14506"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=14506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}