BSD-Itimer

 view release on metacpan or  search on metacpan

Itimer.pm  view on Meta::CPAN

# File:		Itimer.pm
# Author:	Daniel Hagerty, hag@linnaean.org
# Date:		Sun Jul  4 17:05:49 1999
# Description:	Perl interface to BSD derived {g,s}etitimer functions
#
# Copyright (c) 1999 Daniel Hagerty. All rights reserved. This program
# is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself.
#
# $Id: Itimer.pm,v 1.1 1999/07/06 02:56:10 hag Exp $

package BSD::Itimer;

use strict;
use Carp;

use Exporter;
use DynaLoader;
use AutoLoader;

use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD);

Itimer.pm  view on Meta::CPAN


@ISA = qw(Exporter DynaLoader);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
	     ITIMER_PROF
	     ITIMER_REAL
	     ITIMER_REALPROF
	     ITIMER_VIRTUAL
	     getitimer
	     setitimer
	     );

sub AUTOLOAD {
    # This AUTOLOAD is used to 'autoload' constants from the constant()
    # XS function.  If a constant is not found then control is passed
    # to the AUTOLOAD in AutoLoader.

    my $constname;
    ($constname = $AUTOLOAD) =~ s/.*:://;
    croak "& not defined" if $constname eq 'constant';
    my $val = constant($constname, @_ ? $_[0] : 0);
    if ($! != 0) {
	if ($! =~ /Invalid/) {
	    $AutoLoader::AUTOLOAD = $AUTOLOAD;
	    goto &AutoLoader::AUTOLOAD;
	}
	else {
		croak "Your vendor has not defined BSD::Itimer macro $constname";
	}
    }
    {
	no strict "refs";
	*$AUTOLOAD = sub { $val };
    }
    goto &$AUTOLOAD;
}

bootstrap BSD::Itimer $VERSION;

# Preloaded methods go here.

# Autoload methods go after =cut, and are processed by the autosplit program.

1;
__END__
# Below is the stub of documentation for your module. You better edit it!

=head1 NAME

BSD::Itimer - Perl extension for accessing interval timers

=head1 SYNOPSIS

  use BSD::Itimer;
  my($interval_sec, $interval_usec, $current_sec, $current_usec) =
    getitimer(ITIMER_REAL);
  my($interval_sec, $interval_usec, $current_sec, $current_usec) =
    setitimer(ITIMER_REAL, $interval_sec, $interval_usec,
	      $current_sec, $current_usec));

=head1 DESCRIPTION

This module provides access to the interval timers many operating
systems provide from perl.  Interval timers conceptually have
microsecond resolution (hardware typically limits actual granularity),
with the ability to reschedule the timer on a fixed repeating
interval.  There are usually several timers available with a different
concept of "time".

=head1 OVERVIEW

The interval timer is accessed by two exported functions, getitimer
and setitimer.  Most Unix systems have three interval timers available
for program use.  The current BSD::Itimer implementation knows about
the following timers, where implemented:

B<ITIMER_REAL> - This timer decrements in real time.  A SIGALRM is
delivered when this timer expires.

B<ITIMER_VIRTUAL> - This timer decrements in real time when the
calling process is running.  Delivers SIGVTALRM when it expires.

B<ITIMER_PROF> - This timer runs when the calling process is running,
and when the operating system is operating on behalf of the calling
process.  A SIGPROF is delivered when the timer expires.

B<ITIMER_REALPROF> - This timer is available under Solaris only.
Consult the setitimer(2) manual page for more information.

Interval timers are represented as four item integer lists.  The
first two integers comprise the second and microsecond parts of the
timer's repeat interval.  The second pair represent the second and
microsecond parts of the current timer value.

The getitimer function expects a single argument naming the timer to
fetch.  It returns a four element list, or an empty list on failure.

The setitimer function expects a argument naming the timer to set, and
a four element list representing the interval.  It returns the
previous setting of the timer, or an empty list on failure.  Setting a
timer's repeat interval to 0 will cancel the timer after its next
delivery.  Setting it's current value to 0 will immediately cancel the
timer.

=head1 SEE ALSO

perl(1), setitimer(2)

=head1 AUTHOR

Daniel Hagerty <hag@linnaean.org>

=head1 COPYRIGHT

Copyright (c) 1999 Daniel Hagerty. All rights reserved. This program
is free software; you can redistribute it and/or modify it under the
same terms as Perl itself.

Itimer.xs  view on Meta::CPAN

/*
# File:		Itimer.xs
# Author:	Daniel Hagerty, hag@linnaean.org
# Date:		Sun Jul  4 17:01:08 1999
# Description:	XS interface to BSD derived {g,s}etitimer() functions.
#
# Copyright (c) 1999 Daniel Hagerty. All rights reserved. This program
# is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself.
#
# $Id: Itimer.xs,v 1.2 1999/07/28 02:26:50 hag Exp $
*/

#ifdef __cplusplus
extern "C" {
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef __cplusplus
}
#endif

#include <sys/time.h>

static char *rcs_id = "$Id: Itimer.xs,v 1.2 1999/07/28 02:26:50 hag Exp $";

static int
not_here(s)
char *s;
{
    croak("%s not implemented on this architecture", s);
    return -1;
}

static double

Itimer.xs  view on Meta::CPAN

	break;
    }
    errno = EINVAL;
    return 0;

not_there:
    errno = ENOENT;
    return 0;
}

MODULE = BSD::Itimer		PACKAGE = BSD::Itimer

