Ejemplo n.º 1
0
        /// <summary>
        /// Add a module to the system. Adding a module means it (and its associated functionality) will be usable by your application.
        /// You must add a module before you can access any of its features.
        /// </summary>
        /// <param name="moduleType">The module type to add. Must not have already been added. Must not be null.</param>
        /// <exception cref="InvalidOperationException">Thrown if you try to add a module while the system
        /// <see cref="IsRunning">is running</see>. You must add all desired modules before the pipeline starts.</exception>
        /// <exception cref="InvalidOperationException">Thrown if you call this method from any thread other than the
        /// <see cref="MasterThread"/>.</exception>
        /// <exception cref="ArgumentException">Thrown if a module of the same type has already been added.</exception>
        public static void AddModule(Type moduleType)
        {
            if (moduleType == null)
            {
                throw new ArgumentNullException("moduleType");
            }


            lock (staticMutationLock) {
                if (addedModuleList.Any(addedMod => addedMod.GetType() == moduleType))
                {
                    throw new ArgumentException("Module of type '" + moduleType.Name + "' already added.", "moduleType");
                }
                if (IsRunning)
                {
                    throw new InvalidOperationException("Can not add more modules while the system is running.");
                }

                ThrowIfCurrentThreadIsNotMaster();

                ILosgapModule module = (ILosgapModule)Activator.CreateInstance(moduleType, true);

                addedModuleList.Add(module);
                module.ModuleAdded();
                Logger.Log("Module added: " + moduleType.Name + ".");
            }
        }
        //private static readonly Dictionary<ILosgapModule, int> frameCounts = new Dictionary<ILosgapModule, int>();
        //private static readonly Dictionary<ILosgapModule, double> cumulativeTickTimes = new Dictionary<ILosgapModule, double>();
        //private static Stopwatch timer = Stopwatch.StartNew();

        public void Start()
        {
            while (!isDisposed)
            {
#if DEBUG
                frameTimer.Start();
#endif
                for (int i = 0; i < modules.Length; ++i)
                {
#if DEBUG
                    frameTimeoutCulprit = modules[i];
#endif
                    long elapsedMs   = pipelineTimer.ElapsedMilliseconds;
                    long tickDeltaMs = elapsedMs - lastTickTimes[i];
                    if (tickDeltaMs > modules[i].TickIntervalMs)
                    {
                        //if (!frameCounts.ContainsKey(modules[i])) {
                        //	frameCounts.Add(modules[i], 0);
                        //	cumulativeTickTimes.Add(modules[i], 0f);
                        //}
                        //var timeBefore = timer.Elapsed;
                        modules[i].PipelineIterate(ParallelizationProvider, tickDeltaMs);
                        lastTickTimes[i] = elapsedMs;
                        //var iterateTime = (timer.Elapsed - timeBefore).TotalSeconds;
                        //cumulativeTickTimes[modules[i]] += iterateTime;
                        //if (++frameCounts[modules[i]] == 1000) {
                        //	Logger.Log("TIMER: " + modules[i].GetType().Name + ": " + (cumulativeTickTimes[modules[i]] / 1000d) + "s avg");

                        //	frameCounts[modules[i]] = 0;
                        //	cumulativeTickTimes[modules[i]] = 0;
                        //}
                    }
                }

#if DEBUG
                frameTimer.Stop();
#endif

                ParallelizationProvider.MasterHydratePMIQueue();
            }

            ParallelizationProvider.WaitForSlavesToExit();
        }