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

schedule

static method schedule : Go::Schedule ();

Returns the scheduler.

uv_loop

static method uv_loop : UV::Loop ();

Returns the event loop of the scheduler.

go

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

Creates a goroutine.

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.

make

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

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

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.

new_select

static method new_select : Go::Select ();

Creates a new Go::Select object.

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.

sleep

static method sleep : void ($duration : Go::Duration_1l);

Sleeps for the duration $duration.

This is simply the same as Go::Timer#sleep method.

sleep_sec

static method sleep_sec : void ($seconds : double);

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

This is simply the same as Go::Timer#sleep_sec method.

new_timer

static method new_timer : Go::Timer ($duration : Go::Duration_1l);

Creates a new Go::Timer object given the duration $duration.

This is simply the same as Go::Timer#new_timer method.

new_timer_sec

static method new_timer_sec : Go::Timer ($seconds : double);

Same as "new_timer", but creates a timer given the seconds $seconds.

This is simply the same as Go::Timer#new_timer_sec method.

Modules

Repository

SPVM::Go - Github

See Also

Author

Yuki Kimoto kimoto.yuki@gmail.com

Copyright & License

Copyright (c) 2023 Yuki Kimoto

MIT License