Skip to content

Instantly share code, notes, and snippets.

@jmschrack
Created August 4, 2017 02:34
Show Gist options
  • Save jmschrack/df2c151acf5333caec048105896bf45b to your computer and use it in GitHub Desktop.
Save jmschrack/df2c151acf5333caec048105896bf45b to your computer and use it in GitHub Desktop.
Framework for worker threads to pass commands back to the main thread in Unity
/*
MIT License
Copyright 2017 Digital Precept LLC
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MainThreadBridge : MonoBehaviour {
/*
* Static variables
*/
public static MainThreadBridge Initialize(){
if (instance == null) {
GameObject go = new GameObject ();
go.name = "MainThreadBridge";
instance=go.AddComponent<MainThreadBridge> ();
}
return instance;
}
static MainThreadBridge instance;
/*
* Local variables
*/
Queue<MainThreadAction> _queue = new Queue<MainThreadAction> ();
public float maxCPUTime=0.01f;
public int minProcess = 1;
void Update () {
ProcessActions ();
}
void ProcessActions(){
int _processedActions = 0;
float _processStartTime = Time.realtimeSinceStartup;
//if we have commands and we either haven't hit the minimum or we have more time
while(_queue.Count>0&&(_processedActions<minProcess||Time.realtimeSinceStartup-_processStartTime<maxCPUTime)){
MainThreadAction actionToProcess;
//if we don't lock it, it's possible for us to pull a null value off the queue
//when stress testing instantiating 1million gameobjects, there was negligible speed impact
lock (_queue) {
actionToProcess = _queue.Dequeue ();
}
//because I don't take chance
if (actionToProcess == null)
continue;
actionToProcess.command ();
actionToProcess.processed = true;
_processedActions++;
}
}
///<summary>
///Adds the Action to the Main Thread's work queue.
///WARNING: due to the way C# handles delegates, do not use variables named "_queue", "_processedActions", or "_processStartTime"
///</summary>
public void Enqueue(MainThreadAction action){
lock (_queue) {
_queue.Enqueue (action);
}
}
}
public class MainThreadAction{
public delegate void Actions();
public Actions command;
public bool processed=false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment