summaryrefslogtreecommitdiff
path: root/ext/threads/shared
diff options
context:
space:
mode:
authorJarkko Hietaniemi <jhi@iki.fi>2002-07-12 23:44:17 +0000
committerJarkko Hietaniemi <jhi@iki.fi>2002-07-12 23:44:17 +0000
commit83272a45226e83bd136d713158e9b44ace2dbc8d (patch)
treeee348b24fc5020ccd9e375cef8a5eb8e2bcd3d77 /ext/threads/shared
parent484fdf61e8653b10160ba1e8011888f52ab6825a (diff)
downloadperl-83272a45226e83bd136d713158e9b44ace2dbc8d.tar.gz
threads::shared::queue and semaphore become Thread::Semaphore
and Queue. The 5005threads case where the old Semaphore and Queue.pm (they are disguised as .pmx) should get magically installed instead has not been tested. p4raw-id: //depot/perl@17509
Diffstat (limited to 'ext/threads/shared')
-rwxr-xr-xext/threads/shared/Makefile.PL2
-rw-r--r--ext/threads/shared/queue.pm102
-rw-r--r--ext/threads/shared/semaphore.pm90
-rw-r--r--ext/threads/shared/shared.pm2
-rw-r--r--ext/threads/shared/t/queue.t71
-rw-r--r--ext/threads/shared/t/semaphore.t17
6 files changed, 1 insertions, 283 deletions
diff --git a/ext/threads/shared/Makefile.PL b/ext/threads/shared/Makefile.PL
index aa9faa6b52..18ac115a84 100755
--- a/ext/threads/shared/Makefile.PL
+++ b/ext/threads/shared/Makefile.PL
@@ -7,8 +7,6 @@ WriteMakefile(
'VERSION_FROM' => 'shared.pm', # finds $VERSION
'PM' => {
'shared.pm' => '$(INST_LIBDIR)/shared.pm',
- 'queue.pm' => '$(INST_LIBDIR)/shared/queue.pm',
- 'semaphore.pm' => '$(INST_LIBDIR)/shared/semaphore.pm',
},
'PREREQ_PM' => {}, # e.g., Module::Name => 1.1
($] >= 5.005 ? ## Add these new keywords supported since 5.005
diff --git a/ext/threads/shared/queue.pm b/ext/threads/shared/queue.pm
deleted file mode 100644
index 30b6ea29ec..0000000000
--- a/ext/threads/shared/queue.pm
+++ /dev/null
@@ -1,102 +0,0 @@
-package threads::shared::queue;
-
-use threads::shared;
-use strict;
-
-our $VERSION = '1.00';
-
-=head1 NAME
-
-threads::shared::queue - thread-safe queues
-
-=head1 SYNOPSIS
-
- use threads::shared::queue;
- my $q = new threads::shared::queue;
- $q->enqueue("foo", "bar");
- my $foo = $q->dequeue; # The "bar" is still in the queue.
- my $foo = $q->dequeue_nb; # returns "bar", or undef if the queue was
- # empty
- my $left = $q->pending; # returns the number of items still in the queue
-
-=head1 DESCRIPTION
-
-A queue, as implemented by C<threads::shared::queue> is a thread-safe
-data structure much like a list. Any number of threads can safely
-add elements to the end of the list, or remove elements from the head
-of the list. (Queues don't permit adding or removing elements from
-the middle of the list).
-
-=head1 FUNCTIONS AND METHODS
-
-=over 8
-
-=item new
-
-The C<new> function creates a new empty queue.
-
-=item enqueue LIST
-
-The C<enqueue> method adds a list of scalars on to the end of the queue.
-The queue will grow as needed to accommodate the list.
-
-=item dequeue
-
-The C<dequeue> method removes a scalar from the head of the queue and
-returns it. If the queue is currently empty, C<dequeue> will block the
-thread until another thread C<enqueue>s a scalar.
-
-=item dequeue_nb
-
-The C<dequeue_nb> method, like the C<dequeue> method, removes a scalar from
-the head of the queue and returns it. Unlike C<dequeue>, though,
-C<dequeue_nb> won't block if the queue is empty, instead returning
-C<undef>.
-
-=item pending
-
-The C<pending> method returns the number of items still in the queue.
-
-=back
-
-=head1 SEE ALSO
-
-L<threads>, L<threads::shared>
-
-=cut
-
-sub new {
- my $class = shift;
- my @q : shared = @_;
- return bless \@q, $class;
-}
-
-sub dequeue {
- my $q = shift;
- lock(@$q);
- cond_wait @$q until @$q;
- cond_signal @$q if @$q > 1;
- return shift @$q;
-}
-
-sub dequeue_nb {
- my $q = shift;
- lock(@$q);
- return shift @$q;
-}
-
-sub enqueue {
- my $q = shift;
- lock(@$q);
- push @$q, @_ and cond_signal @$q;
-}
-
-sub pending {
- my $q = shift;
- lock(@$q);
- return scalar(@$q);
-}
-
-1;
-
-
diff --git a/ext/threads/shared/semaphore.pm b/ext/threads/shared/semaphore.pm
deleted file mode 100644
index bb114dc5a6..0000000000
--- a/ext/threads/shared/semaphore.pm
+++ /dev/null
@@ -1,90 +0,0 @@
-package threads::shared::semaphore;
-
-use threads::shared;
-
-our $VERSION = '1.00';
-
-=head1 NAME
-
-threads::shared::semaphore - thread-safe semaphores
-
-=head1 SYNOPSIS
-
- use threads::shared::semaphore;
- my $s = new threads::shared::semaphore;
- $s->up; # Also known as the semaphore V -operation.
- # The guarded section is here
- $s->down; # Also known as the semaphore P -operation.
-
- # The default semaphore value is 1.
- my $s = new threads::shared::semaphore($initial_value);
- $s->up($up_value);
- $s->down($up_value);
-
-=head1 DESCRIPTION
-
-Semaphores provide a mechanism to regulate access to resources. Semaphores,
-unlike locks, aren't tied to particular scalars, and so may be used to
-control access to anything you care to use them for.
-
-Semaphores don't limit their values to zero or one, so they can be used to
-control access to some resource that there may be more than one of. (For
-example, filehandles). Increment and decrement amounts aren't fixed at one
-either, so threads can reserve or return multiple resources at once.
-
-=head1 FUNCTIONS AND METHODS
-
-=over 8
-
-=item new
-
-=item new NUMBER
-
-C<new> creates a new semaphore, and initializes its count to the passed
-number. If no number is passed, the semaphore's count is set to one.
-
-=item down
-
-=item down NUMBER
-
-The C<down> method decreases the semaphore's count by the specified number,
-or by one if no number has been specified. If the semaphore's count would drop
-below zero, this method will block until such time that the semaphore's
-count is equal to or larger than the amount you're C<down>ing the
-semaphore's count by.
-
-=item up
-
-=item up NUMBER
-
-The C<up> method increases the semaphore's count by the number specified,
-or by one if no number has been specified. This will unblock any thread blocked
-trying to C<down> the semaphore if the C<up> raises the semaphore count
-above the amount that the C<down>s are trying to decrement it by.
-
-=back
-
-=cut
-
-sub new {
- my $class = shift;
- my $val : shared = @_ ? shift : 1;
- bless \$val, $class;
-}
-
-sub down {
- my $s = shift;
- lock($$s);
- my $inc = @_ ? shift : 1;
- cond_wait $$s until $$s >= $inc;
- $$s -= $inc;
-}
-
-sub up {
- my $s = shift;
- lock($$s);
- my $inc = @_ ? shift : 1;
- ($$s += $inc) > 0 and cond_broadcast $$s;
-}
-
-1;
diff --git a/ext/threads/shared/shared.pm b/ext/threads/shared/shared.pm
index d464caddf7..3016039b83 100644
--- a/ext/threads/shared/shared.pm
+++ b/ext/threads/shared/shared.pm
@@ -116,7 +116,7 @@ C<lock(\$a)> is equivalent to C<lock($a)>, while C<lock(\\$a)> is not.
Note that you cannot explicitly unlock a variable; you can only wait
for the lock to go out of scope. If you need more fine-grained
-control, see L<threads::shared::semaphore>.
+control, see L<Thread::Semaphore>.
=item cond_wait VARIABLE
diff --git a/ext/threads/shared/t/queue.t b/ext/threads/shared/t/queue.t
deleted file mode 100644
index 259f7f54a4..0000000000
--- a/ext/threads/shared/t/queue.t
+++ /dev/null
@@ -1,71 +0,0 @@
-use warnings;
-
-BEGIN {
- chdir 't' if -d 't';
- push @INC ,'../lib';
- require Config; import Config;
- unless ($Config{'useithreads'}) {
- print "1..0 # Skip: might still hang\n";
- exit 0;
- }
-}
-
-use strict;
-use threads;
-use threads::shared::queue;
-
-my $q = new threads::shared::queue;
-$|++;
-print "1..26\n";
-
-my $test : shared = 1;
-
-sub ok {
- lock($test);
- print "ok $test\n";
- $test++;
-}
-
-sub reader {
- my $tid = threads->self->tid;
- my $i = 0;
- while (1) {
- $i++;
-# print "reader (tid $tid): waiting for element $i...\n";
- my $el = $q->dequeue;
- ok();
-# print "ok $test\n"; $test++;
-# print "reader (tid $tid): dequeued element $i: value $el\n";
- select(undef, undef, undef, rand(1));
- if ($el == -1) {
- # end marker
-# print "reader (tid $tid) returning\n";
- return;
- }
- }
-}
-
-my $nthreads = 5;
-my @threads;
-
-for (my $i = 0; $i < $nthreads; $i++) {
- push @threads, threads->new(\&reader, $i);
-}
-
-for (my $i = 1; $i <= 20; $i++) {
- my $el = int(rand(100));
- select(undef, undef, undef, rand(1));
-# print "writer: enqueuing value $el\n";
- $q->enqueue($el);
-}
-
-$q->enqueue((-1) x $nthreads); # one end marker for each thread
-
-for(@threads) {
-# print "waiting for join\n";
- $_->join();
-}
-ok();
-#print "ok $test\n";
-
-
diff --git a/ext/threads/shared/t/semaphore.t b/ext/threads/shared/t/semaphore.t
deleted file mode 100644
index 12b0a3651f..0000000000
--- a/ext/threads/shared/t/semaphore.t
+++ /dev/null
@@ -1,17 +0,0 @@
-use warnings;
-
-BEGIN {
- chdir 't' if -d 't';
- push @INC ,'../lib';
- require Config; import Config;
- unless ($Config{'useithreads'}) {
- print "1..0 # Skip: no threads\n";
- exit 0;
- }
-}
-
-print "1..1\n";
-use threads;
-use threads::shared::semaphore;
-print "ok 1\n";
-