{"id":101006,"date":"2020-05-21T02:12:06","date_gmt":"2020-05-20T18:12:06","guid":{"rendered":"http:\/\/4563.org\/?p=101006"},"modified":"2020-05-21T02:12:06","modified_gmt":"2020-05-20T18:12:06","slug":"aqs-%e9%87%8c%e7%9a%84-setheadandpropagate-%e4%bb%a5%e5%8f%8a%e5%85%b3%e4%ba%8e-propagate-%e4%bf%a1%e5%8f%b7%e7%9a%84%e7%96%91%e9%97%ae%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=101006","title":{"rendered":"AQS \u91cc\u7684 setHeadAndPropagate \u4ee5\u53ca\u5173\u4e8e PROPAGATE \u4fe1\u53f7\u7684\u7591\u95ee\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  AQS \u91cc\u7684 setHeadAndPropagate \u4ee5\u53ca\u5173\u4e8e PROPAGATE \u4fe1\u53f7\u7684\u7591\u95ee\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<pre><code>    private void setHeadAndPropagate(Node node, int propagate) {         Node h = head; \/\/ Record old head for check below         setHead(node);         \/*          * Try to signal next queued node if:          *   Propagation was indicated by caller,          *     or was recorded (as h.waitStatus either before          *     or after setHead) by a previous operation          *     (note: this uses sign-check of waitStatus because          *      PROPAGATE status may transition to SIGNAL.)          * and          *   The next node is waiting in shared mode,          *     or we don't know, because it appears null          *          * The conservatism in both of these checks may cause          * unnecessary wake-ups, but only when there are multiple          * racing acquires\/releases, so most need signals now or soon          * anyway.          *\/         if (propagate &gt; 0 || h == null || h.waitStatus &lt; 0 ||             (h = head) == null || h.waitStatus &lt; 0) {             Node s = node.next;             if (s == null || s.isShared())                 doReleaseShared();         }     } <\/code><\/pre>\n<ol>\n<li>\u4e0a\u9762\u7684 h == null (h = head) == null s == null \u770b\u8d77\u6765\u597d\u50cf\u53ea\u662f\u4e3a\u4e86\u9632\u6b62\u7a7a\u6307\u9488\u5f02\u5e38\uff0c\u8fd8\u662f\u771f\u7684\u4f1a\u51fa\u73b0\u8fd9\u4e9b\u60c5\u51b5\uff0c\u8fd9\u4e9b\u60c5\u51b5\u90fd\u662f\u5565\u573a\u666f\u554a\uff1f<\/li>\n<li>h == null \u548c(h = head) == null \u4e3a\u5565\u8981\u68c0\u67e5\u4e24\u904d\uff0c\u770b\u8d77\u6765\u662f \u4e0b\u4e00\u4e2a\u65f6\u95f4\u8282\u70b9\uff0chead \u5c31\u53ef\u80fd\u53d8\u4e86\uff0c\u4f46\u5982\u679c\u8003\u8651\u8fd9\u79cd\u60c5\u51b5\uff0c\u68c0\u67e5\u4e24\u904d \u96be\u9053\u5c31\u591f\u5417\uff1f<\/li>\n<\/ol>\n<pre><code>    private void doReleaseShared() {         \/*          * Ensure that a release propagates, even if there are other          * in-progress acquires\/releases.  This proceeds in the usual          * way of trying to unparkSuccessor of head if it needs          * signal. But if it does not, status is set to PROPAGATE to          * ensure that upon release, propagation continues.          * Additionally, we must loop in case a new node is added          * while we are doing this. Also, unlike other uses of          * unparkSuccessor, we need to know if CAS to reset status          * fails, if so rechecking.          *\/         for (;;) {             Node h = head;             if (h != null &amp;&amp; h != tail) {                 int ws = h.waitStatus;                 if (ws == Node.SIGNAL) {                     if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0))                         continue;            \/\/ loop to recheck cases                     unparkSuccessor(h);                 }                 else if (ws == 0 &amp;&amp;                          !compareAndSetWaitStatus(h, 0, Node.PROPAGATE))                     continue;                \/\/ loop on failed CAS             }             if (h == head)                   \/\/ loop if head changed                 break;         }     } <\/code><\/pre>\n<p>\u4e0a\u9762\u5f15\u5165\u7684 PROPAGATE \u72b6\u6001\uff0c\u5c31\u662f\u4e3a\u4e86 setHeadAndPropagate \u80fd\u591f\u68c0\u6d4b\u5230\u800c\u5b58\u5728\u7684\u5427\u3002<\/p>\n<ol>\n<li>\u4e0a\u9762\u8fd9\u4e2a\u51fd\u6570\u770b\u8d77\u6765 \u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u5faa\u73af\uff08\u5934\u8282\u70b9\u6ca1\u6709\u53d8\uff09<\/li>\n<li>\u4e3a\u5565\u8981\u4ece SIGNAL \u5230 0 \u518d\u5230 PROPAGATE \u5462\uff0c\u662f\u4e3a\u4e86\u9632 unparkSuccessor \u4e00\u624b\u5417\uff1f<\/li>\n<li>\u611f\u89c9\u597d\u591a\u7591\u95ee\uff0c\u90fd\u4e0d\u77e5\u5982\u4f55\u8bf4\u8d77&#8230;<\/li>\n<\/ol><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>4<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1669443\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u597d\u540e\u6094\uff0c\u6211\u662f\u4e0d\u662f\u5e94\u8be5\u5f04\u4e2a\u5438\u5f15\u4eba\u7684\u6807\u9898\u7684\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1669444\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : guyeu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             1. \u8f93\u5165\u4e0d\u786e\u5b9a\u7684\u60c5\u51b5\u4e0b\uff0c\u5c31\u662f\u5f97\u68c0\u67e5\uff1b<br \/>2. \u8fd9\u7eaf\u7cb9\u662f\u8bed\u8a00\u57fa\u7840\u95ee\u9898\u3002\u3002\u68c0\u67e5\u4e24\u904d\u662f\u56e0\u4e3a\u68c0\u67e5\u7684\u662f\u4fe9\u4e0d\u540c\u7684\u5bf9\u8c61\uff1b<\/p>\n<p>1. \u8fd0\u884c\u671f\u95f4\u5934\u7ed3\u70b9\u662f\u6709\u53ef\u80fd\u53d8\u7684\uff1b<br \/>2. \u5565\u53eb\u4ece SIGNAL \u5230 0 \u518d\u5230 PROPAGATE\uff0c\u90a3\u7279\u4e48\u662f\u4fe9\u903b\u8f91\u5206\u652f\uff1b<br \/>3. \u522b\u8bf4\u4e86\uff1b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1669445\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luckyrayyy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             https:\/\/mingshan.fun\/2019\/02\/02\/aqs-shared\/<br \/>\u4f60\u770b\u8fd9\u7bc7\u6587\u7ae0\uff0c\u68c0\u67e5\u4e24\u8fb9\u662f\u56e0\u4e3a\u8fd9\u662f\u4e24\u4e2a\u5bf9\u8c61\uff0c\u4e00\u4e2a\u662f\u4e4b\u524d\u7684\u5934\u7ed3\u70b9\uff0c\u4e00\u4e2a\u662f\u65b0\u7684\u5934\u7ed3\u70b9\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1669446\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : amiwrong123 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @guyeu <br \/>@luckyrayyy <br \/>\u8c22\u8c22\u56de\u590d\uff0c\u4e4b\u524d\u8fd9\u5757\u786e\u5b9e\u60f3\u9519\u4e86\uff0c\u770b\u6765\u4ee5\u540e\u53d1\u5e16\u524d\u8fd8\u662f\u5f97\u770b\u4ed4\u7ec6\uff0c\u6211\u627f\u8ba4\u9519\u8bef\u3002<\/p>\n<p>\u4e0d\u8fc7\u770b\u5b8c\u8fd8\u662f\u6709\u7591\u95ee\u3002\u5206\u6790\u5982\u4e0b<\/p>\n<p> &#8211; \u5165\u53c2`node`\u6240\u4ee3\u8868\u7684\u7ebf\u7a0b\uff08\u8fd9\u4e2a node \u7684 Thread \u6210\u5458\uff09\u4e00\u5b9a\u662f\u5f53\u524d\u6267\u884c\u7684\u7ebf\u7a0b\uff0c<br \/> &#8211; \u770b\u7b2c\u4e00\u4e2a if \u7684\u5224\u65ad\uff1a<br \/> &#8211; \u5982\u679c`propagate &gt; 0`\u6210\u7acb\u7684\u8bdd\uff0c\u8bf4\u660e\u8fd8\u6709\u5269\u4f59\u5171\u4eab\u9501\u53ef\u4ee5\u83b7\u53d6\uff0c\u90a3\u4e48\u77ed\u8def\u540e\u9762\u6761\u4ef6\u3002<br \/> &#8211; \u5982\u679c`propagate = 0`\u6210\u7acb\u7684\u8bdd\uff0c\u8bf4\u660e\u6ca1\u6709\u5269\u4f59\u5171\u4eab\u9501\u53ef\u4ee5\u83b7\u53d6\u4e86\uff0c\u6309\u7406\u8bf4\u4e0d\u9700\u8981\u5524\u9192\u540e\u7ee7\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u5f88\u591a\u60c5\u51b5\u4e0b\uff0c\u8c03\u7528 doReleaseShared\uff0c\u4f1a\u9020\u6210 acquire thread \u4e0d\u5fc5\u8981\u7684\u5524\u9192\u3002<br \/> &#8211; \u7ee7\u7eed\u770b\uff0c\u5982\u679c`propagate &gt; 0`\u4e0d\u6210\u7acb\uff0c\u800c`h.waitStatus &lt; 0`\u6210\u7acb\u3002\u8fd9\u8bf4\u660e\u65e7 head \u7684 status&lt;0 \u3002\u4f46\u5982\u679c\u4f60\u770b doReleaseShared \u7684\u903b\u8f91\uff0c\u4f1a\u53d1\u73b0\u5728 unparkSuccessor \u4e4b\u524d\u5c31\u4f1a CAS \u8bbe\u7f6e head \u7684 status \u4e3a 0 \u7684\uff0c\u5728 unparkSuccessor \u4e5f\u4f1a\u8fdb\u884c\u4e00\u6b21 CAS \u5c1d\u8bd5\uff0c\u56e0\u4e3a head \u7684 status \u4e3a 0 \u4ee3\u8868\u4e00\u79cd\u4e2d\u95f4\u72b6\u6001\uff08 head \u7684\u540e\u7ee7\u4ee3\u8868\u7684\u7ebf\u7a0b\u5df2\u7ecf\u5524\u9192\uff0c\u4f46\u5b83\u8fd8\u6ca1\u6709\u505a\u5b8c\u5de5\u4f5c\uff09\uff0c\u6216\u8005\u4ee3\u8868 head \u662f tail \u3002\u800c\u8fd9\u91cc\u65e7 head \u7684 status&lt;0\uff0c\u53ea\u80fd\u662f\u7531\u4e8e doReleaseShared \u91cc\u7684`compareAndSetWaitStatus(h, 0, Node.PROPAGATE)`\u7684\u64cd\u4f5c\uff0c\u800c\u4e14\u7531\u4e8e\u5f53\u524d\u6267\u884c setHeadAndPropagate \u7684\u7ebf\u7a0b\u53ea\u4f1a\u5728\u6700\u540e\u4e00\u53e5\u624d\u6267\u884c doReleaseShared\uff0c\u6240\u4ee5\u51fa\u73b0\u8fd9\u79cd\u60c5\u51b5\uff0c\u4e00\u5b9a\u662f\u56e0\u4e3a\u6709\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5728\u8c03\u7528 doReleaseShared \u624d\u80fd\u9020\u6210\uff0c\u800c\u8fd9\u5f88\u53ef\u80fd\u662f\u56e0\u4e3a\u5728\u4e2d\u95f4\u72b6\u6001\u65f6\uff0c\u53c8\u6709\u4eba\u91ca\u653e\u4e86\u5171\u4eab\u9501\u3002<br \/> &#8211; \u7ee7\u7eed\u770b\uff0c\u5982\u679c`propagate &gt; 0`\u4e0d\u6210\u7acb\uff0c\u4e14`h.waitStatus &lt; 0`\u4e0d\u6210\u7acb\uff0c\u800c\u7b2c\u4e8c\u4e2a`h.waitStatus &lt; 0`\u6210\u7acb\u3002\u6ce8\u610f\uff0c\u7b2c\u4e8c\u4e2a`h.waitStatus &lt; 0`\u91cc\u7684 h \u662f\u65b0 head \uff08\u5f88\u53ef\u80fd\u5c31\u662f\u5165\u53c2 node \uff09\u3002\u7b2c\u4e00\u4e2a`h.waitStatus &lt; 0`\u4e0d\u6210\u7acb\u5f88\u6b63\u5e38\uff0c\u56e0\u4e3a\u5b83\u4e00\u822c\u4e3a 0 \u3002\u7b2c\u4e8c\u4e2a`h.waitStatus &lt; 0`\u6210\u7acb\u4e5f\u5f88\u6b63\u5e38\uff0c\u56e0\u4e3a\u53ea\u8981\u65b0 head \u4e0d\u662f\u961f\u5c3e\uff0c\u90a3\u4e48\u65b0 head \u7684 status \u80af\u5b9a\u662f SIGNAL \u3002\u6240\u4ee5\u8fd9\u79cd\u60c5\u51b5\u53ea\u4f1a\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u5524\u9192\u3002<\/p>\n<p>\u7b80\u5355\u7684\u8bf4\uff0c\u6211\u8ba4\u4e3a\uff0c\u68c0\u67e5\u7b2c\u4e00\u4e2a`h.waitStatus &lt; 0`\uff0c\u662f\u56e0\u4e3a\u88ab\u5524\u9192\u7684\u7ebf\u7a0b\u5904\u4e8e\u4e2d\u95f4\u72b6\u6001\uff0c\u800c doReleaseShared \u5728\u8fd9\u4e2a\u4e2d\u95f4\u72b6\u6001\uff0c\u53ea\u4f1a\u8bbe\u7f6e`compareAndSetWaitStatus(h, 0, Node.PROPAGATE)`,\u4e0d\u4f1a\u8c03\u7528 unparkSuccessor(h);\uff0c\u56e0\u4e3a\u7ebf\u7a0b\u5df2\u7ecf\u88ab\u5524\u9192\u4e86\u3002<\/p>\n<p>\u4f46\u68c0\u67e5\u7b2c\u4e8c\u4e2a`h.waitStatus &lt; 0`\u5c31\u6709\u70b9\u60f3\u4e0d\u901a\uff0c\u611f\u89c9\u4ed6\u53ea\u4f1a\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u5524\u9192\uff1f\u597d\u5947\u602a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>AQS \u91cc\u7684 setHeadAnd&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/101006"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=101006"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/101006\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=101006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=101006"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=101006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}