- Timestamp:
- 05/17/11 14:02:33 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/Xml/AsyncBuffer.cs
r6211 r6220 2 2 using System.Collections; 3 3 using System.Collections.Generic; 4 using System.Linq;5 4 using System.Threading; 6 5 7 6 namespace HeuristicLab.Persistence.Default.Xml { 8 7 9 public class AsyncBuffer<T> : I Disposable, IEnumerable<T>, IEnumerable {8 public class AsyncBuffer<T> : IEnumerable<T>, IEnumerable { 10 9 11 10 private IEnumerator<T> it; … … 14 13 private object queueLock; 15 14 private AutoResetEvent elementAdded; 15 private ManualResetEvent done; 16 16 17 17 private Thread thread; 18 private ManualResetEventSlim done;19 18 20 19 private Exception exception; … … 30 29 queueLock = new object(); 31 30 elementAdded = new AutoResetEvent(false); 31 done = new ManualResetEvent(false); 32 32 thread = new Thread(Run); 33 done = new ManualResetEventSlim(false);34 33 thread.Start(); 35 34 } 36 35 37 36 private void Run() { 37 bool hasLock = false; 38 38 try { 39 39 while (it.MoveNext()) { … … 46 46 } catch (Exception x) { 47 47 exception = x; 48 } finally { 49 if (hasLock) 50 Monitor.Exit(queueLock); 48 51 } 49 52 done.Set(); 53 } 54 55 private IEnumerator<T> RealGetEnumerator(LockIndicator li) { 56 Monitor.Enter(queueLock, ref li.HasLock); 57 while (!done.WaitOne(0) || queue.Count > 0) { 58 while (queue.Count == 0 && !done.WaitOne(0)) { 59 li.HasLock = false; 60 Monitor.Exit(queueLock); 61 AutoResetEvent.WaitAny(new WaitHandle[] { done, elementAdded }); 62 Monitor.Enter(queueLock, ref li.HasLock); 63 } 64 if (queue.Count > 0) { 65 T element = queue.Dequeue(); 66 li.HasLock = false; 67 Monitor.Exit(queueLock); 68 yield return element; 69 Monitor.Enter(queueLock, ref li.HasLock); 70 } 71 } 72 li.HasLock = false; 73 Monitor.Exit(queueLock); 74 if (exception != null) 75 throw exception; 50 76 } 51 77 … … 61 87 } 62 88 } 63 64 private IEnumerator<T> RealGetEnumerator(LockIndicator li) {65 Monitor.Enter(queueLock, ref li.HasLock);66 while (!done.IsSet || queue.Any()) {67 while (!queue.Any() && !done.IsSet) {68 li.HasLock = false;69 Monitor.Exit(queueLock);70 AutoResetEvent.WaitAny(new WaitHandle[] { done.WaitHandle, elementAdded });71 Monitor.Enter(queueLock, ref li.HasLock);72 }73 if (queue.Any()) {74 T element = queue.Peek();75 li.HasLock = false;76 Monitor.Exit(queueLock);77 yield return element;78 Monitor.Enter(queueLock, ref li.HasLock);79 queue.Dequeue();80 }81 }82 li.HasLock = false;83 Monitor.Exit(queueLock);84 if (exception != null)85 throw exception;86 }87 89 #endregion 88 90 … … 92 94 } 93 95 #endregion 94 95 #region IDisposable Members96 public void Dispose() {97 if (elementAdded != null)98 elementAdded.Dispose();99 if (done != null)100 done.Dispose();101 }102 #endregion103 96 } 104 97 }
Note: See TracChangeset
for help on using the changeset viewer.