ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Impl/Glib.pm (file contents):
Revision 1.1 by root, Wed Apr 27 01:26:44 2005 UTC vs.
Revision 1.2 by root, Thu Dec 1 18:56:18 2005 UTC

2 2
3use Glib (); 3use Glib ();
4 4
5my $maincontext = Glib::MainContext->default; 5my $maincontext = Glib::MainContext->default;
6 6
7sub new_from_fh { 7my %RWE = (
8 in => 'r',
9 out => 'w',
10 pri => 'e',
11);
12
13sub io {
8 my ($class, $fh) = @_; 14 my ($class, %arg) = @_;
9 bless { fh => $fh }, $class; 15
16 my $self = \%arg, $class;
17 my $rcb = \$self->{cb};
18
19 my @cond;
20 push @cond, "in" if $self->{poll} =~ /r/i;
21 push @cond, "out" if $self->{poll} =~ /w/i;
22 push @cond, "pri" if $self->{poll} =~ /e/i;
23
24 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub {
25 $$rcb->(join "", map $RWE{$_}, @{ $_[1] });
26 ! ! $$rcb
27 };
28
29 $self
10} 30}
11 31
12sub cb { 32sub timer {
13 my ($self, $cb) = @_; 33 my ($class, %arg) = @_;
14 $self->{cb} = $cb; 34
35 my $self = \%arg, $class;
36 my $cb = $self->{cb};
37
38 $self->{source} = add Glib::Timeout $self->{after} * 1000, sub {
39 $cb->();
40 0
41 };
42
15 $self; 43 $self
16} 44}
17 45
18sub poll { 46sub cancel {
19 my ($self, $r, $w, $e) = @_; 47 my ($self) = @_;
48
49 return unless HASH:: eq ref $self;
20 50
21 remove Glib::Source delete $self->{source} if $self->{source}; 51 remove Glib::Source delete $self->{source} if $self->{source};
22 52 $self->{cb} = undef;
23 my @cond; 53 delete $self->{cb};
24 push @cond, "in" if $r;
25 push @cond, "out" if $w;
26 push @cond, "err" if $e;
27
28 my $cb = \$self->{cb}; # avoid $self-reference
29
30 $self->{source} = add_watch Glib::IO fileno $self->{fh}, \@cond, sub {
31 $$cb->(); 1;
32 };
33
34 $self;
35} 54}
36 55
37sub DESTROY { 56sub DESTROY {
38 my ($self) = @_; 57 my ($self) = @_;
39 58
40 remove Glib::Source delete $self->{source} if $self->{source}; 59 $self->cancel;
41} 60}
42 61
43############# 62sub condvar {
44
45sub new_signal {
46 my $class = shift; 63 my $class = shift;
47 64
48 bless \my $x, $class; 65 bless \my $x, $class;
49} 66}
50 67
51sub send { 68sub broadcast {
52 ${$_[0]}++; 69 ${$_[0]}++
53} 70}
54 71
55sub wait { 72sub wait {
56 $maincontext->iteration (1) while !${$_[0]}; 73 $maincontext->iteration (1) while !${$_[0]};
57} 74}
58 75
76$AnyEvent::MODEL = __PACKAGE__;
77
591; 781;
60 79

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines