{"id":5061,"date":"2021-02-07T18:26:03","date_gmt":"2021-02-07T10:26:03","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5061"},"modified":"2021-02-07T18:26:03","modified_gmt":"2021-02-07T10:26:03","slug":"folly%e6%97%a0%e9%94%81%e9%98%9f%e5%88%97%e6%ad%a3%e7%a1%ae%e6%80%a7%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5061","title":{"rendered":"folly\u65e0\u9501\u961f\u5217\u6b63\u786e\u6027\u8bf4\u660e"},"content":{"rendered":"<p class=\"postTitle\"><span style=\"font-size: 16px;\">folly\u65e0\u9501\u961f\u5217\u662ffacebook\u5f00\u6e90\u7684\u4e00\u4e2a\u65e0\u6240\u961f\u5217\uff0c\u4f7f\u7528\u7684\u662f\u5355\u5411\u94fe\u8868\uff0c\u901a\u8fc7compare_exchange\u8bed\u53e5\u5b9e\u73b0\u7684\u591a\u751f\u4ea7\u591a\u6d88\u8d39\u7684\u961f\u5217\uff0c\u6211\u66fe\u7ecf\u82b1\u4e86\u6bd4\u8f83\u591a\u7684\u65f6\u95f4\u5b66\u4e60memory_order\u7684\u8bf4\u660e\uff0c\u5bf9release-acquire\u8bed\u4e49\uff0c\u81ea\u8ba4\u4e3a\u8fd8\u662f\u6bd4\u8f83\u4e86\u89e3\u3002\u5982\u679c\u4e00\u4e2aatomic\u5bf9\u8c61\u4f7f\u7528std<\/span><span class=\"sy4\" style=\"font-size: 16px;\">::<span class=\"me2\">memory_order_release\u8fdb\u884c\u5199\u64cd\u4f5c\uff0c\u800c\u53e6\u5916\u4e00\u4e2a\u7ebf\u7a0b\u4f7f\u7528std::memory_order_acquire\u8fdb\u884c\u8bfb\u64cd\u4f5c\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u7ebf\u7a0b\u4e4b\u95f4\u5f62\u6210\u540c\u6b65\u5173\u7cfb\u3002std::memory_order_release\u4e4b\u524d\u5199\u7684\u6548\u679c\uff0c\u5728std::memory_order_acquire\u4e4b\u540e\u53ef\u89c1\u3002\u4e0d\u8fc7\u5bf9\u4e8e\u591a\u751f\u4ea7\u591a\u6d88\u8d39\u6a21\u578b\uff0c\u5b58\u5728\u591a\u4e2a\u751f\u4ea7\u8005\u7684\u60c5\u51b5\uff0c\u5728\u6709\u591a\u4e2a\u751f\u4ea7\u8005\u7684\u60c5\u51b5\u4e0b\uff0c\u7ed3\u679c\u6b63\u786e\u5417\uff1f<\/span><\/span><\/p>\n<div id=\"cnblogs_post_body\" class=\"blogpost-body\">\n<p><span class=\"sy4\"><span class=\"me2\">\u8fd9\u91cc\u7ed9\u51fafolly\u7684\u6e90\u4ee3\u7801\uff0c\u8fd9\u91cc\u8bf7\u91cd\u70b9\u5173\u6ce8<\/span><\/span>insertHead\u51fd\u6570\u548csweepOnce\u51fd\u6570\u3002<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">\/*\r\n* Copyright 2014-present Facebook, Inc.\r\n*\r\n* Licensed under the Apache License, Version 2.0 (the \"License\");\r\n* you may not use this file except in compliance with the License.\r\n* You may obtain a copy of the License at\r\n*\r\n*   http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n*\r\n* Unless required by applicable law or agreed to in writing, software\r\n* distributed under the License is distributed on an \"AS IS\" BASIS,\r\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n* See the License for the specific language governing permissions and\r\n* limitations under the License.\r\n*\/\r\n\r\n#pragma once\r\n\r\n#include &lt;atomic&gt;\r\n#include &lt;cassert&gt;\r\n#include &lt;utility&gt;\r\n\r\nnamespace folly {\r\n\r\n    \/**\r\n    * A very simple atomic single-linked list primitive.\r\n    *\r\n    * Usage:\r\n    *\r\n    * class MyClass {\r\n    *   AtomicIntrusiveLinkedListHook&lt;MyClass&gt; hook_;\r\n    * }\r\n    *\r\n    * AtomicIntrusiveLinkedList&lt;MyClass, &amp;MyClass::hook_&gt; list;\r\n    * list.insert(&amp;a);\r\n    * list.sweep([] (MyClass* c) { doSomething(c); }\r\n    *\/\r\n    template &lt;class T&gt;\r\n    struct AtomicIntrusiveLinkedListHook {\r\n        T* next{ nullptr };\r\n    };\r\n\r\n    template &lt;class T, AtomicIntrusiveLinkedListHook&lt;T&gt; T::*HookMember&gt;\r\n    class AtomicIntrusiveLinkedList {\r\n    public:\r\n        AtomicIntrusiveLinkedList() {}\r\n        AtomicIntrusiveLinkedList(const AtomicIntrusiveLinkedList&amp;) = delete;\r\n        AtomicIntrusiveLinkedList&amp; operator=(const AtomicIntrusiveLinkedList&amp;) =\r\n            delete;\r\n        AtomicIntrusiveLinkedList(AtomicIntrusiveLinkedList&amp;&amp; other) noexcept {\r\n            auto tmp = other.head_.load();\r\n            other.head_ = head_.load();\r\n            head_ = tmp;\r\n        }\r\n        AtomicIntrusiveLinkedList&amp; operator=(\r\n            AtomicIntrusiveLinkedList&amp;&amp; other) noexcept {\r\n            auto tmp = other.head_.load();\r\n            other.head_ = head_.load();\r\n            head_ = tmp;\r\n\r\n            return *this;\r\n        }\r\n\r\n        \/**\r\n        * Note: list must be empty on destruction.\r\n        *\/\r\n        ~AtomicIntrusiveLinkedList() {\r\n            assert(empty());\r\n        }\r\n\r\n        bool empty() const {\r\n            return head_.load() == nullptr;\r\n        }\r\n\r\n        \/**\r\n        * Atomically insert t at the head of the list.\r\n        * @return True if the inserted element is the only one in the list\r\n        *         after the call.\r\n        *\/\r\n        bool insertHead(T* t) {\r\n            assert(next(t) == nullptr);\r\n\r\n            auto oldHead = head_.load(std::memory_order_relaxed);\r\n            do {\r\n                next(t) = oldHead;\r\n                \/* oldHead is updated by the call below.\r\n                NOTE: we don't use next(t) instead of oldHead directly due to\r\n                compiler bugs (GCC prior to 4.8.3 (bug 60272), clang (bug 18899),\r\n                MSVC (bug 819819); source:\r\n                http:\/\/en.cppreference.com\/w\/cpp\/atomic\/atomic\/compare_exchange *\/\r\n            } while (!head_.compare_exchange_weak(oldHead, t,\r\n                std::memory_order_release,\r\n                std::memory_order_relaxed));\r\n\r\n            return oldHead == nullptr;\r\n        }\r\n\r\n        \/**\r\n        * Replaces the head with nullptr,\r\n        * and calls func() on the removed elements in the order from tail to head.\r\n        * Returns false if the list was empty.\r\n        *\/\r\n        template &lt;typename F&gt;\r\n        bool sweepOnce(F&amp;&amp; func) {\r\n            if (auto head = head_.exchange(nullptr)) {\r\n                auto rhead = reverse(head);\r\n                unlinkAll(rhead, std::forward&lt;F&gt;(func));\r\n                return true;\r\n            }\r\n            return false;\r\n        }\/**\r\n        * Repeatedly replaces the head with nullptr,\r\n        * and calls func() on the removed elements in the order from tail to head.\r\n        * Stops when the list is empty.\r\n        *\/\r\n        template &lt;typename F&gt;\r\n        void sweep(F&amp;&amp; func) {\r\n            while (sweepOnce(func)) {\r\n            }\r\n        }\r\n\r\n        \/**\r\n        * Similar to sweep() but calls func() on elements in LIFO order.\r\n        *\r\n        * func() is called for all elements in the list at the moment\r\n        * reverseSweep() is called.  Unlike sweep() it does not loop to ensure the\r\n        * list is empty at some point after the last invocation.  This way callers\r\n        * can reason about the ordering: elements inserted since the last call to\r\n        * reverseSweep() will be provided in LIFO order.\r\n        *\r\n        * Example: if elements are inserted in the order 1-2-3, the callback is\r\n        * invoked 3-2-1.  If the callback moves elements onto a stack, popping off\r\n        * the stack will produce the original insertion order 1-2-3.\r\n        *\/\r\n        template &lt;typename F&gt;\r\n        void reverseSweep(F&amp;&amp; func) {\r\n            \/\/ We don't loop like sweep() does because the overall order of callbacks\r\n            \/\/ would be strand-wise LIFO which is meaningless to callers.\r\n            auto head = head_.exchange(nullptr);\r\n            unlinkAll(head, std::forward&lt;F&gt;(func));\r\n        }\r\n\r\n    private:\r\n        std::atomic&lt;T*&gt; head_{ nullptr };\r\n\r\n        static T*&amp; next(T* t) {\r\n            return (t-&gt;*HookMember).next;\r\n        }\r\n\r\n        \/* Reverses a linked list, returning the pointer to the new head\r\n        (old tail) *\/\r\n        static T* reverse(T* head) {\r\n            T* rhead = nullptr;\r\n            while (head != nullptr) {\r\n                auto t = head;\r\n                head = next(t);\r\n                next(t) = rhead;\r\n                rhead = t;\r\n            }\r\n            return rhead;\r\n        }\r\n\r\n        \/* Unlinks all elements in the linked list fragment pointed to by `head',\r\n        * calling func() on every element *\/\r\n        template &lt;typename F&gt;\r\n        void unlinkAll(T* head, F&amp;&amp; func) {\r\n            while (head != nullptr) {\r\n                auto t = head;\r\n                head = next(t);\r\n                next(t) = nullptr;\r\n                func(t);\r\n            }\r\n        }\r\n    };\r\n\r\n} \/\/ namespace folly<\/pre>\n<p>\u5982\u679c\u5b58\u5728\u4e24\u4e2a\u7ebf\u7a0b\u5148\u540e\u5411\u540c\u4e00\u4e2a\u961f\u5217\u4e2d\u63d2\u5165\u8282\u70b9\uff0c\u7531\u4e8e\u4e24\u4e2a\u7ebf\u7a0b\u4e2d\u6ca1\u6709\u4e00\u4e2a\u4f7f\u7528acquire\uff0c\u5982\u679c\u4ec5\u6309\u7167release-acquire\u8bed\u4e49\uff0c\u663e\u7136\uff0c\u6b63\u786e\u6027\u65e0\u6cd5\u4fdd\u8bc1\uff0c\u540e\u4e00\u4e2ainsertHead\u51fd\u6570\u4e2d\uff0c\u65e0\u8bba\u662fauto oldHead = head_.load(std::memory_order_relaxed);\uff0c\u8fd8\u662fwhile (!head_.compare_exchange_weak(oldHead, t,\u00a0std::memory_order_release,std::memory_order_relaxed));\u90fd\u53ef\u80fd\u8bfb\u53d6\u7684\u662f\u524d\u4e00\u4e2a\u7ebf\u7a0b\u63d2\u5165\u524d\u7684\u6570\u636e\u3002\u90a3\u4e48\uff0c\u8fd8\u6709\u4ec0\u4e48C++\u8bed\u4e49\uff0c\u53ef\u4ee5\u4fdd\u8bc1folly\u961f\u5217\u7684\u6b63\u786e\u6027\uff1f\u90a3\u5c31\u662frelease sequence\u3002release sequence\u5176\u4e2d\u7684\u4e00\u90e8\u5206\u8bf4\u7684\u662f\uff1a<\/p>\n<p>\u5982\u679c\u4e00\u4e2a\u5b58\u50a8\u4f7f\u7528memory_order_release\u6216\u66f4\u4e25\u683c\u7684\u5185\u5b58\u5e8f\uff0c\u540e\u9762\u8ddf\u7740\u82e5\u5e72\u8bfb-\u6539-\u5199\uff08read-modify-write\uff09\uff08\u53ef\u4ee5\u662f\u540c\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e0d\u540c\u7684\u7ebf\u7a0b\uff09\u64cd\u4f5c\u7684\u8bdd\u3002<\/p>\n<p>\uff081\uff09\u90a3\u4e48\u4e2d\u95f4\u7684\u8bfb-\u6539-\u5199\u64cd\u4f5c \u8bfb\u53d6\u7684\u8981\u4e48\u662f\u524d\u4e00\u6b21\u8bfb-\u6539-\u5199\u7684\u7ed3\u679c\uff0c\u8981\u4e48\u662f\u5b58\u50a8\u7684\u6570\u636e\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5982\u679c\u5b58\u5728\u4e00\u4e2arelease\u64cd\u4f5c\uff0c\u540e\u9762\u8ddf\u7740\u4e00\u4e2a\u8bfb\u6539\u5199\u64cd\u4f5c\u7684\u8bdd\uff0c\u8fd9\u4e2a\u8bfb\u6539\u5199\u64cd\u4f5c\u80af\u5b9a\u4f1a\u5f97\u5230\u4e4b\u524drelease\u64cd\u4f5c\u5199\u5165\u7684\u6548\u679c\u3002\u6211\u4eec\u53ef\u4ee5\u89c2\u5bdf\u5230insertHead\u4e2d\u7684compare_exchange_weak\u4e3a\u4e00\u4e2arelease\u64cd\u4f5c\uff0c\u540c\u65f6\u4e5f\u662f\u4e00\u4e2a\u8bfb\u6539\u5199\u64cd\u4f5c\uff0c\u90a3\u4e48\u524d\u9762\u4e00\u4e2a\u7ebf\u7a0b\u7684\u4fee\u6539\uff0c\u4e00\u5b9a\u4f1a\u5728\u540e\u9762\u4e00\u4e2acompare_exchange_weak\u4e2d\u53ef\u89c1\uff0c\u65e0\u8bba\u662f\u540c\u4e00\u4e2a\u7ebf\u7a0b\u8c03\u7528\uff0c\u8fd8\u662f\u4e0d\u540c\u7ebf\u7a0b\u8c03\u7528\u3002\u6ce8\u610f\u5230auto oldHead = head_.load(std::memory_order_relaxed);\u5f97\u5230\u7684\u7ed3\u679c\u7684\u6b63\u786e\u6027\u4e0e\u5426\uff0c\u4e0d\u5f71\u54cdcompare_exchange_weak\u7684\u6b63\u786e\u6027\uff0c\u56e0\u4e3a\u5982\u679c\u524d\u4e00\u4e2a\u8bfb\u53d6\u7684\u7ed3\u679c\u662f\u65e7\u503c\uff0c\u8fd9\u4e2a\u64cd\u4f5c\u5c31\u4f1a\u5931\u8d25\uff0c\u800c\u4e14\u5c06oldHead\u7684\u503c\u66f4\u65b0\u4e3a\u6700\u65b0\u503c\uff0c\u8fd9\u70b9\u5bf9\u4e8e\u7406\u89e3folly\u7684\u6b63\u786e\u6027\u5f88\u91cd\u8981\u3002\u5176\u4ed6\u7684\u60c5\u51b5\u5e94\u8be5\u6839\u636e\u7c7b\u4f3c\u7684\u539f\u7406\u5f97\u5230\u6b63\u786e\u7684\u89e3\u7b54\uff0c\u8fd9\u91cc\u5c31\u4e0d\u8be6\u7ec6\u8bf4\u660e\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>folly\u65e0\u9501\u961f\u5217\u662ffacebook\u5f00\u6e90<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[119],"class_list":["post-5061","post","type-post","status-publish","format-standard","hentry","tag-03-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5061"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5061\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}