Home / Function/ run() — netty Function Reference

run() — netty Function Reference

Architecture documentation for the run() function in KQueueIoHandler.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb["run()"]
  75a3ba71_0590_fec0_e352_8a91c916c5af["KQueueIoHandler"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|defined in| 75a3ba71_0590_fec0_e352_8a91c916c5af
  bcc78562_0103_1a86_1771_83c771b0359c["kqueueWait()"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|calls| bcc78562_0103_1a86_1771_83c771b0359c
  22d22a93_4930_1c25_d832_877e2ee987af["wakeup()"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|calls| 22d22a93_4930_1c25_d832_877e2ee987af
  8470ab15_e624_678a_adf0_3cfeeb5354c6["wakeup0()"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|calls| 8470ab15_e624_678a_adf0_3cfeeb5354c6
  7640e0e3_85f5_efbd_4f2c_d9c5ed5c87a0["processReady()"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|calls| 7640e0e3_85f5_efbd_4f2c_d9c5ed5c87a0
  9096967a_5239_54dc_5c93_bfaffd5857cb["handleLoopException()"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|calls| 9096967a_5239_54dc_5c93_bfaffd5857cb
  87233553_29d8_ea69_ab7a_ba11b941aba3["processCancelledRegistrations()"]
  fa6e02de_7726_4944_12b6_ecd3e40bbdcb -->|calls| 87233553_29d8_ea69_ab7a_ba11b941aba3
  style fa6e02de_7726_4944_12b6_ecd3e40bbdcb fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueIoHandler.java lines 218–298

    @Override
    public int run(IoHandlerContext context) {
        int handled = 0;
        try {
            int strategy = selectStrategy.calculateStrategy(selectNowSupplier, !context.canBlock());
            switch (strategy) {
                case SelectStrategy.CONTINUE:
                    if (context.shouldReportActiveIoTime()) {
                        context.reportActiveIoTime(0); // Report zero as we did no I/O.
                    }
                    return 0;

                case SelectStrategy.BUSY_WAIT:
                    // fall-through to SELECT since the busy-wait is not supported with kqueue

                case SelectStrategy.SELECT:
                    strategy = kqueueWait(context, WAKEN_UP_UPDATER.getAndSet(this, 0) == 1);

                    // 'wakenUp.compareAndSet(false, true)' is always evaluated
                    // before calling 'selector.wakeup()' to reduce the wake-up
                    // overhead. (Selector.wakeup() is an expensive operation.)
                    //
                    // However, there is a race condition in this approach.
                    // The race condition is triggered when 'wakenUp' is set to
                    // true too early.
                    //
                    // 'wakenUp' is set to true too early if:
                    // 1) Selector is waken up between 'wakenUp.set(false)' and
                    //    'selector.select(...)'. (BAD)
                    // 2) Selector is waken up between 'selector.select(...)' and
                    //    'if (wakenUp.get()) { ... }'. (OK)
                    //
                    // In the first case, 'wakenUp' is set to true and the
                    // following 'selector.select(...)' will wake up immediately.
                    // Until 'wakenUp' is set to false again in the next round,
                    // 'wakenUp.compareAndSet(false, true)' will fail, and therefore
                    // any attempt to wake up the Selector will fail, too, causing
                    // the following 'selector.select(...)' call to block
                    // unnecessarily.
                    //
                    // To fix this problem, we wake up the selector again if wakenUp
                    // is true immediately after selector.select(...).
                    // It is inefficient in that it wakes up the selector for both
                    // the first case (BAD - wake-up required) and the second case
                    // (OK - no wake-up required).

                    if (wakenUp == 1) {
                        wakeup0();
                    }
                    // fall-through
                default:
            }

            if (strategy > 0) {
                handled = strategy;
                if (context.shouldReportActiveIoTime()) {
                    // The Timer starts after the blocking kqueueWait() call returns with events.
                    long activeIoStartTimeNanos = System.nanoTime();
                    processReady(strategy);
                    long activeIoEndTimeNanos = System.nanoTime();
                    context.reportActiveIoTime(activeIoEndTimeNanos - activeIoStartTimeNanos);
                } else {
                    processReady(strategy);
                }
            } else if (context.shouldReportActiveIoTime()) {
                context.reportActiveIoTime(0);
            }

            if (allowGrowing && strategy == eventList.capacity()) {
                //increase the size of the array as we needed the whole space for the events
                eventList.realloc(false);
            }
        } catch (Error e) {
            throw e;
        } catch (Throwable t) {
            handleLoopException(t);
        } finally {
            processCancelledRegistrations();
        }
        return handled;
    }

Domain

Subdomains

Frequently Asked Questions

What does run() do?
run() is a function in the netty codebase, defined in transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueIoHandler.java.
Where is run() defined?
run() is defined in transport-classes-kqueue/src/main/java/io/netty/channel/kqueue/KQueueIoHandler.java at line 218.
What does run() call?
run() calls 6 function(s): handleLoopException, kqueueWait, processCancelledRegistrations, processReady, wakeup, wakeup0.

Analyze Your Own Codebase

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

Try Supermodel Free