Пример #1
0
        static WorkBuffer[] PackThreadedWorkToWorkBuffers(ThreadedWork[] tw)
        {
            if (tw != null)
            {
                WorkBuffer[]        workbuffers = new WorkBuffer[Mathf.CeilToInt((float)tw.Length / WorkBuffer.MaxBufferSize)];
                int                 startIndex  = 0;
                int                 maxIndex    = 5;
                List <ThreadedWork> twList      = new List <ThreadedWork>();
                for (int i = 0; i < workbuffers.Length; i++)
                {
                    startIndex = i * 5;
                    maxIndex   = ((i + 1) * 5 <= tw.Length) ? 5 : 5 - (((i + 1) * 5 - tw.Length));

                    for (int y = 0; y < maxIndex; y++)
                    {
                        twList.Add(tw[startIndex + y]);
                    }

                    workbuffers[i] = new WorkBuffer(twList.ToArray());
                    twList.Clear();
                }
                return(workbuffers);
            }

            return(null);
        }
Пример #2
0
        void PerformWork()
        {
            if (PrivWorkBuffer.Count > 0)
            {
                int numWork = (PrivWorkBuffer.Count > targetWorkCycles) ? targetWorkCycles : PrivWorkBuffer.Count;

                for (int i = 0; i < numWork; i++)
                {
                    WorkBuffer wb = PrivWorkBuffer.Dequeue();

                    foreach (ThreadedWork tw in wb.Jobs)
                    {
                        PrivCompletedBuffer.Add(tw.Work());
                        tw.Dispose();
                    }
                }
            }
        }
Пример #3
0
        public static bool TryIssueWorkToThreads(int timeout)
        {
            ThreadedWork[] tw = workToBeAssigned.ToArray();
            workToBeAssigned.Clear();
            WorkBuffer[] _wbs = PackThreadedWorkToWorkBuffers(tw);
            WorkBuffer[] wbs;

            if (FailureBuffer != null && _wbs != null)
            {
                wbs = new WorkBuffer[FailureBuffer.Length + _wbs.Length];
                FailureBuffer.CopyTo(wbs, 0);
                _wbs.CopyTo(wbs, FailureBuffer.Length);
            }
            else if (FailureBuffer == null && _wbs != null)
            {
                wbs = _wbs;
            }
            else
            {
                wbs = FailureBuffer;
            }

            bool wbLock = TryGetLock(timeout);

            if (wbLock)
            {
                int i = 0;

                foreach (WorkBuffer wb in wbs)
                {
                    WorkBuffers[i].Add(wb);
                    i = (i < Threads.Length - 1) ? i + 1 : 0;
                }

                ReleaseLock();
                wbs = null;
            }

            FailureBuffer = wbs;
            return(wbLock);
        }