Пример #1
0
 // 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);
                 }
             }
         }
     }
 }
Пример #2
0
        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();
        }
Пример #3
0
 public static void Push(QueuedFileChange change)
 {
     lock (changeQueueLock) {
         bool doPulse = changeQueue.Count == 0;
         changeQueue.Enqueue(change);
         if (doPulse)
         {
             lock (workPulsePadlock) {
                 Monitor.Pulse(workPulsePadlock);
             }
         }
     }
 }
Пример #4
0
        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);
        }
Пример #5
0
 private static void ClearCurrent()
 {
     currentChange = null;
 }
Пример #6
0
 private static void ChangeCurrent(QueuedFileChange work)
 {
     currentChange = work;
 }