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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
package Thread::Conveyor::Tied;
# Make sure we have version info for this module
# Make sure we are a belt
# Make sure we do everything by the book from now on
$VERSION = '0.21';
@ISA = qw(Thread::Conveyor);
use strict;
# Make sure we only load stuff when we actually need it
use load;
# Satisfy -require-
1;
#---------------------------------------------------------------------------
# The following subroutines are loaded only on demand
__END__
#---------------------------------------------------------------------------
# Class methods
#---------------------------------------------------------------------------
# IN: 1 class with which to bless the object
# OUT: 1 instantiated object
sub new {
# Create the tied conveyor belt
# And bless reference to belt + its semaphore as an object and return it
tie my @array,'Thread::Tie';
bless [\@array,(tied @array)->semaphore],shift;
} #new
#---------------------------------------------------------------------------
# object methods
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1 shared item on which you can lock
sub semaphore { shift->[1] } # semaphore
#---------------------------------------------------------------------------
# Object methods
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# 2..N parameters to be passed as a box onto the belt
sub put {
# Obtain the object
# Return now if nothing to do
my ($array,$semaphore) = @{shift()};
return unless @_;
# Make sure we're the only one putting things on the belt
# Freeze the parameters and put it in a box on the belt
# Signal the other worker threads that there is a new box on the belt
lock( $semaphore );
push( @$array,Thread::Serialize::freeze( @_ ) );
threads::shared::cond_signal( $semaphore );
} #put
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1..N parameters returned from a box on the belt
sub take {
# Obtain the belt and semaphore
# Create an empty box
my ($array,$semaphore) = @{shift()};
my $box;
# Make sure we're the only one working on the belt
# Wait until someone else puts something on the belt
# Take the box off the belt
# Wake up other worker threads if there are still boxes now
{lock( $semaphore );
threads::shared::cond_wait( $semaphore ) until @$array;
$box = shift( @$array );
threads::shared::cond_signal( $semaphore ) if @$array;
} #$semaphore
# Thaw the contents of the box and return the result
Thread::Serialize::thaw( $box );
} #take
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1..N parameters returned from a box on the belt
sub take_dontwait {
# Obtain the object
# Obtain belt and semaphore
# Make sure we're the only one handling the belt
# Return the result of taking of a box if there is one, or an empty list
my $self = shift;
my ($array,$semaphore) = @{$self};
lock( $semaphore );
return @$array ? $self->take : ();
} #take_dontwait
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1..N references to data-structures in boxes
sub clean {
# Obtain the object
# Return now after cleaning if we're not interested in the result
# Clean the belt and turn the boxes into references
my $self = shift;
return $self->_clean unless wantarray;
map {[Thread::Serialize::thaw( $_ )]} $self->_clean;
} #clean
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1..N references to data-structures in boxes
sub clean_dontwait {
# Obtain the object
# Obtain the belt and semaphore
# Make sure we're the only one handling the belt
# Return the result of cleaning the belt if there are boxes, or an empty list
my $self = shift;
my ($array,$semaphore) = @{$self};
lock( $semaphore );
return @$array ? $self->clean : ();
} #clean_dontwait
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# 2 ordinal number in array to return (default: 0)
# OUT: 1..N parameters returned from a box on the belt
sub peek {
# Obtain the belt and the semaphore
# Create an empty box
my ($array,$semaphore) = @{shift()};
my $box;
# Make sure we're the only one working on the belt
# Wait until someone else puts something on the belt
# Copy the box off the belt
# Wake up other worker threads again
{lock( $semaphore );
threads::shared::cond_wait( $semaphore ) until @$array;
$box = $array->[shift || 0];
threads::shared::cond_signal( $semaphore );
} #$semaphore
# Thaw the contents of the box and return the result
Thread::Serialize::thaw( $box );
} #peek
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# 2 ordinal number in array to return (default: 0)
# OUT: 1..N parameters returned from a box on the belt
sub peek_dontwait {
# Obtain the object
# Obtain the belt and the semaphore
# Make sure we're the only one handling the belt
# Return the result of taking of a box if there is one, or an empty list
my $self = shift;
my ($array,$semaphore) = @{$self};
lock( $semaphore );
return @$array ? $self->peek( @_ ) : ();
} #peek_dontwait
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1 number of boxes still on the belt
sub onbelt { scalar(@{$_[0]->[0]}) } #onbelt
#---------------------------------------------------------------------------
# IN: 1 instantiated object (ignored)
sub maxboxes {
die "Cannot change throttling on a belt that was created unthrottled";
} #maxboxes
#---------------------------------------------------------------------------
# IN: 1 instantiated object (ignored)
sub minboxes {
die "Cannot change throttling on a belt that was created unthrottled";
} #minboxes
#---------------------------------------------------------------------------
# IN: 1 instantiated object
sub shutdown { undef } #shutdown
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1 thread object associated with belt (always undef)
sub thread { undef } #thread
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1 thread id of thread object associated with belt (always undef)
sub tid { undef } #tid
#---------------------------------------------------------------------------
# Internal subroutines
#---------------------------------------------------------------------------
# IN: 1 instantiated object
# OUT: 1..N all frozen boxes on the belt
sub _clean {
# Obtain the belt
# Initialize the list of frozen boxes
my ($array,$semaphore) = @{shift()};
my @frozen;
# Make sure we're the only one accessing the belt
# Wait until there is something on the belt
# Obtain the entire contents of the belt of we want it
# Clean the belt
# Notify the world again
{lock( $semaphore );
threads::shared::cond_wait( $semaphore ) until @$array;
@frozen = @$array if wantarray;
@$array = ();
threads::shared::cond_broadcast( $semaphore );
} #$semaphore
# Return the frozen goods
@frozen;
} #_clean
#---------------------------------------------------------------------------
__END__
=head1 NAME
Thread::Conveyor::Tied - tied array implementation of Thread::Conveyor
=head1 DESCRIPTION
This class should not be called by itself, but only with a call to
L<Thread::Conveyor>.
=head1 AUTHOR
Elizabeth Mattijsen, <liz@dijkmat.nl>.
Please report bugs to <perlbugs@dijkmat.nl>.
=head1 COPYRIGHT
Copyright (c) 2002, 2003, 2004, 2007, 2010 Elizabeth Mattijsen <liz@dijkmat.nl>.
All rights reserved. This program is free software; you can redistribute it
and/or modify it under the same terms as Perl itself.
=head1 SEE ALSO
L<Thread::Conveyor>.
=cut
|