Terraria ModLoader  0.10.1.5
A framework for Terraria mods
Terraria.ModLoader.GLCallLocker Class Reference

FNA uses a single-threaded GL context. This class helps with tracking down related issues and deadlocks. See https://github.com/blushiemagic/tModLoader/issues/237 and https://github.com/FNA-XNA/FNA/blob/master/src/FNAPlatform/OpenGLDevice.cs#L4676 It also improves loading performance on FNA More...

+ Collaboration diagram for Terraria.ModLoader.GLCallLocker:

Static Public Member Functions

static void Enter (object lockObj)
 

Private Member Functions

delegate void hook_ForceToMainThread (orig_ForceToMainThread orig, object self, Action action)
 
delegate void orig_ForceToMainThread (object self, Action action)
 

Static Private Member Functions

static void HookForceToMainThread (orig_ForceToMainThread orig, object self, Action action)
 
static void RunGLActions ()
 

Static Private Attributes

static AutoResetEvent actionQueuedEvent = new AutoResetEvent(false)
 
static FieldInfo f_actions
 
static FieldInfo f_GLDevice = typeof(GraphicsDevice).GetField("GLDevice", BindingFlags.Instance | BindingFlags.NonPublic)
 
static MethodInfo m_RunActions
 
static HashSet< string > pastStackTraces = new HashSet<string>()
 

Detailed Description

FNA uses a single-threaded GL context. This class helps with tracking down related issues and deadlocks. See https://github.com/blushiemagic/tModLoader/issues/237 and https://github.com/FNA-XNA/FNA/blob/master/src/FNAPlatform/OpenGLDevice.cs#L4676 It also improves loading performance on FNA

Definition at line 17 of file GLCallLocker.cs.

Member Function Documentation

static void Terraria.ModLoader.GLCallLocker.Enter ( object  lockObj)
static

Definition at line 19 of file GLCallLocker.cs.

19  {
20 #if WINDOWS
21  Monitor.Enter(lockObj);
22 #else
23  if (Thread.CurrentThread.ManagedThreadId != Main.mainThreadId) {
24  Monitor.Enter(lockObj);
25  return;
26  }
27 
28  while (!Monitor.TryEnter(lockObj)) {
29  RunGLActions();
30  }
31 #endif
32  }
delegate void Terraria.ModLoader.GLCallLocker.hook_ForceToMainThread ( orig_ForceToMainThread  orig,
object  self,
Action  action 
)
private
static void Terraria.ModLoader.GLCallLocker.HookForceToMainThread ( orig_ForceToMainThread  orig,
object  self,
Action  action 
)
staticprivate

Definition at line 108 of file GLCallLocker.cs.

108  {
109  if (ModCompile.DeveloperMode && !ActionsAreSpeedrun && Thread.CurrentThread.ManagedThreadId != Main.mainThreadId) {
110  var stackTrace = new StackTrace(false); // line numbers not supported on mono yet
111  var s = stackTrace.ToString();
112  if (pastStackTraces.Add(s))
113  Logging.tML.Debug("GL function invoked on worker-thread in a single-threaded context. Execution will be delayed till next frame\n" + s);
114  }
115 
116  orig(self, action);
117 
118  if (ActionsAreSpeedrun && Thread.CurrentThread.ManagedThreadId != Main.mainThreadId)
119  actionQueuedEvent.Set();
120  }
static AutoResetEvent actionQueuedEvent
static HashSet< string > pastStackTraces
delegate void Terraria.ModLoader.GLCallLocker.orig_ForceToMainThread ( object  self,
Action  action 
)
private
static void Terraria.ModLoader.GLCallLocker.RunGLActions ( )
staticprivate

Definition at line 95 of file GLCallLocker.cs.

95  {
96  var glDevice = f_GLDevice.GetValue(Main.instance.GraphicsDevice);
97  if (m_RunActions == null)
98  m_RunActions = glDevice.GetType().GetMethod("RunActions", BindingFlags.Instance | BindingFlags.NonPublic);
99 
100  m_RunActions.Invoke(glDevice, new object[0]);
101  }
static MethodInfo m_RunActions
Definition: GLCallLocker.cs:93

Member Data Documentation

AutoResetEvent Terraria.ModLoader.GLCallLocker.actionQueuedEvent = new AutoResetEvent(false)
staticprivate

Definition at line 103 of file GLCallLocker.cs.

FieldInfo Terraria.ModLoader.GLCallLocker.f_actions
staticprivate

Definition at line 92 of file GLCallLocker.cs.

FieldInfo Terraria.ModLoader.GLCallLocker.f_GLDevice = typeof(GraphicsDevice).GetField("GLDevice", BindingFlags.Instance | BindingFlags.NonPublic)
staticprivate

Definition at line 91 of file GLCallLocker.cs.

MethodInfo Terraria.ModLoader.GLCallLocker.m_RunActions
staticprivate

Definition at line 93 of file GLCallLocker.cs.

HashSet<string> Terraria.ModLoader.GLCallLocker.pastStackTraces = new HashSet<string>()
staticprivate

Definition at line 105 of file GLCallLocker.cs.