Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Finish monitor API, add ObjectPipe for moritz++
  • Loading branch information
sorear committed Mar 6, 2011
1 parent 9598dde commit bd64f65
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions lib/Threads.pm6
Expand Up @@ -2,22 +2,55 @@ 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 {
(rnull (rawscall System.Threading.Monitor.Enter (@ {self})))
}
}
method exit() {
Q:CgOp {
(rnull (rawscall System.Threading.Monitor.Exit (@ {self})))
}
}
method enter() { Q:CgOp {
(rnull (rawscall System.Threading.Monitor.Enter (@ {self})))
} }
method exit() { Q:CgOp {
(rnull (rawscall System.Threading.Monitor.Exit (@ {self})))
} }
method pulse() { Q:CgOp {
(rnull (rawscall System.Threading.Monitor.Pulse (@ {self})))
} }
method pulse_all() { Q:CgOp {
(rnull (rawscall System.Threading.Monitor.PulseAll (@ {self})))
} }
method try_enter($t) { Q:CgOp {
(box Bool (rawscall System.Threading.Monitor.TryEnter (@ {self})
(cast int (obj_getnum {$t * 1000}))))
} }
method wait() { Q:CgOp {
(box Bool (rawscall System.Threading.Monitor.Wait (@ {self})))
} }
method try_wait($t) { Q:CgOp {
(box Bool (rawscall System.Threading.Monitor.Wait (@ {self})
(cast int (obj_getnum {$t * 1000}))))
} }
# TODO exception handling
method lock($f) { self.enter; $f(); self.exit }
}
sub lock($m,$f) is export { $m.lock($f); }
class ObjectPipe {
has $!lock = Monitor.new;
has $!queue = [];
method get() {
$!lock.enter;
$!lock.wait until $!queue;
my $value = shift $!queue;
$!lock.exit;
$value;
}
method put($x) {
$!lock.enter;
push $!queue, $x;
$!lock.pulse;
$!lock.exit;
}
}
class Thread is export {
has $!value;
method new($func) {
Expand Down

0 comments on commit bd64f65

Please sign in to comment.