Concurrency
Effective multi-tasking via user-space green-threads
Within computing, concurrency is the ability to manage multiple tasks through simultaneous execution or context-switching. Unlike other programming languages, Talos implements this syntactically by removing function coloring and instead baking concurrency where functions are invoked. This allows Talos to keep the underlying ideas of async/await but with newly invisioned syntax.
Under the hood, Talos then utilizes lightweight processes known as green threads. These are long-running processes that receive scheduled tasks, balancing the workload across the available hardware processors. Since the incoming tasks (eg: asynchronous code) are cheap to spawn, this allows Talos to execute concurrent and performant work. This can then be combined with explicit resource management, to allow Talos to effectively synchronize asynchronous operations.
Colorless Functions
All functions within Talos can be made asynchronous without declaring them as such. This is possible through the use of an execution policy. Functions can be called with a sigil keyword, such that:
// Suppose we have some long-running task.
let foo = fn { ... };
// We can execute the task with the "async" policy.
let future = foo::async();
// ^^----- This is the execution policy!
// Alternatively, the following static method can dispatch asynchronous code.
let _ = Future.async(foo);By removing the async keyword from function declaration, and moving this to the call-site allows developers to make any function asynchronous when invoked.
Alternative Models
Eventually, Talos plans to allow custom execution policies for asynchronous scheduling. This would free developers to use any form of concurrency they desire.
Note:
The current proposal is to use an additional execution policy, such as ::spawn, the takes a scheduler context as
the first argument. This could also open up the freedom to allow for additional types (such as explicit tail-calls).
Awaiting Futures
After a future has been spawned, it can then be awaited on synchronously using the Future.await method.
// Again some long-running task.
let bar = fn { ... };
// Futures can then be safely awaited on synchronously.
let result = bar::async().await();When a future is awaited upon, Talos' runtime queues the current thread isolate to be resolved when the future resolves.
With these combined features, Talos has removed function coloring entirely, whilst also exposing syntax for additional execution policies (such as error handling).
Virtual Threading
When the runtime executes a script, it constructs a thread isolate. Internally, these are virtual tasks that execute Talos code and can be scheduled, cancelled and awaited. For now, the only external interaction that the runtime has with these isolates is through the Future[T] object.
Thread Safety
Internally, most mutations of Talos values are made thread-safe. Each object contains a descriptor (or header) that describes the object and has a one-byte mutex. This mutex is locked when objects are internally updated, resulting in seamless thread safety for developers. For memory safety, see the guide on garbage collection within Talos.
However, variable assignment is inherently not thread-safe but can be made so using the standard library utilities for concurrency and explicit resource management.
Note:
The explicit resource management functionality and the standard library utilities for concurrency have not yet been implemented. Please see the roadmap for the current progress on this functionality.