Tanoda
Loom.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading;
5using UnityEngine;
6using ThreadPriority = System.Threading.ThreadPriority;
7
8//Loom handles threading
9public class Loom : MonoBehaviour
10{
11 private static Loom _current;
12
13 public static Loom Current
14 {
15 get
16 {
17 if (_current == null && Application.isPlaying)
18 {
19 var g = GameObject.Find("Loom");
20 if (g == null) g = new GameObject("Loom");
21
22 _current = g.GetComponent<Loom>() ?? g.AddComponent<Loom>();
23 }
24
25 return _current;
26 }
27 }
28
29 void Awake()
30 {
31 if (_current != null && _current != this)
32 Destroy(gameObject);
33 else
34 _current = this;
35 }
36
37 private readonly List<Action> _actions = new List<Action>();
38
39 public class DelayedQueueItem
40 {
41 public float time;
42 public Action action;
43 public string name;
44 }
45
46 private readonly List<DelayedQueueItem> _delayed = new List<DelayedQueueItem>();
47
48 public static void QueueOnMainThread(Action action, float time, string name)
49 {
50 lock (Current._delayed)
51 {
52 if (Current._delayed.Any(d => d.name == name))
53 return;
54 QueueOnMainThread(action, time);
55 }
56 }
57
58 public static void QueueOnMainThread(Action action, string name)
59 {
60 QueueOnMainThread(action, 0, name);
61 }
62
63 public static void QueueOnMainThread(Action action, float time)
64 {
65 if (time != 0)
66 lock (Current._delayed)
67 {
68 Current._delayed.Add(new DelayedQueueItem {time = Time.time + time, action = action});
69 }
70 else
71 lock (Current._actions)
72 {
73 Current._actions.Add(action);
74 }
75 }
76
77 public static void QueueOnMainThread(Action action)
78 {
79 lock (Current._actions)
80 {
81 Current._actions.Add(action);
82 }
83 }
84
85 public static void RunAsync(Action a)
86 {
87 var t = new Thread(RunAction);
88 t.Priority = ThreadPriority.Normal;
89 t.Start(a);
90 }
91
92 private static void RunAction(object action)
93 {
94 ((Action) action)();
95 }
96
97
98 readonly List<Action> toBeRun = new List<Action>();
99 readonly List<DelayedQueueItem> toBeDelayed = new List<DelayedQueueItem>();
100
101 void Update()
102 {
103 //Process the non-delayed actions
104 lock (_actions)
105 {
106 toBeRun.AddRange(_actions);
107 _actions.Clear();
108 }
109
110 foreach (var a in toBeRun)
111 try
112 {
113 a();
114 }
115 catch (Exception e)
116 {
117 Debug.LogError("Queued Exception: " + e);
118 }
119
120 toBeRun.Clear();
121 lock (_delayed)
122 {
123 toBeDelayed.AddRange(_delayed);
124 }
125
126 foreach (var delayed in toBeDelayed.Where(d => d.time <= Time.time))
127 {
128 lock (_delayed)
129 {
130 _delayed.Remove(delayed);
131 }
132
133 try
134 {
135 delayed.action();
136 }
137 catch (Exception e)
138 {
139 Debug.LogError("Delayed Exception:" + e);
140 }
141 }
142
143 toBeDelayed.Clear();
144 }
145}
System.Threading.ThreadPriority ThreadPriority
Definition: Loom.cs:6
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
Definition: Loom.cs:10
static void QueueOnMainThread(Action action)
Definition: Loom.cs:77
static Loom Current
Definition: Loom.cs:14
static void RunAsync(Action a)
Definition: Loom.cs:85
static void QueueOnMainThread(Action action, float time, string name)
Definition: Loom.cs:48
static void QueueOnMainThread(Action action, float time)
Definition: Loom.cs:63
static void QueueOnMainThread(Action action, string name)
Definition: Loom.cs:58