aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/XMREngine/XMRScriptThread.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/ScriptEngine/XMREngine/XMRScriptThread.cs102
1 files changed, 102 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/XMREngine/XMRScriptThread.cs b/OpenSim/Region/ScriptEngine/XMREngine/XMRScriptThread.cs
new file mode 100644
index 0000000..7103556
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/XMREngine/XMRScriptThread.cs
@@ -0,0 +1,102 @@
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
28using OpenSim.Framework.Monitoring;
29using System;
30using System.Collections.Generic;
31using System.Threading;
32
33namespace OpenSim.Region.ScriptEngine.XMREngine
34{
35 /**
36 * @brief There are NUMSCRIPTHREADWKRS of these.
37 * Each sits in a loop checking the Start and Yield queues for
38 * a script to run and calls the script as a microthread.
39 */
40 public class XMRScriptThread
41 {
42 public bool m_WakeUpThis = false;
43 public DateTime m_LastRanAt = DateTime.MinValue;
44 public int m_ScriptThreadTID = 0;
45 public long m_ScriptExecTime = 0;
46 private Thread thd;
47 private XMREngine engine;
48 public XMRInstance m_RunInstance = null;
49
50 public XMRScriptThread(XMREngine eng, int i)
51 {
52 engine = eng;
53 if(i < 0)
54 thd = XMREngine.StartMyThread(RunScriptThread, "xmrengine script", ThreadPriority.Normal);
55 else
56 thd = XMREngine.StartMyThread(RunScriptThread, "xmrengineExec" + i.ToString(), ThreadPriority.Normal);
57 engine.AddThread(thd, this);
58 m_ScriptThreadTID = thd.ManagedThreadId;
59 }
60
61 public void Terminate()
62 {
63 m_WakeUpThis = true;
64 if(!thd.Join(250))
65 thd.Abort();
66
67 engine.RemoveThread(thd);
68
69 thd = null;
70 }
71
72 /**
73 * @brief Wake up this XMRScriptThread instance.
74 */
75 public void WakeUpScriptThread()
76 {
77 m_WakeUpThis = true;
78 }
79
80 /**
81 * @brief A script instance was just removed from the Start or Yield Queue.
82 * So run it for a little bit then stick in whatever queue it should go in.
83 */
84
85 private void RunScriptThread()
86 {
87 engine.RunScriptThread(this);
88 }
89
90 public void RunInstance (XMRInstance inst)
91 {
92 m_LastRanAt = DateTime.UtcNow;
93 m_ScriptExecTime -= (long)(m_LastRanAt - DateTime.MinValue).TotalMilliseconds;
94 inst.m_IState = XMRInstState.RUNNING;
95 m_RunInstance = inst;
96 XMRInstState newIState = inst.RunOne();
97 m_RunInstance = null;
98 engine.HandleNewIState(inst, newIState);
99 m_ScriptExecTime += (long)(DateTime.UtcNow - DateTime.MinValue).TotalMilliseconds;
100 }
101 }
102}