| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 | // 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.Threading;namespace System.Linq{    class CancellationTokenDisposable : IDisposable    {        private readonly CancellationTokenSource cts = new CancellationTokenSource();        public CancellationToken Token        {            get            {                return cts.Token;            }        }        public void Dispose()        {            if (!cts.IsCancellationRequested)            {                cts.Cancel();            }        }    }    static class Disposable    {        public static IDisposable Create(IDisposable d1, IDisposable d2)        {            return new BinaryDisposable(d1, d2);        }        public static IDisposable Create(Action action)        {            return new AnonymousDisposable(action);        }    }    class BinaryDisposable : IDisposable    {        private IDisposable _d1;        private IDisposable _d2;        public BinaryDisposable(IDisposable d1, IDisposable d2)        {            _d1 = d1;            _d2 = d2;        }        public void Dispose()        {            var d1 = Interlocked.Exchange(ref _d1, null);            if (d1 != null)            {                d1.Dispose();                var d2 = Interlocked.Exchange(ref _d2, null);                if (d2 != null)                {                    d2.Dispose();                }            }        }    }    class AnonymousDisposable : IDisposable    {        private Action _action;        public AnonymousDisposable(Action action)        {            _action = action;        }        public void Dispose() => _action();    }}
 |