privatestaticbooleanshouldParkAfterFailedAcquire(Node pred, Node node){ // 拿到虚拟线程的ws,此时应该为0 int ws = pred.waitStatus; if (ws == Node.SIGNAL) // Node.SIGNAL为-1,ws为0,不进这里 /* * This node has already set status asking a release * to signal it, so it can safely park. */ returntrue; if (ws > 0) { /* * Predecessor was cancelled. Skip over predecessors and * indicate retry. */ do { node.prev = pred = pred.prev; } while (pred.waitStatus > 0); pred.next = node; } else { /* * waitStatus must be 0 or PROPAGATE. Indicate that we * need a signal, but don't park yet. Caller will need to * retry to make sure it cannot acquire before parking. */ // 将前驱节点的ws改为-1,原因在另一篇里面说过了 compareAndSetWaitStatus(pred, ws, Node.SIGNAL); } returnfalse; }
privatevoiddoReleaseShared(){ /* * 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 Node h = head; if (h != null && h != tail) { // 至少有2个Node,h不为null,也不为tail int ws = h.waitStatus; // 在await()中,h的ws已经被改为-1了 if (ws == Node.SIGNAL) { // 将h的ws改为0 if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) // 如果改失败,说明其他线程已经改过了 // 此时的h.next应该指向null了to help gc(之后会分析为啥h出队了) // 那么跳过这轮检查,重新拿head continue; // loop to recheck cases // unpark h的下一个Node对应的线程 unparkSuccessor(h); } // 这个else和本流程无关 elseif (ws == 0 && !compareAndSetWaitStatus(h, 0, Node.PROPAGATE)) continue; // loop on failed CAS } // 这个break非常巧妙,这里先留一个坑,下面再说等于head的退出条件 if (h == head) // loop if head changed break; } }
privatevoidunparkSuccessor(Node node){ /* * If status is negative (i.e., possibly needing signal) try * to clear in anticipation of signalling. It is OK if this * fails or if status is changed by waiting thread. */ int ws = node.waitStatus; // ws已经为0了,外面改过了 if (ws < 0) compareAndSetWaitStatus(node, ws, 0);
/* * Thread to unpark is held in successor, which is normally * just the next node. But if cancelled or apparently null, * traverse backwards from tail to find the actual * non-cancelled successor. */ // 取到“排队”的第一个线程为s Node s = node.next; if (s == null || s.waitStatus > 0) { // ws大于0这种情况我们先不讨论 s = null; for (Node t = tail; t != null && t != node; t = t.prev) if (t.waitStatus <= 0) s = t; } // 如果有排队线程,unpark唤醒它 if (s != null) LockSupport.unpark(s.thread); }
privatevoidsetHeadAndPropagate(Node node, int propagate){ Node h = head; // Record old head for check below // 将当前node设为head,head仍然为虚拟节点 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 > 0 || h == null || h.waitStatus < 0 || // 下面这些||操作是避免中间有别的线程改了head // 于是重新获取head和判断ws (h = head) == null || h.waitStatus < 0) { // node已经unpark完了,取node的下一个节点 Node s = node.next; // 下一个节点为null,或者不为空且为shared if (s == null || s.isShared()) doReleaseShared(); } }
privatevoiddoReleaseShared(){ /* * 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; // 如果h为null了,所有的都unpark了,跳到最底下break if (h != null && h != tail) { // 如果队列中还有线程,继续unpark下一个 int ws = h.waitStatus; if (ws == Node.SIGNAL) { if (!compareAndSetWaitStatus(h, Node.SIGNAL, 0)) continue; // loop to recheck cases unparkSuccessor(h); } elseif (ws == 0 && !compareAndSetWaitStatus(h, 0, Node.PROPAGATE)) continue; // loop on failed CAS } // 全部unpark结束,退出,所有线程的await()停止阻塞 if (h == head) // loop if head changed break; } }