Skip to content

Commit

Permalink
picoev: renaming, doc (#20567)
Browse files Browse the repository at this point in the history
  • Loading branch information
enghitalo committed Feb 10, 2024
1 parent 2d0ed2c commit ee3cd36
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 65 deletions.
12 changes: 8 additions & 4 deletions vlib/picoev/loop_default.c.v
Expand Up @@ -15,13 +15,14 @@ mut:

type LoopType = SelectLoop

// create_select_loop creates a `SelectLoop` struct with `id`
// create_select_loop creates a new `SelectLoop` struct with the given `id`
pub fn create_select_loop(id int) !&SelectLoop {
return &SelectLoop{
id: id
}
}

// updates the events associated with a file descriptor in the event loop
@[direct_array_access]
fn (mut pv Picoev) update_events(fd int, events int) int {
// check if fd is in range
Expand All @@ -31,8 +32,10 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
return 0
}

// performs a single iteration of the select-based event loop
@[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait int) int {
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
// Initializes sets for read, write, and error events
readfds, writefds, errorfds := C.fd_set{}, C.fd_set{}, C.fd_set{}

// setup
Expand All @@ -42,7 +45,7 @@ fn (mut pv Picoev) poll_once(max_wait int) int {

mut maxfd := 0

// find the maximum socket for `select` and add sockets to the fd_sets
// finds the maximum file descriptor and adds sockets to the sets `fd_sets`.
for target in pv.file_descriptors {
if target.loop_id == pv.loop.id {
if target.events & picoev_read != 0 {
Expand All @@ -62,14 +65,15 @@ fn (mut pv Picoev) poll_once(max_wait int) int {

// select and handle sockets if any
tv := C.timeval{
tv_sec: u64(max_wait)
tv_sec: u64(max_wait_in_sec)
tv_usec: 0
}
r := C.@select(maxfd + 1, &readfds, &writefds, &errorfds, &tv)
if r == -1 {
// timeout
return -1
} else if r > 0 {
// Iterates through file descriptors and calls their callbacks for triggered events
for target in pv.file_descriptors {
if target.loop_id == pv.loop.id {
// vfmt off
Expand Down
4 changes: 2 additions & 2 deletions vlib/picoev/loop_freebsd.c.v
Expand Up @@ -156,9 +156,9 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
}

@[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait int) int {
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
ts := C.timespec{
tv_sec: max_wait
tv_sec: max_wait_in_sec
tv_nsec: 0
}

Expand Down
4 changes: 2 additions & 2 deletions vlib/picoev/loop_linux.c.v
Expand Up @@ -101,8 +101,8 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
}

@[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait int) int {
nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events, max_fds, max_wait * 1000)
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events, max_fds, max_wait_in_sec * 1000)

if nevents == -1 {
// timeout has occurred
Expand Down
4 changes: 2 additions & 2 deletions vlib/picoev/loop_macos.c.v
Expand Up @@ -156,9 +156,9 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
}

@[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait int) int {
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
ts := C.timespec{
tv_sec: max_wait
tv_sec: max_wait_in_sec
tv_nsec: 0
}

Expand Down

0 comments on commit ee3cd36

Please sign in to comment.