void OnEnable()
    {
        mCoroutinePool = CoroutineHelper.Factory.CreateCoroutinePool(10);
        mCoroutinePool.Init();

        mCoroutineIDStack = new Stack <long>();
    }
Exemple #2
0
        public void Destroy()
        {
            CurrentMode.Destroy();

            if (null != _coroutinePool)
            {
                _coroutinePool.Destroy();
                _coroutinePool = null;
            }
        }
Exemple #3
0
        static void LazyInit()
        {
            if (Inited == false)
            {
                Inited = true;

                coroutinePool = new CoroutinePool();

                EditorApplication.update += Update;
            }
        }
Exemple #4
0
        public void Unload()
        {
#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                UnloadInternalInEditMode();
                return;
            }
#endif
            CoroutinePool.StartCoroutine(UnloadInternalInPlayMode());
        }
Exemple #5
0
 public static void Uninstall(CoroutinePool pool, IAsyncProcessor processor)
 {
     if (null == pool)
     {
         throw new ArgumentNullException("pool");
     }
     if (processor.ThreadHandle > 0)
     {
         pool.StopCoroutine(processor.ThreadHandle);
     }
     processor.ThreadHandle = 0;
     processor.IsSetup      = false;
 }
Exemple #6
0
 public static void Setup(CoroutinePool pool, IAsyncProcessor processor)
 {
     if (null == pool)
     {
         throw new ArgumentNullException("pool");
     }
     if (processor.IsSetup)
     {
         throw new AsyncRequestAlreadySetupException("Async processor cannot setup twice!");
     }
     processor.IsSetup      = true;
     processor.ThreadHandle = pool.StartCoroutine(ProcessAsync(processor));
     Logger.LogVerbose("Coroutine setup, processor: {0}, handle: {1}", processor, processor.ThreadHandle);
 }
Exemple #7
0
        private void Initialize(BundlerManifest manifest, BundlerOptions options)
        {
            Instance = this;

            _manifest      = manifest;
            _coroutinePool = new CoroutinePool("Bundler", options.MaxAsyncUploadCount);
            _context       = new BundlerContext {
                Options       = options,
                CoroutinePool = _coroutinePool,
            };

            _modes = new Dictionary <BundleModeType, ModeBase>(2);
            _modes[BundleModeType.Bundle]   = new BundleMode(manifest, _searchPaths, _context);
            _modes[BundleModeType.Resource] = new ResourceMode(manifest, _searchPaths, _context);

            var bundleMode = true;
            var logLevel   = Logger.LogLevel.ERROR;

#if UNITY_EDITOR
            bundleMode = EditorPrefs.GetBool("vFrameBundlerModePreferenceKey", false);
            logLevel   = EditorPrefs.GetInt("vFrameBundlerLogLevelPreferenceKey", Logger.LogLevel.ERROR - 1) + 1;
#endif
            if (bundleMode)
            {
                if (manifest != null)
                {
                    SetMode(BundleModeType.Bundle);
                    return;
                }

                Logger.LogInfo("Bundle manifest does not provided, bundle mode will disable.");
            }

            SetMode(BundleModeType.Resource);
            SetLogLevel(logLevel);
        }
