Concurrency

Aria provides colorless concurrency with lightweight tasks, channels, and mutexes. There is no async/await coloring — a function that spawns tasks looks identical to one that doesn't.

Spawning Tasks

// Spawn a task
task := spawn fetchData(url)

// Wait for the result
result := task.await()

Channels

Channels provide typed communication between tasks:

// Create a channel
ch := chan[str]()

// Send and receive
spawn fn() {
    ch.send("hello from task")
}()

msg := ch.recv()
println(msg)  // "hello from task"

Mutexes

Protect shared state with mutexes:

mutex := Mutex.new(0)

scope {
    for i in 0..10 {
        spawn fn() {
            mutex.lock()
            // critical section
            mutex.unlock()
        }()
    }
}

Concurrency Bounds

Types that cross task boundaries must implement Send and Share. These are automatically derived for primitive types:

// Send: can be moved to another task
// Share: can be accessed from multiple tasks

fn spawnWorker[T: Send](data: T) {
    spawn processData(data)
}

Colorless

Unlike Rust's async/await or C++'s co_await, Aria's concurrency has no function coloring. A function that spawns tasks looks identical to one that doesn't — no async keyword, no special return types.

Planned Features

The following concurrency features are specified but not yet implemented:

  • Structured scope { } blocks — automatic join of all spawned tasks when the scope exits
  • select expression — wait on multiple channels simultaneously
  • spawn.detach — fire-and-forget background tasks
  • Directional channelschan.Send[T] and chan.Recv[T]
  • Channel iterationfor msg in channel { }
  • Cancellation tokens — cooperative task cancellation
  • Additional primitivesRWMutex, Atomic, WaitGroup, Once, Barrier

Next Steps