threads 1.47
[p5sagit/p5-mst-13.2.git] / ext / threads / t / libc.t
1 use strict;
2 use warnings;
3
4 BEGIN {
5     if ($ENV{'PERL_CORE'}){
6         chdir 't';
7         unshift @INC, '../lib';
8     }
9     use Config;
10     if (! $Config{'useithreads'}) {
11         print("1..0 # Skip: Perl not compiled with 'useithreads'\n");
12         exit(0);
13     }
14 }
15
16 use ExtUtils::testlib;
17
18 sub 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
32 use threads;
33
34 BEGIN {
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
44     $| = 1;
45     print("1..12\n");   ### Number of tests that will be run ###
46 };
47
48 ok(1, 1, 'Loaded');
49
50 ### Start of Testing ###
51
52 my $i = 10;
53 my $y = 20000;
54
55 my %localtime;
56 for (0..$i) {
57     $localtime{$_} = localtime($_);
58 };
59
60 my $mutex = 2;
61 share($mutex);
62
63 my @threads;
64 for (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);
71                         if ($localtime ne $lt) {
72                             $error++;
73                         }
74                     }
75                     lock($mutex);
76                     while ($mutex != ($_ + 2)) {
77                         cond_wait($mutex);
78                     }
79                     ok($mutex, ! $error, 'localtime safe');
80                     $mutex++;
81                     cond_broadcast($mutex);
82                   });
83     push @threads, $thread;
84 }
85
86 for (@threads) {
87     $_->join();
88 }
89
90 # EOF