NAME

Mutex::Util - Utility functions for Mutex

VERSION

This document describes Mutex::Util version 1.011

SYNOPSIS

# Mutex::Util functions are beneficial inside a class.

package Foo::Bar;

use strict;
use warnings;

our $VERSION = '0.002';

use Mutex::Util;

my $has_threads = $INC{'threads.pm'} ? 1 : 0;
my $tid = $has_threads ? threads->tid : 0;

sub CLONE {
    $tid = threads->tid if $has_threads;
}

sub new {
    my ($class, %obj) = @_;
    $obj{init_pid} = $has_threads ? "$$.$tid" : $$;

    ($^O eq 'MSWin32')
        ? Mutex::Util::sock_pair(\%obj, qw(_r_sock _w_sock))
        : Mutex::Util::pipe_pair(\%obj, qw(_r_sock _w_sock));

    ...

    return bless \%obj, $class;
}

sub DESTROY {
    my ($pid, $obj) = ($has_threads ? "$$.$tid" : $$, @_);

    if ($obj->{init_pid} eq $pid) {
        ($^O eq 'MSWin32')
            ? Mutex::Util::destroy_socks($obj, qw(_w_sock _r_sock))
            : Mutex::Util::destroy_pipes($obj, qw(_w_sock _r_sock));
    }

    return;
}

1;

DESCRIPTION

Useful functions for managing pipe and socket handles stored in a hashref.

API DOCUMENTATION

destroy_pipes ( hashref, list )

Destroy pipes in the hash for given key names.

Mutex::Util::destroy_pipes($hashref, qw(_w_sock _r_sock));

destroy_socks ( hashref, list )

Destroy sockets in the hash for given key names.

Mutex::Util::destroy_socks($hashref, qw(_w_sock _r_sock));

pipe_pair ( hashref, r_name, w_name [, idx ] )

Creates a pair of connected pipes and stores the handles into the hash with given key names representing the two read-write handles. Optionally, pipes may be constructed into an array stored inside the hash.

Mutex::Util::pipe_pair($hashref, qw(_r_sock _w_sock));

$hashref->{_r_sock};
$hashref->{_w_sock};

Mutex::Util::pipe_pair($hashref, qw(_r_sock _w_sock), $_) for 0..3;

$hashref->{_r_sock}[0];
$hashref->{_w_sock}[0];

sock_pair ( hashref, r_name, w_name [, idx ] )

Creates an unnamed pair of sockets and stores the handles into the hash with given key names representing the two read-write handles. Optionally, sockets may be constructed into an array stored inside the hash.

Mutex::Util::sock_pair($hashref, qw(_r_sock _w_sock));

$hashref->{_r_sock};
$hashref->{_w_sock};

Mutex::Util::sock_pair($hashref, qw(_r_sock _w_sock), $_) for 0..3;

$hashref->{_r_sock}[0];
$hashref->{_w_sock}[0];

AUTHOR

Mario E. Roy, <marioeroy AT gmail DOT com>