threads 1.47
[p5sagit/p5-mst-13.2.git] / ext / threads / t / libc.t
CommitLineData
0f1612a7 1use strict;
2use warnings;
83007387 3
4BEGIN {
0f1612a7 5 if ($ENV{'PERL_CORE'}){
6 chdir 't';
7 unshift @INC, '../lib';
8 }
9 use Config;
fc04eb16 10 if (! $Config{'useithreads'}) {
11 print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
12 exit(0);
83007387 13 }
14}
15
16use ExtUtils::testlib;
0f1612a7 17
f2cba68d 18sub ok {
19 my ($id, $ok, $name) = @_;
20
21 # You have to do it this way or VMS will get confused.
22 if ($ok) {
23 print("ok $id - $name\n");
24 } else {
25 print("not ok $id - $name\n");
26 printf("# Failed test at line %d\n", (caller)[2]);
27 }
28
29 return ($ok);
30}
31
58a3a76c 32use threads;
33
fc04eb16 34BEGIN {
58a3a76c 35 eval {
36 require threads::shared;
37 import threads::shared;
38 };
39 if ($@ || ! $threads::shared::threads_shared) {
40 print("1..0 # Skip: threads::shared not available\n");
41 exit(0);
42 }
43
fc04eb16 44 $| = 1;
45 print("1..12\n"); ### Number of tests that will be run ###
46};
83007387 47
f2cba68d 48ok(1, 1, 'Loaded');
49
fc04eb16 50### Start of Testing ###
51
83007387 52my $i = 10;
53my $y = 20000;
fc04eb16 54
83007387 55my %localtime;
fc04eb16 56for (0..$i) {
57 $localtime{$_} = localtime($_);
83007387 58};
fc04eb16 59
f2cba68d 60my $mutex = 2;
83007387 61share($mutex);
fc04eb16 62
83007387 63my @threads;
fc04eb16 64for (0..$i) {
65 my $thread = threads->create(sub {
66 my $arg = $_;
67 my $localtime = $localtime{$arg};
68 my $error = 0;
69 for (0..$y) {
70 my $lt = localtime($arg);
45cd5be7 71 if ($localtime ne $lt) {
fc04eb16 72 $error++;
73 }
74 }
75 lock($mutex);
45cd5be7 76 while ($mutex != ($_ + 2)) {
77 cond_wait($mutex);
78 }
fc04eb16 79 ok($mutex, ! $error, 'localtime safe');
80 $mutex++;
45cd5be7 81 cond_broadcast($mutex);
fc04eb16 82 });
83 push @threads, $thread;
83007387 84}
85
fc04eb16 86for (@threads) {
87 $_->join();
83007387 88}
89
fc04eb16 90# EOF