-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
507 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks.Dataflow; | ||
using BiDaFlow.Blocks; | ||
|
||
namespace BiDaFlow.Actors | ||
{ | ||
public static class SupervisedDataflowBlockExtensions | ||
{ | ||
private static readonly ConditionalWeakTable<object, Dictionary<Type, object>> s_sourceBlocks = new ConditionalWeakTable<object, Dictionary<Type, object>>(); | ||
private static readonly ConditionalWeakTable<object, Dictionary<Type, object>> s_targetBlocks = new ConditionalWeakTable<object, Dictionary<Type, object>>(); | ||
|
||
public static ISourceBlock<TSource> AsSourceBlock<T, TSource>(this SupervisedDataflowBlock<T> supervisedBlock) | ||
where T : class, ISourceBlock<TSource> | ||
{ | ||
throw new NotImplementedException(); | ||
|
||
var dic = s_sourceBlocks.GetOrCreateValue(supervisedBlock); | ||
|
||
lock(dic) | ||
{ | ||
if (dic.TryGetValue(typeof(TSource), out var obj)) | ||
return (ISourceBlock<TSource>)obj; | ||
|
||
var block = new TransformWithoutBufferBlock<TSource, TSource>(x => x, supervisedBlock._taskScheduler, CancellationToken.None); | ||
} | ||
} | ||
|
||
// TODO: AsSourceBlock, AsTargetBlock (and Actor), AsPropagatorBlock | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks.Dataflow; | ||
|
||
namespace BiDaFlow.Internal | ||
{ | ||
internal sealed class LinkedTarget2<T> | ||
{ | ||
public ITargetBlock<T> Target { get; } | ||
|
||
private readonly bool _propagateCompletion; | ||
private readonly Action<LinkedTarget2<T>>? _unlinkCallback; | ||
private int _remainingMessages; | ||
private int _unlinked; | ||
|
||
public LinkedTarget2(ITargetBlock<T> target, int maxMessages, bool propagateCompletion, Action<LinkedTarget2<T>>? unlinkCallback) | ||
{ | ||
if (maxMessages <= 0 && maxMessages != DataflowBlockOptions.Unbounded) | ||
throw new ArgumentOutOfRangeException(nameof(maxMessages)); | ||
|
||
this.Target = target ?? throw new ArgumentNullException(nameof(target)); | ||
this._remainingMessages = maxMessages; | ||
this._propagateCompletion = propagateCompletion; | ||
this._unlinkCallback = unlinkCallback; | ||
} | ||
|
||
public bool Unlinked => this._unlinked != 0; | ||
|
||
public void DecrementRemainingMessages() | ||
{ | ||
if (this._remainingMessages == DataflowBlockOptions.Unbounded) | ||
return; | ||
|
||
var newValue = this._remainingMessages - 1; | ||
if (newValue < 0) throw new InvalidOperationException("newValue is " + newValue); | ||
|
||
this._remainingMessages = newValue; | ||
|
||
if (newValue == 0) this.Unlink(); | ||
} | ||
|
||
public void Complete(Exception? exception) | ||
{ | ||
if (!this._propagateCompletion || this.Unlinked) | ||
return; | ||
|
||
if (exception == null) | ||
{ | ||
this.Target.Complete(); | ||
} | ||
else | ||
{ | ||
this.Target.Fault(exception); | ||
} | ||
} | ||
|
||
public void Unlink() | ||
{ | ||
if (Interlocked.Exchange(ref this._unlinked, 1) == 0) | ||
{ | ||
this._unlinkCallback?.Invoke(this); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.