ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Util.pm
Revision: 1.1
Committed: Thu Aug 16 22:09:12 2001 UTC (22 years, 9 months ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Coro::Util - various utility functions.
4    
5     =head1 SYNOPSIS
6    
7     use Coro::Util;
8    
9     =head1 DESCRIPTION
10    
11     This module implements various utility functions, mostly replacing perl
12     functions by non-blocking counterparts.
13    
14     =over 4
15    
16     =cut
17    
18     package Coro::Util;
19    
20     #use Carp qw(croak);
21    
22     use Coro::Handle;
23     use Coro::Semaphore;
24    
25     use base 'Exporter';
26    
27     @EXPORT = qw(
28     gethostbyname gethostbyaddr
29     );
30    
31     $VERSION = 0.45;
32    
33     $MAXPARALLEL = 16; # max. number of parallel jobs
34    
35     my $jobs = new Coro::Semaphore $MAXPARALLEL;
36    
37     sub _do_asy(&;@) {
38     require POSIX; # just for _exit
39    
40     my $sub = shift;
41     $jobs->down;
42     my $fh;
43     if (0 == open $fh, "-|") {
44     syswrite STDOUT, join "\0", map { unpack "H*", $_ } &$sub;
45     POSIX::_exit(0);
46     }
47     my $buf;
48     $fh = unblock $fh;
49     $fh->read($buf, 16384);
50     close $fh;
51     $jobs->up;
52     my @r = map { pack "H*", $_ } split /\0/, $buf;
53     wantarray ? @r : $r[0];
54     }
55    
56     =item gethostbyname, gethostbyaddr
57    
58     Work exactly like their perl counterparts, but do not block. Currently
59     this is being implemented by forking, so it's not exactly low-cost.
60    
61     =cut
62    
63     sub gethostbyname($) {
64     _do_asy { gethostbyname $_[0] } @_;
65     }
66    
67     sub gethostbyaddr($$) {
68     _do_asy { gethostbyaddr $_[0], $_[1] } @_;
69     }
70    
71     1;
72    
73     =head1 AUTHOR
74    
75     Marc Lehmann <pcg@goof.com>
76     http://www.goof.com/pcg/marc/
77    
78     =cut
79