Skip to content

Commit

Permalink
Thread-Suspend v1.05
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhedden committed Apr 21, 2016
1 parent 993d180 commit 3fb0719
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 31 deletions.
5 changes: 4 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Revision history for Perl extension Thread::Suspend.

1.05 Wed Aug 2 13:54:17 EDT 2006
- More fixes to tests

1.04 Tue Aug 1 13:56:38 EDT 2006
- Fixes in tests
- Fixes to tests

1.03 Thu Jul 13 08:53:36 EDT 2006
- Added caveat to POD concerning locks
Expand Down
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Thread::Suspend version 1.04
Thread::Suspend version 1.05
============================

This module adds suspend and resume operations for threads.
Expand Down
6 changes: 3 additions & 3 deletions lib/Thread/Suspend.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package Thread::Suspend; {
use strict;
use warnings;

our $VERSION = 1.04;
our $VERSION = 1.05;

use threads 1.36;
use threads::shared 1.01;
Expand Down Expand Up @@ -155,7 +155,7 @@ Thread::Suspend - Suspend and resume operations for threads
=head1 VERSION
This document describes Thread::Suspend version 1.04
This document describes Thread::Suspend version 1.05
=head1 SYNOPSIS
Expand Down Expand Up @@ -316,7 +316,7 @@ Thread::Suspend Discussion Forum on CPAN:
L<http://www.cpanforum.com/dist/Thread-Suspend>
Annotated POD for Thread::Suspend:
L<http://annocpan.org/~JDHEDDEN/Thread-Suspend-1.04/lib/Thread/Suspend.pm>
L<http://annocpan.org/~JDHEDDEN/Thread-Suspend-1.05/lib/Thread/Suspend.pm>
L<threads>, L<threads::shared>
Expand Down
19 changes: 15 additions & 4 deletions t/00_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();

threads->yield();
my $begin = $COUNTS{$tid};
sleep(1);
my $end = $COUNTS{$tid};
my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
threads->yield();
sleep(1);
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
Expand All @@ -44,6 +50,7 @@ for (1..3) {
unshift(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);

is(scalar(threads->list()), 3, 'Threads created');

Expand All @@ -54,24 +61,28 @@ foreach my $thr (@threads) {
check($thr, 'running');

$thr->suspend();
threads->yield();
is(scalar(threads->is_suspended()), 1, 'One thread suspended');
ok((threads->is_suspended())[0] == $thr, "Thread $tid suspended");
is($thr->is_suspended(), 1, "Thread $tid suspended");
check($thr, 'stopped');

$thr->suspend();
threads->yield();
is(scalar(threads->is_suspended()), 1, 'One thread suspended');
ok((threads->is_suspended())[0] == $thr, "Thread $tid suspended");
is($thr->is_suspended(), 2, "Thread $tid suspended twice");
check($thr, 'stopped');

$thr->resume();
threads->yield();
is(scalar(threads->is_suspended()), 1, 'One thread suspended');
ok((threads->is_suspended())[0] == $thr, "Thread $tid suspended");
is($thr->is_suspended(), 1, "Thread $tid still suspended");
check($thr, 'stopped');

$thr->resume();
threads->yield();
ok(! threads->is_suspended(), 'No threads suspended');
is($thr->is_suspended(), 0, "Thread $tid not suspended");
check($thr, 'running');
Expand Down
22 changes: 16 additions & 6 deletions t/01_self.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $SIG{'KILL'} = sub { threads->exit(); };
sub thr_func
{
my $tid = threads->tid();
$COUNTS{$tid} = 0;
$COUNTS{$tid} = 1;
threads->self()->suspend();
while (1) {
$COUNTS{$tid}++;
Expand All @@ -24,10 +24,16 @@ sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();

threads->yield();
my $begin = $COUNTS{$tid};
sleep(1);
my $end = $COUNTS{$tid};
my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
threads->yield();
sleep(1);
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
Expand All @@ -40,6 +46,7 @@ for (1..3) {
push(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);

is(scalar(threads->list()), 3, 'Threads created');

Expand All @@ -56,19 +63,22 @@ while (my $thr = pop(@threads)) {
check($thr, 'stopped');

$thr->suspend();
threads->yield();
is(scalar(threads->is_suspended())-1, scalar(@threads), "Threads suspended");
is(scalar(grep { $_ == $thr } threads->is_suspended()), 1, 'In suspend list');
is($thr->is_suspended(), 2, "Thread $tid suspended twice");
check($thr, 'stopped');

$thr->resume();
threads->yield();
is(scalar(threads->is_suspended())-1, scalar(@threads), "Threads suspended");
is(scalar(grep { $_ == $thr } threads->is_suspended()), 1, 'In suspend list');
is($thr->is_suspended(), 1, "Thread $tid still suspended");
check($thr, 'stopped');
is($COUNTS{$tid}, 0, "Thread $tid has 0 count");
is($COUNTS{$tid}, 1, "Thread $tid has 1 count");

$thr->resume();
threads->yield();
is(scalar(threads->is_suspended()), scalar(@threads), "Threads suspended");
is(scalar(grep { $_ == $thr } threads->is_suspended()), 0, 'Not in suspend list');
is($thr->is_suspended(), 0, "Thread $tid not suspended");
Expand Down
19 changes: 15 additions & 4 deletions t/02_signal.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();

threads->yield();
my $begin = $COUNTS{$tid};
sleep(1);
my $end = $COUNTS{$tid};
my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
sleep(1);
threads->yield();
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
Expand All @@ -41,6 +47,7 @@ for (1..1) {
unshift(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);

is(scalar(threads->list()), 1, 'Threads created');

Expand All @@ -51,24 +58,28 @@ foreach my $thr (@threads) {
check($thr, 'running');

$thr->suspend();
threads->yield();
is(scalar(threads->is_suspended()), 1, 'One thread suspended');
ok((threads->is_suspended())[0] == $thr, "Thread $tid suspended");
is($thr->is_suspended(), 1, "Thread $tid suspended");
check($thr, 'stopped');

$thr->suspend();
threads->yield();
is(scalar(threads->is_suspended()), 1, 'One thread suspended');
ok((threads->is_suspended())[0] == $thr, "Thread $tid suspended");
is($thr->is_suspended(), 2, "Thread $tid suspended twice");
check($thr, 'stopped');

$thr->resume();
threads->yield();
is(scalar(threads->is_suspended()), 1, 'One thread suspended');
ok((threads->is_suspended())[0] == $thr, "Thread $tid suspended");
is($thr->is_suspended(), 1, "Thread $tid still suspended");
check($thr, 'stopped');

$thr->resume();
threads->yield();
ok(! threads->is_suspended(), 'No threads suspended');
is($thr->is_suspended(), 0, "Thread $tid not suspended");
check($thr, 'running');
Expand Down
20 changes: 16 additions & 4 deletions t/03_detach.t
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();

threads->yield();
my $begin = $COUNTS{$tid};
sleep(1);
my $end = $COUNTS{$tid};
my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
threads->yield();
sleep(1);
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
Expand All @@ -43,13 +49,15 @@ for (1..3) {
push(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);

is(scalar(threads->list()), 3, 'Threads created');

foreach my $thr (@threads) {
$thr->detach();
}
threads->yield();
sleep(1);

is(scalar(threads->list()), 0, 'Threads detached');

Expand All @@ -60,19 +68,23 @@ foreach my $thr (@threads) {
check($thr, 'running');

$thr->suspend();
threads->yield();
is($thr->is_suspended(), 1, "Thread $tid suspended");
check($thr, 'stopped');

$thr->suspend();
threads->yield();
ok(! threads->is_suspended(), 'No reported suspended threads');
is($thr->is_suspended(), 2, "Thread $tid suspended twice");
check($thr, 'stopped');

$thr->resume();
threads->yield();
is($thr->is_suspended(), 1, "Thread $tid still suspended");
check($thr, 'stopped');

$thr->resume();
threads->yield();
is($thr->is_suspended(), 0, "Thread $tid not suspended");
check($thr, 'running');

Expand Down
35 changes: 31 additions & 4 deletions t/04_all.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@ sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();

threads->yield();
my $begin = $COUNTS{$tid};
sleep(1);
my $end = $COUNTS{$tid};
my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
threads->yield();
sleep(1);
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
Expand All @@ -44,6 +50,7 @@ for (1..3) {
unshift(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);

is(scalar(threads->list()), 3, 'Threads created');
ok(! threads->is_suspended(), 'No threads suspended');
Expand All @@ -56,6 +63,8 @@ foreach my $thr (@threads) {
# Test all threads

my @suspended = threads->suspend();
threads->yield();
sleep(1);
is(scalar(@suspended), 3, 'Suspended threads');
foreach my $thr (@suspended) {
is(scalar(grep { $_ == $thr } @threads), 1, 'Thread suspended');
Expand All @@ -73,20 +82,26 @@ foreach my $thr (@threads) {
}

is(scalar(threads->suspend()), 3, 'Suspending again');
threads->yield();
sleep(1);
foreach my $thr (@threads) {
my $tid = $thr->tid();
is($thr->is_suspended(), 2, "Thread $tid suspended");
check($thr, 'stopped');
}

is(scalar(threads->resume()), 3, 'Resuming once');
threads->yield();
sleep(1);
foreach my $thr (@threads) {
my $tid = $thr->tid();
is($thr->is_suspended(), 1, "Thread $tid suspended");
check($thr, 'stopped');
}

is(scalar(threads->resume()), 3, 'Resuming again');
threads->yield();
sleep(1);
foreach my $thr (@threads) {
my $tid = $thr->tid();
is($thr->is_suspended(), 0, "Thread $tid not suspended");
Expand All @@ -96,10 +111,14 @@ foreach my $thr (@threads) {
# Test threads with extra suspends

is($threads[1]->suspend(), $threads[1], 'Suspend thread');
threads->yield();
sleep(1);
is(scalar(threads->is_suspended()), 1, '1 thread suspended');
check($threads[1], 'stopped');

@suspended = threads->suspend();
threads->yield();
sleep(1);
is(scalar(@suspended), 3, 'Suspended threads');
foreach my $thr (@suspended) {
is(scalar(grep { $_ == $thr } @threads), 1, 'Thread suspended');
Expand All @@ -113,12 +132,16 @@ foreach my $thr (@threads) {
}

is(scalar(threads->resume()), 3, 'Resuming threads');
threads->yield();
sleep(1);
is(scalar($threads[0]->is_suspended()), 0, 'Thread not suspended');
is(scalar($threads[1]->is_suspended()), 1, 'Thread suspended');
is(scalar($threads[2]->is_suspended()), 0, 'Thread not suspended');
check($threads[1], 'stopped');

is($threads[1]->resume(), $threads[1], 'Thread resumed');
threads->yield();
sleep(1);

foreach my $thr (@threads) {
my $tid = $thr->tid();
Expand All @@ -132,12 +155,16 @@ $threads[1]->detach();
ok($threads[1]->is_detached(), 'Thread detached');

@suspended = threads->suspend();
threads->yield();
sleep(1);
is(scalar(@suspended), 2, 'Suspended threads');
is(scalar(grep { $_ == $threads[0] } @suspended), 1, 'Thread suspended');
is(scalar(grep { $_ == $threads[2] } @suspended), 1, 'Thread suspended');
is(scalar($threads[1]->is_suspended()), 0, 'Thread not suspended');

is(scalar(threads->resume()), 2, 'Resuming threads');
threads->yield();
sleep(1);

foreach my $thr (@threads) {
my $tid = $thr->tid();
Expand Down
Loading

0 comments on commit 3fb0719

Please sign in to comment.