View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.core5.reactor;
28  
29  import java.util.concurrent.atomic.AtomicInteger;
30  
31  final class IOWorkers {
32  
33      interface Selector {
34  
35          SingleCoreIOReactor next();
36  
37      }
38  
39      static Selector newSelector(final SingleCoreIOReactor[] dispatchers) {
40          return isPowerOfTwo(dispatchers.length)
41                          ? new PowerOfTwoSelector(dispatchers)
42                          : new GenericSelector(dispatchers);
43      }
44  
45      private static boolean isPowerOfTwo(final int val) {
46          return (val & -val) == val;
47      }
48  
49      private static void validate(final SingleCoreIOReactor dispatcher) {
50          if (dispatcher.getStatus() == IOReactorStatus.SHUT_DOWN) {
51              throw new IOReactorShutdownException("I/O reactor has been shut down");
52          }
53      }
54  
55      private static final class PowerOfTwoSelector implements Selector {
56  
57          private final AtomicInteger idx = new AtomicInteger(0);
58          private final SingleCoreIOReactor[] dispatchers;
59  
60          PowerOfTwoSelector(final SingleCoreIOReactor[] dispatchers) {
61              this.dispatchers = dispatchers;
62          }
63  
64          @Override
65          public SingleCoreIOReactor next() {
66              final SingleCoreIOReactor dispatcher = dispatchers[idx.getAndIncrement() & (dispatchers.length - 1)];
67              validate(dispatcher);
68              return dispatcher;
69          }
70      }
71  
72      private static final class GenericSelector implements Selector {
73  
74          private final AtomicInteger idx = new AtomicInteger(0);
75          private final SingleCoreIOReactor[] dispatchers;
76  
77          GenericSelector(final SingleCoreIOReactor[] dispatchers) {
78              this.dispatchers = dispatchers;
79          }
80  
81          @Override
82          public SingleCoreIOReactor next() {
83              final SingleCoreIOReactor dispatcher = dispatchers[(idx.getAndIncrement() & Integer.MAX_VALUE) % dispatchers.length];
84              validate(dispatcher);
85              return dispatcher;
86          }
87      }
88  
89  }