Name

SPVM::Go - Goroutines of The Go Programming Language

Description

Go class in SPVM provide goroutines and channels in Go language.

Usage

use Go;

Go->go(method : void () {
  
  my $ch = Go->make;
  
  Go->go([$ch : ,] method : void () {
    
    $ch->write(1);
  });
  
  my $value = (int)$ch->read;
});

Go->gosched;

Details

Go class provides a runtime environment that implements Goroutines and Channels, mimicking the behavior of the Go programming language.

Unlike standard polling-based implementations that consume CPU cycles while waiting, this module leverages the high-performance event loop provided by libuv.

Efficient Waiting via Event Loop

When a Goroutine performs an operation that requires waiting, such as:

  • Timer-based waiting (e.g., time.sleep)

  • I/O operations (e.g., reading/writing sockets)

  • Channel communication (waiting for data or space)

The scheduler does not use busy-waiting or idle-looping. Instead, it registers the operation with the libuv event loop and suspends the Goroutine.

The event loop puts the process into a low-power wait state, consuming virtually zero CPU resources while waiting for the OS to signal that the event is ready. Once the event occurs (e.g., a timer expires, data arrives, or a channel becomes ready), the event loop notifies the scheduler, which then resumes the Goroutine efficiently.

This architecture ensures high scalability and low CPU usage, allowing for thousands of concurrent Goroutines to coexist within a single SPVM process without significant overhead.

Class Methods

go

static method go : void ($task : Callback);

Creates a goroutine.

make

static method make : Go::Channel ($capacity : int = 0);

Creates a channel(Go::Channel) given the capacity of its buffer $capacity.

new_select

static method new_select : Go::Select ();

Creats a new Go::Select object.

gosched

static method gosched : void ();

Suspends the current goroutine.

The control is transferred to the scheduler.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

gosched_io_read

Deprecated: This method will be removed in the near future.

static method gosched_io_read : void ($fd : int, $timeout_duration : Go::Duration_1l = undef);

Suspends the current goroutine for IO reading given the file descriptor $fd and the value of the timeout nanosecnods $timeout_duration.

The control is transferred to the scheduler.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

$timeout must be greater than 0. Otherwise an exception is thrown.

$timeout must be less than or equal to Fn->INT_MAX. Otherwise an exception is thrown.

If IO timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

gosched_io_read_sec

Deprecated: This method will be removed in the near future.

static method gosched_io_read_sec : void ($fd : int, $timeout_sec : double = 0);

Same as "gosched_io_read"" in ", but the timeout is the seconds $timeout_sec.

gosched_io_write

Deprecated: This method will be removed in the near future.

static method gosched_io_write : void ($fd : int, $timeout_duration : Go::Duration_1l = undef);

Suspends the current goroutine for IO writing given the file descriptor $fd and the timeout $timeout.

The control is transferred to the scheduler.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

$timeout must be greater than 0. Otherwise an exception is thrown.

$timeout must be less than or equal to Fn->INT_MAX. Otherwise an exception is thrown.

If IO timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

gosched_io_write_sec

Deprecated: This method will be removed in the near future.

static method gosched_io_write_sec : void ($fd : int, $timeout_sec : double = 0);

Same as "gosched_io_write"" in ", but the timeout is the seconds $timeout_sec.

gosched_poll_write

static method gosched_poll_write : void ($context : Go::Context, $fd : int);

Suspends the current goroutine for poll writing given the context $context and the file descriptor $fd.

gosched_pipe_read_v2

static method gosched_pipe_read_v2 : int ($context : Go::Context, $fd : int, $string : mutable string, $length : int = -1, $offset : int = 0);

Suspends the current goroutine for pipe reading given the context $context, the file descriptor $fd, the buffer $string, the length $length, and the offset $offset.

Note: gosched_pipe_read will be replaced by the implementation of gosched_pipe_read_v2.

gosched_pipe_write_v2

static method gosched_pipe_write_v2 : int ($context : Go::Context, $fd : int, $string : string, $length : int = -1, $offset : int = 0);

Suspends the current goroutine for pipe writing given the context $context, the file descriptor $fd, the buffer $string, the length $length, and the offset $offset.

