| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 | // Licensed to the .NET Foundation under one or more agreements.// The .NET Foundation licenses this file to you under the Apache 2.0 License.// See the LICENSE file in the project root for more information. using System;using System.Collections.Generic;using System.Reactive.Disposables;using System.Text;using System.Threading;using System.Linq;namespace System.Reactive.Linq.ObservableImpl{    internal sealed class AmbManyArray<T> : BasicProducer<T>    {        readonly IObservable<T>[] sources;        public AmbManyArray(IObservable<T>[] sources)        {            this.sources = sources;        }        protected override IDisposable Run(IObserver<T> observer)        {            return AmbCoordinator<T>.Create(observer, sources);        }    }    internal sealed class AmbManyEnumerable<T> : BasicProducer<T>    {        readonly IEnumerable<IObservable<T>> sources;        public AmbManyEnumerable(IEnumerable<IObservable<T>> sources)        {            this.sources = sources;        }        protected override IDisposable Run(IObserver<T> observer)        {            var sourcesEnumerable = this.sources;            var sources = default(IObservable<T>[]);            try            {                sources = sourcesEnumerable.ToArray();            }            catch (Exception ex)            {                observer.OnError(ex);                return Disposable.Empty;            }            return AmbCoordinator<T>.Create(observer, sources);        }    }    internal sealed class AmbCoordinator<T> : IDisposable    {        readonly IObserver<T> downstream;        readonly InnerObserver[] observers;        int winner;        internal AmbCoordinator(IObserver<T> downstream, int n)        {            this.downstream = downstream;            var o = new InnerObserver[n];            for (int i = 0; i < n; i++)            {                o[i] = new InnerObserver(this, i);            }            observers = o;            Volatile.Write(ref winner, -1);        }        internal static IDisposable Create(IObserver<T> observer, IObservable<T>[] sources)        {            var n = sources.Length;            if (n == 0)            {                observer.OnCompleted();                return Disposable.Empty;            }            if (n == 1)            {                return sources[0].Subscribe(observer);            }            var parent = new AmbCoordinator<T>(observer, n);            parent.Subscribe(sources);            return parent;        }        internal void Subscribe(IObservable<T>[] sources)        {            for (var i = 0; i < observers.Length; i++)            {                var inner = Volatile.Read(ref observers[i]);                if (inner == null)                {                    break;                }                inner.OnSubscribe(sources[i].Subscribe(inner));            }        }        public void Dispose()        {            for (var i = 0; i < observers.Length; i++)            {                Interlocked.Exchange(ref observers[i], null)?.Dispose();            }        }        bool TryWin(int index)        {            if (Volatile.Read(ref winner) == -1 && Interlocked.CompareExchange(ref winner, index, -1) == -1)            {                for (var i = 0; i < observers.Length; i++)                {                    if (index != i)                    {                        Interlocked.Exchange(ref observers[i], null)?.Dispose();                    }                }                return true;            }            return false;        }        internal sealed class InnerObserver : IObserver<T>, IDisposable        {            readonly IObserver<T> downstream;            readonly AmbCoordinator<T> parent;            readonly int index;            IDisposable upstream;            bool won;            public InnerObserver(AmbCoordinator<T> parent, int index)            {                this.downstream = parent.downstream;                this.parent = parent;                this.index = index;            }            public void Dispose()            {                Interlocked.Exchange(ref upstream, BooleanDisposable.True)?.Dispose();            }            public void OnCompleted()            {                if (won)                {                    downstream.OnCompleted();                }                else                if (parent.TryWin(index))                {                    won = true;                    downstream.OnCompleted();                }                Dispose();            }            public void OnError(Exception error)            {                if (won)                {                    downstream.OnError(error);                }                else                if (parent.TryWin(index))                {                    won = true;                    downstream.OnError(error);                }                Dispose();            }            public void OnNext(T value)            {                if (won)                {                    downstream.OnNext(value);                }                else                if (parent.TryWin(index))                {                    won = true;                    downstream.OnNext(value);                } else                {                    Dispose();                }            }            internal void OnSubscribe(IDisposable d)            {                if (Interlocked.CompareExchange(ref upstream, d, null) != null)                {                    d?.Dispose();                }            }        }    }}
 |