r/Compilers • u/NXDLang • Sep 30 '26
Designing async semantics for a new language. What would you do differently?
I've been spending the last few days implementing async support in NXD, a systems programming language I'm developing; that also has 3 transpilation targets.
Recent work completed:
• `Task[T]` semantic type
• `AWAIT Task[T] -> T`
• `ProcessHandle[T]`
• `AwaitGroup[T]`
• Completion-order result semantics
• Semantic validation and diagnostics for async operations
I'm currently working through the remaining messaging primitives (SEND is next).
One thing I've found interesting while implementing this is that many languages expose similar async syntax, but the underlying semantics differ significantly. After spending time studying other implementations, I found myself focusing less on the keyword await and more on the type relationships and execution model behind it.
For example, in my current design:
`Task[T]`
`AWAIT -> T`
`ProcessHandle[T]`
Represents a spawned process producing T
`AwaitGroup[T]`
Allows waiting on multiple processes
Results are returned in completion order
The completion-order behavior felt natural from an async perspective, but it also raised questions about predictability versus throughput, ordering guarantees, and how much of the runtime model should be exposed to the programmer.
For those who have designed or implemented async systems, compilers, runtimes, or schedulers:
Looking back, what async design decision caused the most trouble later?
Was it task representation, cancellation, ordering guarantees, await semantics, scheduler behavior, channels/messages, error propagation, or something else entirely?
3
u/alphaglosined Sep 30 '26
You have gone about this kinda backwards. But you are not alone, C# did it in this direction also.
The issue they have ended up having is a bad language transformation, that could be substantially improved, except the runtime and library is not designed for it. They've been working to improve this situation for a while now.
It is important to focus on the language transformation, the details of library are kinda unimportant with the help of features like templates and implicit construction.
Here is the design I came up for D, and my language will be adopting a very similar design as well: https://github.com/dlang/DIPs/blob/master/DIPs/1NNN-RAC.md
5
u/FloweyTheFlower420 Sep 30 '26
Don't make stuff like this a language level thing. The actual language itself needs only to provide the primitives, and the rest is the responsibility of a runtime/standard library. This way it's much easier to implement whatever semantics a developer might want.
4
u/phischu Sep 30 '26
There is a recent survey on differences between async/await across languages.
3
u/binarycow Sep 30 '26
Make it so you don't actually need the async/await keyword.
In C#, the task starts as a "hot task", running in the background until you await it. So you can do this:
In that example, the first task starts, then the second task starts, then you wait for the second to finish, then you wait for the first to finish.
Thing is, this is the unusual thing. Usually you start and then await a task before moving on.
So perhaps, invert the logic.
6
u/TechnoEmpress Sep 30 '26
Do NOT separate Async from the rest of the language, JS & Rust doing so was a grave mistake. Do it like Haskell or Koka where Async is just another effect that can be used alongside others. Otherwise you create a chasm between non-async and async functions and libraries.
2
u/SwedishFindecanor Sep 30 '26
I haven't delved much into this, but I think my approach would be based on structured concurrency where a child tasks always completes (or throws) before the task that spawned it. Then exception handling and stacks get easier to reason about.
2
u/mamcx Sep 30 '26
The major thing, IMHO, is what exactly is the control flow to be implemented (see the phischu link).
I means, how will things look on execution? where are the tasks? how can I manipulate(if any) them? can they move?, etc.
So, imagine put all of this into a graph/flow visualization.
And very important!
How I coordinate among tasks?
I don't care about the types but how the hell I will manage all this stuff!.
I think is missing in all of this do the whole analysis before start implementing the solution.
--
P.D: Also look at https://without.boats/blog/the-registers-of-rust/
P.D.2: I fan of https://zguide.zeromq.org/docs/chapter2/ because it illustrates what are the actual patterns your user will wanna do themselves. Sadly, I don't think any language actually help with this
P.D.3: Check the Erlang papers and special the idea of using supervisors. That is very neat!
2
u/MitchAlsup Oct 01 '26
"Looking back, what async design decision caused the most trouble later?"
Not treading asynchronously arriving events just like a "regular OS" treats interrupts. You are going to want {who receives this signal, at what priority, at what privilege, with what data}.
Where: "who receives" may be a list of threads/tasks and the first one to responds gets that event--just like a <small> number of cores can be watching the same interrupt table. This is fundamentally a directed acyclic graph--set up by a trusted piece of code--and simply used throughout the run of the whole application.
Where: "Priority" sorts the waiting events until a thread/task is ready and appropriate to handle said event.
Where: "Privilege" selects if this thread/task is allowed to do somethings most tasks are not allowed to perform {kill, stop, spin-down, spin-up, power, ...}
Where: "data" is likely encoded in the form of an MSI-X messate (32-bits). The message at the event handler is used to access some buffer which contains parameters of what the event handler is supposed to do {and agreed upon as a contract between sender and receiver}.
Then, the programming model need the effect of a Deferred Procedure Call {or softIRQ} so the higher priority work can be minimized and then a clean transition down in priority for cleaning up what does not need high priority as resources become available. Done right this improves interrupt responsiveness.
There are a LOT of reasons that OSs are organized the way they are, gained from decades of not being exactly right (70's), to pretty close (90s), to right (2020s). Consider a file residing on an SSD partition. There may be 5 different layers of the "file management" part of Linux OS that took part in setting up the request. So there are likely to be 5 layers of DPCs to be performed, each one cleaning up the books of that layer by code written for that layer.
You shold basically treat the sending side like a device down the PCIe bus/tree, talking to interrupt tables through the I/O MMU, which logs interrupts in tables and informs cores that interrupts are pending. {{Don't forget the Address-Translation-Service and the Page-Request-Service the I/O MMU performs on behalf of the "device". }}
You shold basically treat the receiving side like an interrupt table attached to a thread/task. Upon arrival at entry point, this receiving thread is going to have to store this thread's state somewhere before processing the message and be re-entrant, recursive, so that one can return to the state this thread was in prior to the envent happening. You can bundle this in the libraty or in the interrupt handling.
1
u/joao_rosac Oct 06 '26
O
AwaitGroupé uma barreira inflexível. Se uma subtarefa falhar, o compilador deve cancelar todas as tarefas irmãs ativas e aguardar a limpeza de cada uma antes de permitir que o erro suba para a função pai. Nunca aborte rotinas arbitrariamente. Interrompa tarefas apenas nos pontos de suspensão (AWAIT). Transmita o sinal de cancelamento implicitamente através da ABI do NXD, mantendo a sintaxe do usuário limpa de tokens manuais. Se uma máquina de estado for destruída antes de sua conclusão natural, o escalonador tem a obrigação estrita de executar todos os blocosdeferou destrutores associados até aquele ponto, liberando locks e memória. Para viabilizar a linguagem em sistemas bare-metal e facilitar a transpilação para C, implemente aTask[T]como uma máquina de estado alocada inteiramente na stack do chamador, evitando o custo de garbage collection ou boxing.