summaryrefslogtreecommitdiff
path: root/dist/threads/t/stack.t
blob: 0dcc9470276cab44024f0823715919b489826960 (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
use strict;
use warnings;

BEGIN {
    use Config;
    if (! $Config{'useithreads'}) {
        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
        exit(0);
    }
}

my $frame_size;
my $frames;
my $size;

BEGIN {
    # XXX Note that if the default stack size happens to be the same as these
    # numbers, that test 2 would return success just out of happenstance.
    # This possibility could be lessened by choosing $frames to be something
    # less likely than a power of 2
    $frame_size = 4096;
    $frames     = 128;
    $size       = $frames * $frame_size;
}

use ExtUtils::testlib;

sub ok {
    my ($id, $ok, $name) = @_;

    # You have to do it this way or VMS will get confused.
    if ($ok) {
        print("ok $id - $name\n");
    } else {
        print("not ok $id - $name\n");
        printf("# Failed test at line %d\n", (caller)[2]);
    }

    return ($ok);
}

sub is {
    my ($id, $got, $expected, $name) = @_;

    my $ok = ok($id, $got == $expected, $name);
    if (! $ok) {
        print("     GOT: $got\n");
        print("EXPECTED: $expected\n");
    }

    return ($ok);
}

BEGIN {
    $| = 1;
    print("1..18\n");   ### Number of tests that will be run ###
};

use threads ('stack_size' => $size);
ok(1, 1, 'Loaded');

### Start of Testing ###

my $actual_size = threads->get_stack_size();

{
    if ($actual_size > $size) {
        print("ok 2 # skip because system needs larger minimum stack size\n");
        $size = $actual_size;
    }
    else {
        is(2, $actual_size, $size, 'Stack size set in import');
    }
}

my $size_plus_quarter = $size * 1.25;   # 128 frames map to 160
is(3, threads->set_stack_size($size_plus_quarter), $size,
        'Set returns previous value');
is(4, threads->get_stack_size(), $size_plus_quarter,
        'Get stack size');

threads->create(
    sub {
        is(5, threads->get_stack_size(), $size_plus_quarter,
                'Get stack size in thread');
        is(6, threads->self()->get_stack_size(), $size_plus_quarter,
                'Thread gets own stack size');
        is(7, threads->set_stack_size($size), $size_plus_quarter,
                'Thread changes stack size');
        is(8, threads->get_stack_size(), $size,
                'Get stack size in thread');
        is(9, threads->self()->get_stack_size(), $size_plus_quarter,
                'Thread stack size unchanged');
    }
)->join();

is(10, threads->get_stack_size(), $size,
        'Default thread sized changed in thread');

threads->create(
    { 'stack' => $size_plus_quarter },
    sub {
        is(11, threads->get_stack_size(), $size,
                'Get stack size in thread');
        is(12, threads->self()->get_stack_size(), $size_plus_quarter,
                'Thread gets own stack size');
    }
)->join();

my $thr = threads->create( { 'stack' => $size_plus_quarter }, sub { } );

$thr->create(
    sub {
        is(13, threads->get_stack_size(), $size,
                'Get stack size in thread');
        is(14, threads->self()->get_stack_size(), $size_plus_quarter,
                'Thread gets own stack size');
    }
)->join();

my $size_plus_eighth  = $size * 1.125;  # 128 frames map to 144
$thr->create(
    { 'stack' => $size_plus_eighth },
    sub {
        is(15, threads->get_stack_size(), $size,
                'Get stack size in thread');
        is(16, threads->self()->get_stack_size(), $size_plus_eighth,
                'Thread gets own stack size');
        is(17, threads->set_stack_size($size_plus_quarter), $size,
                'Thread changes stack size');
    }
)->join();

$thr->join();

is(18, threads->get_stack_size(), $size_plus_quarter,
        'Default thread sized changed in thread');

exit(0);

# EOF