Concurrency, Actors & Tasks
Relevant source files
The following files were used as context for generating this wiki page:
Pop Lang provides a multi-layered concurrency model built on the foundations of structured tasks, isolated actors, and distributed clusters. Unlike legacy systems that rely on shared-state threads and locks as a primary interface, Pop Lang favors structured concurrency with explicit ownership, cancellation, and isolation boundaries.
The system is designed to be backend-neutral, ensuring that asynchronous semantics remain consistent whether running on a reference interpreter, a virtual machine, or native code via LLVM. The core primitives—tasks, channels, and actors—are integrated into the language's Standard Library and runtime interface (PLRI).
Sources: README.md:144-152, LIST.md:139-147, architecture/21-cli-tooling-and-code-units.md:111-120
Structured Concurrency and Tasks
Pop Lang implements structured concurrency where units of asynchronous work, called Tasks, are organized into hierarchical groups. This structure ensures that tasks have a defined lifetime and do not outlive their parent scope, preventing common issues like leaked background work or orphaned processes.
Task Groups and Cancellation
Tasks are managed through Task.Group, which provides a scope for spawning child tasks and handling their collective outcome. Cancellation in Pop Lang is explicit and cooperative, utilizing CancelToken and Task.CancelSource to propagate signals through the task hierarchy.
The diagram shows how a Parent Task uses a Task Group to manage Child Tasks, with cancellation signals propagating downward from a Cancel Source.
Sources: architecture/22.6-standard-library-implementation-plan.md:28-32, crates/tools/architecture-tests/src/tests.rs:720-738
Task State and Lifecycle
The runtime manages tasks through several states to ensure safe execution and resource reclamation.
| State | Description |
|---|---|
Spawned | The task has been created but not yet scheduled. |
Running | The task is currently executing on a scheduler. |
Suspended | The task is waiting for an I/O event, a channel message, or a timer. |
Completed | The task has finished execution successfully. |
Failed | The task terminated due to an error or panic. |
Cancelled | The task was stopped by a cancellation signal. |
Sources: architecture/22.6-standard-library-implementation-plan.md:28-32, LIST.md:139-143
Actors and Isolation
Pop Lang actors provide a model for stateful, concurrent entities that interact solely through asynchronous message passing. This "Share nothing" architecture eliminates data races by ensuring that actor-local state is never accessed directly by other parts of the system.
Actor Mailboxes and Supervision
Each actor possesses a private, typed mailbox. Communications are handled through Channel primitives, supporting both bounded and unbounded buffers. Actors follow a supervision model where parents can define restart policies and monitor the health of their children.
This sequence illustrates message passing between actors and the supervision flow where a failure triggers a restart policy.
Sources: LIST.md:144-147, architecture/22.6-standard-library-implementation-plan.md:28-32
Key Actor Primitives
- Mailboxes: Private queues for incoming typed messages.
- Monitors: Mechanisms to observe the lifecycle of another actor.
- Supervision: Hierarchical management of actor failures and restarts.
- Admission Control: Bounded limits on how many messages an actor accepts to prevent resource exhaustion.
Sources: LIST.md:144-147, architecture/19-architecture-conformance-and-regression-policy.md:175-180
Distributed Concurrency (Clusters)
The Actor model extends across network boundaries through the Cluster system. Distributed actors use authenticated endpoints and explicit partial failure handling to manage communication in a distributed environment.
Cluster Management
The Cluster library manages membership, placement of actors across nodes, and health monitoring. Communication between nodes utilizes authenticated and encrypted channels to ensure secure data transfer.
| Component | Responsibility |
|---|---|
Cluster | Manages node membership and actor placement. |
Net | Provides the underlying transport for cluster communication. |
Crypto | Handles authentication and encryption of node-to-node links. |
Identity | Manages the verified identities of distributed actors. |
Sources: LIST.md:148-150, architecture/22.6-standard-library-implementation-plan.md:32-34
Implementation Constraints and Invariants
To ensure the safety and predictability of the concurrency system, several architectural invariants are enforced by the compiler and runtime.
- Structured Cleanup: All tasks and actors must support LIFO cleanup and explicit resource disposal. Sources: architecture/19-architecture-conformance-and-regression-policy.md:158-160
- Isolation Proofs: The compiler must verify that actor-local state is isolated and that shared immutable data is truly immutable. Sources: architecture/19-architecture-conformance-and-regression-policy.md:195-200
- Deterministic Scheduling: The task scheduler must provide deterministic behavior for tests, allowing for reproducible execution of concurrent code. Sources: architecture/22.6-standard-library-implementation-plan.md:28-32
- No Hidden State: Global mutable state and implicit task ownership are rejected to maintain clear visibility of concurrent dependencies. Sources: architecture/21-cli-tooling-and-code-units.md:120-130
Concurrency Prelude Types
The following types are reserved in the Pop.Standard prelude to support the concurrency model:
Task<T>: A handle to an asynchronous operation returningT.Task.Group: A scope for managing a collection of related tasks.CancelToken: A token used to check for cancellation signals.Iterable<T>andIterator<T>: Core protocols for sequence-based iteration, often utilized in streaming data between concurrent tasks.
Sources: crates/tools/architecture-tests/src/tests.rs:720-738, LIST.md:13-18
Summary
Concurrency in Pop Lang is centered on Structured Tasks for local asynchronous work and Actors for stateful, isolated entities. By combining these with a robust Cluster system for distribution, Pop Lang provides a scalable and safe environment for modern software development. The model prioritizes explicit control over task lifetimes, cooperative cancellation, and strict isolation to eliminate the data races common in traditional multi-threaded programming.