Last active
August 29, 2015 14:13
-
-
Save ab5tract/4c2b8cc439d8470cbfda to your computer and use it in GitHub Desktop.
perl 5 + OO::Monitors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ perl6 thread-p5.pl6 | |
===SORRY!=== | |
Cannot look up attributes in a type object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl6 | |
use OO::Monitors; | |
use Inline::Perl5; | |
monitor Perl5::Pool is conditioned( <interpreters-available> ) { | |
has @!pool; | |
has SetHash $!pool-mask; | |
has $.pool-size; | |
# method new(%args) { | |
# my $pool-size = %args<pool-size> // 5; | |
# my @pool; | |
# @pool[$_] = Inline::Perl5.new for 0..^$!pool-size; | |
# %args.perl.say; | |
# my @pool; | |
# self.bless( :$pool-size, :@pool ); | |
# } | |
method interpret( :$perl5-code ) { | |
unless +@!pool { | |
@!pool[$_] = Inline::Perl5.new for 0..^$!pool-size; | |
} | |
while @!pool.keys.Set !(>) $!pool-mask { | |
wait-condition(<interpreters-available>); | |
} | |
meet-condition(<interpreters-available>); | |
my $idx = @!pool.keys.first({ $!pool-mask !(cont) $_ }); | |
say $idx; | |
my $interpreter = @!pool[ $idx ]; | |
until my $response = $interpreter.run( $perl5-code ) { | |
$!pool-mask{$idx} = 1; | |
} | |
return $response; | |
} | |
} | |
my $pool = Perl5::Pool.new( :pool-size(8) ); | |
my @results = await do for ^8 -> $thread { | |
start { | |
my $perl5-code = "sleep { (0..0.5).pick }; print 'hello from thread $thread';"; | |
say $pool.interpret( :$perl5-code ); | |
$thread; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment