Skip to content

Instantly share code, notes, and snippets.

@tadzik
Created August 24, 2011 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadzik/1169203 to your computer and use it in GitHub Desktop.
Save tadzik/1169203 to your computer and use it in GitHub Desktop.
yayitssegfault!
┌─[tadzik@yavin4]─[~/src/perl/IO-Select]
└─[%]─> PERL6LIB=lib parrot -g ms2 ~/src/nom/perl6.pbc test.pl
(IO<26532416>,).list
(IO<26532416>,).list
┌─[tadzik@yavin4]─[~/src/perl/IO-Select]
└─[%]─> PERL6LIB=lib parrot -g gms ~/src/nom/perl6.pbc test.pl
(IO<14171808>,).list
zsh: segmentation fault PERL6LIB=lib parrot -g gms ~/src/nom/perl6.pbc test.pl
pir::loadlib__Ps('select');
class IO::Select {
has $!pmc;
has $!iter = 'a';
has %!handles;
submethod BUILD {
$!pmc := pir::new__Ps('Select');
}
method add(IO $handle) {
%!handles{$!iter} = $handle;
my $fh := nqp::getattr(
pir::perl6_decontainerize__PP($handle), IO, '$!PIO'
);
my $mode = 4;
$mode += 2 if nqp::p6box_s($fh.mode) ~~ /w/;
$mode += 1 if nqp::p6box_s($fh.mode) ~~ /r/;
# XXX No idea how to obtain an actual fd or any other unique
# identifier, so I'll just assign consequent letters of alphabet
# to each one
$!pmc.update($fh, nqp::unbox_s($!iter), nqp::unbox_i($mode));
$!iter.=succ;
}
method remove(IO $handle) {
$!pmc.remove(nqp::getattr($handle, IO, '$!PIO'));
}
method can_read($timeout as Num) {
my Mu $ids := nqp::getattr(
$!pmc.can_write(nqp::unbox_n($timeout)), Parcel, '$!storage'
);
my Int $elems := nqp::p6box_i(pir::elements($ids));
my @res;
loop (my Int $i = 0; $i < $elems; $i++) {
my Str $item := nqp::p6box_s(nqp::atpos($ids, nqp::unbox_i($i)));
@res.push: $item;
}
return @res.map: { %!handles{$_} };
}
method can_write($timeout as Num) {
my Mu $ids := nqp::getattr(
$!pmc.can_write(nqp::unbox_n($timeout)), Parcel, '$!storage'
);
my Int $elems := nqp::p6box_i(pir::elements($ids));
my @res;
loop (my Int $i = 0; $i < $elems; $i++) {
my Str $item := nqp::p6box_s(nqp::atpos($ids, nqp::unbox_i($i)));
@res.push: $item;
}
return @res.map: { %!handles{$_} };
}
}
use IO::Select;
my $r = open 'README';
my $w = open 'WRITEME', :w;
my $select = IO::Select.new;
$select.add($r);
$select.add($w);
say $select.can_read(0).perl;
say $select.can_write(0).perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment