XMRScriptThread.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using OpenSim.Framework;
  28. using OpenSim.Framework.Monitoring;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Threading;
  32. namespace OpenSim.Region.ScriptEngine.Yengine
  33. {
  34. public partial class Yengine
  35. {
  36. private int m_WakeUpOne = 0;
  37. public object m_WakeUpLock = new object();
  38. private Dictionary<int, XMRInstance> m_RunningInstances = new Dictionary<int, XMRInstance>();
  39. private bool m_SuspendScriptThreadFlag = false;
  40. private bool m_WakeUpThis = false;
  41. public DateTime m_LastRanAt = DateTime.MinValue;
  42. public long m_ScriptExecTime = 0;
  43. public void StartThreadWorker(int i, ThreadPriority priority, string sceneName)
  44. {
  45. Thread thd;
  46. if(i >= 0)
  47. thd = Yengine.StartMyThread(RunScriptThread, "YScript" + i.ToString() + " (" + sceneName +")", priority, -1);
  48. else
  49. thd = Yengine.StartMyThread(RunScriptThread, "YScript", priority, -1);
  50. lock(m_WakeUpLock)
  51. m_RunningInstances.Add(thd.ManagedThreadId, null);
  52. }
  53. public void StopThreadWorkers()
  54. {
  55. lock(m_WakeUpLock)
  56. {
  57. while(m_RunningInstances.Count != 0)
  58. {
  59. Monitor.PulseAll(m_WakeUpLock);
  60. Monitor.Wait(m_WakeUpLock, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2);
  61. }
  62. }
  63. }
  64. /**
  65. * @brief Something was just added to the Start or Yield queue so
  66. * wake one of the RunScriptThread() instances to run it.
  67. */
  68. public void WakeUpOne()
  69. {
  70. lock(m_WakeUpLock)
  71. {
  72. m_WakeUpOne++;
  73. Monitor.Pulse(m_WakeUpLock);
  74. }
  75. }
  76. public void SuspendThreads()
  77. {
  78. lock(m_WakeUpLock)
  79. {
  80. m_SuspendScriptThreadFlag = true;
  81. Monitor.PulseAll(m_WakeUpLock);
  82. }
  83. }
  84. public void ResumeThreads()
  85. {
  86. lock(m_WakeUpLock)
  87. {
  88. m_SuspendScriptThreadFlag = false;
  89. Monitor.PulseAll(m_WakeUpLock);
  90. }
  91. }
  92. /**
  93. * @brief Thread that runs the scripts.
  94. *
  95. * There are NUMSCRIPTHREADWKRS of these.
  96. * Each sits in a loop checking the Start and Yield queues for
  97. * a script to run and calls the script as a microthread.
  98. */
  99. private void RunScriptThread()
  100. {
  101. int tid = System.Threading.Thread.CurrentThread.ManagedThreadId;
  102. ThreadStart thunk;
  103. XMRInstance inst;
  104. bool didevent;
  105. while(!m_Exiting)
  106. {
  107. Yengine.UpdateMyThread();
  108. lock(m_WakeUpLock)
  109. {
  110. // Maybe there are some scripts waiting to be migrated in or out.
  111. thunk = null;
  112. if(m_ThunkQueue.Count > 0)
  113. thunk = m_ThunkQueue.Dequeue();
  114. // Handle 'xmr resume/suspend' commands.
  115. else if(m_SuspendScriptThreadFlag && !m_Exiting)
  116. {
  117. Monitor.Wait(m_WakeUpLock, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2);
  118. continue;
  119. }
  120. }
  121. if(thunk != null)
  122. {
  123. thunk();
  124. continue;
  125. }
  126. if(m_StartProcessing)
  127. {
  128. // If event just queued to any idle scripts
  129. // start them right away. But only start so
  130. // many so we can make some progress on yield
  131. // queue.
  132. int numStarts;
  133. didevent = false;
  134. for(numStarts = 5; numStarts >= 0; --numStarts)
  135. {
  136. lock(m_StartQueue)
  137. inst = m_StartQueue.RemoveHead();
  138. if(inst == null)
  139. break;
  140. if (inst.m_IState == XMRInstState.SUSPENDED)
  141. continue;
  142. if (inst.m_IState != XMRInstState.ONSTARTQ)
  143. throw new Exception("bad state");
  144. RunInstance(inst, tid);
  145. if(m_SuspendScriptThreadFlag || m_Exiting)
  146. continue;
  147. didevent = true;
  148. }
  149. // If there is something to run, run it
  150. // then rescan from the beginning in case
  151. // a lot of things have changed meanwhile.
  152. //
  153. // These are considered lower priority than
  154. // m_StartQueue as they have been taking at
  155. // least one quantum of CPU time and event
  156. // handlers are supposed to be quick.
  157. lock(m_YieldQueue)
  158. inst = m_YieldQueue.RemoveHead();
  159. if(inst != null)
  160. {
  161. if (inst.m_IState == XMRInstState.SUSPENDED)
  162. continue;
  163. if (inst.m_IState != XMRInstState.ONYIELDQ)
  164. throw new Exception("bad state");
  165. RunInstance(inst, tid);
  166. continue;
  167. }
  168. // If we left something dangling in the m_StartQueue or m_YieldQueue, go back to check it.
  169. if(didevent)
  170. continue;
  171. }
  172. // Nothing to do, sleep.
  173. lock(m_WakeUpLock)
  174. {
  175. if(!m_WakeUpThis && (m_WakeUpOne <= 0) && !m_Exiting)
  176. Monitor.Wait(m_WakeUpLock, Watchdog.DEFAULT_WATCHDOG_TIMEOUT_MS / 2);
  177. m_WakeUpThis = false;
  178. if((m_WakeUpOne > 0) && (--m_WakeUpOne > 0))
  179. Monitor.Pulse(m_WakeUpLock);
  180. }
  181. }
  182. lock(m_WakeUpLock)
  183. m_RunningInstances.Remove(tid);
  184. Yengine.MyThreadExiting();
  185. }
  186. /**
  187. * @brief A script instance was just removed from the Start or Yield Queue.
  188. * So run it for a little bit then stick in whatever queue it should go in.
  189. */
  190. private void RunInstance(XMRInstance inst, int tid)
  191. {
  192. m_LastRanAt = DateTime.UtcNow;
  193. m_ScriptExecTime -= (long)(m_LastRanAt - DateTime.MinValue).TotalMilliseconds;
  194. inst.m_IState = XMRInstState.RUNNING;
  195. lock(m_WakeUpLock)
  196. m_RunningInstances[tid] = inst;
  197. XMRInstState newIState = inst.RunOne();
  198. lock(m_WakeUpLock)
  199. m_RunningInstances[tid] = null;
  200. HandleNewIState(inst, newIState);
  201. m_ScriptExecTime += (long)(DateTime.UtcNow - DateTime.MinValue).TotalMilliseconds;
  202. }
  203. }
  204. }