| 1 |
# |
| 2 |
# make sure v1 works in threads. |
| 3 |
# |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use version 0.77; |
| 7 |
use Config; |
| 8 |
|
| 9 |
BEGIN { |
| 10 |
unless ($Config{useithreads}) { |
| 11 |
print "1..0 # SKIP no ithreads\n"; |
| 12 |
exit 0; |
| 13 |
} |
| 14 |
my $v = version->parse($Config{version}); |
| 15 |
if ($v >= '5.9.5' and $v < '5.10.1') { |
| 16 |
# See note in t/5persist/threads.t. |
| 17 |
print "1..0 # SKIP threads broken in Perl_parser_dup\n"; |
| 18 |
exit 0; |
| 19 |
} |
| 20 |
if ($Config{osname} eq 'openbsd' and $Config{osvers} eq '7.0') { |
| 21 |
print "1..0 # SKIP OpenBSD 7.0 threads broken?\n"; |
| 22 |
exit 0; |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
use threads; |
| 27 |
use threads::shared; |
| 28 |
use Thread::Semaphore; |
| 29 |
use MyTest; |
| 30 |
|
| 31 |
use vars qw(@OPTS); |
| 32 |
|
| 33 |
BEGIN { |
| 34 |
@OPTS = qw(uuid5); |
| 35 |
ok 1, 'began'; |
| 36 |
} |
| 37 |
|
| 38 |
use UUID @OPTS; |
| 39 |
ok 1, 'loaded'; |
| 40 |
|
| 41 |
my $sync = 0; |
| 42 |
my $seen = {}; |
| 43 |
my $mutex = Thread::Semaphore->new(0); # locked |
| 44 |
note 'mutex init'; |
| 45 |
share($sync); |
| 46 |
note 'shared sync'; |
| 47 |
share($seen); |
| 48 |
note 'shared seen'; |
| 49 |
share($mutex); |
| 50 |
note 'shared mutex'; |
| 51 |
|
| 52 |
my ($t10, $t11, $t12, $t13, $t14, $t15, $t16, $t17, $t18, $t19); |
| 53 |
|
| 54 |
my $cnt = 0; |
| 55 |
$t10 = threads->create(\&doit, ++$cnt); |
| 56 |
note 'threads created'; |
| 57 |
$t11 = threads->create(\&doit, ++$cnt); |
| 58 |
note 'threads created'; |
| 59 |
$t12 = threads->create(\&doit, ++$cnt); |
| 60 |
note 'threads created'; |
| 61 |
$t13 = threads->create(\&doit, ++$cnt); |
| 62 |
note 'threads created'; |
| 63 |
$t14 = threads->create(\&doit, ++$cnt); |
| 64 |
note 'threads created'; |
| 65 |
$t15 = threads->create(\&doit, ++$cnt); |
| 66 |
note 'threads created'; |
| 67 |
$t16 = threads->create(\&doit, ++$cnt); |
| 68 |
note 'threads created'; |
| 69 |
$t17 = threads->create(\&doit, ++$cnt); |
| 70 |
note 'threads created'; |
| 71 |
$t18 = threads->create(\&doit, ++$cnt); |
| 72 |
note 'threads created'; |
| 73 |
$t19 = threads->create(\&doit, ++$cnt); |
| 74 |
note 'threads created'; |
| 75 |
|
| 76 |
note 'waiting'; |
| 77 |
my $timeout = 20; # 5 secs |
| 78 |
while (1) { |
| 79 |
my $s; |
| 80 |
{ lock($sync); $s = $sync } |
| 81 |
note "so far: $s"; |
| 82 |
last if $s >= 10; |
| 83 |
select undef, undef, undef, 0.25; |
| 84 |
next if --$timeout > 0; |
| 85 |
fail 'Waiting too long'; |
| 86 |
plan skip_all => 'HUNG'; |
| 87 |
} |
| 88 |
|
| 89 |
note 'do it'; |
| 90 |
$mutex->up(10); # thundering herd! |
| 91 |
|
| 92 |
$t10->join; $t11->join; $t12->join; $t13->join; $t14->join; |
| 93 |
$t15->join; $t16->join; $t17->join; $t18->join; $t19->join; |
| 94 |
|
| 95 |
sub doit { |
| 96 |
note 'in doit()'; |
| 97 |
my $i = shift; |
| 98 |
note "reporting $i"; |
| 99 |
{ lock $sync; ++$sync } |
| 100 |
note "requesting $i"; |
| 101 |
$mutex->down; |
| 102 |
note "generating $i"; |
| 103 |
for ( 1 .. 500 ) { |
| 104 |
my $uu = uuid5(dns => 'www.example.com'); |
| 105 |
{ lock $seen; ++$seen->{$uu} } |
| 106 |
} |
| 107 |
note "releasing $i"; |
| 108 |
$mutex->up; |
| 109 |
} |
| 110 |
|
| 111 |
is scalar(keys %$seen), 1, 'all dupes'; |
| 112 |
#if ((scalar keys %$seen) != 0) { |
| 113 |
# note "$_ $seen->{$_}" |
| 114 |
# for sort keys %$seen; |
| 115 |
#} |
| 116 |
|
| 117 |
done_testing; |