Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 266 additions & 0 deletions crates/moonbit/src/async/async_abi.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
// #region subtask

///|
priv struct SubTask {
handle : Int
state : SubTaskState
}

///|
priv enum SubTaskState {
Starting = 0
Started = 1
Returned = 2
Cancelled_before_started = 3
Cancelled_before_returned = 4
}

///|
fn SubTaskState::from(int : Int) -> SubTaskState {
match int {
0 => Starting
1 => Started
2 => Returned
3 => Cancelled_before_started
4 => Cancelled_before_returned
_ => panic()
}
}

///|
fn SubTask::from(code : Int) -> SubTask {
{ handle: code >> 4, state: SubTaskState::from(code & 0xf) }
}

///|
/// None : the subtask is blocked
fn SubTask::cancel(self : SubTask) -> SubTaskState? {
let result = subtask_cancel(self.handle)
if result == -1 {
None
} else {
Some(SubTaskState::from(subtask_cancel(self.handle)))
}
}

// #endregion

// #region events

///|
priv enum EventCode {
None = 0
SubTask = 1
StreamRead = 2
StreamWrite = 3
FutureRead = 4
FutureWrite = 5
TaskCancelled = 6
}

///|
fn EventCode::from(int : Int) -> EventCode {
match int {
0 => EventCode::None
1 => EventCode::SubTask
2 => EventCode::StreamRead
3 => EventCode::StreamWrite
4 => EventCode::FutureRead
5 => EventCode::FutureWrite
6 => EventCode::TaskCancelled
_ => panic()
}
}

///|
priv enum Events {
None
Subtask(Int, SubTaskState)
StreamRead(Int, StreamResult)
StreamWrite(Int, StreamResult)
FutureRead(Int, FutureReadResult)
FutureWrite(Int, FutureWriteResult)
TaskCancelled
}

///|
fn Events::new(code : EventCode, i : Int, j : Int) -> Events {
match code {
None => None
SubTask => Subtask(i, SubTaskState::from(j))
StreamRead => StreamRead(i, StreamResult::from(j))
StreamWrite => StreamWrite(i, StreamResult::from(j))
FutureRead => FutureRead(i, FutureReadResult::from(j))
FutureWrite => FutureWrite(i, FutureWriteResult::from(j))
TaskCancelled => TaskCancelled
}
}

// #endregion

// #region waitable set

///|
struct WaitableSet(Int) derive(Eq, Show, Hash)

///|
fn WaitableSet::new() -> WaitableSet {
WaitableSet(waitable_set_new())
}

///|
fn WaitableSet::drop(self : Self) -> Unit {
waitable_set_drop(self.0)
}

// #endregion

// #region Future

///|
priv enum FutureReadResult {
Completed = 0
Cancelled = 1
}

///|
fn FutureReadResult::from(int : Int) -> FutureReadResult {
match int {
0 => Completed
1 => Cancelled
_ => panic()
}
}

///|
priv enum FutureWriteResult {
Completed = 0
Dropped = 1
Cancelled = 2
}

///|
fn FutureWriteResult::from(int : Int) -> FutureWriteResult {
match int {
0 => Completed
1 => Dropped
2 => Cancelled
_ => panic()
}
}

// #endregion

// #region Stream

///|
priv struct StreamResult {
progress : Int
copy_result : CopyResult
}

///|
fn StreamResult::from(int : Int) -> StreamResult {
let progress = int >> 4
let copy_result = CopyResult::from(int & 0xf)
{ progress, copy_result }
}

///|
priv enum CopyResult {
Completed = 0
Dropped = 1
Cancelled = 2
}

///|
fn CopyResult::from(int : Int) -> CopyResult {
match int {
0 => Completed
1 => Dropped
2 => Cancelled
_ => panic()
}
}

// #endregion

// #region callback code

///|
/// Code to let the runtime know what to do in the callback
priv enum CallbackCode {
Completed
Yield
Wait(WaitableSet)
Poll(WaitableSet)
}

///|
fn CallbackCode::encode(self : Self) -> Int {
match self {
Completed => 0
Yield => 1
Wait(id) => 2 | (id.0 << 4)
Poll(id) => 3 | (id.0 << 4)
}
}

///|
fn CallbackCode::decode(int : Int) -> CallbackCode {
let id = int >> 4
match int & 0xf {
0 => Completed
1 => Yield
2 => Wait(id)
3 => Poll(id)
_ => panic()
}
}

// #endregion

// #region Component async primitives

///|
/// Return whether is cancelled.
/// Use for non-callback implementation.
fn _yield() -> Bool = "$root" "[cancellable][yield]"

///|
fn _backpressure_inc() = "$root" "[backpressure-inc]"

///|
fn _backpressure_dec() = "$root" "[backpressure-dec]"

///|
fn subtask_cancel(id : Int) -> Int = "$root" "[subtask-cancel]"

///|
fn subtask_drop(id : Int) = "$root" "[subtask-drop]"

///|
pub fn context_set(task : Int) = "$root" "[context-set-0]"

///|
pub fn context_get() -> Int = "$root" "[context-get-0]"

///|
fn tls_set(tls : Int) = "$root" "[context-set-0]"

///|
fn tls_get() -> Int = "$root" "[context-get-0]"

///|
pub fn task_cancel() = "[export]$root" "[task-cancel]"

///|
fn waitable_set_new() -> Int = "$root" "[waitable-set-new]"

///|
fn waitable_set_drop(set : Int) = "$root" "[waitable-set-drop]"

///|
fn waitable_join(waitable : Int, set : Int) = "$root" "[waitable-join]"

// #endregion
21 changes: 21 additions & 0 deletions crates/moonbit/src/async/async_primitive.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
async fn[T] async_suspend(
cb : ((T) -> Unit, (Cancelled) -> Unit) -> Unit,
) -> T raise Cancelled = "%async.suspend"

///|
fn run_async(f : async () -> Unit noraise) = "%async.run"
Loading
Loading