Lambda/Thread.pm

filter: Statementstmt 34.1% 29 / 85 Branchbran 3.5% 1 / 28 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 41.1% 7 / 17 Podpod 80.0% 4 / 5 totaltotal 27.5% 41 / 149 CC 17 SCAR 29.8
CC
17
Cov
25%
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
15 2 our $DEBUG = $IO::Lambda::DEBUG{thread};
16
17 2 our @EXPORT_OK = qw(threaded new_thread);
18 2 our %EXPORT_TAGS = (all => \@EXPORT_OK);
19
20 0 ▶sub _d { "threaded(" . _o($_[0]) . ")" }
Subroutine _d: not called CC=1 SCAR=6.9
21
22 sub thread_init
23 {
24 0 ▶ my ( $r, $cb, $pass_handle, @param) = @_;
Subroutine thread_init: not called CC=6 SCAR=37.4
Pod undocumented
25
26 0 ▶ $SIG{KILL} = sub { threads-> exit(0) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 $SIG{PIPE} = 'IGNORE';
28 0 eval "END { IO::Lambda::__end(); };";
29 0 ▶ warn "thread(", threads->tid, ") started\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
30
31 0 my @ret;
32 0 ▶ eval { @ret = $cb-> (( $pass_handle ? $r : ()), @param) if $cb };
Branch: 2 branches
BranchTrueFalseTotal
if $cb 0 0 0
$pass_handle ? : 0 0 0
33
34 0 ▶ warn "thread(", threads->tid, ") ended: [@ret]\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
35 0 close($r);
36 0 undef $r;
37 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
38
39 0 return @ret;
40 }
41
42 sub new_thread
43 {
44 0 ▶ return undef, $DISABLED if $DISABLED;
Subroutine new_thread: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DISABLED 0 0 0
45
46 0 my ( @args, $cb, $pass_handle, @param);
47 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
48 0 ( $cb, $pass_handle, @param) = @_;
49
50 0 my $r = IO::Handle-> new;
51 0 my $w = IO::Handle-> new;
52 0 socketpair( $r, $w, AF_UNIX, SOCK_STREAM, PF_UNSPEC);
53 0 $w-> blocking(0);
54
55 0 my ($t) = threads-> create(
56 @args,
57 \&thread_init,
58 $r, $cb, $pass_handle, @param
59 );
60
61 0 close($r);
62
63 0 ▶ warn "new thread(", $t->tid, ")\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
64 0 return ($t, $w);
65 }
66
67 # overridden IO::Lambda methods
68
69 sub DESTROY
70 {
71 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=4 SCAR=30.0
72
73 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
74
75 0 ▶ close($self->{socket}) if $self-> {socket};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"socket"} 0 0 0
76 0 delete @{$self}{qw(socket thread)};
77
78 0 $self-> SUPER::DESTROY;
79 }
80
81 0 ▶sub thread { $_[0]-> {thread} }
Subroutine thread: not called CC=1 SCAR=6.9
Pod documented
82 0 ▶sub socket { $_[0]-> {socket} }
Subroutine socket: not called CC=1 SCAR=6.9
Pod documented
83
84 sub threaded(&)
85 {
86 0 ▶ my $cb = shift;
Subroutine threaded: not called CC=1 SCAR=6.9
Pod documented
87
88 # use overridden IO::Lambda, because we need
89 # give the caller a chance to join
90 # for it, if the lambda gets terminated
91 __PACKAGE__-> new( sub {
92 # Save context. This is needed because the caller
93 # may have his own this. lambda(&) does the same
94 # protection
95 0 ▶ my $this = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
96 0 my @frame = swap_frame($this);
97
98 0 ▶ warn _d($this), " started\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
99
100 # can start a thread?
101 0 my ( $t, $r) = new_thread( $cb, 1 );
102 0 ▶ return $r unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 0 0
103
104 # save this
105 0 $this-> {tid} = threads-> tid;
106 0 $this-> {thread} = $t;
107 0 $this-> {socket} = $r;
108
109 # now wait
110 0 context $this-> {socket};
111 readable {
112 0 ▶ my $this = this;
Subroutine __ANON__: not called CC=2 SCAR=17.9
113 0 delete $this-> {thread};
114 0 close($this-> {socket});
115 0 delete @{$this}{qw(socket thread)};
116 0 $this-> clear;
117 0 ▶ warn _d($this), " joining\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $IO::Lambda::Thread::DEBUG 0 0 0
118 0 $t-> join;
119 0 };
120
121 # restore context
122 0 swap_frame(@frame);
123 0 });
124 }
125
126 2 1;
127
128 __DATA__
« Lambda/Poll.pm ↑ Summary Lambda/DBI.pm »