1
0

XMRInstMain.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 System;
  28. using System.Threading;
  29. using System.Reflection;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.ScriptEngine.Interfaces;
  35. using OpenSim.Region.ScriptEngine.Shared;
  36. using OpenSim.Region.Framework.Scenes;
  37. using log4net;
  38. // This class exists in the main app domain
  39. //
  40. namespace OpenSim.Region.ScriptEngine.Yengine
  41. {
  42. /**
  43. * @brief Which queue it is in as far as running is concerned,
  44. * ie, m_StartQueue, m_YieldQueue, m_SleepQueue, etc.
  45. * Allowed transitions:
  46. * Starts in CONSTRUCT when constructed
  47. * CONSTRUCT->ONSTARTQ : only by thread that constructed and compiled it
  48. * IDLE->ONSTARTQ,RESETTING : by any thread but must have m_QueueLock when transitioning
  49. * ONSTARTQ->RUNNING,RESETTING : only by thread that removed it from m_StartQueue
  50. * ONYIELDQ->RUNNING,RESETTING : only by thread that removed it from m_YieldQueue
  51. * ONSLEEPQ->REMDFROMSLPQ : by any thread but must have m_SleepQueue when transitioning
  52. * REMDFROMSLPQ->ONYIELDQ,RESETTING : only by thread that removed it from m_SleepQueue
  53. * RUNNING->whatever1 : only by thread that transitioned it to RUNNING
  54. * whatever1 = IDLE,ONSLEEPQ,ONYIELDQ,ONSTARTQ,SUSPENDED,FINISHED
  55. * FINSHED->whatever2 : only by thread that transitioned it to FINISHED
  56. * whatever2 = IDLE,ONSTARTQ,DISPOSED
  57. * SUSPENDED->ONSTARTQ : by any thread (NOT YET IMPLEMENTED, should be under some kind of lock?)
  58. * RESETTING->ONSTARTQ : only by the thread that transitioned it to RESETTING
  59. */
  60. public enum XMRInstState
  61. {
  62. CONSTRUCT, // it is being constructed
  63. IDLE, // nothing happening (finished last event and m_EventQueue is empty)
  64. ONSTARTQ, // inserted on m_Engine.m_StartQueue
  65. RUNNING, // currently being executed by RunOne()
  66. ONSLEEPQ, // inserted on m_Engine.m_SleepQueue
  67. REMDFROMSLPQ, // removed from m_SleepQueue but not yet on m_YieldQueue
  68. ONYIELDQ, // inserted on m_Engine.m_YieldQueue
  69. FINISHED, // just finished handling an event
  70. SUSPENDED, // m_SuspendCount > 0
  71. RESETTING, // being reset via external call
  72. DISPOSED // has been disposed
  73. }
  74. public partial class XMRInstance: XMRInstAbstract, IDisposable
  75. {
  76. /******************************************************************\
  77. * This module contains the instance variables for XMRInstance. *
  78. \******************************************************************/
  79. public const int MAXEVENTQUEUE = 64;
  80. public static readonly DetectParams[] zeroDetectParams = new DetectParams[0];
  81. public static readonly object[] zeroObjectArray = new object[0];
  82. public static readonly ILog m_log =
  83. LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  84. public XMRInstance m_NextInst; // used by XMRInstQueue
  85. public XMRInstance m_PrevInst;
  86. // For a given m_Item.AssetID, do we have the compiled object code and where
  87. // is it?
  88. public static object m_CompileLock = new object();
  89. private static Dictionary<string, ScriptObjCode> m_CompiledScriptObjCode = new Dictionary<string, ScriptObjCode>();
  90. public XMRInstState m_IState;
  91. public bool m_ForceRecomp = false;
  92. public SceneObjectPart m_Part = null;
  93. public uint m_LocalID = 0;
  94. public TaskInventoryItem m_Item = null;
  95. public UUID m_ItemID;
  96. public UUID m_PartUUID;
  97. private string m_CameFrom;
  98. private string m_ScriptObjCodeKey;
  99. private Yengine m_Engine = null;
  100. private string m_ScriptBasePath;
  101. private string m_StateFileName;
  102. public string m_SourceCode;
  103. public bool m_PostOnRez;
  104. private DetectParams[] m_DetectParams = null;
  105. public int m_StartParam = 0;
  106. public StateSource m_StateSource;
  107. public string m_DescName;
  108. private bool[] m_HaveEventHandlers = new bool[(int)ScriptEventCode.Size];
  109. public int m_StackSize;
  110. public int m_HeapSize;
  111. private ArrayList m_CompilerErrors;
  112. private DateTime m_LastRanAt = DateTime.MinValue;
  113. //private string m_RunOnePhase = "hasn't run";
  114. //private string m_CheckRunPhase = "hasn't checked";
  115. public int m_InstEHEvent = 0; // number of events dequeued (StartEventHandler called)
  116. public int m_InstEHSlice = 0; // number of times handler timesliced (ResumeEx called)
  117. public double m_CPUTime = 0; // accumulated CPU time (milliseconds)
  118. public double m_SliceStart = 0; // when did current exec start
  119. // If code needs to have both m_QueueLock and m_RunLock,
  120. // be sure to lock m_RunLock first then m_QueueLock, as
  121. // that is the order used in RunOne().
  122. // These locks are currently separated to allow the script
  123. // to call API routines that queue events back to the script.
  124. // If we just had one lock, then the queuing would deadlock.
  125. // guards m_DetachQuantum, m_EventQueue, m_EventCounts, m_Running, m_Suspended
  126. public Object m_QueueLock = new Object();
  127. // true if allowed to accept new events
  128. public bool m_Running = true;
  129. // queue of events that haven't been acted upon yet
  130. public LinkedList<EventParams> m_EventQueue = new LinkedList<EventParams>();
  131. // number of events of each code currently in m_EventQueue.
  132. private int[] m_EventCounts = new int[(int)ScriptEventCode.Size];
  133. // locked whilst running on the microthread stack (or about to run on it or just ran on it)
  134. private Object m_RunLock = new Object();
  135. // script won't step while > 0. bus-atomic updates only.
  136. private int m_SuspendCount = 0;
  137. // don't run any of script until this time
  138. // or until one of these events are queued
  139. public DateTime m_SleepUntil = DateTime.MinValue;
  140. public int m_SleepEventMask1 = 0;
  141. public int m_SleepEventMask2 = 0;
  142. private XMRLSL_Api m_XMRLSLApi;
  143. /*
  144. * Makes sure migration data version is same on both ends.
  145. */
  146. public static byte migrationVersion = 11;
  147. // Incremented each time script gets reset.
  148. public int m_ResetCount = 0;
  149. // Scripts start suspended now. This means that event queues will
  150. // accept events, but will not actually run them until the core
  151. // tells it it's OK. This is needed to prevent loss of link messages
  152. // in complex objects, where no event can be allowed to run until
  153. // all possible link message receivers' queues are established.
  154. // Guarded by m_QueueLock.
  155. public bool m_Suspended = true;
  156. // We really don't want to save state for a script that hasn't had
  157. // a chance to run, because it's state will be blank. That would
  158. // cause attachment state loss.
  159. public bool m_HasRun = false;
  160. // When llDie is executed within the attach(NULL_KEY) event of
  161. // a script being detached to inventory, the DeleteSceneObject call
  162. // it causes will delete the script instances before their state can
  163. // be saved. Therefore, the instance needs to know that it's being
  164. // detached to inventory, rather than to ground.
  165. // Also, the attach(NULL_KEY) event needs to run with priority, and
  166. // it also needs to have a limited quantum.
  167. // If this is nonzero, we're detaching to inventory.
  168. // Guarded by m_QueueLock.
  169. private int m_DetachQuantum = 0;
  170. // Finally, we need to wait until the quantum is done, or the script
  171. // suspends itself. This should be efficient, so we use an event
  172. // for it instead of spinning busy.
  173. // It's born ready, but will be reset when the detach is posted.
  174. // It will then be set again on suspend/completion
  175. private ManualResetEvent m_DetachReady = new ManualResetEvent(true);
  176. // llmineventdelay support
  177. double m_minEventDelay = 0.0;
  178. double m_nextEventTime = 0.0;
  179. private static readonly Dictionary<string, ScriptEventCode> m_eventCodeMap = new Dictionary<string, ScriptEventCode>()
  180. {
  181. {"attach", ScriptEventCode.attach},
  182. {"at_rot_target", ScriptEventCode.at_rot_target},
  183. {"at_target", ScriptEventCode.at_target},
  184. {"collision", ScriptEventCode.collision},
  185. {"collision_end", ScriptEventCode.collision_end},
  186. {"collision_start", ScriptEventCode.collision_start},
  187. {"control", ScriptEventCode.control},
  188. {"dataserver", ScriptEventCode.dataserver},
  189. {"email", ScriptEventCode.email},
  190. {"http_response", ScriptEventCode.http_response},
  191. {"land_collision", ScriptEventCode.land_collision},
  192. {"land_collision_end", ScriptEventCode.land_collision_end},
  193. {"land_collision_start", ScriptEventCode.land_collision_start},
  194. {"listen", ScriptEventCode.listen},
  195. {"money", ScriptEventCode.money},
  196. {"moving_end", ScriptEventCode.moving_end},
  197. {"moving_start", ScriptEventCode.moving_start},
  198. {"not_at_rot_target", ScriptEventCode.not_at_rot_target},
  199. {"not_at_target", ScriptEventCode.not_at_target},
  200. {"remote_data", ScriptEventCode.remote_data},
  201. {"run_time_permissions", ScriptEventCode.run_time_permissions},
  202. {"state_entry", ScriptEventCode.state_entry},
  203. {"state_exit", ScriptEventCode.state_exit},
  204. {"timer", ScriptEventCode.timer},
  205. {"touch", ScriptEventCode.touch},
  206. {"touch_end", ScriptEventCode.touch_end},
  207. {"touch_start", ScriptEventCode.touch_start},
  208. {"transaction_result", ScriptEventCode.transaction_result},
  209. {"object_rez", ScriptEventCode.object_rez},
  210. {"changed", ScriptEventCode.changed},
  211. {"link_message", ScriptEventCode.link_message},
  212. {"no_sensor", ScriptEventCode.no_sensor},
  213. {"on_rez", ScriptEventCode.on_rez},
  214. {"sensor", ScriptEventCode.sensor},
  215. {"http_request", ScriptEventCode.http_request},
  216. {"path_update", ScriptEventCode.path_update},
  217. {"linkset_data", ScriptEventCode.linkset_data}
  218. };
  219. }
  220. }