Note: gosched_pipe_write will be replaced by the implementation of gosched_pipe_write_v2.

gosched_io_cancel

static method gosched_io_cancel : void ($fd : int);

Cancels suspending the current goroutine for IO read/write given the file descriptor $fd

The control is transferred to the scheduler.

gosched_pipe_cancel

static method gosched_pipe_cancel : void ($fd : int);

Cancels suspending the current goroutine for pipe read/write given the file descriptor $fd

The control is transferred to the scheduler.

sleep

static method sleep : void ($duration : Go::Duration_1l = undef)

Sleeps the nanoseconds $duration.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

$seconds must be less than or equal to Fn->INT_MAX. Otherwise an exception is thrown.

sleep_sec

static method sleep_sec : void ($sec : double = 0);

Same as "sleep", but sleeps the seconds $sec.

new_wait_group

static method new_wait_group : Go::Sync::WaitGroup ($add : int = 0);

Creates a new Go::Sync::WaitGroup object given the initial counter value $add.

new_command

static method new_command : Go::OS::Exec::Cmd ($context : Go::Context, $command : string[]);

Creates a new Go::OS::Exec::Cmd object given the context $context and the command arguments $command.

ENV_DEBUG

static method ENV_DEBUG : int ();

Casts the value of ""SPVM_GO_DEBUG" environment variable to int type and returns it.

Modules

Enviroment Variables

SPVM_GO_DEBUG

Repository

SPVM::Go - Github

See Also

Author

Yuki Kimoto kimoto.yuki@gmail.com

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT Licensepackage SPVM::Go;

our $VERSION = "0.047";

1;

Name

SPVM::Go - Goroutines of The Go Programming Language

Description

Go class in SPVM provide goroutines and channels in Go language.

Usage

use Go;

Go->go(method : void () {
  
  my $ch = Go->make;
  
  Go->go([has ch : Go::Channel = $ch] method : void () {
    
    my $ch = $self->{ch};
    
    $ch->write(1);
  });
  
  my $ok = 0;
  
  my $value = (int)$ch->read(\$ok);
});

Go->gosched;

Details

Go class provides a runtime environment that implements Goroutines and Channels, mimicking the behavior of the Go programming language.

Unlike standard polling-based implementations that consume CPU cycles while waiting, this module leverages the high-performance event loop provided by libuv.

Efficient Waiting via Event Loop

When a Goroutine performs an operation that requires waiting, such as:

  • Timer-based waiting (e.g., time.sleep)

  • I/O operations (e.g., reading/writing sockets)

  • Channel communication (waiting for data or space)

The scheduler does not use busy-waiting or idle-looping. Instead, it registers the operation with the libuv event loop and suspends the Goroutine.

The event loop puts the process into a low-power wait state, consuming virtually zero CPU resources while waiting for the OS to signal that the event is ready. Once the event occurs (e.g., a timer expires, data arrives, or a channel becomes ready), the event loop notifies the scheduler, which then resumes the Goroutine efficiently.

This architecture ensures high scalability and low CPU usage, allowing for thousands of concurrent Goroutines to coexist within a single SPVM process without significant overhead.

Class Methods

go

static method go : void ($task : Callback);

Creates a goroutine.

make

static method make : Go::Channel ($capacity : int = 0);

Creates a channel(Go::Channel) given the capacity of its buffer $capacity.

new_select

static method new_select : Go::Select ();

Creats a new Go::Select object.

gosched

static method gosched : void ();

Suspends the current goroutine.

The control is transferred to the scheduler.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

gosched_io_read

Deprecated: This method will be removed in the near future.

static method gosched_io_read : void ($fd : int, $timeout_duration : Go::Duration_1l = undef);

Suspends the current goroutine for IO reading given the file descriptor $fd and the value of the timeout nanosecnods $timeout_duration.

The control is transferred to the scheduler.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

$timeout must be greater than 0. Otherwise an exception is thrown.

$timeout must be less than or equal to Fn->INT_MAX. Otherwise an exception is thrown.

If IO timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

gosched_io_read_sec

Deprecated: This method will be removed in the near future.

