All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hu.akarnokd.rxjava3.fibers.FlowableTransformFiberScheduler Maven / Gradle / Ivy

Go to download

Library to interoperate between RxJava 3 and Virtual Threads (aka Fibers, Project Loom)

There is a newer version: 0.0.18
Show newest version
/*
 * Copyright 2019 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package hu.akarnokd.rxjava3.fibers;

import java.util.Objects;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;

import org.reactivestreams.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.core.Scheduler.Worker;
import io.reactivex.rxjava3.internal.queue.SpscArrayQueue;
import io.reactivex.rxjava3.internal.util.BackpressureHelper;

final class FlowableTransformFiberScheduler extends Flowable
implements FlowableTransformer {

    final Flowable source;

    final FiberTransformer transformer;

    final Scheduler scheduler;

    final int prefetch;

    FlowableTransformFiberScheduler(Flowable source,
            FiberTransformer transformer, Scheduler scheduler, int prefetch) {
        this.source = source;
        this.transformer = transformer;
        this.scheduler = scheduler;
        this.prefetch = prefetch;
    }

    @Override
    public Publisher apply(Flowable upstream) {
        return new FlowableTransformFiberScheduler<>(upstream, transformer, scheduler, prefetch);
    }

    @Override
    protected void subscribeActual(Subscriber s) {
        var worker = scheduler.createWorker();

        var executor = Executors.newThreadExecutor(Thread.ofVirtual().scheduler(worker::schedule).factory());

        var parent = new WorkerTransformFiberSubscriber<>(s, transformer, worker, prefetch, executor);
        source.subscribe(parent);
        // Ignoring the Fiber because it can lead to awkward interruptions if cancelled
        executor.submit(parent);
    }

    static final class WorkerTransformFiberSubscriber extends TransformFiberSubscriber {

        private static final long serialVersionUID = 6360560993564811498L;

        final Worker worker;

        ExecutorService executor;

        WorkerTransformFiberSubscriber(Subscriber downstream,
                FiberTransformer transformer, Worker worker,
                int prefetch, ExecutorService executor) {
            super(downstream, transformer, prefetch);
            this.worker = worker;
            this.executor = executor;
        }

        @Override
        protected void cleanup() {
            worker.dispose();
            var e = executor;
            executor = null;
            if (e != null) {
                e.close();
            }
        }
    }

    abstract static class TransformFiberSubscriber extends AtomicLong
    implements FlowableSubscriber, Subscription, FiberEmitter, Callable {

        private static final long serialVersionUID = -4702456711290258571L;

        Subscriber downstream;

        final FiberTransformer transformer;

        final int prefetch;

        final AtomicLong requested;

        final ResumableFiber producerReady;

        final ResumableFiber consumerReady;

        final SpscArrayQueue queue;

        Subscription upstream;

        volatile boolean done;
        Throwable error;

        volatile boolean cancelled;

        static final Throwable STOP = new Throwable("Downstream cancelled");

        long produced;

        TransformFiberSubscriber(Subscriber downstream,
                FiberTransformer transformer,
                int prefetch) {
            this.downstream = downstream;
            this.transformer = transformer;
            this.prefetch = prefetch;
            this.requested = new AtomicLong();
            this.producerReady = new ResumableFiber();
            this.consumerReady = new ResumableFiber();
            this.queue = new SpscArrayQueue<>(prefetch);
        }

        @Override
        public void onSubscribe(Subscription s) {
            upstream = s;
            downstream.onSubscribe(this);
            s.request(prefetch);
        }

        @Override
        public void onNext(T t) {
            queue.offer(t);
            if (getAndIncrement() == 0) {
                producerReady.resume();
            }
        }

        @Override
        public void onError(Throwable t) {
            error = t;
            onComplete();
        }

        @Override
        public void onComplete() {
            done = true;
            if (getAndIncrement() == 0) {
                producerReady.resume();
            }
        }

        @Override
        public void emit(R item) throws Throwable {
            Objects.requireNonNull(item, "item is null");

            var p = produced;
            while (requested.get() == p && !cancelled) {
                consumerReady.await();
            }

            if (cancelled) {
                throw STOP;
            }

            downstream.onNext(item);

            produced = p + 1;
        }

        @Override
        public void request(long n) {
            BackpressureHelper.add(requested, n);
            consumerReady.resume();
        }

        @Override
        public void cancel() {
            cancelled = true;
            // cleanup(); don't kill the worker

            producerReady.resume();
            consumerReady.resume();
        }

        @Override
        public Void call() {
            try {
                try {
                    var consumed = 0L;
                    var limit = prefetch - (prefetch >> 2);
                    var wip = 0L;

                    while (!cancelled) {
                        var d = done;
                        var v = queue.poll();
                        var empty = v == null;

                        if (d && empty) {
                            var ex = error;
                            if (ex != null) {
                                downstream.onError(ex);
                            } else {
                                downstream.onComplete();
                            }
                            break;
                        }

                        if (!empty) {

                            if (++consumed == limit) {
                                consumed = 0;
                                upstream.request(limit);
                            }

                            transformer.transform(v, this);

                            continue;
                        }

                        wip = addAndGet(-wip);
                        if (wip == 0L) {
                            producerReady.await();
                        }
                    }
                } catch (Throwable ex) {
                    if (ex != STOP && !cancelled) {
                        upstream.cancel();
                        downstream.onError(ex);
                    }
                    return null;
                }
            } finally {
                queue.clear();
                downstream = null;
                cleanup();
            }
            return null;
        }

        protected abstract void cleanup();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy