void Awake()
    {
        Application.targetFrameRate = 25;
        myThreadScheduler           = Loom.CreateThreadPoolScheduler();

        //--------------- Ending Single threaded routine --------------------
        threadA = Loom.StartSingleThread(EndingSingleThreadCoroutine, System.Threading.ThreadPriority.Normal, true);
        //--------------- Ending Single threaded routine --------------------

        //--------------- Continues Single threaded routine --------------------
        threadB = Loom.StartSingleThread(ContinuesSingleThreadCoroutine, System.Threading.ThreadPriority.Normal, true);
        //--------------- Continues Single threaded routine --------------------

        //--------------- Start Multithreaded packages --------------------
        int i = TestWorkerObjects;

        IThreadWorkerObject[] workerObjects = new IThreadWorkerObject[TestWorkerObjects];

        while (--i > -1)
        {
            workerObjects[i] = new LotsOfNumbers(UnityEngine.Random.Range(minCalculations, maxCalculations));
        }

        myThreadScheduler.StartASyncThreads(workerObjects, OnThreadWorkComplete, OnWorkerObjectDone, maxThreads);
        StartCoroutine(AbortAllThreadsAfterDelay());
        //--------------- Start Multithreaded packages --------------------
    }
Esempio n. 2
0
    //--------------------------------------- 4 MULTI THREADED WORK EXECUTION OVERLOADS --------------------------------------
    //--------------------------------------- 4 MULTI THREADED WORK EXECUTION OVERLOADS --------------------------------------



    //--------------------------------------- THREAD POOL SCHEDULAR --------------------------------------
    //--------------------------------------- THREAD POOL SCHEDULAR --------------------------------------

    #region THREAD POOL SCHEDULAR


    /// <summary>
    /// Unlike "StartMultithreadedWorkloadExecution", you will have to build your own IThreadWorkerObject.
    /// Downside: It requires some extra work. Upside: you got more controll over what goes in and comes out
    /// Infact: You can create you own polymorphed IThreadWorkerObject-array, each ellement being a completely different type. For example: the statemachines of enemies are IThreadWorkerObject's and the array contains completely different classes with enemies/AI-behaviours.
    /// </summary>
    /// <param name="workerObjects">An array of IThreadWorkerObject objects to be handled by the threads. If you want multiple cores/threads to be active, make sure that the number of IThreadWorkerObject's proves matches/exeeds your preferred number maxWorkingThreads. </param>
    /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param>
    /// <param name="onPackageExecuted">Fires foreach finished re-packaged set of workLoad-object</param>
    /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one Core to run on. (quadcore == 1 Core Mainthread, 3 cores used by the ThreadPoolScheduler)</param>
    /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param>
    /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param>
    /// <returns>A ThreadPoolScheduler that handles all the repackaged workLoad-Objects</returns>
    public static ThreadPoolScheduler StartMultithreadedWorkerObjects(IThreadWorkerObject[] workerObjects, ThreadPoolSchedulerEvent onCompleteCallBack, ThreadedWorkCompleteEvent onPackageExecuted = null, int maxThreads = -1, ThreadPoolScheduler scheduler = null, bool safeMode = true)
    {
        if (scheduler == null)
        {
            scheduler = CreateThreadPoolScheduler();
        }

        scheduler.StartASyncThreads(workerObjects, onCompleteCallBack, onPackageExecuted, maxThreads, safeMode);
        return(scheduler);
    }
    private void RestartThreadPoolWork()
    {
        //if (tickTock && !myThreadScheduler.isBusy && workerObjects != null)
        if (workerObjects != null)
        {
            //--------------- Update WorkerObjects first--------------------
            colliders = GetColliders();

            int i = workerObjects.Length;
            while (--i > -1)
            {
                UpdateWorkerObjectStats(workerObjects[i]);
            }
            //--------------- Update WorkerObjects first --------------------

            //--------------- Then restart the calculations --------------------
            myThreadScheduler.ForceToMainThread = !MultithreadingEnabled;
            myThreadScheduler.StartASyncThreads(workerObjects, onThreadWorkComplete, null, ThreadingMaxThreads);
            //--------------- Then restart the calculations --------------------
        }
    }
    private void InitThreadPool()
    {
        //--------------- Cap the number of threads --------------------
        int maxThreads = ThreadingMaxThreads;

        if (maxThreads <= 0)
        {
            maxThreads = Mathf.Max(SystemInfo.processorCount - 1, 1);
        }
        //--------------- Cap the number of threads --------------------

        //--------------- Spread the Flocks over multiple worker-packages --------------------
        int ThreadingPoolPackages = ThreadingPackagesPerThread * maxThreads;

        int flocksPerBlock = Mathf.CeilToInt((float)FlockingSpawnCount / (float)ThreadingPoolPackages);

        workerObjects = new FlockingDataWorker[ThreadingPoolPackages];

        int i        = ThreadingPoolPackages;
        int startIdx = 0;

        while (--i > -1)
        {
            FlockingDataWorker workerBlock = new FlockingDataWorker();
            UpdateWorkerObjectStats(workerBlock);

            workerBlock.startWorkIndex = startIdx;
            workerBlock.endWorkIndex   = Mathf.Min(flockers.Length, startIdx + flocksPerBlock);
            workerObjects[i]           = workerBlock;
            startIdx += flocksPerBlock;
        }
        //--------------- Spread the Flocks over multiple worker-packages --------------------


        myThreadScheduler = Loom.CreateThreadPoolScheduler();
        myThreadScheduler.ForceToMainThread = !MultithreadingEnabled;
        myThreadScheduler.StartASyncThreads(workerObjects, onThreadWorkComplete, null, maxThreads);
    }
