1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
|
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using Mono.Tasklets;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
/***************************\
* Use standard C# code *
* - uses system threads *
\***************************/
namespace OpenSim.Region.ScriptEngine.XMREngine {
public class ScriptUThread_Sys : IScriptUThread, IDisposable
{
private Exception except;
private int active; // -1: hibernating
// 0: exited
// 1: running
private object activeLock = new object ();
private XMRInstance instance;
public ScriptUThread_Sys (XMRInstance instance)
{
this.instance = instance;
}
/**
* @brief Start script event handler from the beginning.
* Return when either the script event handler completes
* or the script calls Hiber().
* @returns null: script did not throw any exception so far
* else: script threw an exception
*/
public Exception StartEx ()
{
lock (activeLock) {
/*
* We should only be called when script is inactive.
*/
if (active != 0) throw new Exception ("active=" + active);
/*
* Tell CallSEHThread() to run script event handler in a thread.
*/
active = 1;
TredPoo.RunSomething (CallSEHThread);
/*
* Wait for script to call Hiber() or for script to
* return back out to CallSEHThread().
*/
while (active > 0) {
Monitor.Wait (activeLock);
}
}
/*
* Return whether or not script threw an exception.
*/
return except;
}
/**
* @brief We now want to run some more script code from where it last hibernated
* until it either finishes the script event handler or until the script
* calls Hiber() again.
*/
public Exception ResumeEx ()
{
lock (activeLock) {
/*
* We should only be called when script is hibernating.
*/
if (active >= 0) throw new Exception ("active=" + active);
/*
* Tell Hiber() to return back to script.
*/
active = 1;
Monitor.PulseAll (activeLock);
/*
* Wait for script to call Hiber() again or for script to
* return back out to CallSEHThread().
*/
while (active > 0) {
Monitor.Wait (activeLock);
}
}
/*
* Return whether or not script threw an exception.
*/
return except;
}
/**
* @brief Script is being closed out.
* Terminate thread asap.
*/
public void Dispose ()
{
lock (activeLock) {
instance = null;
Monitor.PulseAll (activeLock);
}
}
/**
* @brief Determine if script is active.
* Returns: 0: nothing started or has returned
* Resume() must not be called
* Start() may be called
* Hiber() must not be called
* -1: thread has called Hiber()
* Resume() may be called
* Start() may be called
* Hiber() must not be called
* 1: thread is running
* Resume() must not be called
* Start() must not be called
* Hiber() may be called
*/
public int Active ()
{
return active;
}
/**
* @brief This thread executes the script event handler code.
*/
private void CallSEHThread ()
{
lock (activeLock) {
if (active <= 0) throw new Exception ("active=" + active);
except = null; // assume completion without exception
try {
instance.CallSEH (); // run script event handler
} catch (Exception e) {
except = e; // threw exception, save for Start()/Resume()
}
active = 0; // tell Start() or Resume() we're done
Monitor.PulseAll (activeLock);
}
}
/**
* @brief Called by the script event handler whenever it wants to hibernate.
*/
public void Hiber ()
{
if (active <= 0) throw new Exception ("active=" + active);
// tell Start() or Resume() we are hibernating
active = -1;
Monitor.PulseAll (activeLock);
// wait for Resume() or Dispose() to be called
while ((active < 0) && (instance != null)) {
Monitor.Wait (activeLock);
}
// don't execute any more script code, just exit
if (instance == null) {
throw new AbortedByDisposeException ();
}
}
/**
* @brief Number of remaining stack bytes.
*/
public int StackLeft ()
{
return 0x7FFFFFFF;
}
public class AbortedByDisposeException : Exception, IXMRUncatchable { }
/**
* @brief Pool of threads that run script event handlers.
*/
private class TredPoo {
private static readonly TimeSpan idleTimeSpan = new TimeSpan (0, 0, 1, 0, 0); // 1 minute
private static int tredPooAvail = 0;
private static object tredPooLock = new object ();
private static Queue<ThreadStart> tredPooQueue = new Queue<ThreadStart> ();
/**
* @brief Queue a function for execution in a system thread.
*/
public static void RunSomething (ThreadStart entry)
{
lock (tredPooLock) {
tredPooQueue.Enqueue (entry);
Monitor.Pulse (tredPooLock);
if (tredPooAvail < tredPooQueue.Count) {
new TredPoo ();
}
}
}
/**
* @brief Start a new system thread.
* It will shortly attempt to dequeue work or if none,
* add itself to the available thread list.
*/
private TredPoo ()
{
Thread thread = new Thread (Main);
thread.Name = "XMRUThread_sys";
thread.IsBackground = true;
thread.Start ();
tredPooAvail ++;
}
/**
* @brief Executes items from the queue or waits a little while
* if nothing. If idle for a while, it exits.
*/
private void Main ()
{
int first = 1;
ThreadStart entry;
while (true) {
lock (tredPooLock) {
tredPooAvail -= first;
first = 0;
while (tredPooQueue.Count <= 0) {
tredPooAvail ++;
bool keepgoing = Monitor.Wait (tredPooLock, idleTimeSpan);
-- tredPooAvail;
if (!keepgoing) return;
}
entry = tredPooQueue.Dequeue ();
}
entry ();
}
}
}
}
}
/*************************************\
* Use Mono.Tasklets.Continuations *
* - memcpy's stack *
\*************************************/
namespace OpenSim.Region.ScriptEngine.XMREngine {
public partial class XMRInstance {
public Mono.Tasklets.Continuation engstack;
public Mono.Tasklets.Continuation scrstack;
}
public class ScriptUThread_Con : IScriptUThread, IDisposable
{
private XMRInstance instance;
public ScriptUThread_Con (XMRInstance instance)
{
this.instance = instance;
}
private const int SAVEENGINESTACK = 0;
private const int LOADENGINESTACK = 1;
private const int SAVESCRIPTSTACK = 2;
private const int LOADSCRIPTSTACK = 3;
private Exception except;
private int active;
/**
* @brief Start script event handler from the beginning.
* Return when either the script event handler completes
* or the script calls Hiber().
* @returns null: script did not throw any exception so far
* else: script threw an exception
*/
public Exception StartEx ()
{
/*
* Save engine stack so we know how to jump back to engine in case
* the script calls Hiber().
*/
switch (instance.engstack.Store (SAVEENGINESTACK)) {
/*
* Engine stack has been saved, start running the event handler.
*/
case SAVEENGINESTACK: {
/*
* Run event handler according to stackFrames.
* In either case it is assumed that stateCode and eventCode
* indicate which event handler is to be called and that ehArgs
* points to the event handler argument list.
*/
active = 1;
except = null;
try {
instance.CallSEH ();
} catch (Exception e) {
except = e;
}
/*
* We now want to return to the script engine.
* Setting active = 0 means the microthread has exited.
* We need to call engstack.Restore() in case the script called Hiber()
* anywhere, we want to return out the corresponding Restore() and not the
* Start().
*/
active = 0;
instance.engstack.Restore (LOADENGINESTACK);
throw new Exception ("returned from Restore()");
}
/*
* Script called Hiber() somewhere so just return back out.
*/
case LOADENGINESTACK: {
break;
}
default: throw new Exception ("bad engstack code");
}
return except;
}
public void Dispose ()
{ }
/**
* @brief Determine if script is active.
* Returns: 0: nothing started or has returned
* Resume() must not be called
* Start() may be called
* Hiber() must not be called
* -1: thread has called Hiber()
* Resume() may be called
* Start() may be called
* Hiber() must not be called
* 1: thread is running
* Resume() must not be called
* Start() must not be called
* Hiber() may be called
*/
public int Active ()
{
return active;
}
/**
* @brief Called by the script wherever it wants to hibernate.
* So this means to save the scripts stack in 'instance.scrstack' then
* restore the engstack to cause us to return back to the engine.
*/
public void Hiber ()
{
/*
* Save where we are in the script's code in 'instance.scrstack'
* so we can wake the script when Resume() is called.
*/
switch (instance.scrstack.Store (SAVESCRIPTSTACK)) {
/*
* Script's stack is now saved in 'instance.scrstack'.
* Reload the engine's stack from 'instance.engstack' and jump to it.
*/
case SAVESCRIPTSTACK: {
active = -1;
instance.engstack.Restore (LOADENGINESTACK);
throw new Exception ("returned from Restore()");
}
/*
* Resume() was just called and we want to resume executing script code.
*/
case LOADSCRIPTSTACK: {
break;
}
default: throw new Exception ("bad scrstack code");
}
}
/**
* @brief We now want to run some more script code from where it last hibernated
* until it either finishes the script event handler or until the script
* calls Hiber() again.
*/
public Exception ResumeEx ()
{
/*
* Save where we are in the engine's code in 'instance.engstack'
* so if the script calls Hiber() again or exits, we know how to get
* back to the engine.
*/
switch (instance.engstack.Store (SAVEENGINESTACK)) {
/*
* This is original call to Resume() from the engine,
* jump to where we left off within Hiber().
*/
case SAVEENGINESTACK: {
active = 1;
instance.scrstack.Restore (LOADSCRIPTSTACK);
throw new Exception ("returned from Restore()");
}
/*
* Script has called Hiber() again, so return back to
* script engine code.
*/
case LOADENGINESTACK: {
break;
}
default: throw new Exception ("bad engstack code");
}
return except;
}
/**
* @brief Number of remaining stack bytes.
*/
public int StackLeft ()
{
return 0x7FFFFFFF;
}
}
}
/***********************************\
* Use Mono.Tasklets.MMRUThreads *
* - switches stack pointer *
\***********************************/
namespace OpenSim.Region.ScriptEngine.XMREngine {
public class ScriptUThread_MMR : IScriptUThread, IDisposable
{
private static Exception uthread_looked;
private static Type uttype;
private static Type uthread_entry;
private static MethodInfo uthread_dispose;
private static MethodInfo uthread_startex;
private static MethodInfo uthread_resumex;
private static MethodInfo uthread_suspend;
private static MethodInfo uthread_active;
private static MethodInfo uthread_stackleft;
public static Exception LoadMono ()
{
if ((uthread_looked == null) && (uthread_stackleft == null)) {
try {
Assembly mt = Assembly.Load ("Mono.Tasklets");
uttype = mt.GetType ("Mono.Tasklets.MMRUThread", true);
uthread_entry = mt.GetType ("Mono.Tasklets.MMRUThread+Entry", true);
uthread_dispose = uttype.GetMethod ("Dispose"); // no parameters, no return value
uthread_startex = uttype.GetMethod ("StartEx"); // takes uthread_entry delegate as parameter, returns exception
uthread_resumex = uttype.GetMethod ("ResumeEx"); // takes exception as parameter, returns exception
uthread_suspend = uttype.GetMethod ("Suspend", new Type[] { }); // no return value
uthread_active = uttype.GetMethod ("Active"); // no parameters, returns int
uthread_stackleft = uttype.GetMethod ("StackLeft"); // no parameters, returns IntPtr
} catch (Exception e) {
uthread_looked = new NotSupportedException ("'mmr' thread model requires patched mono", e);
}
}
return uthread_looked;
}
private static object[] resumex_args = new object[] { null };
private object uthread; // type MMRUThread
private object[] startex_args = new object[1];
public ScriptUThread_MMR (XMRInstance instance)
{
this.uthread = Activator.CreateInstance (uttype, new object[] { (IntPtr) instance.m_StackSize, instance.m_DescName });
startex_args[0] = Delegate.CreateDelegate (uthread_entry, instance, "CallSEH");
}
public void Dispose ()
{
uthread_dispose.Invoke (uthread, null);
uthread = null;
}
public Exception StartEx ()
{
return (Exception) uthread_startex.Invoke (uthread, startex_args);
}
public Exception ResumeEx ()
{
return (Exception) uthread_resumex.Invoke (uthread, resumex_args);
}
public void Hiber ()
{
uthread_suspend.Invoke (null, null);
}
public int Active ()
{
return (int) uthread_active.Invoke (uthread, null);
}
public int StackLeft ()
{
return (int) (IntPtr) uthread_stackleft.Invoke (null, null);
}
}
}
|