View Javadoc
1   /*
2    * Copyright 2013 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  package io.netty.util;
17  
18  import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
19  
20  import static io.netty.util.internal.ObjectUtil.checkPositive;
21  
22  /**
23   * Abstract base class for classes wants to implement {@link ReferenceCounted}.
24   */
25  public abstract class AbstractReferenceCounted implements ReferenceCounted {
26  
27      private static final AtomicIntegerFieldUpdater<AbstractReferenceCounted> refCntUpdater =
28              AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class, "refCnt");
29  
30      private volatile int refCnt = 1;
31  
32      @Override
33      public final int refCnt() {
34          return refCnt;
35      }
36  
37      /**
38       * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
39       */
40      protected final void setRefCnt(int refCnt) {
41          refCntUpdater.set(this, refCnt);
42      }
43  
44      @Override
45      public ReferenceCounted retain() {
46          return retain0(1);
47      }
48  
49      @Override
50      public ReferenceCounted retain(int increment) {
51          return retain0(checkPositive(increment, "increment"));
52      }
53  
54      private ReferenceCounted retain0(int increment) {
55          int oldRef = refCntUpdater.getAndAdd(this, increment);
56          if (oldRef <= 0 || oldRef + increment < oldRef) {
57              // Ensure we don't resurrect (which means the refCnt was 0) and also that we encountered an overflow.
58              refCntUpdater.getAndAdd(this, -increment);
59              throw new IllegalReferenceCountException(oldRef, increment);
60          }
61          return this;
62      }
63  
64      @Override
65      public boolean release() {
66          return release0(1);
67      }
68  
69      @Override
70      public boolean release(int decrement) {
71          return release0(checkPositive(decrement, "decrement"));
72      }
73  
74      private boolean release0(int decrement) {
75          int oldRef = refCntUpdater.getAndAdd(this, -decrement);
76          if (oldRef == decrement) {
77              deallocate();
78              return true;
79          } else if (oldRef < decrement || oldRef - decrement > oldRef) {
80              // Ensure we don't over-release, and avoid underflow.
81              refCntUpdater.getAndAdd(this, decrement);
82              throw new IllegalReferenceCountException(oldRef, decrement);
83          }
84          return false;
85      }
86  
87      /**
88       * Called once {@link #refCnt()} is equals 0.
89       */
90      protected abstract void deallocate();
91  }