Repository: netty/netty
File: common/src/main/java/io/netty/util/concurrent/FastThreadLocalThread.java (Java)

Code before:
        if (currentThread() instanceof FastThreadLocalThread) {
            throw new IllegalStateException("Caller is a real FastThreadLocalThread");
        }
        fallbackThreads.updateAndGet(arr -> {
            if (arr == null) {
                return new long[] { id };
            }
            int index = Arrays.binarySearch(arr, id);
            if (index >= 0) {
                throw new IllegalStateException("Reentrant call to run()");
            }
            index = ~index;
            long[] next = new long[arr.length + 1];
            System.arraycopy(arr, 0, next, 0, index);
            next[index] = id;
            System.arraycopy(arr, index, next, index + 1, arr.length - index);
            return next;
        });
        try {
            runnable.run();
        } finally {
            fallbackThreads.getAndUpdate(arr -> {
                if (arr == null) {
                    return null;
                }
                if (arr.length == 1 && arr[0] == id) {
                    return null;
                }
                int index = Arrays.binarySearch(arr, id);
                if (index < 0) {
                    return arr;
                }
                long[] next = new long[arr.length - 1];
                System.arraycopy(arr, 0, next, 0, index);
                System.arraycopy(arr, index + 1, next, index, arr.length - index - 1);
                return next;
            });
            FastThreadLocal.removeAll();
        }
    }

    /**
     * Query whether this thread is allowed to perform blocking calls or not.
     * {@link FastThreadLocalThread}s are often used in event-loops, where blocking calls are forbidden in order to
     * prevent event-loop stalls, so this method returns {@code false} by default.
     * <p>
     * Subclasses of {@link FastThreadLocalThread} can override this method if they are not meant to be used for
     * running event-loops.
     *
     * @return {@code false}, unless overriden by a subclass.
     */

Review comment (suggestion):
```suggestion
                if (arr == null || (arr.length == 1 && arr[0] == id)) {
```

Code after:
        }
        long id = current.getId();
        fallbackThreads.updateAndGet(arr -> {
            if (arr == null) {
                return new long[] { id };
            }
            int index = Arrays.binarySearch(arr, id);
            if (index >= 0) {
                throw new IllegalStateException("Reentrant call to run()");
            }
            index = ~index; // same as -(index + 1)
            long[] next = new long[arr.length + 1];
            System.arraycopy(arr, 0, next, 0, index);
            next[index] = id;
            System.arraycopy(arr, index, next, index + 1, arr.length - index);
            return next;
        });
        try {
            runnable.run();
        } finally {
            fallbackThreads.getAndUpdate(arr -> {
                if (arr == null || (arr.length == 1 && arr[0] == id)) {
                    return null;
                }
                int index = Arrays.binarySearch(arr, id);
                if (index < 0) {
                    return arr;
                }
                long[] next = new long[arr.length - 1];
                System.arraycopy(arr, 0, next, 0, index);
                System.arraycopy(arr, index + 1, next, index, arr.length - index - 1);
                return next;
            });
            FastThreadLocal.removeAll();
        }
    }

    /**
     * Query whether this thread is allowed to perform blocking calls or not.
     * {@link FastThreadLocalThread}s are often used in event-loops, where blocking calls are forbidden in order to
     * prevent event-loop stalls, so this method returns {@code false} by default.
     * <p>
     * Subclasses of {@link FastThreadLocalThread} can override this method if they are not meant to be used for
     * running event-loops.
     *
     * @return {@code false}, unless overriden by a subclass.
     */
    public boolean permitBlockingCalls() {
        return false;
    }
}