BufferBlock.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // BufferBlock.cs
  2. //
  3. // Copyright (c) 2011 Jérémie "garuma" Laval
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. //
  24. #if NET_4_0 || MOBILE
  25. using System;
  26. using System.Threading.Tasks;
  27. using System.Collections.Generic;
  28. using System.Collections.Concurrent;
  29. namespace System.Threading.Tasks.Dataflow
  30. {
  31. public sealed class BufferBlock<T> : IPropagatorBlock<T, T>, ITargetBlock<T>, IDataflowBlock, ISourceBlock<T>, IReceivableSourceBlock<T>
  32. {
  33. static readonly DataflowBlockOptions defaultOptions = new DataflowBlockOptions ();
  34. DataflowBlockOptions dataflowBlockOptions;
  35. CompletionHelper compHelper = CompletionHelper.GetNew ();
  36. MessageBox<T> messageBox;
  37. MessageVault<T> vault;
  38. MessageOutgoingQueue<T> outgoing;
  39. BlockingCollection<T> messageQueue = new BlockingCollection<T> ();
  40. TargetBuffer<T> targets = new TargetBuffer<T> ();
  41. DataflowMessageHeader headers = DataflowMessageHeader.NewValid ();
  42. public BufferBlock () : this (defaultOptions)
  43. {
  44. }
  45. public BufferBlock (DataflowBlockOptions dataflowBlockOptions)
  46. {
  47. if (dataflowBlockOptions == null)
  48. throw new ArgumentNullException ("dataflowBlockOptions");
  49. this.dataflowBlockOptions = dataflowBlockOptions;
  50. this.messageBox = new PassingMessageBox<T> (messageQueue, compHelper, () => outgoing.IsCompleted, ProcessQueue, dataflowBlockOptions);
  51. this.outgoing = new MessageOutgoingQueue<T> (compHelper, () => messageQueue.IsCompleted);
  52. this.vault = new MessageVault<T> ();
  53. }
  54. public DataflowMessageStatus OfferMessage (DataflowMessageHeader messageHeader,
  55. T messageValue,
  56. ISourceBlock<T> source,
  57. bool consumeToAccept)
  58. {
  59. return messageBox.OfferMessage (this, messageHeader, messageValue, source, consumeToAccept);
  60. }
  61. public IDisposable LinkTo (ITargetBlock<T> target, bool unlinkAfterOne)
  62. {
  63. var result = targets.AddTarget (target, unlinkAfterOne);
  64. ProcessQueue ();
  65. return result;
  66. }
  67. public T ConsumeMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target, out bool messageConsumed)
  68. {
  69. return vault.ConsumeMessage (messageHeader, target, out messageConsumed);
  70. }
  71. public void ReleaseReservation (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
  72. {
  73. vault.ReleaseReservation (messageHeader, target);
  74. }
  75. public bool ReserveMessage (DataflowMessageHeader messageHeader, ITargetBlock<T> target)
  76. {
  77. return vault.ReserveMessage (messageHeader, target);
  78. }
  79. public bool TryReceive (Predicate<T> filter, out T item)
  80. {
  81. return outgoing.TryReceive (filter, out item);
  82. }
  83. public bool TryReceiveAll (out IList<T> items)
  84. {
  85. return outgoing.TryReceiveAll (out items);
  86. }
  87. void ProcessQueue ()
  88. {
  89. ITargetBlock<T> target;
  90. T input;
  91. while (messageQueue.TryTake (out input)) {
  92. if ((target = targets.Current) != null)
  93. target.OfferMessage (headers.Increment (), input, this, false);
  94. else
  95. outgoing.AddData (input);
  96. }
  97. if (!outgoing.IsEmpty && (target = targets.Current) != null)
  98. outgoing.ProcessForTarget (target, this, false, ref headers);
  99. }
  100. public void Complete ()
  101. {
  102. messageBox.Complete ();
  103. outgoing.Complete ();
  104. }
  105. public void Fault (Exception ex)
  106. {
  107. compHelper.Fault (ex);
  108. }
  109. public Task Completion {
  110. get {
  111. return compHelper.Completion;
  112. }
  113. }
  114. public int Count {
  115. get {
  116. return outgoing.Count;
  117. }
  118. }
  119. }
  120. }
  121. #endif