ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Impl/Glib.pm
Revision: 1.4
Committed: Fri Dec 30 01:28:31 2005 UTC (18 years, 5 months ago) by root
Branch: MAIN
CVS Tags: rel-0_4
Changes since 1.3: +6 -5 lines
Log Message:
*** empty log message ***

File Contents

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