File: 04_thread.t

package info (click to toggle)
perl 5.20.2-3%2Bdeb8u11
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 102,964 kB
  • sloc: perl: 555,553; ansic: 214,041; sh: 38,121; pascal: 8,783; cpp: 3,895; makefile: 2,393; xml: 2,325; yacc: 1,741
file content (62 lines) | stat: -rw-r--r-- 1,598 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!perl
use strict; use warnings;
use Test::More;
my $n_tests;

use Hash::Util::FieldHash qw( :all);
my $ob_reg = Hash::Util::FieldHash::_ob_reg;

{
    my $n_basic;
    BEGIN {
        $n_basic = 6; # 6 tests per call of basic_func()
        $n_tests += 5*$n_basic;
    }
    my %h;
    fieldhash %h;

    sub basic_func {
        my $level = shift;
        
        my @res;
        my $push_is = sub {
            my ( $hash, $should, $name) = @_;
            push @res, [ scalar keys %$hash, $should, $name];
        };
            
        my $obj = [];
        $push_is->( \ %h, 0, "$level: initially clear");
        $push_is->( $ob_reg, 0, "$level: ob_reg initially clear");
        $h{ $obj} = 123;
        $push_is->( \ %h, 1, "$level: one object");
        $push_is->( $ob_reg, 1, "$level: ob_reg one object");
        undef $obj;
        $push_is->( \ %h, 0, "$level: garbage collected");
        $push_is->( $ob_reg, 0, "$level: ob_reg garbage collected");
        @res;
    }

    &is( @$_) for basic_func( "home");

    SKIP: {
        require Config;
        skip "No thread support", 3*$n_basic unless
            $Config::Config{ usethreads};
        require threads;
        my ( $t) = threads->create( \ &basic_func, "thread 1");
        &is( @$_) for $t->join;

        &is( @$_) for basic_func( "back home");

        ( $t) = threads->create( sub {
            my ( $t) = threads->create( \ &basic_func, "thread 2");
            $t->join;
        });
        &is( @$_) for $t->join;
    }

    &is( @$_) for basic_func( "back home again");

}

BEGIN { plan tests => $n_tests }