The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Fsdb::Support::Freds - an abstraction over fork and/or ithreads

SYNOPSIS

    use Fsdb::Support::Freds;
    my $fred = new Fsdb::Support::Freds('new thread to do foo');
    # or
    my $fred = new Fsdb::Support::Freds('demo_fred', 
        sub { child_stuff(); exit 0; },
        sub { say "child is done\n"; } );
    $fred->join();
    # or 
    $fred->detach();

This package provides an abstraction over fork that is something like Perl's ithreads. Our goal is to abstract process creation and collection, but none of the shared data like ithreads.

(Why "Freds"? Because it's fork-based thread-like things, and "Tasks" seems too generic.)

new

    $fsdb = new Fsdb::Support::Freds($description, $child_sub, $ending_sub);

For a process, labeling it with optional $DESCRIPTION then running optional $CHILD_SUB in the subprocess, then running optional $ENDING_SUB in the parent process when it exits.

$ENDING_SUB is passed three arguments, the fred, the shell exit code (typically 0 for success or non-zero for failure), and the wait return code (the shell exit code shifted, plus signal number).

It is the job of the $CHILD_SUB to exit if it wants. Otherwise this function returns to the caller.

is_child

    $fred->is_child();

Are we the child? Returns undef if parent.

info

    $info = $fred->info();

Return a string description of the Fred.

error

    $fred->error();

Non-zero if in error state.

exit_code

    $fred->exit_code($full);

Exit code of a termintated fred. With $FULL, turn the full version (including errors). Typically "0" means success.

_post_join

    $fred->_post_join();

Internal cleanup after $FRED is terminated.

join

    $fred->join();

Join a fred (wait for the process to finish). Returns -1 on error (Including if not in the parent.)

join_any

    my $fred = Fsdb::Support::Freds::join_any($BLOCK);

Join on some pending fred, without blocking (default) or blocking (if $BLOCK) is set. Returns -1 on error. Returns 0 if something is running but not finished.

Returns the $FRED that ends.

join_all

    my $fred = Fsdb::Support::Freds::join_all();

Reap all pending threads.

END

Detect any non-reaped processes.