diff --git a/Implab.Test/AsyncTests.cs b/Implab.Test/AsyncTests.cs --- a/Implab.Test/AsyncTests.cs +++ b/Implab.Test/AsyncTests.cs @@ -244,7 +244,7 @@ namespace Implab.Test { [TestMethod] public void ChainedMapTest() { - using (var pool = new WorkerPool(4,4,0)) { + using (var pool = new WorkerPool(0,100,0)) { int count = 10000; double[] args = new double[count]; @@ -255,7 +255,7 @@ namespace Implab.Test { var t = Environment.TickCount; var res = args - .ChainedMap( + .ChainedMap2( x => pool.Invoke( () => Math.Sin(x * x) ), diff --git a/Implab.v11.suo b/Implab.v11.suo index 1a41f7b2d0cb05a0444b1048004dea186d59b1e0..12ae8b2c29f54ee7562c00d4b4e5890f43c7d69a GIT binary patch literal 114688 zc%1EB3wRsVmA=aR;gAPO${PcLLa-Fc582KGBFk11VkgFO3=1ZSERP+lHzhol@}@}xbfKkbXg6)yHX*QW>BsKJhF#zTTH-zD&Rl6U8c8#f zY)jU?{*LFNJ9F;y+;h+U@0mlto__3sCr*2ZsKO;=G&wOak&IK^8>Ngejga%>4Q>0o?$r+0+fXp{?`L-AU0wp7bkt!T;M+!juYiH=;41t z5pNFA7jQjJe*Qf9ctX`qrpwQr1II5DsrZ@pKNXH=0-X(10yIg! ze=Z!y0IdQ#gGh1)?@yMGJK=~WPzGcMvH;BkvI5zF%7H3?DuJqi>_F8(H9*+R<^wGN zS_rg=pll=LQlP~^4xl<9Cr~|51CR@73D8oYWk8n!EeC1@S|O)tB^+B6`E8ZY+u%3_ z=n9}Kfv#4>X@~Po8f7$?0ekZn70(+L*Lc2FuG_D}ahrUO^iBEvW;kvK`ZmxGpzi?P z0`y&=?*ZMac!tsU;ohA% z>mLI>s<{8SeEy_-{3#rN0`xS{GeFM*JqPqW&0==Yoe!qNvKt8?#$5-X^f0K`| z%g0~J$6v|EU(3fg;rJV%-^$mA;rJgwzX$pQ&|C8TKPvcQe|TGQ&BCAMdqWw9QvM(N z>l?Q*`M*DR^52BKzeM!Qzaxape|BC+>)#KYJiyf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{x3wqyIHZ_nLz()#C0PyXm=le+x76xV|<%Ey_g1g27LBG%M& z{D*VIDU&8c3_3%aG&w=XFd9nK&r|uiDM_Qc98N*Nw+4g$u%*@47q)22U;WRwMtptE zkaL(h=1e(Ql%h*$v$nV+L98IGAAG|(3o$O!8Soc%4YBfHou#5|0RN@e0Y+C_$>F{4 zUU<=6<7@7Hy{h|2QW;D`YDN9j8q?P6-rsol9;@f9QrA5t%6FZAIw7+s#CH;qTeBzl8p4G!%IM<@j%o|K|8_ zj{oNPZ;t=w_-~H?=J;=p|IRloO+F#q@*DD%x1hTG#5m>QT7F(3@$wFmlfN*ne8s@! zCo(DT;YJ+)okRThqtWZ*`}PsZyA#KMbNqMl;lE=&VcPzuh=knX_-~H?=Fn7*|K|Ab z!N7m>kUDVuH^+Z-{C9f%HwG>Iw-$C_ZQA$6t~maf$gJxbdIEf`3%v bze#rGuf$Jr{5Qvc7aab3nDKkVjsO0CIj5A! diff --git a/Implab/Parallels/ArrayTraits.cs b/Implab/Parallels/ArrayTraits.cs --- a/Implab/Parallels/ArrayTraits.cs +++ b/Implab/Parallels/ArrayTraits.cs @@ -167,5 +167,46 @@ namespace Implab.Parallels { return promise.Anyway(() => semaphore.Dispose()); } + + /* + this method is pretty fast, but it may cause a stack overflow if an element transformation is made faster then the next operation is + be chained, in this case the syncronous callback invocation will occur + + public static Promise ChainedMap2(this TSrc[] source, ChainedOperation transform, int threads) { + if (source == null) + throw new ArgumentNullException("source"); + if (transform == null) + throw new ArgumentNullException("transform"); + if (threads <= 0) + throw new ArgumentOutOfRangeException("Threads number must be greater then zero"); + + var promise = new Promise(); + var res = new TDst[source.Length]; + var index = -1; // we will start with increment + var len = source.Length; + var pending = len; + + Action callback = null; + callback = (current) => { + if (current < len) { + transform(source[current]) + .Then( + x => { + res[current] = x; + if (Interlocked.Decrement(ref pending) == 0) + promise.Resolve(res); + else + callback(Interlocked.Increment(ref index)); + }, + e => promise.Reject(e) + ); + } + }; + + for (int i = 0; i < threads; i++) + callback(Interlocked.Increment(ref index)); + return promise; + } + */ } } diff --git a/Implab/Parallels/DispatchPool.cs b/Implab/Parallels/DispatchPool.cs --- a/Implab/Parallels/DispatchPool.cs +++ b/Implab/Parallels/DispatchPool.cs @@ -151,7 +151,10 @@ namespace Implab.Parallels { } } else { // if there is no sleeping threads in the pool - StartWorker(); + if (!StartWorker()) + // we haven't started a new thread, but the current can be on the way and it can't process the queue + // send it a signal to spin again + SignalThread(); } } @@ -281,7 +284,6 @@ namespace Implab.Parallels { m_hasTasks.Dispose(); else SignalThread(); // wake next worker - unit = default(TUnit); break; } diff --git a/Implab/Parallels/MTQueue.cs b/Implab/Parallels/MTQueue.cs --- a/Implab/Parallels/MTQueue.cs +++ b/Implab/Parallels/MTQueue.cs @@ -44,7 +44,7 @@ namespace Implab.Parallels { // this is the last element, // then try to update the tail if (first != Interlocked.CompareExchange(ref m_last, null, first)) { - // this is a ace condition + // this is a race condition if (m_last == null) // the queue is empty return false;