View Javadoc
1   /*
2    * Copyright 2014 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package io.netty.util.internal;
18  
19  import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
20  
21  public abstract class MpscLinkedQueueNode<T> {
22  
23      @SuppressWarnings("rawtypes")
24      private static final AtomicReferenceFieldUpdater<MpscLinkedQueueNode, MpscLinkedQueueNode> nextUpdater;
25  
26      static {
27          @SuppressWarnings("rawtypes")
28          AtomicReferenceFieldUpdater<MpscLinkedQueueNode, MpscLinkedQueueNode> u;
29  
30          u = PlatformDependent.newAtomicReferenceFieldUpdater(MpscLinkedQueueNode.class, "next");
31          if (u == null) {
32              u = AtomicReferenceFieldUpdater.newUpdater(MpscLinkedQueueNode.class, MpscLinkedQueueNode.class, "next");
33          }
34          nextUpdater = u;
35      }
36  
37      @SuppressWarnings("unused")
38      private volatile MpscLinkedQueueNode<T> next;
39  
40      final MpscLinkedQueueNode<T> next() {
41          return next;
42      }
43  
44      final void setNext(final MpscLinkedQueueNode<T> newNext) {
45          // Similar to 'next = newNext', but slightly faster (storestore vs loadstore)
46          // See: http://robsjava.blogspot.com/2013/06/a-faster-volatile.html
47          nextUpdater.lazySet(this, newNext);
48      }
49  
50      public abstract T value();
51  
52      /**
53       * Sets the element this node contains to {@code null} so that the node can be used as a tombstone.
54       */
55      protected T clearMaybe() {
56          return value();
57      }
58  
59      /**
60       * Unlink to allow GC'ed
61       */
62      void unlink() {
63          setNext(null);
64      }
65  }