NAME ^

src/pmc/timer.pmc - Timer

SYNOPSIS ^

    new P0, .Timer
    set P0[.PARROT_TIMER_SEC], I_seconds        # whole seconds
    set P0[.PARROT_TIMER_USEC], I_micro seconds # and/or micro seconds
    set P0[.PARROT_TIMER_NSEC], N_seconds_frac  # or fraction in seconds
    set P0[.PARROT_TIMER_REPEAT], I_repeat      # 0 = one shot ...
    set P0[.PARROT_TIMER_HANDLER], P_sub        # set handler sub PMC
    invoke P0                                   # start timer
    set P0[.PARROT_TIMER_RUNNING], 1            # same

    new P0, .Timer, P1                          # set everything

    set I0, P0[.PARROT_TIMER_SEC]               # query current timer status
    set N0, P0[.PARROT_TIMER_NSEC]
    ...
    set P0[.PARROT_TIMER_RUNNING], 0            # turn timer off

DESCRIPTION ^

This is the Timer base class

Running timers are kept in a linked list. Each timer has a tick count, which gets decremented if the system timer expires. If the tick count reaches zero, the timer handler gets invoked via Parrot's event handling code.

The Timer resolution is operating system dependent. It's only guaranteed that the Timer will fire some time after the programmed interval.

The Timer stops after invoking the handler (repeat+1) times. To create a Timer that will run forever, set "repeat" to -1. Turning the Timer off preserves set values; the Timer is not destroyed.

When setting both PARROT_TIMER_SEC and PARROT_TIMER_USEC it must be done in that sequence, whole seconds first. If a timer is constructed with an initializer and it should be run immediately, the PARROT_TIMER_RUNNING value has to be in the last key of the initializer.

Functions ^

static void add_timer(Interp *interp, PMC *pmc)

Adds a new timer event to the event queue.

static void del_timer(Interp *interp, PMC *pmc)

Deletes the timer event.

void init()

Initializes the timer.

void init_pmc(PMC *init)

See docs/pdds/pdd02_vtables.pod for initializers.

void destroy()

Destroys the timer.

INTVAL get_integer_keyed_int(INTVAL key)

Returns the timer info for key.

PMC *get_pmc_keyed_int(INTVAL key)

Returns the PMC associated with key.

FLOATVAL get_number_keyed_int(INTVAL key)

Returns the number associated with key.

void set_integer_keyed_int(INTVAL key, INTVAL value)

Sets the value associated with key to value.

void set_pmc_keyed_int(INTVAL key, PMC *value)

Sets the PMC associated with key to *value.

opcode_t *invoke(void *next)

Adds the timer to the event queue.

void set_number_keyed_int(INTVAL key, FLOATVAL value)

Sets the floating-point value associated with key to value.

HISTORY ^

Initial proposal by leo 2003.07.15

Initial revision 2003.07.17

Switch to events 2004.01.07


parrot