Esempio n. 5
0
    void Start()
    {
        DontDestroyOnLoad(this);
        myThreadScheduler = UThread.CreateThreadPoolScheduler();
        //myThreadScheduler.ForceToMainThread = true;
        //UThread.StartSingleThread(LoadConfig, System.Threading.ThreadPriority.Normal, true);
        workerObjects = new FlockingDataWorker[1];
        int maxThreads = -1;

        if (maxThreads <= 0)
        {
            maxThreads = Mathf.Max(SystemInfo.processorCount - 1, 1);
        }

        FlockingDataWorker workerBlock = new FlockingDataWorker();

        workerObjects[0] = workerBlock;

        myThreadScheduler.ForceToMainThread = true;
        myThreadScheduler.StartASyncThreads(workerObjects, onThreadWorkComplete, null, maxThreads);

        StartCoroutine(LoadPosFile());
        StartCoroutine(LoadSummonPosFile());
    }
Esempio n. 6
0
        /// <summary>
        /// Helps spreading the same repetetive workload over multiple threads/cores in an easy way.
        /// </summary>
        /// <typeparam name="D">Generic-Type of the delegate that will be executed and compute the workload</typeparam>
        /// <typeparam name="T">Generic-Type of the object you want to be computed by the executor</typeparam>
        /// <param name="executor">A (static) method that computes one workLoad-object at a time</param>
        /// <param name="workLoad">An array with objects you want to get computed by the executor</param>
        /// <param name="onComplete">Fired when all re-packaged workLoad-objects are finished computing</param>
        /// <param name="onPackageComplete">Fires foreach finished re-packaged set of workLoad-object</param>
        /// <param name="maxThreads"> Lets you choose how many threads will be run simultaneously by the threadpool. Default: -1 == number of cores minus one, to make sure the MainThread has at least one Core to run on. (quadcore == 1 Core Mainthread, 3 cores used by the ThreadPoolScheduler)</param>
        /// <param name="scheduler">If Null, a new ThreadPoolScheduler will be instantiated.</param>
        /// <param name="safeMode">Executes all the computations within try-catch events, logging it the message + stacktrace</param>
        /// <returns>A ThreadPoolScheduler that handles all the repackaged workLoad-Objects =</returns>
        public static ThreadPoolScheduler StartMultithreadedWorkloadExecution <D, T>(D executor, T[] workLoad, object extraArgument, MultithreadedWorkloadComplete <T> onComplete, MultithreadedWorkloadPackageComplete <T> onPackageComplete, int maxThreads = -1, ThreadPoolScheduler scheduler = null, bool safeMode = true)
        {
            if (scheduler == null)
            {
                scheduler = Loom.CreateThreadPoolScheduler();
            }
            else if (scheduler.isBusy)
            {
                Debug.LogError("Provided Scheduler stil busy!!!");
            }

            if (maxThreads <= 0)
            {
                maxThreads = Mathf.Max(SystemInfo.processorCount - 1, 1);
            }

            int packagesPerThread = 1;

            if (maxThreads > 1) //If we are running in just one thread at a time, just use one, if more, for sake of better cpu-saturation, subdive into smaller packages per Core.
            {
                packagesPerThread = 2;
            }

            int packages          = Mathf.Min(maxThreads * packagesPerThread, workLoad.Length);
            int objectsPerPackage = (int)Mathf.Ceil((float)workLoad.Length / (float)packages);

            ThreadWorkDistribution <T>[] workerPackages = new ThreadWorkDistribution <T> [packages];
            Type delegateType = typeof(D);
            //Debug.Log(delegateType.FullName);

            int count = 0;

            for (int i = 0; i < packages; i++)
            {
                int packagedSize = Mathf.Min(workLoad.Length - count, objectsPerPackage);
                if (delegateType == typeof(ThreadWorkloadExecutor <T>))
                {
                    workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutor <T>), workLoad, count, count + packagedSize);
                }
                else if (delegateType == typeof(ThreadWorkloadExecutorIndexed <T>))
                {
                    workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutorIndexed <T>), workLoad, count, count + packagedSize);
                }
                else if (delegateType == typeof(ThreadWorkloadExecutorArg <T>))
                {
                    workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutorArg <T>), workLoad, extraArgument, count, count + packagedSize);
                }
                else if (delegateType == typeof(ThreadWorkloadExecutorArgIndexed <T>))
                {
                    workerPackages[i] = new ThreadWorkDistribution <T>((executor as ThreadWorkloadExecutorArgIndexed <T>), workLoad, extraArgument, count, count + packagedSize);
                }

                workerPackages[i].ID = i;
                count += objectsPerPackage;
            }



            //--------------- Store session data --------------------
            ThreadWorkDistributionSession <T> sessionData = new ThreadWorkDistributionSession <T>();

            sessionData.workLoad          = workLoad;
            sessionData.onComplete        = onComplete;
            sessionData.onPackageComplete = onPackageComplete;
            sessionData.packages          = workerPackages;
            //--------------- Store session data --------------------


            scheduler.StartASyncThreads(workerPackages, sessionData.onCompleteBubble, sessionData.onPackageCompleteBubble, maxThreads, safeMode);
            return(scheduler);
        }
        public static ThreadPoolScheduler StartMultithreadedWorkloadExecution <D, T>(D executor, T[] workLoad, object extraArgument, MultithreadedWorkloadComplete <T> onComplete, MultithreadedWorkloadPackageComplete <T> onPackageComplete, int maxThreads = -1, ThreadPoolScheduler scheduler = null, bool safeMode = true)
        {
            bool flag = scheduler == null;

            if (flag)
            {
                scheduler = Loom.CreateThreadPoolScheduler();
            }
            else
            {
                bool isBusy = scheduler.isBusy;
                if (isBusy)
                {
                    Debug.LogError("Provided Scheduler stil busy!!!");
                }
            }
            bool flag2 = maxThreads <= 0;

            if (flag2)
            {
                maxThreads = Mathf.Max(SystemInfo.processorCount - 1, 1);
            }
            int  num   = 1;
            bool flag3 = maxThreads > 1;

            if (flag3)
            {
                num = 2;
            }
            int num2 = Mathf.Min(maxThreads * num, workLoad.Length);
            int num3 = (int)Mathf.Ceil((float)workLoad.Length / (float)num2);

            ThreadWorkDistribution <T>[] array = new ThreadWorkDistribution <T> [num2];
            Type typeFromHandle = typeof(D);
            int  num4           = 0;
            int  num6;

            for (int i = 0; i < num2; i = num6 + 1)
            {
                int  num5  = Mathf.Min(workLoad.Length - num4, num3);
                bool flag4 = typeFromHandle == typeof(ThreadWorkloadExecutor <T>);
                if (flag4)
                {
                    array[i] = new ThreadWorkDistribution <T>(executor as ThreadWorkloadExecutor <T>, workLoad, num4, num4 + num5);
                }
                else
                {
                    bool flag5 = typeFromHandle == typeof(ThreadWorkloadExecutorIndexed <T>);
                    if (flag5)
                    {
                        array[i] = new ThreadWorkDistribution <T>(executor as ThreadWorkloadExecutorIndexed <T>, workLoad, num4, num4 + num5);
                    }
                    else
                    {
                        bool flag6 = typeFromHandle == typeof(ThreadWorkloadExecutorArg <T>);
                        if (flag6)
                        {
                            array[i] = new ThreadWorkDistribution <T>(executor as ThreadWorkloadExecutorArg <T>, workLoad, extraArgument, num4, num4 + num5);
                        }
                        else
                        {
                            bool flag7 = typeFromHandle == typeof(ThreadWorkloadExecutorArgIndexed <T>);
                            if (flag7)
                            {
                                array[i] = new ThreadWorkDistribution <T>(executor as ThreadWorkloadExecutorArgIndexed <T>, workLoad, extraArgument, num4, num4 + num5);
                            }
                        }
                    }
                }
                array[i].ID = i;
                num4       += num3;
                num6        = i;
            }
            ThreadWorkDistributionSession <T> threadWorkDistributionSession = new ThreadWorkDistributionSession <T>();

            threadWorkDistributionSession.workLoad          = workLoad;
            threadWorkDistributionSession.onComplete        = onComplete;
            threadWorkDistributionSession.onPackageComplete = onPackageComplete;
            threadWorkDistributionSession.packages          = array;
            scheduler.StartASyncThreads(array, new ThreadPoolSchedulerEvent(threadWorkDistributionSession.onCompleteBubble), new ThreadedWorkCompleteEvent(threadWorkDistributionSession.onPackageCompleteBubble), maxThreads, safeMode);
            return(scheduler);
        }