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
|
#!perl
use strict;
use warnings;
use Config;
BEGIN {
# Yes, this is really necessary
if ($Config{useithreads} && $^O ne 'gnu') {
require threads;
threads->import();
require Test::More;
Test::More->import(tests => 7);
}
else {
require Test::More;
Test::More->import(skip_all => "No threading support enabled");
}
}
use File::Map qw/map_anonymous sync :lock/;
use Time::HiRes qw/sleep time/;
use Test::Warnings;
map_anonymous my $variable, 1024;
substr $variable, 0, 5, "Horse";
my $counter;
alarm 5;
my $thread1 = async {
lock_map $variable;
wait_until { $counter++ } $variable;
is($counter, 2, 'Counter is 2');
};
sleep .1;
do {
lock_map $variable;
notify $variable;
};
$thread1->join;
ok(1, "First notification worked");
my $thread2 = async {
lock_map $variable;
wait_until { $counter++ } $variable;
};
sleep .1;
{
lock_map $variable;
notify $variable;
}
$thread2->join;
ok(1, "Second notification worked");
{
my $start = time;
threads->create(\&sleeper, "Camel")->detach;
lock_map $variable;
my $foo = wait_until { substr($_, 0, 5) eq "Camel" } $variable;
is($foo, 1, '$foo == 1');
cmp_ok(time - $start, '>', 0.19, "Must have waited");
is(substr($variable, 0, 5), "Camel", 'Variable should contain "Camel"');
}
sub sleeper {
sleep .1;
my $word = shift;
{
lock_map $variable;
notify $variable;
}
sleep .1;
{
lock_map $variable;
substr $variable, 0, 5, $word;
notify $variable;
}
}
|