You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2016/06/05 09:51:07 UTC

[06/19] lucene-solr:master: sequence numbers: removed synchronized in DocumentsWriterDeleteQueue.add

sequence numbers: removed synchronized in DocumentsWriterDeleteQueue.add


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/8b0b0c93
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/8b0b0c93
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/8b0b0c93

Branch: refs/heads/master
Commit: 8b0b0c934063c773b124cdcd560d7824de9ae5af
Parents: 39de689
Author: Mike McCandless <mi...@apache.org>
Authored: Thu May 26 05:39:12 2016 -0400
Committer: Mike McCandless <mi...@apache.org>
Committed: Thu May 26 05:39:12 2016 -0400

----------------------------------------------------------------------
 .../index/DocumentsWriterDeleteQueue.java       | 29 +++++++++++++++++---
 1 file changed, 25 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/8b0b0c93/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java
----------------------------------------------------------------------
diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java
index abb735d..f14c783 100644
--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java
+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java
@@ -150,10 +150,31 @@ final class DocumentsWriterDeleteQueue implements Accountable {
     return seqNo;
   }
 
-  synchronized long add(Node<?> newNode) {
-    tail.next = newNode;
-    tail = newNode;
-    return seqNo.getAndIncrement();
+  long add(Node<?> newNode) {
+    /*
+     * this non-blocking / 'wait-free' linked list add was inspired by Apache
+     * Harmony's ConcurrentLinkedQueue Implementation.
+     */
+    while (true) {
+      final Node<?> currentTail = tail;
+      final Node<?> tailNext = currentTail.next;
+      if (tail == currentTail && tailNext == null) {
+        /*
+         * we are in quiescent state and can try to insert the newNode to the
+         * current tail if we fail to insert we just retry the operation since
+         * somebody else has already added its newNode
+         */
+        if (currentTail.casNext(null, newNode)) {
+          /*
+           * now that we are done we need to advance the tail
+           */
+          long mySeqNo = seqNo.getAndIncrement();
+          boolean result = tailUpdater.compareAndSet(this, currentTail, newNode);
+          assert result;
+          return mySeqNo;
+        }
+      }
+    }
   }
 
   boolean anyChanges() {