Home / Function/ SelectorTuple() — netty Function Reference

SelectorTuple() — netty Function Reference

Architecture documentation for the SelectorTuple() function in NioIoHandler.java from the netty codebase.

Function java Buffer Search calls 2 called by 1

Entity Profile

Dependency Diagram

graph TD
  d43dc98f_6738_d8a6_e838_daf73bfcfdca["SelectorTuple()"]
  db526e28_8aae_a182_b56b_dc12824d89f5["NioIoHandler"]
  d43dc98f_6738_d8a6_e838_daf73bfcfdca -->|defined in| db526e28_8aae_a182_b56b_dc12824d89f5
  d7459f93_7efd_7ef7_da95_0bb0f544b413["SelectorTuple()"]
  d7459f93_7efd_7ef7_da95_0bb0f544b413 -->|calls| d43dc98f_6738_d8a6_e838_daf73bfcfdca
  d7459f93_7efd_7ef7_da95_0bb0f544b413["SelectorTuple()"]
  d43dc98f_6738_d8a6_e838_daf73bfcfdca -->|calls| d7459f93_7efd_7ef7_da95_0bb0f544b413
  bfebd9e6_05a3_0eea_3b0f_b34d5870ab36["run()"]
  d43dc98f_6738_d8a6_e838_daf73bfcfdca -->|calls| bfebd9e6_05a3_0eea_3b0f_b34d5870ab36
  style d43dc98f_6738_d8a6_e838_daf73bfcfdca fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

transport/src/main/java/io/netty/channel/nio/NioIoHandler.java lines 143–234

    private SelectorTuple openSelector() {
        final Selector unwrappedSelector;
        try {
            unwrappedSelector = provider.openSelector();
        } catch (IOException e) {
            throw new ChannelException("failed to open a new selector", e);
        }

        if (DISABLE_KEY_SET_OPTIMIZATION) {
            return new SelectorTuple(unwrappedSelector);
        }

        Object maybeSelectorImplClass = AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                try {
                    return Class.forName(
                            "sun.nio.ch.SelectorImpl",
                            false,
                            PlatformDependent.getSystemClassLoader());
                } catch (Throwable cause) {
                    return cause;
                }
            }
        });

        if (!(maybeSelectorImplClass instanceof Class) ||
                // ensure the current selector implementation is what we can instrument.
                !((Class<?>) maybeSelectorImplClass).isAssignableFrom(unwrappedSelector.getClass())) {
            if (maybeSelectorImplClass instanceof Throwable) {
                Throwable t = (Throwable) maybeSelectorImplClass;
                logger.trace("failed to instrument a special java.util.Set into: {}", unwrappedSelector, t);
            }
            return new SelectorTuple(unwrappedSelector);
        }

        final Class<?> selectorImplClass = (Class<?>) maybeSelectorImplClass;
        final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();

        Object maybeException = AccessController.doPrivileged(new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                try {
                    Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
                    Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");

                    if (PlatformDependent.javaVersion() >= 9 && PlatformDependent.hasUnsafe()) {
                        // Let us try to use sun.misc.Unsafe to replace the SelectionKeySet.
                        // This allows us to also do this in Java9+ without any extra flags.
                        long selectedKeysFieldOffset = PlatformDependent.objectFieldOffset(selectedKeysField);
                        long publicSelectedKeysFieldOffset =
                                PlatformDependent.objectFieldOffset(publicSelectedKeysField);

                        if (selectedKeysFieldOffset != -1 && publicSelectedKeysFieldOffset != -1) {
                            PlatformDependent.putObject(
                                    unwrappedSelector, selectedKeysFieldOffset, selectedKeySet);
                            PlatformDependent.putObject(
                                    unwrappedSelector, publicSelectedKeysFieldOffset, selectedKeySet);
                            return null;
                        }
                        // We could not retrieve the offset, lets try reflection as last-resort.
                    }

                    Throwable cause = ReflectionUtil.trySetAccessible(selectedKeysField, true);
                    if (cause != null) {
                        return cause;
                    }
                    cause = ReflectionUtil.trySetAccessible(publicSelectedKeysField, true);
                    if (cause != null) {
                        return cause;
                    }

                    selectedKeysField.set(unwrappedSelector, selectedKeySet);
                    publicSelectedKeysField.set(unwrappedSelector, selectedKeySet);
                    return null;
                } catch (NoSuchFieldException | IllegalAccessException e) {
                    return e;
                }
            }
        });

Domain

Subdomains

Called By

Frequently Asked Questions

What does SelectorTuple() do?
SelectorTuple() is a function in the netty codebase, defined in transport/src/main/java/io/netty/channel/nio/NioIoHandler.java.
Where is SelectorTuple() defined?
SelectorTuple() is defined in transport/src/main/java/io/netty/channel/nio/NioIoHandler.java at line 143.
What does SelectorTuple() call?
SelectorTuple() calls 2 function(s): SelectorTuple, run.
What calls SelectorTuple()?
SelectorTuple() is called by 1 function(s): SelectorTuple.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free