From 883f7dde3884bca10f324f6a31e0882cf23c04d8 Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Fri, 24 Apr 2009 05:33:23 +0000 Subject: * Implements Microthreading for MRM scripting. * This is achieved through two new keywords "microthreaded" and "relax". example: public microthreaded void MyFunc(...) { ... relax; ... } --- .../Test/Microthreads/MicrothreadSample.txt | 40 ++++++++++++++++++++ .../Scripting/Minimodule/Test/TestModule.cs | 43 ++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/Microthreads/MicrothreadSample.txt (limited to 'OpenSim/Region/OptionalModules/Scripting/Minimodule/Test') diff --git a/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/Microthreads/MicrothreadSample.txt b/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/Microthreads/MicrothreadSample.txt new file mode 100644 index 0000000..dc15c47 --- /dev/null +++ b/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/Microthreads/MicrothreadSample.txt @@ -0,0 +1,40 @@ +//MRM:C# +using System.Collections; +using System.Collections.Generic; +using OpenSim.Region.OptionalModules.Scripting.Minimodule; + +namespace OpenSim +{ + class MiniModule : MRMBase + { + public microthreaded void MicroThreadFunction(string testparam) + { + Host.Object.Say("Hello " + testparam); + + relax; // the 'relax' keyword gives up processing time. + // and should be inserted before, after or in + // any computationally "heavy" zones. + + int c = 500; + while(c-- < 0) { + Host.Object.Say("C=" + c); + relax; // Putting 'relax' in microthreaded loops + // is an easy way to lower the CPU tax + // on your script. + } + + } + + public override void Start() + { + Host.Microthreads.Run( + MicroThreadFunction("World!") + ); + } + + public override void Stop() + { + + } + } +} diff --git a/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/TestModule.cs b/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/TestModule.cs index 702ac74..73af7f0 100644 --- a/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/TestModule.cs +++ b/OpenSim/Region/OptionalModules/Scripting/Minimodule/Test/TestModule.cs @@ -25,12 +25,55 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +using System.Collections; +using System.Collections.Generic; using OpenSim.Region.OptionalModules.Scripting.Minimodule; namespace OpenSim { class MiniModule : MRMBase { + // private microthreaded Function(params...) + private IEnumerable TestMicrothread(string param) + { + Host.Console.Info("Microthreaded " + param); + // relax; + yield return null; + Host.Console.Info("Microthreaded 2" + param); + yield return null; + int c = 100; + while(c-- < 0) + { + Host.Console.Info("Microthreaded Looped " + c + " " + param); + yield return null; + } + } + + public void Microthread(IEnumerable thread) + { + + } + + public void RunMicrothread() + { + List threads = new List(); + threads.Add(TestMicrothread("A").GetEnumerator()); + threads.Add(TestMicrothread("B").GetEnumerator()); + threads.Add(TestMicrothread("C").GetEnumerator()); + + Microthread(TestMicrothread("Ohai")); + + int i = 0; + while(threads.Count > 0) + { + i++; + bool running = threads[i%threads.Count].MoveNext(); + + if (!running) + threads.Remove(threads[i%threads.Count]); + } + } + public override void Start() { // Say Hello -- cgit v1.1