Changeset 50ab04


Ignore:
Timestamp:
11/04/09 19:57:38 (3 years ago)
Author:
Tomash Brechko <tomash.brechko@…>
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)
Message:

Make module thread-safe: implement CLONE().

Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • Changes

    r2bdfa3 r50ab04  
    77        Changes since 0.17: 
    88 
    9         ??? 
     9        Make module thread-safe with Perl >= 5.7.2. 
    1010 
    1111 
  • Fast.xs

    r0e6dcf r50ab04  
    613613 
    614614void 
    615 DESTROY(memd) 
     615_destroy(memd) 
    616616        Cache_Memcached_Fast *  memd 
    617617    PROTOTYPE: $ 
  • MANIFEST

    rfa82f9 r50ab04  
    2323t/pod.t 
    2424t/utf8.t 
     25t/threads.t 
    2526lib/Cache/Memcached/Fast.pm 
    2627src/Makefile.PL 
  • lib/Cache/Memcached/Fast.pm

    r2bdfa3 r50ab04  
    558558 
    559559 
     560our %instance; 
     561 
    560562sub new { 
    561563    my Cache::Memcached::Fast $class = shift; 
     
    583585    $conf->{serialize_methods} ||= [ \&Storable::nfreeze, \&Storable::thaw ]; 
    584586 
    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 
     599sub 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 
     613sub DESTROY { 
     614    my ($memd) = @_; 
     615 
     616    return unless $$memd; 
     617 
     618    delete $instance{$$memd}; 
     619 
     620    Cache::Memcached::Fast::_destroy($memd); 
    586621} 
    587622 
     
    13061341 
    13071342 
     1343=head1 Threads 
     1344 
     1345This module is thread-safe when used with Perl >= 5.7.2.  As with 
     1346other Perl data each thread gets its own copy of 
     1347Cache::Memcached::Fast object that is in scope when the thread is 
     1348created.  Such copies share no state, and may be used concurrently. 
     1349For 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 
     1362Here both C<set>s will be executed concurrently, and the value of 
     1363I<key> will be either I<main value> or I<thread value>, depending on 
     1364the timing of operations.  Note that C<$memd> inside C<thread_job> 
     1365internally refers to a different Cache::Memcached::Fast object than 
     1366C<$memd> from the outer scope.  Each object has its own connections to 
     1367servers, its own counter of outstanding replies for L</nowait> mode, 
     1368etc. 
     1369 
     1370New object copy is created with the same constructor arguments, but 
     1371initially is not connected to any server (even when master copy has 
     1372open connections).  No file descriptor is allocated until the command 
     1373is executed through this new object. 
     1374 
     1375You may safely create Cache::Memcached::Fast object from threads other 
     1376than main thread, and/or pass them as parameters to threads::new(). 
     1377However you can't return the object from top-level thread function. 
     1378I.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 
     1390This is a Perl limitation (see L<threads/"BUGS AND LIMITATIONS">). 
     1391 
     1392 
    13081393=head1 BUGS 
    13091394 
Note: See TracChangeset for help on using the changeset viewer.