示例#1
0
        /// <summary>
        /// Each runner has a protocol address space of 24bits, and
        /// there can be at most 255 types of runners in the system.
        /// </summary>
        public ProgramRunner(ILanguageRuntimeProvider runtime_provider, TrinityFFIModule module)
        {
            if (s_runtime_cnt == c_runtime_cnt_max)
            {
                throw new InvalidOperationException("Maximum number of language runtime providers reached.");
            }

            m_runtimeProvider = runtime_provider;
            m_runtimes        = new BlockingCollection <ILanguageRuntime>(new ConcurrentQueue <ILanguageRuntime>());
            m_module          = module;
            m_runtime_type_id = s_runtime_cnt++;


            //  Two situations where we just allocate a single runtime:
            //  1. The provider specified that only one runtime can be created.
            //  2. The provider claims that a runtime has multi-threading capabilities.
            if (runtime_provider.RuntimeModel == RuntimeModel.SingleRuntime ||
                runtime_provider.ThreadingModel == ThreadingModel.MultiThreaded)
            {
                _AllocSingleRuntime();
            }
            else
            {
                _AllocMultiRuntime();
            }

            m_singleThreaded = (m_runtimeProvider.ThreadingModel == ThreadingModel.SingleThreaded);
        }
示例#2
0
        private void _OnCommunicationInstanceStart()
        {
            m_module = Global.CommunicationInstance.GetCommunicationModule <TrinityFFIModule>();
            Log.WriteLine("Scanning for foreign runtime providers.");
            m_providers = AssemblyUtility.GetAllClassInstances <ILanguageRuntimeProvider>();
            foreach (var runtime_provider in m_providers)
            {
                try
                {
                    ProgramRunner runner = new ProgramRunner(runtime_provider, m_module);
                    Log.WriteLine("Discovered foreign runtime provider '{0}'.", runtime_provider.Name);
                    foreach (var format in runtime_provider.SupportedSuffix)
                    {
                        m_runners[format] = runner;
                        Log.WriteLine(LogLevel.Debug, "Use {0} to load *.{1}.", runtime_provider.Name, format);
                    }
                }
                catch (Exception ex)
                {
                    Log.WriteLine(LogLevel.Error, "Failed to load foreign runtime provider '{0}':{1}", runtime_provider.Name, ex.ToString());
                }
            }

            Log.WriteLine("Scanning for FFI Programs.");

            string dir;

            string[] files;

            dir   = FFIConfig.Instance.ProgramDirectory;
            dir   = FileUtility.CompletePath(dir, create_nonexistent: true);
            files = Directory.GetFiles(dir);

            foreach (var file in files)
            {
                try
                {
                    var suffix = Path.GetExtension(file);
                    if (m_runners.TryGetValue(suffix, out var runner))
                    {
                        Log.WriteLine("Loading program {0} with {1}.", Path.GetFileName(file), runner.RuntimeName);
                        runner.LoadProgram(file);
                    }
                }
                catch { }
            }
        }