PROTOTYPES: enable

double
constant(name,arg)
	char *		name
	int		arg


MODULE = BSD::Itimer		PACKAGE = BSD::Itimer	PREFIX=bsd_

void
bsd_getitimer(which)
	int	which
PREINIT:
	struct itimerval it;
	int err;
PPCODE:
	err = getitimer(which, &it);
	if(err < 0) {
	    XSRETURN_EMPTY;
        }
	EXTEND(sp, 4);
	PUSHs(sv_2mortal(newSViv(it.it_interval.tv_sec)));
	PUSHs(sv_2mortal(newSViv(it.it_interval.tv_usec)));
	PUSHs(sv_2mortal(newSViv(it.it_value.tv_sec)));
	PUSHs(sv_2mortal(newSViv(it.it_value.tv_usec)));

void
bsd_setitimer(which, ival_sec, ival_usec, val_sec, val_usec)
	int	which
	int	ival_sec
	int	ival_usec
	int	val_sec
	int	val_usec
PREINIT:
	struct itimerval setiv, getiv;
	int err;
PPCODE:
	setiv.it_interval.tv_sec = ival_sec;
	setiv.it_interval.tv_usec = ival_usec;
	setiv.it_value.tv_sec = val_sec;
	setiv.it_value.tv_usec = val_usec;
	err = setitimer(which, &setiv, &getiv);
	if(err < 0) {
	    XSRETURN_EMPTY;
        }
	EXTEND(sp, 4);
	PUSHs(sv_2mortal(newSViv(getiv.it_interval.tv_sec)));
	PUSHs(sv_2mortal(newSViv(getiv.it_interval.tv_usec)));
	PUSHs(sv_2mortal(newSViv(getiv.it_value.tv_sec)));
	PUSHs(sv_2mortal(newSViv(getiv.it_value.tv_usec)));

MANIFEST  view on Meta::CPAN

Itimer.pm
Itimer.xs
MANIFEST
Makefile.PL
README
test.pl

Makefile.PL  view on Meta::CPAN

# File:		Makefile.PL
# Author:	Daniel Hagerty, hag@linnaean.org
# Date:		Mon Jul  5 18:21:08 1999
# Description:	Makefile for BSD::Itimer
#
# Copyright (c) 1999 Daniel Hagerty. All rights reserved. This program
# is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself.
#
# $Id: Makefile.PL,v 1.2 1999/07/28 02:27:34 hag Exp $

use ExtUtils::MakeMaker;

# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
    'NAME'	=> 'BSD::Itimer',
    'VERSION_FROM' => 'Itimer.pm', # finds $VERSION
    'LIBS'	=> [''],   # e.g., '-lm'
    'DEFINE'	=> '',     # e.g., '-DHAVE_SOMETHING'
    'INC'	=> '',     # e.g., '-I/usr/include/other'

    'dist'    => {
		    COMPRESS => 'gzip -9f',
		    SUFFIX   => '.gz',
		    DIST_DEFAULT => 'all tardist',
		 },
);

README  view on Meta::CPAN

BSD::Itimer allows you to access the interval timer many Unix systems
provide without resorting to non-portable syscalls.

This is version 0.8.  It has a very simple functional interface to the
get/setitimer functions.  Suggestions or code implementing a nicer
interface welcome; I can be reached as hag@linnaean.org .

BSD::Itimer is available from my home page,
http://www.linnaean.org/~hag/ , and your nearest CPAN mirror in
CPAN/authors/id/H/HA/HAG.

Version 0.8 has been tested on:

Linux 2.0.36
NetBSD 1.4
FreeBSD 2.2.6
SunOS 4.1.4
Solaris 2.6

test.pl  view on Meta::CPAN

# File:		test.pl
# Author:	Daniel Hagerty, hag@linnaean.org
# Date:		Mon Jul  5 18:16:25 1999
# Description:	Module test script for BSD::Itimer
#
# Copyright (c) 1999 Daniel Hagerty. All rights reserved. This program
# is free software; you can redistribute it and/or modify it under the
# same terms as Perl itself.
#
# $Id: test.pl,v 1.1 1999/07/06 02:56:11 hag Exp $

# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

test.pl  view on Meta::CPAN


use strict;

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

my $loaded;

BEGIN { $| = 1; print "1..3\n"; }
END {print "not ok 1\n" unless $loaded;}
use BSD::Itimer;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):

##
# Get ITIMER_REAL; should be (0,0,0,0)

my ($interval_sec, $interval_usec, $current_sec, $current_usec) =
    getitimer(ITIMER_REAL);

my @frob = getitimer(ITIMER_REAL);

if(($interval_sec != 0) || ($interval_usec != 0) ||
   ($current_sec != 0) || ($current_usec != 0)) {
    print "not ok 2\n";
} else {
    print "ok 2\n";
}

##
# Set ITIMER_REAL to 10 seconds and see what happens

test.pl  view on Meta::CPAN

my $now;

sub alrm {
    $got_alrm = 1;
    $now = time;
}
$SIG{"ALRM"} = \&alrm;

my $start = time;

setitimer(ITIMER_REAL, 0, 0, 10, 0);

# Tight loop until the alarm happens.  Could call pause if we imported
# it from posix.
until($got_alrm) {
}

my $delta = $now - $start;

# Wish I could make this a more reasonable test, but there are too
# many reasons for us not to get scheduled.



( run in 0.530 second using v1.01-cache-2.11-cpan-49f99fa48dc )