Exemple #8
0
        void InitBuiltins(GlobalScope globs)
        {
            globs.Define(Int);
            globs.Define(Float);
            globs.Define(Bool);
            globs.Define(String);
            globs.Define(Void);
            globs.Define(Any);
            globs.Define(ClassType);

            {
                var fn = new FuncSymbolNative("suspend", Type("void"),
                                              delegate(VM.Frame frm, FuncArgsInfo args_info, ref BHS status)
                {
                    return(CoroutineSuspend.Instance);
                }
                                              );
                globs.Define(fn);
            }

            {
                var fn = new FuncSymbolNative("yield", Type("void"),
                                              delegate(VM.Frame frm, FuncArgsInfo args_info, ref BHS status)
                {
                    return(CoroutinePool.New <CoroutineYield>(frm.vm));
                }
                                              );
                globs.Define(fn);
            }

            //TODO: this one is controversary, it's defined for BC for now
            {
                var fn = new FuncSymbolNative("fail", Type("void"),
                                              delegate(VM.Frame frm, FuncArgsInfo args_info, ref BHS status)
                {
                    status = BHS.FAILURE;
                    return(null);
                }
                                              );
                globs.Define(fn);
            }

            {
                var fn = new FuncSymbolNative("start", Type("int"),
                                              delegate(VM.Frame frm, FuncArgsInfo args_info, ref BHS status)
                {
                    var val_ptr = frm.stack.Pop();
                    int id      = frm.vm.Start((VM.FuncPtr)val_ptr._obj, frm).id;
                    val_ptr.Release();
                    frm.stack.Push(Val.NewNum(frm.vm, id));
                    return(null);
                },
                                              new FuncArgSymbol("p", TypeFunc("void"))
                                              );
                globs.Define(fn);
            }

            {
                var fn = new FuncSymbolNative("stop", Type("void"),
                                              delegate(VM.Frame frm, FuncArgsInfo args_info, ref BHS status)
                {
                    var fid = (int)frm.stack.PopRelease().num;
                    frm.vm.Stop(fid);
                    return(null);
                },
                                              new FuncArgSymbol("fid", Type("int"))
                                              );
                globs.Define(fn);
            }

            {
                var fn = new FuncSymbolNative("type", Type("Type"),
                                              delegate(VM.Frame frm, FuncArgsInfo args_info, ref BHS status)
                {
                    var o = frm.stack.Pop();
                    frm.stack.Push(Val.NewObj(frm.vm, o.type, ClassType));
                    o.Release();
                    return(null);
                },
                                              new FuncArgSymbol("o", Type("any"))
                                              );
                globs.Define(fn);
            }
        }
Exemple #9
0
 protected override void Awake()
 {
     _pool = new CoroutinePool();
 }
Exemple #10
0
        static void Main()
        {
            var random = new Random();

            using (var co = Coroutine.Create(CoFunction1(5, 10))) {
                while (co.Resume())
                {
                    if (co.Status != CoStatus.Waiting)
                    {
                        Console.WriteLine(co.Result.ReturnValue.ToString());
                    }
                }
            }

            using (var co = Coroutine.Create(CoFunction2(10, 100))) {
                var i = 0;

                while (co.Resume(new { x = random.Next(10), y = random.Next(10) }))
                {
                    if (co.Status != CoStatus.Waiting)
                    {
                        Console.WriteLine(co.Result.ReturnValue.ToString());
                    }

                    i++;
                    if (i > 5000000 && !timedOut)
                    {
                        timedOut = true;
                        Console.WriteLine("timed out");
                    }
                }
            }

            var            wrapped = Coroutine.Wrap(CoFunction1(5, 10));
            CoResult <int> wrappedResult;

            while ((wrappedResult = wrapped()) == true)
            {
                if (wrappedResult.Status != CoStatus.Waiting)
                {
                    Console.WriteLine("Wrapped coroutine: " + wrappedResult.ReturnValue);
                }
            }

            var sync = Coroutine.MakeSyncWithArgs <int, int, int>(CoFunction2);

            Console.WriteLine("Synchronized coroutine result: " + sync(new { x = 10, y = 100 }, 8, 20));

            Console.WriteLine("Press any key to run coroutine pool test");
            Console.ReadKey(true);

            var pool   = new CoroutinePool();
            var resets = new Dictionary <Coroutine, int>();

            for (int i = 0; i < 5; i++)
            {
                var co = Coroutine.Create(PoolFunc(random.Next()));
                pool.Add(co);
                resets[co] = 0;
            }

            var reset = true;

            pool.OnEnd += (c) => {
                if (reset)
                {
                    c.Reset();
                    resets[c]++;
                }
            };

            ConsoleKey key;

            do
            {
                while (!Console.KeyAvailable)
                {
                    Console.Clear();
                    Console.WriteLine("Press 'q' to quit, 'r' to disable resets");
                    Console.WriteLine();

                    pool.ResumeAll();

                    var index = 1;
                    foreach (var co in pool)
                    {
                        Console.SetCursorPosition(0, 1 + index);
                        Console.WriteLine("Coroutine " + index + ": " + co.ReturnValue);
                        Console.SetCursorPosition(20, 1 + index);
                        Console.WriteLine("Resets: " + resets[co]);
                        index++;
                    }

                    System.Threading.Thread.Sleep(50);

                    if (pool.IsEmpty)
                    {
                        System.Threading.Thread.Sleep(200);
                        break;
                    }
                }

                if (pool.IsEmpty)
                {
                    break;
                }

                key = Console.ReadKey(true).Key;
                if (key == ConsoleKey.R)
                {
                    reset = false;
                }
            } while (key != ConsoleKey.Q);
        }
