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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
$|=1;
print "1..6\n";
use Coro;
use Coro::Semaphore;
{
my $sem = new Coro::Semaphore 2;
my $rand = 0;
sub xrand {
$rand = ($rand * 121 + 2121) % 212121;
$rand / 212120
}
my $counter;
$_->join for
map {
async {
my $current = $Coro::current;
for (1..100) {
cede if 0.2 > xrand;
Coro::async_pool { $current->ready } if 0.2 > xrand;
$counter += $sem->count;
my $guard = $sem->guard;
cede; cede; cede; cede;
}
}
} 1..15
;
print $counter == 998 ? "" : "not ", "ok 1 # $counter\n";
}
# check terminate
{
my $sem = new Coro::Semaphore 0;
$as1 = async {
my $g = $sem->guard;
print "not ok 2\n";
};
$as2 = async {
my $g = $sem->guard;
print "ok 2\n";
};
cede;
$sem->up; # wake up as1
$as1->cancel; # destroy as1 before it could ->guard
$as1->join;
$as2->join;
}
# check throw
{
my $sem = new Coro::Semaphore 0;
$as1 = async {
my $g = eval {
$sem->guard;
};
print $@ ? "" : "not ", "ok 3\n";
};
$as2 = async {
my $g = $sem->guard;
print "ok 4\n";
};
cede;
$sem->up; # wake up as1
$as1->throw (1); # destroy as1 before it could ->guard
$as1->join;
$as2->join;
}
# check wait
{
my $sem = new Coro::Semaphore 0;
$as1 = async {
$sem->wait;
print "ok 5\n";
};
$as2 = async {
my $g = $sem->guard;
print "ok 6\n";
};
cede;
$sem->up; # wake up as1
$as1->join;
$as2->join;
}
|