public void ThreadedInstantiateTasked(GameObject prefab, Transform place, bool setChild, Action <UnityEngine.Object> callback) { Monitor.Enter(registeredTasks); iSCDThreadContainer container; container = ContainerForID(int.Parse(Thread.CurrentThread.Name)); Monitor.Enter(container); TaskInformationPackage package = new TaskInformationPackage(); package.task = () => { GameObject obj = Instantiate(prefab, place.position, place.rotation) as GameObject; package.returns = obj; if (setChild) { obj.transform.SetParent(place); } package.finished = true; callback(package.returns); }; registeredTasks.Add(package); Monitor.Exit(container); Monitor.Exit(registeredTasks); }
public void DispatchMainThread(Action task) { if (Thread.CurrentThread.Name == null) { throw new System.Exception("iSCDRuntime: Already in main thread"); } Monitor.Enter(registeredTasks); iSCDThreadContainer container = ContainerForID(int.Parse(Thread.CurrentThread.Name)); Monitor.Enter(container); Action callback = () => { container.Resume(); //iSCDTools.LogVerbose("ISCD Runtime : " + iSCDTools.FormatThreadName(container) + " dispatched back from main thread."); }; TaskInformationPackage package = new TaskInformationPackage(task, callback, container.iSCD_RuntimeID); registeredTasks.Add(package); Monitor.Exit(registeredTasks); //iSCDTools.LogVerbose("ISCD Runtime : " + iSCDTools.FormatThreadName(container) + " ready to dispatch to main thread."); container.Pause(); container.WaitForResetEvent(); Monitor.Exit(container); if (IsDebugging()) { debugger.CountMainThreadDispatchs(); } }
//int tasksDoneThisFrame = 0; void Update() { long startedTime = iSCDTimeUtilities.MillisecondsSinceJanFirst1970(); long maxTimeCost = (long)(1f / targetedFramerate * 100f); long lastestStopTime = startedTime + maxTimeCost; lock (registeredTasks) { while (lastestStopTime > iSCDTimeUtilities.MillisecondsSinceJanFirst1970()) { //Debug.Log ("s" + iSCDTimeUtilities.MillisecondsSinceJanFirst1970 ().ToString() + "t" + lastestStopTime); if (registeredTasks.Count <= 0) { break; } TaskInformationPackage package = registeredTasks [0]; try{ if (package.task != null) { package.task(); } if (package.finishCallback != null) { package.finishCallback(); } if (package.taskFinishCallback != null) { package.taskFinishCallback(package.callerThreadID); } } catch (Exception e) { Debug.LogError("iSCDRuntime: Uncatched exception raised in main thread queue, aborting thread " + package.callerThreadID.ToString()); Debug.LogError("Exception: " + e.ToString()); AbortThread(package.callerThreadID); } finally{ registeredTasks.RemoveAt(0); //tasksDoneThisFrame++; } } } //Debug.Log (tasksDoneThisFrame); //tasksDoneThisFrame = 0; }
public UnityEngine.Object ThreadedInstantiate(GameObject prefab, Vector3 position, Quaternion rotation) { iSCDThreadContainer container; lock (activeContainers) container = ContainerForID(int.Parse(Thread.CurrentThread.Name)); TaskInformationPackage package = new TaskInformationPackage(); package.task = () => { package.returns = Instantiate(prefab, position, rotation); package.finished = true; }; lock (registeredTasks) registeredTasks.Add(package); while (!package.finished) { ; } return(package.returns); }
public void DispatchTaskToMainThread(Action task, Action <int> taskFinishCallback) { Monitor.Enter(registeredTasks); iSCDThreadContainer container = ContainerForID(int.Parse(Thread.CurrentThread.Name)); Monitor.Enter(container); TaskInformationPackage package; package = new TaskInformationPackage(task, null, taskFinishCallback, container.iSCD_RuntimeID); registeredTasks.Add(package); Monitor.Exit(registeredTasks); Thread.Sleep(1); Monitor.Exit(container); if (IsDebugging()) { debugger.CountMainThreadDispatchs(); } }