Exemple #11
0
        /// <summary>
        /// コンストラクタ
        /// </summary>
        protected InfernoScript()
        {
            Interval = 16;

            //初期化をちょっと遅延させる
            Observable.Interval(TimeSpan.FromMilliseconds(10))
            .Where(_ => InfernoCore.Instance != null)
            .First()
            .Subscribe(_ =>
            {
                InfernoCore.Instance.PedsNearPlayer.Subscribe(x => _cachedPeds        = x);
                InfernoCore.Instance.VehicleNearPlayer.Subscribe(x => _cachedVehicles = x);
                InfernoCore.Instance.PlayerPed.Subscribe(x => cahcedPlayerPed         = x);
                InfernoCore.Instance.PlayerVehicle.Subscribe(x => PlayerVehicle.Value = x);
            });

            OnTickAsObservable =
                Observable.FromEventPattern <EventHandler, EventArgs>(h => h.Invoke, h => Tick += h, h => Tick -= h)
                .Select(_ => Unit.Default).Share();     //Subscribeされたらイベントハンドラを登録する

            OnThinnedTickAsObservable =
                OnTickAsObservable.ThrottleFirst(TimeSpan.FromMilliseconds(100), InfernoScriptScheduler)
                .Share();

            OnDrawingTickAsObservable = DrawingCore.OnDrawingTickAsObservable;

            OnAllOnCommandObservable = CreateInputKeywordAsObservable("allon");

            //スケジューラ実行
            OnTickAsObservable.Subscribe(_ => infernoScheduler?.Run());

            // SynchronizationContextの実行
            OnTickAsObservable
            .Subscribe(_ =>
            {
                if (SynchronizationContext.Current == null)
                {
                    SynchronizationContext.SetSynchronizationContext(InfernoSynchronizationContext);
                }
                InfernoSynchronizationContext.Update();
            });

            //タイマのカウント
            OnThinnedTickAsObservable
            .Where(_ => _counterList.Any())
            .Subscribe(_ =>
            {
                foreach (var c in _counterList)
                {
                    c.Update(100);
                }
                //完了状態にあるタイマを全て削除
                _counterList.RemoveAll(x => x.IsCompleted);
            });

            _coroutinePool = new CoroutinePool(5);

            //コルーチンを実行する
            CreateTickAsObservable(TimeSpan.FromMilliseconds(_coroutinePool.ExpectExecutionInterbalMillSeconds))
            .Subscribe(_ => _coroutinePool.Run());


            OnAbortAsync.Subscribe(_ =>
            {
                IsActive = false;
                foreach (var e in _autoReleaseEntities.Where(x => x.IsSafeExist()))
                {
                    e.MarkAsNoLongerNeeded();
                }
                _autoReleaseEntities.Clear();
            });

            try
            {
                Setup();
            }
            catch (Exception e)
            {
                LogWrite(e.ToString());
            }
        }