查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  package io.netty.util.internal.shaded.org.jctools.queues;
15  
16  import io.netty.util.internal.shaded.org.jctools.util.InternalAPI;
17  
18  @InternalAPI
19  public final class IndexedQueueSizeUtil
20  {
21      public static int size(IndexedQueue iq)
22      {
23          /*
24           * It is possible for a thread to be interrupted or reschedule between the read of the producer and
25           * consumer indices, therefore protection is required to ensure size is within valid range. In the
26           * event of concurrent polls/offers to this method the size is OVER estimated as we read consumer
27           * index BEFORE the producer index.
28           */
29          long after = iq.lvConsumerIndex();
30          long size;
31          while (true)
32          {
33              final long before = after;
34              final long currentProducerIndex = iq.lvProducerIndex();
35              after = iq.lvConsumerIndex();
36              if (before == after)
37              {
38                  size = (currentProducerIndex - after);
39                  break;
40              }
41          }
42          // Long overflow is impossible (), so size is always positive. Integer overflow is possible for the unbounded
43          // indexed queues.
44          if (size > Integer.MAX_VALUE)
45          {
46              return Integer.MAX_VALUE;
47          }
48          else
49          {
50              return (int) size;
51          }
52      }
53  
54      public static boolean isEmpty(IndexedQueue iq)
55      {
56          // Order matters!
57          // Loading consumer before producer allows for producer increments after consumer index is read.
58          // This ensures this method is conservative in it's estimate. Note that as this is an MPMC there is
59          // nothing we can do to make this an exact method.
60          return (iq.lvConsumerIndex() == iq.lvProducerIndex());
61      }
62  
63      @InternalAPI
64      public interface IndexedQueue
65      {
66          long lvConsumerIndex();
67  
68          long lvProducerIndex();
69      }
70  }