xref: /openbsd-src/gnu/usr.bin/perl/dist/threads/t/exit.t (revision 9f11ffb7133c203312a01e4b986886bc88c7d74b)
1b39c5158Smillertuse strict;
2b39c5158Smillertuse warnings;
3b39c5158Smillert
4b39c5158SmillertBEGIN {
5b39c5158Smillert    require($ENV{PERL_CORE} ? '../../t/test.pl' : './t/test.pl');
6b39c5158Smillert
7b39c5158Smillert    use Config;
8b39c5158Smillert    if (! $Config{'useithreads'}) {
9b39c5158Smillert        skip_all(q/Perl not compiled with 'useithreads'/);
10b39c5158Smillert    }
11b39c5158Smillert}
12b39c5158Smillert
13b39c5158Smillertour $TODO;
14b39c5158Smillert
15b39c5158Smillertuse ExtUtils::testlib;
16b39c5158Smillert
17b39c5158Smillertuse threads;
18b39c5158Smillert
19b39c5158SmillertBEGIN {
20b39c5158Smillert    if (! eval 'use threads::shared; 1') {
21b39c5158Smillert        skip_all('threads::shared not available');
22b39c5158Smillert    }
23b39c5158Smillert
24b39c5158Smillert    $| = 1;
25b39c5158Smillert    print("1..18\n");   ### Number of tests that will be run ###
26b39c5158Smillert};
27b39c5158Smillert
28b39c5158Smillertok(1, 'Loaded');
29b39c5158Smillert
30b39c5158Smillert### Start of Testing ###
31b39c5158Smillert
32b39c5158Smillert$SIG{'__WARN__'} = sub {
33b39c5158Smillert    my $msg = shift;
34b39c5158Smillert    ok(0, "WARN in main: $msg");
35b39c5158Smillert};
36b39c5158Smillert$SIG{'__DIE__'} = sub {
37b39c5158Smillert    my $msg = shift;
38b39c5158Smillert    ok(0, "DIE in main: $msg");
39b39c5158Smillert};
40b39c5158Smillert
41b39c5158Smillert
42b39c5158Smillertmy $thr = threads->create(sub {
43b39c5158Smillert    threads->exit();
44b39c5158Smillert    return (99);  # Not seen
45b39c5158Smillert});
46b39c5158Smillertok($thr, 'Created: threads->exit()');
47b39c5158Smillertmy $rc = $thr->join();
48b39c5158Smillertok(! defined($rc), 'Exited: threads->exit()');
49b39c5158Smillert
50b39c5158Smillert
51*9f11ffb7Safresh1run_perl(prog => 'use threads 2.21;' .
52b39c5158Smillert                 'threads->exit(86);' .
53b39c5158Smillert                 'exit(99);',
54b39c5158Smillert         nolib => ($ENV{PERL_CORE}) ? 0 : 1,
55b39c5158Smillert         switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]);
56b39c5158Smillert{
57b39c5158Smillert    local $TODO = 'VMS exit semantics not like POSIX exit semantics' if $^O eq 'VMS';
58b39c5158Smillert    is($?>>8, 86, 'thread->exit(status) in main');
59b39c5158Smillert}
60b39c5158Smillert
61b39c5158Smillert$thr = threads->create({'exit' => 'thread_only'}, sub {
62b39c5158Smillert                                                    exit(1);
63b39c5158Smillert                                                    return (99);  # Not seen
64b39c5158Smillert                                                  });
65b39c5158Smillertok($thr, 'Created: thread_only');
66b39c5158Smillert$rc = $thr->join();
67b39c5158Smillertok(! defined($rc), 'Exited: thread_only');
68b39c5158Smillert
69b39c5158Smillert
70b39c5158Smillert$thr = threads->create(sub {
71b39c5158Smillert    threads->set_thread_exit_only(1);
72b39c5158Smillert    exit(1);
73b39c5158Smillert    return (99);  # Not seen
74b39c5158Smillert});
75b39c5158Smillertok($thr, 'Created: threads->set_thread_exit_only');
76b39c5158Smillert$rc = $thr->join();
77b39c5158Smillertok(! defined($rc), 'Exited: threads->set_thread_exit_only');
78b39c5158Smillert
79b39c5158Smillert
80b39c5158Smillertmy $WAIT :shared = 1;
81b39c5158Smillert$thr = threads->create(sub {
82b39c5158Smillert    lock($WAIT);
83b39c5158Smillert    while ($WAIT) {
84b39c5158Smillert        cond_wait($WAIT);
85b39c5158Smillert    }
86b39c5158Smillert    exit(1);
87b39c5158Smillert    return (99);  # Not seen
88b39c5158Smillert});
89b39c5158Smillertthreads->yield();
90b39c5158Smillertok($thr, 'Created: $thr->set_thread_exit_only');
91b39c5158Smillert$thr->set_thread_exit_only(1);
92b39c5158Smillert{
93b39c5158Smillert    lock($WAIT);
94b39c5158Smillert    $WAIT = 0;
95b39c5158Smillert    cond_broadcast($WAIT);
96b39c5158Smillert}
97b39c5158Smillert$rc = $thr->join();
98b39c5158Smillertok(! defined($rc), 'Exited: $thr->set_thread_exit_only');
99b39c5158Smillert
100b39c5158Smillert
101*9f11ffb7Safresh1run_perl(prog => 'use threads 2.21 qw(exit thread_only);' .
102b39c5158Smillert                 'threads->create(sub { exit(99); })->join();' .
103b39c5158Smillert                 'exit(86);',
104b39c5158Smillert         nolib => ($ENV{PERL_CORE}) ? 0 : 1,
105b39c5158Smillert         switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]);
106b39c5158Smillert{
107b39c5158Smillert    local $TODO = 'VMS exit semantics not like POSIX exit semantics' if $^O eq 'VMS';
108b39c5158Smillert    is($?>>8, 86, "'use threads 'exit' => 'thread_only'");
109b39c5158Smillert}
110b39c5158Smillert
111*9f11ffb7Safresh1my $out = run_perl(prog => 'use threads 2.21;' .
112b39c5158Smillert                           'threads->create(sub {' .
113b39c5158Smillert                           '    exit(99);' .
114b39c5158Smillert                           '});' .
115b39c5158Smillert                           'sleep(1);' .
116b39c5158Smillert                           'exit(86);',
117b39c5158Smillert                   nolib => ($ENV{PERL_CORE}) ? 0 : 1,
118b39c5158Smillert                   switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ],
119b39c5158Smillert                   stderr => 1);
120b39c5158Smillert{
121b39c5158Smillert    local $TODO = 'VMS exit semantics not like POSIX exit semantics' if $^O eq 'VMS';
122b39c5158Smillert    is($?>>8, 99, "exit(status) in thread");
123b39c5158Smillert}
124b8851fccSafresh1like($out, qr/1 finished and unjoined/, "exit(status) in thread");
125b39c5158Smillert
126b39c5158Smillert
127*9f11ffb7Safresh1$out = run_perl(prog => 'use threads 2.21 qw(exit thread_only);' .
128b39c5158Smillert                        'threads->create(sub {' .
129b39c5158Smillert                        '   threads->set_thread_exit_only(0);' .
130b39c5158Smillert                        '   exit(99);' .
131b39c5158Smillert                        '});' .
132b39c5158Smillert                        'sleep(1);' .
133b39c5158Smillert                        'exit(86);',
134b39c5158Smillert                nolib => ($ENV{PERL_CORE}) ? 0 : 1,
135b39c5158Smillert                switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ],
136b39c5158Smillert                stderr => 1);
137b39c5158Smillert{
138b39c5158Smillert    local $TODO = 'VMS exit semantics not like POSIX exit semantics' if $^O eq 'VMS';
139b39c5158Smillert    is($?>>8, 99, "set_thread_exit_only(0)");
140b39c5158Smillert}
141b8851fccSafresh1like($out, qr/1 finished and unjoined/, "set_thread_exit_only(0)");
142b39c5158Smillert
143b39c5158Smillert
144*9f11ffb7Safresh1run_perl(prog => 'use threads 2.21;' .
145b39c5158Smillert                 'threads->create(sub {' .
146b39c5158Smillert                 '   $SIG{__WARN__} = sub { exit(99); };' .
147b39c5158Smillert                 '   die();' .
148b39c5158Smillert                 '})->join();' .
149b39c5158Smillert                 'exit(86);',
150b39c5158Smillert         nolib => ($ENV{PERL_CORE}) ? 0 : 1,
151b39c5158Smillert         switches => ($ENV{PERL_CORE}) ? [] : [ '-Mblib' ]);
152b39c5158Smillert{
153b39c5158Smillert    local $TODO = 'VMS exit semantics not like POSIX exit semantics' if $^O eq 'VMS';
154b39c5158Smillert    is($?>>8, 99, "exit(status) in thread warn handler");
155b39c5158Smillert}
156b39c5158Smillert
157b39c5158Smillert$thr = threads->create(sub {
158b39c5158Smillert    $SIG{__WARN__} = sub { threads->exit(); };
159b39c5158Smillert    local $SIG{__DIE__} = 'DEFAULT';
160b39c5158Smillert    die('Died');
161b39c5158Smillert});
162b39c5158Smillertok($thr, 'Created: threads->exit() in thread warn handler');
163b39c5158Smillert$rc = $thr->join();
164b39c5158Smillertok(! defined($rc), 'Exited: threads->exit() in thread warn handler');
165b39c5158Smillert
166b39c5158Smillertexit(0);
167b39c5158Smillert
168b39c5158Smillert# EOF
169