##// END OF EJS Templates
fixed blocking queue
cin -
r139:041b77711262 v2
parent child
Show More
@@ -23,10 +23,13 namespace Implab.Parallels {
23 23
24 24 public T GetItem(int timeout) {
25 25 T item;
26
27 if (!TryDequeue(out item)) {
26 28 var t1 = Environment.TickCount;
27 29 var dt = timeout;
30
31 lock (m_lock) {
28 32 while (!TryDequeue(out item)) {
29 lock (m_lock)
30 33 if (!Monitor.Wait(m_lock, dt))
31 34 throw new TimeoutException();
32 35 if (timeout >= 0) {
@@ -35,14 +38,18 namespace Implab.Parallels {
35 38 throw new TimeoutException();
36 39 }
37 40 }
41 }
42 }
38 43 return item;
39 44 }
40 45
41 46 public T GetItem() {
42 47 T item;
48 if (!TryDequeue(out item))
49 lock (m_lock) {
43 50 while (!TryDequeue(out item))
44 lock (m_lock)
45 51 Monitor.Wait(m_lock);
52 }
46 53 return item;
47 54 }
48 55
@@ -51,18 +58,24 namespace Implab.Parallels {
51 58
52 59 var buffer = new T[max];
53 60 int actual;
61 if (!TryDequeueRange(buffer, 0, max, out actual)) {
54 62 var t1 = Environment.TickCount;
55 63 var dt = timeout;
64
65 lock (m_lock) {
56 66 while (!TryDequeueRange(buffer,0,max,out actual)) {
57 lock (m_lock)
67
58 68 if (!Monitor.Wait(m_lock, dt))
59 69 throw new TimeoutException();
70
60 71 if (timeout >= 0) {
61 72 dt = timeout - Environment.TickCount + t1;
62 73 if (dt < 0)
63 74 throw new TimeoutException();
64 75 }
65 76 }
77 }
78 }
66 79
67 80 var data = new T[actual];
68 81 Array.Copy(buffer, data, actual);
@@ -74,8 +87,9 namespace Implab.Parallels {
74 87
75 88 var buffer = new T[max];
76 89 int actual;
90 if (!TryDequeueRange(buffer, 0, max, out actual))
91 lock (m_lock)
77 92 while (!TryDequeueRange(buffer, 0, max, out actual))
78 lock (m_lock)
79 93 Monitor.Wait(m_lock);
80 94
81 95 var data = new T[actual];
General Comments 0
You need to be logged in to leave comments. Login now