picoev: fix documentation (#24811)

This commit is contained in:
Laurent Cheylus 2025-06-29 11:59:14 +02:00 committed by GitHub
parent b909e2eb37
commit 6cbffd53ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 50 additions and 44 deletions

View File

@ -15,14 +15,14 @@ mut:
type LoopType = SelectLoop type LoopType = SelectLoop
// create_select_loop creates a new `SelectLoop` struct with the given `id` // create_select_loop creates a new `SelectLoop` struct with the given `id`.
pub fn create_select_loop(id int) !&SelectLoop { pub fn create_select_loop(id int) !&SelectLoop {
return &SelectLoop{ return &SelectLoop{
id: id id: id
} }
} }
// updates the events associated with a file descriptor in the event loop // updates the events associated with a file descriptor in the event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) update_events(fd int, events int) int { fn (mut pv Picoev) update_events(fd int, events int) int {
// check if fd is in range // check if fd is in range
@ -32,7 +32,7 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
return 0 return 0
} }
// performs a single iteration of the select-based event loop // performs a single iteration of the select-based event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int { fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
// Initializes sets for read, write, and error events // Initializes sets for read, write, and error events

View File

@ -34,7 +34,7 @@ mut:
type LoopType = KqueueLoop type LoopType = KqueueLoop
// create_kqueue_loop creates a new kernel event queue with loop_id=`id` // create_kqueue_loop creates a new kernel event queue with loop_id=`id`.
pub fn create_kqueue_loop(id int) !&KqueueLoop { pub fn create_kqueue_loop(id int) !&KqueueLoop {
mut loop := &KqueueLoop{ mut loop := &KqueueLoop{
id: id id: id
@ -48,7 +48,7 @@ pub fn create_kqueue_loop(id int) !&KqueueLoop {
return loop return loop
} }
// ev_set sets a new `kevent` with file descriptor `index` // ev_set sets a new `kevent` with file descriptor `index`.
@[inline] @[inline]
pub fn (mut pv Picoev) ev_set(index int, operation int, events int) { pub fn (mut pv Picoev) ev_set(index int, operation int, events int) {
mut filter := 0 mut filter := 0
@ -65,26 +65,26 @@ pub fn (mut pv Picoev) ev_set(index int, operation int, events int) {
} }
// backend_build uses the lower 8 bits to store the old events and the higher 8 // backend_build uses the lower 8 bits to store the old events and the higher 8
// bits to store the next file descriptor in `Target.backend` // bits to store the next file descriptor in `Target.backend`.
@[inline] @[inline]
fn backend_build(next_fd int, events u32) int { fn backend_build(next_fd int, events u32) int {
return int((u32(next_fd) << 8) | (events & 0xff)) return int((u32(next_fd) << 8) | (events & 0xff))
} }
// get the lower 8 bits // get the lower 8 bits.
@[inline] @[inline]
fn backend_get_old_events(backend int) int { fn backend_get_old_events(backend int) int {
return backend & 0xff return backend & 0xff
} }
// get the higher 8 bits // get the higher 8 bits.
@[inline] @[inline]
fn backend_get_next_fd(backend int) int { fn backend_get_next_fd(backend int) int {
return backend >> 8 return backend >> 8
} }
// apply pending processes all changes for the file descriptors and updates `loop.changelist` // apply pending processes all changes for the file descriptors and updates `loop.changelist`
// if `aplly_all` is `true` the changes are immediately applied // if `aplly_all` is `true` the changes are immediately applied.
fn (mut pv Picoev) apply_pending_changes(apply_all bool) int { fn (mut pv Picoev) apply_pending_changes(apply_all bool) int {
mut total, mut nevents := 0, 0 mut total, mut nevents := 0, 0

View File

@ -36,8 +36,8 @@ mut:
type LoopType = EpollLoop type LoopType = EpollLoop
// create_epoll_loop creates a new epoll instance for and returns an // create_epoll_loop creates a new epoll instance and returns an
// `EpollLoop` struct with `id` // `EpollLoop` struct with `id`.
pub fn create_epoll_loop(id int) !&EpollLoop { pub fn create_epoll_loop(id int) !&EpollLoop {
mut loop := &EpollLoop{ mut loop := &EpollLoop{
id: id id: id
@ -51,6 +51,7 @@ pub fn create_epoll_loop(id int) !&EpollLoop {
return loop return loop
} }
// updates the events associated with a file descriptor in the event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) update_events(fd int, events int) int { fn (mut pv Picoev) update_events(fd int, events int) int {
// check if fd is in range // check if fd is in range
@ -103,6 +104,7 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
return 0 return 0
} }
// performs a single iteration of the select-based event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int { fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events[0], max_fds, max_wait_in_sec * 1000) nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events[0], max_fds, max_wait_in_sec * 1000)

View File

@ -34,7 +34,7 @@ mut:
type LoopType = KqueueLoop type LoopType = KqueueLoop
// create_kqueue_loop creates a new kernel event queue with loop_id=`id` // create_kqueue_loop creates a new kernel event queue with loop_id=`id`.
pub fn create_kqueue_loop(id int) !&KqueueLoop { pub fn create_kqueue_loop(id int) !&KqueueLoop {
mut loop := &KqueueLoop{ mut loop := &KqueueLoop{
id: id id: id
@ -48,7 +48,7 @@ pub fn create_kqueue_loop(id int) !&KqueueLoop {
return loop return loop
} }
// ev_set sets a new `kevent` with file descriptor `index` // ev_set sets a new `kevent` with file descriptor `index`.
@[inline] @[inline]
pub fn (mut pv Picoev) ev_set(index int, operation int, events int) { pub fn (mut pv Picoev) ev_set(index int, operation int, events int) {
mut filter := 0 mut filter := 0
@ -65,26 +65,26 @@ pub fn (mut pv Picoev) ev_set(index int, operation int, events int) {
} }
// backend_build uses the lower 8 bits to store the old events and the higher 8 // backend_build uses the lower 8 bits to store the old events and the higher 8
// bits to store the next file descriptor in `Target.backend` // bits to store the next file descriptor in `Target.backend`.
@[inline] @[inline]
fn backend_build(next_fd int, events u32) int { fn backend_build(next_fd int, events u32) int {
return int((u32(next_fd) << 8) | (events & 0xff)) return int((u32(next_fd) << 8) | (events & 0xff))
} }
// get the lower 8 bits // get the lower 8 bits.
@[inline] @[inline]
fn backend_get_old_events(backend int) int { fn backend_get_old_events(backend int) int {
return backend & 0xff return backend & 0xff
} }
// get the higher 8 bits // get the higher 8 bits.
@[inline] @[inline]
fn backend_get_next_fd(backend int) int { fn backend_get_next_fd(backend int) int {
return backend >> 8 return backend >> 8
} }
// apply pending processes all changes for the file descriptors and updates `loop.changelist` // apply pending processes all changes for the file descriptors and updates `loop.changelist`
// if `aplly_all` is `true` the changes are immediately applied // if `aplly_all` is `true` the changes are immediately applied.
fn (mut pv Picoev) apply_pending_changes(apply_all bool) int { fn (mut pv Picoev) apply_pending_changes(apply_all bool) int {
mut total, mut nevents := 0, 0 mut total, mut nevents := 0, 0
@ -123,6 +123,7 @@ fn (mut pv Picoev) apply_pending_changes(apply_all bool) int {
return total return total
} }
// updates the events associated with a file descriptor in the event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) update_events(fd int, events int) int { fn (mut pv Picoev) update_events(fd int, events int) int {
// check if fd is in range // check if fd is in range
@ -157,6 +158,7 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
return 0 return 0
} }
// performs a single iteration of the select-based event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int { fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
ts := C.timespec{ ts := C.timespec{

View File

@ -34,7 +34,7 @@ mut:
type LoopType = KqueueLoop type LoopType = KqueueLoop
// create_kqueue_loop creates a new kernel event queue with loop_id=`id` // create_kqueue_loop creates a new kernel event queue with loop_id=`id`.
pub fn create_kqueue_loop(id int) !&KqueueLoop { pub fn create_kqueue_loop(id int) !&KqueueLoop {
mut loop := &KqueueLoop{ mut loop := &KqueueLoop{
id: id id: id
@ -48,7 +48,7 @@ pub fn create_kqueue_loop(id int) !&KqueueLoop {
return loop return loop
} }
// ev_set sets a new `kevent` with file descriptor `index` // ev_set sets a new `kevent` with file descriptor `index`.
@[inline] @[inline]
pub fn (mut pv Picoev) ev_set(index int, operation int, events int) { pub fn (mut pv Picoev) ev_set(index int, operation int, events int) {
mut filter := 0 mut filter := 0
@ -65,7 +65,7 @@ pub fn (mut pv Picoev) ev_set(index int, operation int, events int) {
} }
// backend_build uses the lower 8 bits to store the old events and the higher 8 // backend_build uses the lower 8 bits to store the old events and the higher 8
// bits to store the next file descriptor in `Target.backend` // bits to store the next file descriptor in `Target.backend`.
@[inline] @[inline]
fn backend_build(next_fd int, events u32) int { fn backend_build(next_fd int, events u32) int {
return int((u32(next_fd) << 8) | (events & 0xff)) return int((u32(next_fd) << 8) | (events & 0xff))
@ -84,7 +84,7 @@ fn backend_get_next_fd(backend int) int {
} }
// apply pending processes all changes for the file descriptors and updates `loop.changelist` // apply pending processes all changes for the file descriptors and updates `loop.changelist`
// if `aplly_all` is `true` the changes are immediately applied // if `aplly_all` is `true` the changes are immediately applied.
fn (mut pv Picoev) apply_pending_changes(apply_all bool) int { fn (mut pv Picoev) apply_pending_changes(apply_all bool) int {
mut total, mut nevents := 0, 0 mut total, mut nevents := 0, 0

View File

@ -36,8 +36,8 @@ mut:
type LoopType = EpollLoop type LoopType = EpollLoop
// create_epoll_loop creates a new epoll instance for and returns an // create_epoll_loop creates a new epoll instance and returns an
// `EpollLoop` struct with `id` // `EpollLoop` struct with `id`.
pub fn create_epoll_loop(id int) !&EpollLoop { pub fn create_epoll_loop(id int) !&EpollLoop {
mut loop := &EpollLoop{ mut loop := &EpollLoop{
id: id id: id
@ -51,6 +51,7 @@ pub fn create_epoll_loop(id int) !&EpollLoop {
return loop return loop
} }
// updates the events associated with a file descriptor in the event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) update_events(fd int, events int) int { fn (mut pv Picoev) update_events(fd int, events int) int {
// check if fd is in range // check if fd is in range
@ -103,6 +104,7 @@ fn (mut pv Picoev) update_events(fd int, events int) int {
return 0 return 0
} }
// performs a single iteration of the select-based event loop.
@[direct_array_access] @[direct_array_access]
fn (mut pv Picoev) poll_once(max_wait_in_sec int) int { fn (mut pv Picoev) poll_once(max_wait_in_sec int) int {
nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events[0], max_fds, max_wait_in_sec * 1000) nevents := C.epoll_wait(pv.loop.epoll_fd, &pv.loop.events[0], max_fds, max_wait_in_sec * 1000)

View File

@ -4,29 +4,29 @@ import net
import picohttpparser import picohttpparser
import time import time
// maximum size of the event queue // maximum size of the event queue.
pub const max_queue = 4096 pub const max_queue = 4096
// event for incoming data ready to be read on a socket // event for incoming data ready to be read on a socket.
pub const picoev_read = 1 pub const picoev_read = 1
// event for socket ready for writing // event for socket ready for writing.
pub const picoev_write = 2 pub const picoev_write = 2
// event indicating a timeout has occurred // event indicating a timeout has occurred.
pub const picoev_timeout = 4 pub const picoev_timeout = 4
// flag for adding a file descriptor to the event loop // flag for adding a file descriptor to the event loop.
pub const picoev_add = 0x40000000 pub const picoev_add = 0x40000000
// flag for removing a file descriptor from the event loop // flag for removing a file descriptor from the event loop.
pub const picoev_del = 0x20000000 pub const picoev_del = 0x20000000
// event read/write // event read/write.
pub const picoev_readwrite = 3 pub const picoev_readwrite = 3
// Target is a data representation of everything that needs to be associated with a single // Target is a data representation of everything that needs to be associated with a single
// file descriptor (connection) // file descriptor (connection).
pub struct Target { pub struct Target {
pub mut: pub mut:
fd int // file descriptor fd int // file descriptor
@ -37,7 +37,7 @@ pub mut:
backend int backend int
} }
// Config configures the Picoev instance with server settings and callbacks // Config configures the Picoev instance with server settings and callbacks.
pub struct Config { pub struct Config {
pub: pub:
port int = 8080 port int = 8080
@ -80,7 +80,7 @@ pub:
user_data voidptr = unsafe { nil } user_data voidptr = unsafe { nil }
} }
// init fills the `file_descriptors` array // init fills the `file_descriptors` array.
pub fn (mut pv Picoev) init() { pub fn (mut pv Picoev) init() {
// assert max_fds > 0 // assert max_fds > 0
pv.num_loops = 0 pv.num_loops = 0
@ -89,7 +89,7 @@ pub fn (mut pv Picoev) init() {
} }
} }
// add a file descriptor to the event loop // add a file descriptor to the event loop.
@[direct_array_access] @[direct_array_access]
pub fn (mut pv Picoev) add(fd int, events int, timeout int, callback voidptr) int { pub fn (mut pv Picoev) add(fd int, events int, timeout int, callback voidptr) int {
if pv == unsafe { nil } || fd < 0 || fd >= max_fds { if pv == unsafe { nil } || fd < 0 || fd >= max_fds {
@ -110,7 +110,7 @@ pub fn (mut pv Picoev) add(fd int, events int, timeout int, callback voidptr) in
return 0 return 0
} }
// remove a file descriptor from the event loop // remove a file descriptor from the event loop.
@[direct_array_access] @[direct_array_access]
pub fn (mut pv Picoev) delete(fd int) int { pub fn (mut pv Picoev) delete(fd int) int {
if fd < 0 || fd >= max_fds { if fd < 0 || fd >= max_fds {
@ -146,7 +146,7 @@ fn (mut pv Picoev) loop_once(max_wait_in_sec int) int {
} }
// set_timeout sets the timeout in seconds for a file descriptor. If a timeout occurs // set_timeout sets the timeout in seconds for a file descriptor. If a timeout occurs
// the file descriptors target callback is called with a timeout event // the file descriptors target callback is called with a timeout event.
@[direct_array_access; inline] @[direct_array_access; inline]
fn (mut pv Picoev) set_timeout(fd int, secs int) { fn (mut pv Picoev) set_timeout(fd int, secs int) {
assert fd < max_fds assert fd < max_fds
@ -159,7 +159,7 @@ fn (mut pv Picoev) set_timeout(fd int, secs int) {
// handle_timeout loops over all file descriptors and removes them from the loop // handle_timeout loops over all file descriptors and removes them from the loop
// if they are timed out. Also the file descriptors target callback is called with a // if they are timed out. Also the file descriptors target callback is called with a
// timeout event // timeout event.
@[direct_array_access; inline] @[direct_array_access; inline]
fn (mut pv Picoev) handle_timeout() { fn (mut pv Picoev) handle_timeout() {
mut to_remove := []int{} mut to_remove := []int{}
@ -176,7 +176,7 @@ fn (mut pv Picoev) handle_timeout() {
} }
} }
// accept_callback accepts a new connection from `listen_fd` and adds it to the event loop // accept_callback accepts a new connection from `listen_fd` and adds it to the event loop.
fn accept_callback(listen_fd int, events int, cb_arg voidptr) { fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
mut pv := unsafe { &Picoev(cb_arg) } mut pv := unsafe { &Picoev(cb_arg) }
accepted_fd := accept(listen_fd) accepted_fd := accept(listen_fd)
@ -204,7 +204,7 @@ fn accept_callback(listen_fd int, events int, cb_arg voidptr) {
pv.add(accepted_fd, picoev_read, pv.timeout_secs, raw_callback) pv.add(accepted_fd, picoev_read, pv.timeout_secs, raw_callback)
} }
// close_conn closes the socket `fd` and removes it from the loop // close_conn closes the socket `fd` and removes it from the loop.
@[inline] @[inline]
pub fn (mut pv Picoev) close_conn(fd int) { pub fn (mut pv Picoev) close_conn(fd int) {
if pv.delete(fd) != 0 { if pv.delete(fd) != 0 {
@ -213,7 +213,7 @@ pub fn (mut pv Picoev) close_conn(fd int) {
close_socket(fd) close_socket(fd)
} }
// raw_callback handles raw events (read, write, timeout) for a file descriptor // raw_callback handles raw events (read, write, timeout) for a file descriptor.
@[direct_array_access] @[direct_array_access]
fn raw_callback(fd int, events int, context voidptr) { fn raw_callback(fd int, events int, context voidptr) {
mut pv := unsafe { &Picoev(context) } mut pv := unsafe { &Picoev(context) }
@ -299,7 +299,7 @@ fn default_error_callback(data voidptr, req picohttpparser.Request, mut res pico
res.end() res.end()
} }
// new creates a `Picoev` struct and initializes the main loop // new creates a `Picoev` struct and initializes the main loop.
pub fn new(config Config) !&Picoev { pub fn new(config Config) !&Picoev {
listening_socket_fd := listen(config) or { listening_socket_fd := listen(config) or {
elog('Error during listen: ${err}') elog('Error during listen: ${err}')
@ -340,7 +340,7 @@ pub fn new(config Config) !&Picoev {
return pv return pv
} }
// serve starts the event loop for accepting new connections // serve starts the event loop for accepting new connections.
// See also picoev.new(). // See also picoev.new().
pub fn (mut pv Picoev) serve() { pub fn (mut pv Picoev) serve() {
spawn update_date_string(mut pv) spawn update_date_string(mut pv)
@ -349,7 +349,7 @@ pub fn (mut pv Picoev) serve() {
} }
} }
// update_date updates the date field of the Picoev instance every second for HTTP headers // update_date updates the date field of the Picoev instance every second for HTTP headers.
fn update_date_string(mut pv Picoev) { fn update_date_string(mut pv Picoev) {
for { for {
// get GMT (UTC) time for the HTTP Date header // get GMT (UTC) time for the HTTP Date header

View File

@ -87,7 +87,7 @@ fn fatal_socket_error(fd int) bool {
return true return true
} }
// listen creates a listening tcp socket and returns its file descriptor // listen creates a listening tcp socket and returns its file descriptor.
fn listen(config Config) !int { fn listen(config Config) !int {
// not using the `net` modules sockets, because not all socket options are defined // not using the `net` modules sockets, because not all socket options are defined
fd := C.socket(config.family, net.SocketType.tcp, 0) fd := C.socket(config.family, net.SocketType.tcp, 0)