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