| 1 | ############################################################################### | ||
| 2 | ## ---------------------------------------------------------------------------- | ||
| 3 | ## Channel for producer(s) and many consumers supporting threads only. | ||
| 4 | ## | ||
| 5 | ############################################################################### | ||
| 6 | |||
| 7 | package MCE::Channel::Threads; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | |||
| 12 | no warnings qw( uninitialized once ); | ||
| 13 | |||
| 14 | our $VERSION = '1.904'; | ||
| 15 | |||
| 16 | use threads; | ||
| 17 | use threads::shared; | ||
| 18 | |||
| 19 | use base 'MCE::Channel'; | ||
| 20 | |||
| 21 | my $LF = "\012"; Internals::SvREADONLY($LF, 1); | ||
| 22 | my $is_MSWin32 = ( $^O eq 'MSWin32' ) ? 1 : 0; | ||
| 23 | my $freeze = MCE::Channel::_get_freeze(); | ||
| 24 | my $thaw = MCE::Channel::_get_thaw(); | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | my ( $class, %obj ) = ( @_, impl => 'Threads' ); | ||
| 28 | |||
| 29 | $obj{init_pid} = MCE::Channel::_pid(); | ||
| 30 | MCE::Util::_sock_pair( \%obj, 'p_sock', 'c_sock' ); | ||
| 31 | MCE::Util::_sock_pair( \%obj, 'p2_sock', 'c2_sock' ) if $is_MSWin32; | ||
| 32 | |||
| 33 | # locking for the consumer side of the channel | ||
| 34 | $obj{cr_mutex} = threads::shared::share( my $cr_mutex ); | ||
| 35 | $obj{cw_mutex} = threads::shared::share( my $cw_mutex ); | ||
| 36 | |||
| 37 | # optionally, support many-producers writing and reading | ||
| 38 | $obj{pr_mutex} = threads::shared::share( my $pr_mutex ) if $obj{mp}; | ||
| 39 | $obj{pw_mutex} = threads::shared::share( my $pw_mutex ) if $obj{mp}; | ||
| 40 | |||
| 41 | return bless \%obj, $class; | ||
| 42 | } | ||
| 43 | |||
| 44 | ############################################################################### | ||
| 45 | ## ---------------------------------------------------------------------------- | ||
| 46 | ## Queue-like methods. | ||
| 47 | ## | ||
| 48 | ############################################################################### | ||
| 49 | |||
| 50 | sub end { | ||
| 51 | my ( $self ) = @_; | ||
| 52 | |||
| 53 | local $\ = undef if (defined $\); | ||
| 54 | MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32; | ||
| 55 | print { $self->{p_sock} } pack('i', -1); | ||
| 56 | |||
| 57 | $self->{ended} = 1; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub enqueue { | ||
| 61 | my $self = shift; | ||
| 62 | return MCE::Channel::_ended('enqueue') if $self->{ended}; | ||
| 63 | |||
| 64 | local $\ = undef if (defined $\); | ||
| 65 | |||
| 66 | { | ||
| 67 | CORE::lock $self->{pw_mutex} if $self->{pw_mutex}; | ||
| 68 | MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32; | ||
| 69 | |||
| 70 | while ( @_ ) { | ||
| 71 | my $data = $freeze->([ shift ]); | ||
| 72 | print { $self->{p_sock} } pack('i', length $data), $data; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return 1; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub dequeue { | ||
| 80 | my ( $self, $count ) = @_; | ||
| 81 | $count = 1 if ( !$count || $count < 1 ); | ||
| 82 | |||
| 83 | if ( $count == 1 ) { | ||
| 84 | my ( $plen, $data ); | ||
| 85 | |||
| 86 | { | ||
| 87 | CORE::lock $self->{cr_mutex}; | ||
| 88 | MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32; | ||
| 89 | MCE::Util::_sysread( $self->{c_sock}, $plen, 4 ); | ||
| 90 | |||
| 91 | my $len = unpack('i', $plen); | ||
| 92 | if ( $len < 0 ) { | ||
| 93 | $self->end; | ||
| 94 | return wantarray ? () : undef; | ||
| 95 | } | ||
| 96 | |||
| 97 | MCE::Channel::_read( $self->{c_sock}, $data, $len ); | ||
| 98 | } | ||
| 99 | |||
| 100 | wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1]; | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | my ( $plen, @ret ); | ||
| 104 | |||
| 105 | { | ||
| 106 | CORE::lock $self->{cr_mutex}; | ||
| 107 | MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32; | ||
| 108 | |||
| 109 | while ( $count-- ) { | ||
| 110 | MCE::Util::_sysread( $self->{c_sock}, $plen, 4 ); | ||
| 111 | |||
| 112 | my $len = unpack('i', $plen); | ||
| 113 | if ( $len < 0 ) { | ||
| 114 | $self->end; | ||
| 115 | last; | ||
| 116 | } | ||
| 117 | |||
| 118 | MCE::Channel::_read( $self->{c_sock}, my($data), $len ); | ||
| 119 | push @ret, @{ $thaw->($data) }; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | wantarray ? @ret : $ret[-1]; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub dequeue_nb { | ||
| 128 | my ( $self, $count ) = @_; | ||
| 129 | $count = 1 if ( !$count || $count < 1 ); | ||
| 130 | |||
| 131 | my ( $plen, @ret ); | ||
| 132 | |||
| 133 | { | ||
| 134 | CORE::lock $self->{cr_mutex}; | ||
| 135 | |||
| 136 | while ( $count-- ) { | ||
| 137 | MCE::Util::_nonblocking( $self->{c_sock}, 1 ); | ||
| 138 | MCE::Util::_sysread( $self->{c_sock}, $plen, 4 ); | ||
| 139 | MCE::Util::_nonblocking( $self->{c_sock}, 0 ); | ||
| 140 | |||
| 141 | my $len; $len = unpack('i', $plen) if $plen; | ||
| 142 | if ( !$len || $len < 0 ) { | ||
| 143 | $self->end if defined $len && $len < 0; | ||
| 144 | last; | ||
| 145 | } | ||
| 146 | |||
| 147 | MCE::Channel::_read( $self->{c_sock}, my($data), $len ); | ||
| 148 | push @ret, @{ $thaw->($data) }; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | wantarray ? @ret : $ret[-1]; | ||
| 153 | } | ||
| 154 | |||
| 155 | ############################################################################### | ||
| 156 | ## ---------------------------------------------------------------------------- | ||
| 157 | ## Methods for two-way communication; producer(s) to consumers. | ||
| 158 | ## | ||
| 159 | ############################################################################### | ||
| 160 | |||
| 161 | sub send { | ||
| 162 | my $self = shift; | ||
| 163 | return MCE::Channel::_ended('send') if $self->{ended}; | ||
| 164 | |||
| 165 | my $data = $freeze->([ @_ ]); | ||
| 166 | |||
| 167 | local $\ = undef if (defined $\); | ||
| 168 | |||
| 169 | { | ||
| 170 | CORE::lock $self->{pw_mutex} if $self->{pw_mutex}; | ||
| 171 | MCE::Util::_sock_ready_w( $self->{p_sock} ) if $is_MSWin32; | ||
| 172 | print { $self->{p_sock} } pack('i', length $data), $data; | ||
| 173 | } | ||
| 174 | |||
| 175 | return 1; | ||
| 176 | } | ||
| 177 | |||
| 178 | sub recv { | ||
| 179 | my ( $self ) = @_; | ||
| 180 | my ( $plen, $data ); | ||
| 181 | |||
| 182 | { | ||
| 183 | CORE::lock $self->{cr_mutex}; | ||
| 184 | MCE::Util::_sock_ready( $self->{c_sock} ) if $is_MSWin32; | ||
| 185 | MCE::Util::_sysread( $self->{c_sock}, $plen, 4 ); | ||
| 186 | |||
| 187 | my $len = unpack('i', $plen); | ||
| 188 | if ( $len < 0 ) { | ||
| 189 | $self->end; | ||
| 190 | return wantarray ? () : undef; | ||
| 191 | } | ||
| 192 | |||
| 193 | MCE::Channel::_read( $self->{c_sock}, $data, $len ); | ||
| 194 | } | ||
| 195 | |||
| 196 | wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1]; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub recv_nb { | ||
| 200 | my ( $self ) = @_; | ||
| 201 | my ( $plen, $data ); | ||
| 202 | |||
| 203 | { | ||
| 204 | CORE::lock $self->{cr_mutex}; | ||
| 205 | MCE::Util::_nonblocking( $self->{c_sock}, 1 ); | ||
| 206 | MCE::Util::_sysread( $self->{c_sock}, $plen, 4 ); | ||
| 207 | MCE::Util::_nonblocking( $self->{c_sock}, 0 ); | ||
| 208 | |||
| 209 | my $len; $len = unpack('i', $plen) if $plen; | ||
| 210 | if ( !$len || $len < 0 ) { | ||
| 211 | $self->end if defined $len && $len < 0; | ||
| 212 | return wantarray ? () : undef; | ||
| 213 | } | ||
| 214 | |||
| 215 | MCE::Channel::_read( $self->{c_sock}, $data, $len ); | ||
| 216 | } | ||
| 217 | |||
| 218 | wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1]; | ||
| 219 | } | ||
| 220 | |||
| 221 | ############################################################################### | ||
| 222 | ## ---------------------------------------------------------------------------- | ||
| 223 | ## Methods for two-way communication; consumers to producer(s). | ||
| 224 | ## | ||
| 225 | ############################################################################### | ||
| 226 | |||
| 227 | sub send2 { | ||
| 228 | my $self = shift; | ||
| 229 | my $data = $freeze->([ @_ ]); | ||
| 230 | |||
| 231 | local $\ = undef if (defined $\); | ||
| 232 | |||
| 233 | { | ||
| 234 | my $c_sock = $self->{c2_sock} || $self->{c_sock}; | ||
| 235 | |||
| 236 | CORE::lock $self->{cw_mutex}; | ||
| 237 | MCE::Util::_sock_ready_w( $c_sock ) if $is_MSWin32; | ||
| 238 | print { $c_sock } pack('i', length $data), $data; | ||
| 239 | } | ||
| 240 | |||
| 241 | return 1; | ||
| 242 | } | ||
| 243 | |||
| 244 | sub recv2 { | ||
| 245 | my ( $self ) = @_; | ||
| 246 | my ( $plen, $data ); | ||
| 247 | |||
| 248 | local $/ = $LF if ( $/ ne $LF ); | ||
| 249 | |||
| 250 | { | ||
| 251 | my $p_sock = $self->{p2_sock} || $self->{p_sock}; | ||
| 252 | my $pr_mutex = $self->{pr_mutex}; | ||
| 253 | |||
| 254 | CORE::lock $pr_mutex if $pr_mutex; | ||
| 255 | MCE::Util::_sock_ready( $p_sock ) if $is_MSWin32; | ||
| 256 | |||
| 257 | ( $pr_mutex || $is_MSWin32 ) | ||
| 258 | ? MCE::Util::_sysread( $p_sock, $plen, 4 ) | ||
| 259 | : read( $p_sock, $plen, 4 ); | ||
| 260 | |||
| 261 | my $len = unpack('i', $plen); | ||
| 262 | |||
| 263 | ( $pr_mutex || $is_MSWin32 ) | ||
| 264 | ? MCE::Channel::_read( $p_sock, $data, $len ) | ||
| 265 | : read( $p_sock, $data, $len ); | ||
| 266 | } | ||
| 267 | |||
| 268 | wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1]; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub recv2_nb { | ||
| 272 | my ( $self ) = @_; | ||
| 273 | my ( $plen, $data ); | ||
| 274 | |||
| 275 | local $/ = $LF if ( $/ ne $LF ); | ||
| 276 | |||
| 277 | { | ||
| 278 | my $p_sock = $self->{p2_sock} || $self->{p_sock}; | ||
| 279 | my $pr_mutex = $self->{pr_mutex}; | ||
| 280 | |||
| 281 | CORE::lock $pr_mutex if $pr_mutex; | ||
| 282 | MCE::Util::_nonblocking( $p_sock, 1 ); | ||
| 283 | |||
| 284 | ( $pr_mutex || $is_MSWin32 ) | ||
| 285 | ? MCE::Util::_sysread( $p_sock, $plen, 4 ) | ||
| 286 | : read( $p_sock, $plen, 4 ); | ||
| 287 | |||
| 288 | MCE::Util::_nonblocking( $p_sock, 0 ); | ||
| 289 | |||
| 290 | my $len; $len = unpack('i', $plen) if $plen; | ||
| 291 | |||
| 292 | return wantarray ? () : undef unless $len; | ||
| 293 | |||
| 294 | ( $pr_mutex || $is_MSWin32 ) | ||
| 295 | ? MCE::Channel::_read( $p_sock, $data, $len ) | ||
| 296 | : read( $p_sock, $data, $len ); | ||
| 297 | } | ||
| 298 | |||
| 299 | wantarray ? @{ $thaw->($data) } : ( $thaw->($data) )->[-1]; | ||
| 300 | } | ||
| 301 | |||
| 302 | 1; | ||
| 303 | |||
| 304 | __END__ |