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  
28  package org.apache.hc.core5.reactor;
29  
30  import java.util.concurrent.TimeUnit;
31  import java.util.concurrent.atomic.AtomicReference;
32  
33  import org.apache.hc.core5.io.CloseMode;
34  import org.apache.hc.core5.io.Closer;
35  import org.apache.hc.core5.util.Args;
36  import org.apache.hc.core5.util.TimeValue;
37  
38  class MultiCoreIOReactor implements IOReactor {
39  
40      private final IOReactor[] ioReactors;
41      private final Thread[] threads;
42      private final AtomicReference<IOReactorStatus> status;
43  
44      MultiCoreIOReactor(final IOReactor[] ioReactors, final Thread[] threads) {
45          super();
46          this.ioReactors = ioReactors.clone();
47          this.threads = threads.clone();
48          this.status = new AtomicReference<>(IOReactorStatus.INACTIVE);
49      }
50  
51      @Override
52      public IOReactorStatus getStatus() {
53          return this.status.get();
54      }
55  
56      /**
57       * Activates all worker I/O reactors.
58       * The I/O main reactor will start reacting to I/O events and triggering
59       * notification methods. The worker I/O reactor in their turn will start
60       * reacting to I/O events and dispatch I/O event notifications to the
61       * {@link IOEventHandler} associated with the given I/O session.
62       */
63      public final void start() {
64          if (this.status.compareAndSet(IOReactorStatus.INACTIVE, IOReactorStatus.ACTIVE)) {
65              for (int i = 0; i < this.threads.length; i++) {
66                  this.threads[i].start();
67              }
68          }
69      }
70  
71      @Override
72      public final void initiateShutdown() {
73          if (this.status.compareAndSet(IOReactorStatus.ACTIVE, IOReactorStatus.SHUTTING_DOWN)) {
74              for (int i = 0; i < this.ioReactors.length; i++) {
75                  final IOReactor ioReactor = this.ioReactors[i];
76                  ioReactor.initiateShutdown();
77              }
78          }
79      }
80  
81      @Override
82      public final void awaitShutdown(final TimeValue waitTime) throws InterruptedException {
83          Args.notNull(waitTime, "Wait time");
84          final long deadline = System.currentTimeMillis() + waitTime.toMillis();
85          long remaining = waitTime.toMillis();
86          for (int i = 0; i < this.ioReactors.length; i++) {
87              final IOReactor ioReactor = this.ioReactors[i];
88              if (ioReactor.getStatus().compareTo(IOReactorStatus.SHUT_DOWN) < 0) {
89                  ioReactor.awaitShutdown(TimeValue.of(remaining, TimeUnit.MILLISECONDS));
90                  remaining = deadline - System.currentTimeMillis();
91                  if (remaining <= 0) {
92                      return;
93                  }
94              }
95          }
96          for (int i = 0; i < this.threads.length; i++) {
97              final Thread thread = this.threads[i];
98              thread.join(remaining);
99              remaining = deadline - System.currentTimeMillis();
100             if (remaining <= 0) {
101                 return;
102             }
103         }
104     }
105 
106     @Override
107     public final void close(final CloseMode closeMode) {
108         final IOReactorStatus currentStatus = this.status.get();
109         if (currentStatus == IOReactorStatus.INACTIVE || currentStatus == IOReactorStatus.SHUT_DOWN) {
110             return;
111         }
112         if (closeMode == CloseMode.GRACEFUL) {
113             initiateShutdown();
114             try {
115                 awaitShutdown(TimeValue.ofSeconds(5));
116             } catch (final InterruptedException e) {
117                 Thread.currentThread().interrupt();
118             }
119         } else {
120             this.status.compareAndSet(IOReactorStatus.ACTIVE, IOReactorStatus.SHUTTING_DOWN);
121             for (int i = 0; i < this.ioReactors.length; i++) {
122                 Closer.close(this.ioReactors[i], CloseMode.IMMEDIATE);
123             }
124             for (int i = 0; i < this.threads.length; i++) {
125                 this.threads[i].interrupt();
126             }
127         }
128         this.status.set(IOReactorStatus.SHUT_DOWN);
129     }
130 
131     @Override
132     public final void close() {
133         close(CloseMode.GRACEFUL);
134     }
135 
136     @Override
137     public String toString() {
138         return super.toString() + " [status=" + status + "]";
139     }
140 
141 }