//------------------------------------------------------------------------------- /// <summary> /// start created task. /// </summary> /// <param name="taskHandle">task identifier.</param> //------------------------------------------------------------------------------- #region Execute / Destory public void ExecuteTask(TaskHandle taskHandle) { TaskType taskType = GetTaskByHandle(taskHandle); QueueEntry queueEntry = new QueueEntry(taskType); m_queueLock.TryEnterWriteLock(1000); m_taskQueue.Enqueue(queueEntry); m_queueLock.ExitWriteLock(); }
//------------------------------------------------------------------------------- /// <summary> /// Destroys created task. /// </summary> /// <param name="taskHandle">task identifier.</param> //------------------------------------------------------------------------------- public void DestroyTask(TaskHandle taskHandle) { TaskType taskType = GetTaskByHandle(taskHandle); Console.WriteLine("Removing " + taskType.Name); m_dictionaryLock.TryEnterWriteLock(5); m_tasks.Remove(taskHandle); m_dictionaryLock.ExitWriteLock(); }
//---------------------------------------------------------------------------------- /// <summary> /// unload assets /// </summary> /// <param name="task"></param> //---------------------------------------------------------------------------------- public void Unload(Object task) { Task <Object> theTask = task as Task <Object>; Asset theAsset = theTask.UserData as Asset; TaskHandle handle = TaskManager.Instance.CreateTask <bool>(theAsset, theAsset.UnloadAssets, false); TaskManager.Instance.ExecuteTask(handle); TaskManager.Instance.GetTaskByHandle(handle).Event.WaitOne(); theTask.ReturnData = theAsset; }
//------------------------------------------------------------------------------- //------------------------------------------------------------------------------- public ManualResetEvent GetResetEvent(TaskHandle taskHandle) { ManualResetEvent resetEvent; m_dictionaryLock.TryEnterReadLock(5); { resetEvent = m_tasks[taskHandle].Event; } m_dictionaryLock.ExitReadLock(); return(resetEvent); }
//------------------------------------------------------------------------------- /// <summary> /// accessors /// </summary> /// <param name="taskHandle">the handle for the task to access.</param> /// <returns>Task</returns> //------------------------------------------------------------------------------- #region Get Functions public TaskType GetTaskByHandle(TaskHandle taskHandle) { TaskType taskType; m_dictionaryLock.TryEnterReadLock(5); { taskType = m_tasks[taskHandle]; } m_dictionaryLock.ExitReadLock(); return(taskType); }
//------------------------------------------------------------------------------- /// <summary> /// overloaded: Creates a task. /// </summary> /// <typeparam name="T">data type.</typeparam> /// <param name="aTaskCompletedCallBacks"> callback array upon complete.</param> /// <param name="userData">user data to be used in task.</param> /// <param name="task">thread</param> /// <param name="bContinuousExecution">if allow continuous execution.</param> /// <returns></returns> //------------------------------------------------------------------------------- public TaskHandle CreateTask <T>(TaskCompleted[] aTaskCompletedCallBacks, object userData, ParameterizedThreadStart task, bool bContinuousExecution) { Task <T> theTask = new Task <T>(aTaskCompletedCallBacks, userData, task, bContinuousExecution); theTask.Name = "Task " + m_nTaskCount; TaskHandle taskHandle = new TaskHandle(m_nTaskCount++); theTask.Handle = taskHandle; m_dictionaryLock.TryEnterWriteLock(5); { m_tasks.Add(taskHandle, theTask); } m_dictionaryLock.ExitWriteLock(); return(taskHandle); }
//------------------------------------------------------------------------------- /// <summary> /// Get return data from task after completion. /// </summary> /// <typeparam name="T">data type</typeparam> /// <param name="taskHandle">handle for particular task.</param> /// <returns>data from task.</returns> //------------------------------------------------------------------------------- public T GetReturnData <T>(TaskHandle taskHandle) { TaskType task; m_dictionaryLock.TryEnterReadLock(5); task = m_tasks[taskHandle]; m_dictionaryLock.ExitReadLock(); Debug.Assert(task != null); Type type = task.GetType(); PropertyInfo property = type.GetProperty("ReturnData"); MethodInfo methodInfo = property.GetGetMethod(); T returnData = (T)methodInfo.Invoke(task, null); DestroyTask(taskHandle); return(returnData); }
//------------------------------------------------------------------------------- /// <summary> /// Creates a new task. /// </summary> /// <typeparam name="T">T is the stored data type</typeparam> /// <param name="userData">user data to be used in the task.</param> /// <param name="task">the thread for the task.</param> /// <param name="bContinuousExecution">if the task is to continuously execute and readd its self.</param> /// <returns></returns> //------------------------------------------------------------------------------- #region Create Task Methods public TaskHandle CreateTask <T>(object userData, ParameterizedThreadStart task, bool bContinuousExecution) { //create reset event object. ManualResetEvent resetEvent = new ManualResetEvent(false); Task <T> theTask = new Task <T>(resetEvent, userData, task, bContinuousExecution); theTask.Name = "Task " + m_nTaskCount; //create new task handle to identify this task TaskHandle taskHandle = new TaskHandle(m_nTaskCount++); theTask.Handle = taskHandle; //lock and add task to dictionary. m_dictionaryLock.TryEnterWriteLock(5); { m_tasks.Add(taskHandle, theTask); } m_dictionaryLock.ExitWriteLock(); return(taskHandle); }
//---------------------------------------------------------------------------------- /// <summary> /// Stream assets in. /// </summary> /// <param name="szAssetGroupName"></param> /// <returns></returns> //---------------------------------------------------------------------------------- private StreamChunk StreamAssets(String szAssetGroupName) { List <LoadingTask> loadingTasks = new List <LoadingTask>(); List <ManualResetEvent> resetEvents = new List <ManualResetEvent>(); RegionLoader regionLoader = new RegionLoader(m_iServerProvider); regionLoader.RootDirectory = "Content"; StreamChunkDefinition streamChunkDefinition; String szFullPath = szAssetGroupName; streamChunkDefinition = regionLoader.Load <StreamChunkDefinition>(szFullPath); List <TaskHandle> aTaskHandles = new List <TaskHandle>(streamChunkDefinition.m_assetDefinitions.Count); StreamChunk streamChunk = new StreamChunk(szAssetGroupName); foreach (AssetDefinition assetDef in streamChunkDefinition.m_assetDefinitions) { #if MT_LOAD #region Multi-Threaded Version LoadingTask loadTask = new LoadingTask(); TaskHandle taskHandle = TaskManager.Instance.CreateTask <Object>(new LoadingData(assetDef, regionLoader), loadTask.Load, false); aTaskHandles.Add(taskHandle); loadingTasks.Add(loadTask); #endregion #else #region Single-Thread Version // Object assetObject; // String szFullName = assetDef.m_szDirectory + assetDef.m_szAssetName; // Type creationType; // // creationType = Type.GetType(assetDef.m_szAssetType, true); // Debug.Assert(creationType != null); // // MethodInfo genericMethod = methodInfo.MakeGenericMethod(creationType); // assetObject = genericMethod.Invoke(contentManager, new object[] { szFullName }); // Debug.Assert(assetObject != null); // // streamChunk.AddAsset(assetObject); #endregion #endif } //These two steps could be merged to one...saving memory ...but doing it like this for clarity atm. foreach (TaskHandle taskHandle in aTaskHandles) { TaskManager.Instance.ExecuteTask(taskHandle); resetEvents.Add(TaskManager.Instance.GetTaskByHandle(taskHandle).Event); } WaitHandle.WaitAll(resetEvents.ToArray()); Console.WriteLine("Load Job done."); foreach (LoadingTask task in loadingTasks) { streamChunk.AddAsset(task.HashCode, task.AssetObject); } streamChunk.OwnContentManager = regionLoader; return(streamChunk); }