Last active
January 26, 2016 21:26
-
-
Save realdecisions/e46fa7380b610bbf095e to your computer and use it in GitHub Desktop.
Perl implementation of hooks for fork aka AFTER_FORK.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fork::global_hook; | |
use strict; | |
use B; | |
use Devel::Gladiator; | |
use constant { | |
BLESSED => 0x00100000 | |
}; | |
BEGIN { | |
*CORE::GLOBAL::fork = sub() { | |
my $ret = CORE::fork; | |
if(defined($ret) && $ret == 0) { | |
find_and_call(); | |
} | |
return $ret; | |
}; | |
} | |
sub find_and_call { | |
my $arena = Devel::Gladiator::walk_arena; | |
foreach my $sv (@$arena) {#works slowly on the big arena | |
my $bsv = B::svref_2object($sv); | |
if ( $bsv->isa('B::SV') ) { | |
if ( $bsv->FLAGS & BLESSED) { | |
if ( my $ref = $sv->can('AFTER_FORK_OBJ') ) { | |
$ref->($sv); | |
} | |
} | |
elsif ( ref $bsv eq 'B::HV' ) { | |
if ( my $named_hash = $bsv->NAME ) { | |
if ( my $ref = $named_hash->can('AFTER_FORK') ) { | |
$ref->(); | |
} | |
} | |
} | |
} | |
} | |
@$arena = ();#free arena | |
(); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment