File: PrimaryThreadLoader.cs

package info (click to toggle)
monogame 2.5.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,060 kB
  • ctags: 10,325
  • sloc: cs: 65,996; xml: 591; makefile: 22; ansic: 8
file content (88 lines) | stat: -rw-r--r-- 2,799 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Collections.Generic;

namespace Microsoft.Xna.Framework
{
    /// <summary>
    /// Interface used to add an object to be loaded on the primary thread
    /// </summary>
    interface IPrimaryThreadLoaded
    {
        bool Load();
    }

    /// <summary>
    /// Static class that is called before every draw to load resources that need to finish loading on the primary thread
    /// </summary>
    internal static class PrimaryThreadLoader
    {
        private static readonly object ListLockObject = new object();
        private static readonly List<IPrimaryThreadLoaded> NeedToLoad = new List<IPrimaryThreadLoaded>(); 
        private static readonly List<IPrimaryThreadLoaded> RemoveList = new List<IPrimaryThreadLoaded>();
        private static DateTime _lastUpdate = DateTime.UtcNow;

        public static void AddToList(IPrimaryThreadLoaded primaryThreadLoaded)
        {
            lock (ListLockObject)
            {
                NeedToLoad.Add(primaryThreadLoaded);
            }
        }

        public static void RemoveFromList(IPrimaryThreadLoaded primaryThreadLoaded)
        {
            lock (ListLockObject)
            {
                NeedToLoad.Remove(primaryThreadLoaded);
            }
        }

        public static void RemoveFromList(List<IPrimaryThreadLoaded> primaryThreadLoadeds)
        {
            lock (ListLockObject)
            {
                foreach (var primaryThreadLoaded in primaryThreadLoadeds)
                {
                    NeedToLoad.Remove(primaryThreadLoaded);
                }
            }
        }

        public static void Clear()
        {
            lock(ListLockObject)
            {
                NeedToLoad.Clear();
            }
        }

        /// <summary>
        /// Loops through list and loads the item.  If successful, it is removed from the list.
        /// </summary>
        public static void DoLoads()
        {
            if((DateTime.UtcNow - _lastUpdate).Milliseconds < 250) return;

            _lastUpdate = DateTime.UtcNow;
            lock (ListLockObject)
            {
                for (int i = 0; i < NeedToLoad.Count; i++)
                {
                    var primaryThreadLoaded = NeedToLoad[i];
                    if (primaryThreadLoaded.Load())
                    {
                        RemoveList.Add(primaryThreadLoaded);
                    }
                }

                for (int i = 0; i < RemoveList.Count; i++)
                {
                    var primaryThreadLoaded = RemoveList[i];
                    NeedToLoad.Remove(primaryThreadLoaded);
                }

                RemoveList.Clear();
            }
        }
    }
}