void AllocateWork(Chunk chunk, TerrainWorkerAction action)
        {
            if (GlobalNetwork.IsServer && action != TerrainWorkerAction.Populate)
            {
                return;
            }

            chunk.BeingWorkedOn = true;

            // Find the first worker that isnt busy, or the worker with the smallest
            // work count and add the action to it's queue.
            TerrainWorker worker = null;

            if (action != TerrainWorkerAction.CalculateLighting)
            {
                for (int i = 0; i < workers.Length; i++)
                {
                    TerrainWorker _worker = workers[i];
                    if (!_worker.IsBusy && (worker == null || _worker.WorkCount < worker.WorkCount))
                    {
                        worker = _worker;
                    }
                    else if (i == workers.Length - 1 && worker == null)
                    {
                        worker = _worker;
                    }
                }
            }
            else
            {
                worker = workers[workers.Length - 1];
            }

            worker.Enqueue(chunk, action);
        }
Example #2
0
    public void Start()
    {
        int opsPerWorker = opsPerIteration / numThreads;

        tWorkers = new TerrainWorker[numThreads];

        for (int i = 0; i < numThreads; i++)
        {
            _debug("Creating new thread: " + i.ToString());
            tWorkers [i] = new TerrainWorker(opsPerWorker);
            tWorkers [i].start();
        }
    }
        void CreateWorkers()
        {
            if (workers == null)
            {
                workers = new TerrainWorker[Math.Max(Environment.ProcessorCount - (GlobalNetwork.IsServer ? 2 : 1), 2)];
                for (int i = 0; i < workers.Length; i++)
                {
                    workers[i] = new TerrainWorker();
                }

                DashCMD.WriteImportant("[FixedTerrain] Created {0} background threads.", workers.Length);
            }

            for (int i = 0; i < workers.Length; i++)
            {
                workers[i].SetTerrain(this);
            }
        }