| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- // 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();
- }
- }
- }
- class Disposable : IDisposable
- {
- private static Action s_nop = () => { };
- private Action _dispose;
- public Disposable(Action dispose)
- {
- _dispose = dispose;
- }
- public static IDisposable Create(IDisposable d1, IDisposable d2)
- {
- return new BinaryDisposable(d1, d2);
- }
- public void Dispose()
- {
- var dispose = Interlocked.Exchange(ref _dispose, s_nop);
- dispose();
- }
- }
- 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();
- }
- }
- }
- }
- }
|