Name

SPVM::IO::Poll - poll system call

Description

IO::Poll class in SPVM has methods for poll system call.

Usage

use IO::Poll;
use Sys::Poll::Constant as POLL;

my $poll = IO::Poll->new;
 
$poll->set_mask($input_fd => POLL->POLLIN);
$poll->set_mask($output_fd => POLL->POLLOUT);

$poll->poll($timeout);

my $events = $poll->events($input_fd);

Details

Porting

This class is a Perl's IO::Poll porting to SPVM.

Fields

pollfd_array

has pollfd_array : Sys::Poll::PollfdArray;

A Sys::Poll::PollfdArray object.

fd_indexes_h

has fd_indexes_h : Hash of Int;

A hash whose keys are file descriptors and whose values are indexes of "pollfd_array".

disabled_fd_indexes

has disabled_fd_indexes : IntList;

A list of indexes of disabled file descriptors of "pollfd_array".

Class Methods

new

static method new : IO::Poll ();

Creates a new IO::Poll object and returns it.

Implementation:

A Sys::Poll::PollfdArray object is created and sets "pollfd_array" field to it.

A Hash object is created and sets "fd_indexes_h" field to it.

An IntList object is created and sets "disabled_fd_indexes" field to it.

Instance Methods

fds

method fds : int[] ();

Returns file descriptors.

Implementation:

Returned file descriptors are non-negative file descriptors in "pollfd_array".

mask

method mask : int ($fd : int);

Returns the current event mask for the file descriptor $fd.

If $fd is not found, returns 0.

Exceptions:

The file descriptor $fd must be greater than or equal to 0. Otherwise, an exception is thrown.

set_mask

method set_mask : void ($fd : int, $event_mask : int);

If the event mask $event_mask is not 0, adds the file descriptor $fd.

If $event_mask is 0, removes the file descriptor $fd.

Implementation:

This method with $event_mask 0 is not remove a file descriptor from "pollfd_array". It sets the file descriptor to -1 that means disabled.

poll system call ignores file descriptors that have a negative value.

This method with $fd and $event_mask non-zero set a disabled file descriptor to $fd if there are disabled file descriptors.

With this impelmenetaion, the computational complexity of addition and removal of this method is O(1).

Exceptions:

The file descriptor $fd must be greater than or equal to 0. Otherwise, an exception is thrown.

remove

method remove : void ($fd : int);

Removes the file descriptor $fd.

This method is the same as the following code using "set_mask" method.

$poll->set_mask($fd, 0);

poll

method poll : int ($timeout : double = -1);

Calls poll system call given the timeout seconds $timeout.

If $timeout is a negative value, the call blocks.

Returns the number of file descriptors which had events happen.

This method calls Sys::Poll#poll method.

Exceptions:

Excetpions thrown by Sys::Poll#poll method could be thrown.

events

method events : int ($fd : int);

Returns the event mask which represents the events that happened on the file descriptor $fd during the last "poll" method call.

See Also

Copyright & License

Copyright (c) 2024 Yuki Kimoto

MIT License