Skip to content

Commit

Permalink
Thread-Suspend v1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
jdhedden committed Apr 21, 2016
1 parent fde70f4 commit f9820ca
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 364 deletions.
3 changes: 3 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Revision history for Perl extension Thread::Suspend.

1.13 Mon Feb 25 22:17:19 2008
- Fixes to tests

1.12 Fri Feb 22 22:19:37 2008
- Allow installation on non-threaded Perls

Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ t/03_detach.t
t/04_all.t
t/05_some.t
t/99_pod.t
t/test.pl
examples/suspend.pl
META.yml Module meta-data (added by MakeMaker)
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.12
Thread::Suspend version 1.13
============================

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.12';
our $VERSION = '1.13';

use threads 1.39;
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.12
This document describes Thread::Suspend version 1.13
=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.12/lib/Thread/Suspend.pm>
L<http://annocpan.org/~JDHEDDEN/Thread-Suspend-1.13/lib/Thread/Suspend.pm>
Source repository:
L<http://code.google.com/p/thread-suspend/>
Expand Down
69 changes: 23 additions & 46 deletions t/00_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@ BEGIN {
use threads;
use threads::shared;

use Test::More 'tests' => 57;

### Preamble ###

our $nthreads;
BEGIN { $nthreads = 3; }
use Test::More 'tests' => 3 + 18 * $nthreads;


### Load module ###

use_ok('Thread::Suspend');

Expand All @@ -22,85 +30,54 @@ if ($Thread::Suspend::VERSION) {

can_ok('threads', qw(suspend is_suspended resume));

my %COUNTS :shared;

$SIG{'KILL'} = sub { threads->exit(); };
### Setup ###

sub thr_func
{
my $tid = threads->tid();
while (++$COUNTS{$tid}) {
threads->yield();
}
}

sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();
require 't/test.pl';

my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
threads->yield() for (1..3);
sleep(1);
threads->yield() for (1..3);
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
ok($begin == $end, "Thread $tid stopped");
}
}
my @threads = make_threads($nthreads);

my @threads;
for (1..3) {
unshift(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);

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

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

ok(! threads->is_suspended(), 'No threads suspended');
is($thr->is_suspended(), 0, "Thread $tid not suspended");
check($thr, 'running');
check($thr, 'running', __LINE__);

$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');
check($thr, 'stopped', __LINE__);

$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');
check($thr, 'stopped', __LINE__);

$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');
check($thr, 'stopped', __LINE__);

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

foreach my $thr (@threads) {
$thr->kill('KILL')->join();
}

### Cleanup ###

$_->kill('KILL')->join() foreach (@threads);

# EOF
77 changes: 34 additions & 43 deletions t/01_self.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,89 +12,80 @@ BEGIN {
use threads;
use threads::shared;

use Test::More 'tests' => 56;

### Preamble ###

our $nthreads;
BEGIN { $nthreads = 3; }
use Test::More 'tests' => 3 + 17 * $nthreads;


### Load module ###

use_ok('Thread::Suspend');

my %COUNTS :shared;

$SIG{'KILL'} = sub { threads->exit(); };
### Setup ###

sub thr_func
require 't/test.pl';

sub counter2
{
my $tid = threads->tid();
$COUNTS{$tid} = 1;
threads->self()->suspend();
while (++$COUNTS{$tid}) {
while (1) {
{
lock(@::COUNTS);
$::COUNTS[$tid]++;
}
threads->yield();
}
}

sub check {
my ($thr, $running) = @_;
my $tid = $thr->tid();

my ($begin, $end);
do {
do {
threads->yield();
$begin = $COUNTS{$tid};
} while (! $begin);
threads->yield() for (1..3);
sleep(1);
threads->yield() for (1..3);
$end = $COUNTS{$tid};
} while (! $end);
if ($running eq 'running') {
ok($begin < $end, "Thread $tid running");
} else {
ok($begin == $end, "Thread $tid stopped");
}
}

my @threads;
for (1..3) {
push(@threads, threads->create('thr_func'));
}
threads->yield();
sleep(1);
push(@threads, threads->create('counter2')) for (1..$nthreads);
is(scalar(threads->list()), $nthreads, 'Threads created');


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

pause(0.1);
foreach my $thr (threads->is_suspended()) {
is(scalar(grep { $_ == $thr } @threads), 1, 'In suspend list');
}
ok(! @::COUNTS, 'No threads running');


while (my $thr = pop(@threads)) {
while (my $thr = shift(@threads)) {
my $tid = $thr->tid();

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

$thr->suspend();
threads->yield();
is(scalar(threads->is_suspended())-1, scalar(@threads), "Threads suspended");
is(scalar(threads->is_suspended()), scalar(@threads)+1, "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');
check($thr, 'stopped', __LINE__);

$thr->resume();
threads->yield();
is(scalar(threads->is_suspended())-1, scalar(@threads), "Threads suspended");
is(scalar(threads->is_suspended()), scalar(@threads)+1, "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}, 1, "Thread $tid has 1 count");
check($thr, 'stopped', __LINE__);

$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");
check($thr, 'running');
check($thr, 'running', __LINE__);

# Cleanup
$thr->kill('KILL')->join();
}

Expand Down
Loading

0 comments on commit f9820ca

Please sign in to comment.