Lambda/Thread.pm

filter: Statementstmt 34.8% 30 / 86 Branchbran 6.6% 2 / 30 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 41.1% 7 / 17 Podpod 80.0% 4 / 5 totaltotal 27.9% 45 / 161 CC 17 SCAR 29.8
CC
17
Cov
26%
thread_init
42.0
new_thread
42.0
DESTROY
20.0
CRAP 19.7
« Lambda/Poll.pm ↑ Summary Lambda/DBI.pm »
1 # $Id: Thread.pm,v 1.25 2010/03/27 19:51:24 dk Exp $
2 package IO::Lambda::Thread;
3 2 ▶use base qw(IO::Lambda);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use IO::Lambda qw(:all :dev swap_frame);
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 our $DISABLED;
12 2 eval { require threads; };
13 2 ▶$DISABLED = $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 0 2
14 2 ▶$DISABLED = "broken threads on this perl version" if
Branch: 1 branch
BranchTrueFalseTotal
if not defined $IO::Lambda::Thread::DISABLED and $^O =~ /win32/i and $] < "5.02" 0 2 2
Condition33%2/6
not defined $IO::Lambda::Thread::DISABLED and $^O =~ /win32/i and $] < "5.02"
!ll&&!rl&&r
200
not defined $IO::Lambda::Thread::DISABLED and $^O =~ /win32/i
!ll&&!rl&&r
200
MC/DC0%0/3
not defined $IO::Lambda::Thread::DISABLED and $^O =~ /win32/i and $] < "5.02"
not defined $IO::Lambda::Thread::DISABLED $^O =~ /win32/i $] < "5.02"
Truth table25%1/4
not(A) and B and C
A= not defined $IO::Lambda::Thread::DISABLEDB= $^O =~ /win32/iC= $] < "5.02"
ABCresult
0XX0
10X0
1100
1111
15 !defined $DISABLED && $^O =~ /win32/i && $] < 5.020;
16
17 2 our $DEBUG = $IO::Lambda::DEBUG{thread};
18
19 2 our @EXPORT_OK = qw(threaded new_thread);
20 2 our %EXPORT_TAGS = (all => \@EXPORT_OK);
21
22 0 ▶sub _d { "threaded(" . _o($_[0]) . ")" }
Subroutine _d: not called CC=1 SCAR=6.9
23
24 sub thread_init
25 0 ▶{
Subroutine thread_init: not called CC=6 SCAR=37.4
Pod undocumented
26 0 my ( $r, $cb, $pass_handle, @param) = @_;
27
28 0 ▶ $SIG{KILL} = sub { threads-> exit(0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 0 $SIG{PIPE} = 'IGNORE';
30 0 eval "END { IO::Lambda::__end(); };";
31 0 ▶ warn "thread(", threads->tid, ") started\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
32
33 0 my @ret;
34 0 ▶ eval { @ret = $cb-> (( $pass_handle ? $r : ()), @param) if $cb };
Branch: 2 branches
BranchTrueFalseTotal
if $cb 0 0 0
$pass_handle ? : 0 0 0
35
36 0 ▶ warn "thread(", threads->tid, ") ended: [@ret]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
37 0 close($r);
38 0 undef $r;
39 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
40
41 0 return @ret;
42 }
43
44 sub new_thread
45 0 ▶{
Subroutine new_thread: not called CC=6 SCAR=37.4
Pod documented
46 0 ▶ return undef, $DISABLED if $DISABLED;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DISABLED 0 0 0
47
48 0 my ( @args, $cb, $pass_handle, @param);
49 0 ▶ @args = shift if $_[0] and ref($_[0]) and ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and ref $_[0] and ref $_[0] eq "HASH" 0 0 0
Condition0%0/6
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
$_[0] and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/4
A and B and C
A= $_[0]B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
50 0 ( $cb, $pass_handle, @param) = @_;
51
52 0 my $r = IO::Handle-> new;
53 0 my $w = IO::Handle-> new;
54 0 socketpair( $r, $w, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
55 0 $w-> blocking(0);
56
57 0 my ($t) = threads-> create(
58 @args,
59 \&thread_init,
60 $r, $cb, $pass_handle, @param
61 );
62
63 0 close($r);
64
65 0 ▶ warn "new thread(", $t->tid, ")\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
66 0 return ($t, $w);
67 }
68
69 # overridden IO::Lambda methods
70
71 sub DESTROY
72 0 ▶{
Subroutine DESTROY: not called CC=4 SCAR=30.0
73 0 my $self = shift;
74
75 0 ▶ return if defined($self->{tid}) and $self->{tid} != threads-> tid;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"tid"} and $self->{"tid"} != "threads"->tid 0 0 0
Condition0%0/3
defined $self->{"tid"} and $self->{"tid"} != "threads"->tid
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"tid"} and $self->{"tid"} != "threads"->tid
defined $self->{"tid"} $self->{"tid"} != "threads"->tid
Truth table0%0/3
A and B
A= defined $self->{"tid"}B= $self->{"tid"} != "threads"->tid
ABresult
0X0
100
111
76
77 0 ▶ close($self->{socket}) if $self-> {socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 0 0 0
78 0 delete @{$self}{qw(socket thread)};
79
80 0 $self-> SUPER::DESTROY;
81 }
82
83 0 ▶sub thread { $_[0]-> {thread} }
Subroutine thread: not called CC=1 SCAR=6.9
Pod documented
84 0 ▶sub socket { $_[0]-> {socket} }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
85
86 sub threaded(&)
87 0 ▶{
Subroutine threaded: not called CC=1 SCAR=6.9
Pod documented
88 0 my $cb = shift;
89
90 # use overridden IO::Lambda, because we need
91 # give the caller a chance to join
92 # for it, if the lambda gets terminated
93 0 ▶ __PACKAGE__-> new( sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
94 # Save context. This is needed because the caller
95 # may have his own this. lambda(&) does the same
96 # protection
97 0 my $this = shift;
98 0 my @frame = swap_frame($this);
99
100 0 ▶ warn _d($this), " started\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
101
102 # can start a thread?
103 0 my ( $t, $r) = new_thread( $cb, 1 );
104 0 ▶ return $r unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 0 0
105
106 # save this
107 0 $this-> {tid} = threads-> tid;
108 0 $this-> {thread} = $t;
109 0 $this-> {socket} = $r;
110
111 # now wait
112 0 context $this-> {socket};
113 0 ▶ readable {
Subroutine __ANON__: not called CC=2 SCAR=17.9
114 0 my $this = this;
115 0 delete $this-> {thread};
116 0 close($this-> {socket});
117 0 delete @{$this}{qw(socket thread)};
118 0 $this-> clear;
119 0 ▶ warn _d($this), " joining\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
120 0 $t-> join;
121 0 };
122
123 # restore context
124 0 swap_frame(@frame);
125 0 });
126 }
127
128 2 1;
129
130 __DATA__
« Lambda/Poll.pm ↑ Summary Lambda/DBI.pm »