Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Core/Reactive/Concurrency/Synchronization.Synchronize.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 #if !NO_PERF
4 using System;
5
6 namespace System.Reactive.Concurrency
7 {
8 class Synchronize<TSource> : Producer<TSource>
9 {
10 private readonly IObservable<TSource> _source;
11 private readonly object _gate;
12
13 public Synchronize(IObservable<TSource> source, object gate)
14 {
15 _source = source;
16 _gate = gate;
17 }
18
19 public Synchronize(IObservable<TSource> source)
20 {
21 _source = source;
22 }
23
24 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "2", Justification = "Visibility restricted to friend assemblies. Those should be correct by inspection.")]
25 protected override IDisposable Run(IObserver<TSource> observer, IDisposable cancel, Action<IDisposable> setSink)
26 {
27 var sink = new _(this, observer, cancel);
28 setSink(sink);
29 return _source.SubscribeSafe(sink);
30 }
31
32 class _ : Sink<TSource>, IObserver<TSource>
33 {
34 private readonly Synchronize<TSource> _parent;
35 private readonly object _gate;
36
37 public _(Synchronize<TSource> parent, IObserver<TSource> observer, IDisposable cancel)
38 : base(observer, cancel)
39 {
40 _parent = parent;
41 _gate = _parent._gate ?? new object();
42 }
43
44 public void OnNext(TSource value)
45 {
46 lock (_gate)
47 {
48 base._observer.OnNext(value);
49 }
50 }
51
52 public void OnError(Exception error)
53 {
54 lock (_gate)
55 {
56 base._observer.OnError(error);
57 base.Dispose();
58 }
59 }
60
61 public void OnCompleted()
62 {
63 lock (_gate)
64 {
65 base._observer.OnCompleted();
66 base.Dispose();
67 }
68 }
69 }
70 }
71 }
72 #endif