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 io.netty.util.internal.PlatformDependent;
19  
20  import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
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  
29      static {
30          AtomicIntegerFieldUpdater<AbstractReferenceCounted> updater =
31                  PlatformDependent.newAtomicIntegerFieldUpdater(AbstractReferenceCounted.class, "refCnt");
32          if (updater == null) {
33              updater = AtomicIntegerFieldUpdater.newUpdater(AbstractReferenceCounted.class, "refCnt");
34          }
35          refCntUpdater = updater;
36      }
37  
38      private volatile int refCnt = 1;
39  
40      @Override
41      public final int refCnt() {
42          return refCnt;
43      }
44  
45      /**
46       * An unsafe operation intended for use by a subclass that sets the reference count of the buffer directly
47       */
48      protected final void setRefCnt(int refCnt) {
49          this.refCnt = refCnt;
50      }
51  
52      @Override
53      public ReferenceCounted retain() {
54          for (;;) {
55              int refCnt = this.refCnt;
56              if (refCnt == 0) {
57                  throw new IllegalReferenceCountException(0, 1);
58              }
59              if (refCnt == Integer.MAX_VALUE) {
60                  throw new IllegalReferenceCountException(Integer.MAX_VALUE, 1);
61              }
62              if (refCntUpdater.compareAndSet(this, refCnt, refCnt + 1)) {
63                  break;
64              }
65          }
66          return this;
67      }
68  
69      @Override
70      public ReferenceCounted retain(int increment) {
71          if (increment <= 0) {
72              throw new IllegalArgumentException("increment: " + increment + " (expected: > 0)");
73          }
74  
75          for (;;) {
76              int refCnt = this.refCnt;
77              if (refCnt == 0) {
78                  throw new IllegalReferenceCountException(0, 1);
79              }
80              if (refCnt > Integer.MAX_VALUE - increment) {
81                  throw new IllegalReferenceCountException(refCnt, increment);
82              }
83              if (refCntUpdater.compareAndSet(this, refCnt, refCnt + increment)) {
84                  break;
85              }
86          }
87          return this;
88      }
89  
90      @Override
91      public ReferenceCounted touch() {
92          return touch(null);
93      }
94  
95      @Override
96      public boolean release() {
97          for (;;) {
98              int refCnt = this.refCnt;
99              if (refCnt == 0) {
100                 throw new IllegalReferenceCountException(0, -1);
101             }
102 
103             if (refCntUpdater.compareAndSet(this, refCnt, refCnt - 1)) {
104                 if (refCnt == 1) {
105                     deallocate();
106                     return true;
107                 }
108                 return false;
109             }
110         }
111     }
112 
113     @Override
114     public boolean release(int decrement) {
115         if (decrement <= 0) {
116             throw new IllegalArgumentException("decrement: " + decrement + " (expected: > 0)");
117         }
118 
119         for (;;) {
120             int refCnt = this.refCnt;
121             if (refCnt < decrement) {
122                 throw new IllegalReferenceCountException(refCnt, -decrement);
123             }
124 
125             if (refCntUpdater.compareAndSet(this, refCnt, refCnt - decrement)) {
126                 if (refCnt == decrement) {
127                     deallocate();
128                     return true;
129                 }
130                 return false;
131             }
132         }
133     }
134 
135     /**
136      * Called once {@link #refCnt()} is equals 0.
137      */
138     protected abstract void deallocate();
139 }