static method gosched_io_read_sec : void ($fd : int, $timeout_sec : double = 0);

Same as "gosched_io_read"" in ", but the timeout is the seconds $timeout_sec.

gosched_io_write

Deprecated: This method will be removed in the near future.

static method gosched_io_write : void ($fd : int, $timeout_duration : Go::Duration_1l = undef);

Suspends the current goroutine for IO writing given the file descriptor $fd and the timeout $timeout.

The control is transferred to the scheduler.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

$timeout must be greater than 0. Otherwise an exception is thrown.

$timeout must be less than or equal to Fn->INT_MAX. Otherwise an exception is thrown.

If IO timeout occurs, an exception is thrown set eval_error_id to the basic type ID of the Go::Error::IOTimeout class.

gosched_io_write_sec

Deprecated: This method will be removed in the near future.

static method gosched_io_write_sec : void ($fd : int, $timeout_sec : double = 0);

Same as "gosched_io_write"" in ", but the timeout is the seconds $timeout_sec.

gosched_poll_write

static method gosched_poll_write : void ($context : Go::Context, $fd : int);

Suspends the current goroutine for poll writing given the context $context and the file descriptor $fd.

gosched_pipe_read_v2

static method gosched_pipe_read_v2 : int ($context : Go::Context, $fd : int, $string : mutable string, $length : int = -1, $offset : int = 0);

Suspends the current goroutine for pipe reading given the context $context, the file descriptor $fd, the buffer $string, the length $length, and the offset $offset.

Note: gosched_pipe_read will be replaced by the implementation of gosched_pipe_read_v2.

gosched_pipe_write_v2

static method gosched_pipe_write_v2 : int ($context : Go::Context, $fd : int, $string : string, $length : int = -1, $offset : int = 0);

Suspends the current goroutine for pipe writing given the context $context, the file descriptor $fd, the buffer $string, the length $length, and the offset $offset.

Note: gosched_pipe_write will be replaced by the implementation of gosched_pipe_write_v2.

gosched_io_cancel

static method gosched_io_cancel : void ($fd : int);

Cancels suspending the current goroutine for IO read/write given the file descriptor $fd

The control is transferred to the scheduler.

gosched_pipe_cancel

static method gosched_pipe_cancel : void ($fd : int);

Cancels suspending the current goroutine for pipe read/write given the file descriptor $fd

The control is transferred to the scheduler.

sleep

static method sleep : void ($duration : Go::Duration_1l = undef)

Sleeps the nanoseconds $duration.

Exceptions:

This method must be called from the main thread. Otherwise an exception is thrown.

$seconds must be less than or equal to Fn->INT_MAX. Otherwise an exception is thrown.

sleep_sec

static method sleep_sec : void ($sec : double = 0);

Same as "sleep", but sleeps the seconds $sec.

new_wait_group

static method new_wait_group : Go::Sync::WaitGroup ($add : int = 0);

Creates a new Go::Sync::WaitGroup object given the initial counter value $add.

new_command

static method new_command : Go::OS::Exec::Cmd ($context : Go::Context, $command : string[]);

Creates a new Go::OS::Exec::Cmd object given the context $context and the command arguments $command.

after

static method after : Go::Channel ($duration : Go::Duration_1l = undef);

Waits for the duration $duration and then sends a value to the returned channel, mimicking time.After in the Go programming language.

after_sec

static method after_sec : Go::Channel ($sec : double = 0);

Same as "after", but waits for the seconds $sec.

bg

static method bg : Go::Context ();

Returns a non-nil, empty Go::Context that is never canceled, has no values, and has no deadline, acting as a shortcut for Go::Context-background>.

todo

static method todo : Go::Context ();

Returns a non-nil, empty Go::Context to be used when it is unclear which context to use or it is yet to be available, acting as a shortcut for Go::Context-todo>.

ENV_DEBUG

static method ENV_DEBUG : int ();

Casts the value of ""SPVM_GO_DEBUG" environment variable to int type and returns it.

Modules

Enviroment Variables

SPVM_GO_DEBUG

Repository

SPVM::Go - Github

See Also

Author

Yuki Kimoto kimoto.yuki@gmail.com

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License