Changeset 50ab04
- Timestamp:
- 11/04/09 19:57:38 (3 years ago)
- Branches:
- master
- Children:
- 0f1abf
- Parents:
- 0e6dcf
- git-author:
- Tomash Brechko <tomash.brechko@…> (11/04/09 19:57:38)
- git-committer:
- Tomash Brechko <tomash.brechko@…> (11/04/09 19:57:38)
- Files:
-
- 1 added
- 4 edited
-
Changes (modified) (1 diff)
-
Fast.xs (modified) (1 diff)
-
MANIFEST (modified) (1 diff)
-
lib/Cache/Memcached/Fast.pm (modified) (3 diffs)
-
t/threads.t (added)
Legend:
- Unmodified
- Added
- Removed
-
Changes
r2bdfa3 r50ab04 7 7 Changes since 0.17: 8 8 9 ???9 Make module thread-safe with Perl >= 5.7.2. 10 10 11 11 -
Fast.xs
r0e6dcf r50ab04 613 613 614 614 void 615 DESTROY(memd)615 _destroy(memd) 616 616 Cache_Memcached_Fast * memd 617 617 PROTOTYPE: $ -
MANIFEST
rfa82f9 r50ab04 23 23 t/pod.t 24 24 t/utf8.t 25 t/threads.t 25 26 lib/Cache/Memcached/Fast.pm 26 27 src/Makefile.PL -
lib/Cache/Memcached/Fast.pm
r2bdfa3 r50ab04 558 558 559 559 560 our %instance; 561 560 562 sub new { 561 563 my Cache::Memcached::Fast $class = shift; … … 583 585 $conf->{serialize_methods} ||= [ \&Storable::nfreeze, \&Storable::thaw ]; 584 586 585 return Cache::Memcached::Fast::_new($class, $conf); 587 my $memd = Cache::Memcached::Fast::_new($class, $conf); 588 589 if (eval "require Scalar::Util") { 590 my $context = [$memd, $conf]; 591 Scalar::Util::weaken($context->[0]); 592 $instance{$$memd} = $context; 593 } 594 595 return $memd; 596 } 597 598 599 sub CLONE { 600 my ($class) = @_; 601 602 my @contexts = values %instance; 603 %instance = (); 604 foreach my $context (@contexts) { 605 my $memd = Cache::Memcached::Fast::_new($class, $context->[1]); 606 ${$context->[0]} = $$memd; 607 $instance{$$memd} = $context; 608 $$memd = 0; 609 } 610 } 611 612 613 sub DESTROY { 614 my ($memd) = @_; 615 616 return unless $$memd; 617 618 delete $instance{$$memd}; 619 620 Cache::Memcached::Fast::_destroy($memd); 586 621 } 587 622 … … 1306 1341 1307 1342 1343 =head1 Threads 1344 1345 This module is thread-safe when used with Perl >= 5.7.2. As with 1346 other Perl data each thread gets its own copy of 1347 Cache::Memcached::Fast object that is in scope when the thread is 1348 created. Such copies share no state, and may be used concurrently. 1349 For example: 1350 1351 use threads; 1352 1353 my $memd = new Cache::Memcached::Fast({...}); 1354 1355 sub thread_job { 1356 $memd->set("key", "thread value"); 1357 } 1358 1359 threads->new(\&thread_job); 1360 $memd->set("key", "main value"); 1361 1362 Here both C<set>s will be executed concurrently, and the value of 1363 I<key> will be either I<main value> or I<thread value>, depending on 1364 the timing of operations. Note that C<$memd> inside C<thread_job> 1365 internally refers to a different Cache::Memcached::Fast object than 1366 C<$memd> from the outer scope. Each object has its own connections to 1367 servers, its own counter of outstanding replies for L</nowait> mode, 1368 etc. 1369 1370 New object copy is created with the same constructor arguments, but 1371 initially is not connected to any server (even when master copy has 1372 open connections). No file descriptor is allocated until the command 1373 is executed through this new object. 1374 1375 You may safely create Cache::Memcached::Fast object from threads other 1376 than main thread, and/or pass them as parameters to threads::new(). 1377 However you can't return the object from top-level thread function. 1378 I.e., the following won't work: 1379 1380 use threads; 1381 1382 sub thread_job { 1383 return new Cache::Memcached::Fast({...}); 1384 } 1385 1386 my $thread = threads->new(\&thread_job); 1387 1388 my $memd = $thread->join; # The object will be destroyed here. 1389 1390 This is a Perl limitation (see L<threads/"BUGS AND LIMITATIONS">). 1391 1392 1308 1393 =head1 BUGS 1309 1394
Note: See TracChangeset
for help on using the changeset viewer.
