Exemple #1
0
        /// <summary>
        /// Removes the worker from the workers List and
        /// destroies the worker thread.
        /// </summary>
        /// <param name="worker">Worker.</param>
        public void DestroyWorkerThread(CoflnetThreadWorker worker)
        {
            // remove them from the pool
            workers.Remove(worker);

            worker.Stop();
        }
Exemple #2
0
        /// <summary>
        /// Spawn or remove WorkerThreads from the pool
        /// </summary>
        protected void CheckWorkerBalance()
        {
            // only check every 100 fixed updates and/or commands
            // if there are more than 100 fixed updates between the next fixedUpdate
            internalCommandIndex++;
            if (internalCommandIndex % 100 != 0)
            {
                return;
            }

            int overloadedCount              = 0;
            int underloadedCount             = 0;
            CoflnetThreadWorker targetWorker = null;

            foreach (var item in workers)
            {
                if (item.queuedCommand.Count > 50)
                {
                    overloadedCount++;
                }
                else if (item.queuedCommand.Count == 0)
                {
                    if (targetWorker == null)
                    {
                        targetWorker = item;
                    }
                    underloadedCount++;
                }
            }

            // only create new workers when there are more than 50% overloaded
            // remember that this is an int so if there would be only one worker
            // it would start a second one (1 (workers.Count) / 2 = 0) if its >=
            if (overloadedCount > workers.Count / 2)
            {
                CreateWorkerThread();
            }
            int minThreashold = workers.Count / 2;

            // make sure there are allways minWorkerCount worker threads
            if (minThreashold < minWorkerCount)
            {
                minThreashold = minWorkerCount;
            }
            else if (underloadedCount > workers.Count / 2 && workers.Count >= minThreashold)
            {
                DestroyWorkerThread(targetWorker);
            }
        }
Exemple #3
0
        public void ExecuteCommand(Command command, CommandData data)
        {
            CommandItem comandWithData = new CommandItem(command, data);

            // find worker
            CoflnetThreadWorker worker = GetWorkerForUser(data.SenderId);

            // stack commandItem on the worker
            // it will be executed asyncronously
            worker.queuedCommand.Enqueue(comandWithData);

            internalCommandIndex++;

            // check every 100 commands that workers aren't overloaded
            if (internalCommandIndex % 100 != 0)
            {
                return;
            }
            //CheckWorkerBalance();
        }
Exemple #4
0
        public void CreateWorkerThread()
        {
            // More threads than processors causes to much context switching
            // one core is reserved for the main thread
            if (Environment.ProcessorCount - 1 <= workers.Count)
            {
                return;
            }



            CoflnetThreadWorker worker = new CoflnetThreadWorker();

            ThreadStart threadStart = new ThreadStart(worker.ExecuteNextCommand);


            Thread thread = new Thread(threadStart, 1000000);

            // tell the worker its thread
            worker.CurrentThread = thread;
            thread.Start();
            // add the worker to the pool
            workers.Add(worker);
        }