// Accessed only by the thread: private static void ThreadWorker() { while (true) { if (HasFileInQueue()) { try { currentChange.Apply(fileDatabases); } catch (Exception e) { Debug.LogError("Async file change error: " + e); } ClearCurrent(); } else { QueuedFileChange work = TryToGetNext(); if (work != null) { ChangeCurrent(work); } else { lock (workPulsePadlock) { Monitor.Wait(workPulsePadlock); } } } } }
public static void InitThreadPool() { workPulsePadlock = new object(); currentChange = null; changeQueueLock = new object(); changeQueue = new Queue <QueuedFileChange>(100); // Arbitrary starting size - just so long as some capacity is expected shutdown = false; thread = new Thread(new ThreadStart(ThreadWorker)); thread.Start(); }
public static void Push(QueuedFileChange change) { lock (changeQueueLock) { bool doPulse = changeQueue.Count == 0; changeQueue.Enqueue(change); if (doPulse) { lock (workPulsePadlock) { Monitor.Pulse(workPulsePadlock); } } } }
private static QueuedFileChange TryToGetNext() { QueuedFileChange work = null; lock (changeQueueLock) { while (work == null && changeQueue.Count > 0) { work = changeQueue.Dequeue(); // If we're shutting down, throw away any queued loading work if (shutdown == true) { if (work.GetType() == typeof(QueuedFileLoad)) { work = null; } } } } return(work); }
private static void ClearCurrent() { currentChange = null; }
private static void ChangeCurrent(QueuedFileChange work) { currentChange = work; }