void register(SctpChannelImpl channel, ChannelFuture future) { boolean server = !(channel instanceof SctpClientChannel); Runnable registerTask = new RegisterTask(channel, future, server); notificationHandler = new SctpNotificationHandler(channel); Selector selector; synchronized (startStopLock) { if (!started) { // Open a selector if this worker didn't start yet. try { this.selector = selector = Selector.open(); } catch (Throwable t) { throw new ChannelException("Failed to create a selector.", t); } // Start the worker thread with the new Selector. boolean success = false; try { DeadLockProofWorker.start(executor, this); success = true; } finally { if (!success) { // Release the Selector if the execution fails. try { selector.close(); } catch (Throwable t) { if (logger.isWarnEnabled()) { logger.warn("Failed to close a selector.", t); } } this.selector = selector = null; // The method will return to the caller at this point. } } } else { // Use the existing selector if this worker has been started. selector = this.selector; } assert selector != null && selector.isOpen(); started = true; boolean offered = registerTaskQueue.offer(registerTask); assert offered; } if (wakenUp.compareAndSet(false, true)) { selector.wakeup(); } }
private void bind( SctpServerChannelImpl channel, ChannelFuture future, SocketAddress localAddress) { boolean bound = false; boolean bossStarted = false; try { channel.serverChannel.bind(localAddress, channel.getConfig().getBacklog()); bound = true; channel.setBound(); future.setSuccess(); fireChannelBound(channel, channel.getLocalAddress()); Executor bossExecutor = ((SctpServerSocketChannelFactory) channel.getFactory()).bossExecutor; DeadLockProofWorker.start(bossExecutor, new Boss(channel)); bossStarted = true; } catch (Throwable t) { future.setFailure(t); fireExceptionCaught(channel, t); } finally { if (!bossStarted && bound) { close(channel, future); } } }