View Javadoc
1   /*
2    * Copyright 2018 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    *   https://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.netty5.channel.local;
17  
18  import io.netty5.channel.Channel;
19  import io.netty5.channel.IoExecutionContext;
20  import io.netty5.channel.IoHandle;
21  import io.netty5.channel.IoHandler;
22  import io.netty5.channel.IoHandlerFactory;
23  import io.netty5.util.internal.StringUtil;
24  
25  import java.util.HashSet;
26  import java.util.Set;
27  import java.util.concurrent.locks.LockSupport;
28  
29  /**
30   * {@link IoHandler} implementation for {@link LocalChannel} and {@link LocalServerChannel}.
31   */
32  public final class LocalHandler implements IoHandler {
33      private final Set<LocalChannelUnsafe> registeredChannels = new HashSet<>(64);
34      private volatile Thread executionThread;
35  
36      private LocalHandler() { }
37  
38      /**
39       * Returns a new {@link IoHandlerFactory} that creates {@link LocalHandler} instances.
40       */
41      public static IoHandlerFactory newFactory() {
42          return LocalHandler::new;
43      }
44  
45      private static LocalChannelUnsafe cast(IoHandle handle) {
46          if (handle instanceof LocalChannelUnsafe) {
47              return (LocalChannelUnsafe) handle;
48          }
49          throw new IllegalArgumentException("IoHandle of type " + StringUtil.simpleClassName(handle) + " not supported");
50      }
51  
52      @Override
53      public int run(IoExecutionContext runner) {
54          if (executionThread == null) {
55              executionThread = Thread.currentThread();
56          }
57          if (runner.canBlock()) {
58              // Just block until there is a task ready to process or wakeup(...) is called.
59              LockSupport.parkNanos(this, runner.delayNanos(System.nanoTime()));
60          }
61          return 0;
62      }
63  
64      @Override
65      public void wakeup(boolean inEventLoop) {
66          if (!inEventLoop) {
67              Thread thread = executionThread;
68              if (thread != null) {
69                  // Wakeup if we block at the moment.
70                  LockSupport.unpark(thread);
71              }
72          }
73      }
74  
75      @Override
76      public void prepareToDestroy() {
77          for (LocalChannelUnsafe unsafe : registeredChannels) {
78              unsafe.closeTransportNow();
79          }
80          registeredChannels.clear();
81      }
82  
83      @Override
84      public void destroy() {
85      }
86  
87      @Override
88      public void register(IoHandle handle) {
89          LocalChannelUnsafe unsafe = cast(handle);
90          if (registeredChannels.add(unsafe)) {
91              unsafe.registerTransportNow();
92          }
93      }
94  
95      @Override
96      public void deregister(IoHandle handle) {
97          LocalChannelUnsafe unsafe = cast(handle);
98          if (registeredChannels.remove(unsafe)) {
99              unsafe.deregisterTransportNow();
100         }
101     }
102 
103     @Override
104     public boolean isCompatible(Class<? extends IoHandle> handleType) {
105         return LocalChannelUnsafe.class.isAssignableFrom(handleType);
106     }
107 }