Skip to content

Commit

Permalink
Add multithreading support
Browse files Browse the repository at this point in the history
  • Loading branch information
sorear committed Aug 15, 2010
1 parent 0c64692 commit 38b3ab2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -15,6 +15,10 @@ Test.dll
Test.dll.so
Test.cs
Test_ast.store
Threads.dll
Threads.dll.so
Threads.cs
Threads_ast.store
MONKEY_TYPING.dll
MONKEY_TYPING.dll.so
MONKEY_TYPING.cs
Expand Down
20 changes: 20 additions & 0 deletions Kernel.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading;
namespace Niecza {
// We like to reuse continuation objects for speed - every function only
// creates one kind of continuation, but tweaks a field for exact return
Expand Down Expand Up @@ -780,6 +781,25 @@ public class Kernel {
}
}

public static Frame StartP6Thread(Frame th, IP6 sub) {
Thread thr = new Thread(delegate () {
Frame current = sub.Invoke(th, new Variable[0], null);

while (current != th) {
try {
current = current.Continue();
} catch (Exception ex) {
ExceptionPacket ep = new FatalException(
new CLRImportObject(ex));
current = ep.SearchForHandler(current);
}
}
});
thr.Start();
th.resultSlot = thr;
return th;
}

public static void RunLoop(DynBlockDelegate boot) {
Kernel.TraceCont = (Environment.GetEnvironmentVariable("NIECZA_TRACE") != null);
Frame root_f = new Frame(null, null, boot);
Expand Down
35 changes: 35 additions & 0 deletions Threads.pm6
@@ -0,0 +1,35 @@
module Threads;

# Should be a role, since it can be applied to any class with minimal overhead
class Monitor is export {
method enter() {
Q:CgOp {
(prog (rawscall System.Threading.Monitor.Enter:m,Void
(@ (l self))) (null Variable))
}
}
method exit() {
Q:CgOp {
(prog (rawscall System.Threading.Monitor.Exit:m,Void
(@ (l self))) (null Variable))
}
}
# TODO exception handling
method lock($f) { self.enter; $f(); self.exit }
}

class Thread is export {
method new($func) {
Q:CgOp { (box Thread (rawsccall
Kernel.StartP6Thread:c,System.Threading.Thread (@ (l $func)))) }
}

method join() {
Q:CgOp { (prog (rawcall (unbox System.Threading.Thread (@ (l self))) Join:m,Void) (null Variable)) }
}

method sleep($time) {
my $t = $time * 1000;
Q:CgOp { (prog (rawscall System.Threading.Thread.Sleep:m,Void (cast Int32 (unbox Double (@ (l $t))))) (null Variable)) }
}
}

0 comments on commit 38b3ab2

Please sign in to comment.