privatestaticfinalintNOW=0; // for untimed poll, tryTransfer privatestaticfinalintASYNC=1; // for offer, put, add privatestaticfinalintSYNC=2; // for transfer, take privatestaticfinalintTIMED=3; // for timed poll, tryTransfer
privateExfer(E e, boolean haveData, int how, long nanos) { if (haveData && (e == null)) thrownewNullPointerException(); Nodes=null; // the node to append, if needed
retry: for (;;) { // restart on append race
for (Nodeh= head, p = h; p != null;) { // find & match first node //从head开始一直向后匹配 booleanisData= p.isData; Objectitem= p.item; if (item != p && (item != null) == isData) {//有效节点 if (isData == haveData) //节点与此次操作模式一致,无法匹配 break; if (p.casItem(item, e)) { //匹配成功,cas修改为指定元素 for (Nodeq= p; q != h;) { Noden= q.next; // update by 2 unless singleton if (head == h && casHead(h, n == null ? q : n)) { //更新head为匹配节点的next节点 //旧head节点指向自身等待回收 h.forgetNext(); break; } // advance and retry //cas失败,重新获取head if ((h = head) == null || (q = h.next) == null || !q.isMatched()) //如果head的next节点未被匹配,跳出循环,不更新head,即松弛度小于2 break; // unless slack < 2 } //唤醒节点上等待的线程 LockSupport.unpark(p.waiter); return LinkedTransferQueue.<E>cast(item); } } //匹配失败,继续向后查找节点 Noden= p.next; p = (p != n) ? n : (h = head); // Use head if p offlist }
//未找到匹配节点,吧当前节点假如到队列尾 if (how != NOW) { // No matches available if (s == null) s = newNode(e, haveData); //将新节点s添加到队列尾并返回s的前驱节点 Nodepred= tryAppend(s, haveData); if (pred == null) //与其它不同模式线程竞争是失败重新循环 continue retry; // lost race vs opposite mode if (how != ASYNC)//同步操作,等待匹配 return awaitMatch(s, pred, e, (how == TIMED), nanos); } return e; // not waiting } }
privateNodetryAppend(Node s, boolean haveData) { for (Nodet= tail, p = t;;) { // move p to last node and append Node n, u; // temps for reads of next & tail if (p == null && (p = head) == null) { //链表未初始化 if (casHead(null, s)) //将s作为head节点 return s; // initialize } elseif (p.cannotPrecede(haveData)) returnnull; // lost race vs opposite mode elseif ((n = p.next) != null) // not last; keep traversing p = p != t && t != (u = tail) ? (t = u) : // stale tail (p != n) ? n : null; // restart if off list elseif (!p.casNext(null, s)) p = p.next; // re-read on CAS failure else { if (p != t) { // update if slack now >= 2 while ((tail != t || !casTail(t, s)) && (t = tail) != null && (s = t.next) != null && // advance and retry (s = s.next) != null && s != t); } return p; } } }
finalvoidunsplice(Node pred, Node s) { s.forgetContents(); // forget unneeded fields /* * See above for rationale. Briefly: if pred still points to * s, try to unlink s. If s cannot be unlinked, because it is * trailing node or pred might be unlinked, and neither pred * nor s are head or offlist, add to sweepVotes, and if enough * votes have accumulated, sweep. */ if (pred != null && pred != s && pred.next == s) { Noden= s.next; if (n == null || (n != s && pred.casNext(s, n) && pred.isMatched())) { //解除s节点的链接 for (;;) { // check if at, or could be, head Nodeh= head; if (h == pred || h == s || h == null) return; // at head or list empty if (!h.isMatched()) break; Nodehn= h.next; if (hn == null) return; // now empty if (hn != h && casHead(h, hn))//更新head h.forgetNext(); // advance head } if (pred.next != pred && s.next != s) { // recheck if offlist for (;;) { // sweep now if enough votes intv= sweepVotes; if (v < SWEEP_THRESHOLD) { if (casSweepVotes(v, v + 1)) break; } elseif (casSweepVotes(v, 0)) { sweep(); break; } } } } } }
sweep
解除从头不遍历时遇到的已经被匹配的节点的链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
privatevoidsweep() { for (Nodep= head, s, n; p != null && (s = p.next) != null; ) { if (!s.isMatched()) // Unmatched nodes are never self-linked p = s; elseif ((n = s.next) == null) // trailing node is pinned break; elseif (s == n) // stale // No need to also check for p == s, since that implies s == n p = head; else p.casNext(s, n); } }