docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
+63
-4
@@ -20,11 +20,13 @@ use endor_core::{
|
||||
|
||||
use crate::{block::Block, mount::Mount, ui_bridge::desktop_type, window::ManagedWindow};
|
||||
|
||||
// 普通对象的私有任务字典属性名;Block/Mount/Window 则把同一拥有关系保存在宿主 payload。
|
||||
const SCOPE: &str = "\0tessera.tasks";
|
||||
const MAX_TASKS: usize = 1024;
|
||||
const MAX_OWNER_TASKS: usize = 64;
|
||||
const MAX_READY: usize = 64;
|
||||
|
||||
// 调度器只保存弱任务身份,不因排队把任务、回调或 owner 隐式保活。
|
||||
type Index = Arc<Mutex<Vec<WeakObjectRef>>>;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -52,17 +54,22 @@ impl State {
|
||||
#[derive(Debug, Clone)]
|
||||
/// 托管异步对象,保存 owner/key、结果与真正拥有的回调/协程/等待引用。
|
||||
struct Task {
|
||||
// owner 通过 scope 字典强持有 Task,Task 反向弱引用 owner,避免任务把已关闭页面永久保活。
|
||||
owner: WeakObjectRef,
|
||||
key: String,
|
||||
// 单调时钟截止点;ready 与到期是两个独立条件,I/O 完成不受系统日期调整影响。
|
||||
due: Instant,
|
||||
callback: Option<ObjectRef>,
|
||||
state: State,
|
||||
// 定时/协作任务创建即 ready,但仍等 due;I/O 任务等 provider 结果写回后才 ready。
|
||||
ready: bool,
|
||||
text: Option<Arc<str>>,
|
||||
error: Option<Arc<str>>,
|
||||
// 仅任务 payload 持有 I/O 租约;取消/终态清除此边即可撤销后台交付并归还额度。
|
||||
lease: Option<Arc<crate::io_tasks::Lease>>,
|
||||
cooperative: bool,
|
||||
coroutine: Option<ObjectRef>,
|
||||
// 协程 await 的对象是真实托管强边;等待期间由它保活被等待值,不以弱调度索引代替。
|
||||
waiting: Option<ObjectRef>,
|
||||
result: Option<Value>,
|
||||
}
|
||||
@@ -83,6 +90,7 @@ impl ObjectPayload for Task {
|
||||
fn poll_await(&self) -> Result<endor_core::AwaitPoll, VmError> {
|
||||
match self.state {
|
||||
State::Pending | State::Running => Ok(endor_core::AwaitPoll::Pending),
|
||||
// 协作任务优先返回保存的任意 Value,I/O 返回 text,普通定时器默认为 Nil。
|
||||
State::Completed => Ok(endor_core::AwaitPoll::Ready(
|
||||
self.result.clone().unwrap_or_else(|| {
|
||||
self.text
|
||||
@@ -126,6 +134,7 @@ fn object(value: &Value) -> Result<ObjectRef, VmError> {
|
||||
}
|
||||
/// 取得 owner 关联的托管任务作用域,供 key 替换和取消使用。
|
||||
fn scope(store: &ObjectStore, owner: ObjectRef) -> Result<Option<ObjectRef>, VmError> {
|
||||
// scope 的位置依 owner 类别而异;普通对象即使使用私有属性,也要验证该值确为字典。
|
||||
let payload = store.object(owner)?.payload();
|
||||
if let Some(value) = payload.as_any().downcast_ref::<Block>() {
|
||||
return Ok(value.tasks);
|
||||
@@ -153,6 +162,7 @@ fn install_scope(
|
||||
owner: ObjectRef,
|
||||
scope: ObjectRef,
|
||||
) -> Result<(), VmError> {
|
||||
// 宿主 payload 通过 replace_payload 建立受 GC 追踪的强边,普通对象通过 set_attr;均不能绕过写屏障。
|
||||
let payload = cx.store().object(owner)?.payload();
|
||||
if let Some(value) = payload.as_any().downcast_ref::<Block>() {
|
||||
let mut value = value.clone();
|
||||
@@ -178,6 +188,7 @@ fn eligible(store: &ObjectStore, owner: ObjectRef, desktop: ObjectRef) -> Result
|
||||
if !mount.mounted {
|
||||
return Ok(false);
|
||||
}
|
||||
// Mount 仅 mounted 不足以交付,所属 Page 还必须在某个活动窗口页面注册表里,关闭再打开不能恢复旧任务。
|
||||
if let Some(page) = mount.page {
|
||||
// No retained root is needed for this synchronous borrowed lookup.
|
||||
let windows = object(&store.get_attr(desktop, "windows")?)?;
|
||||
@@ -192,6 +203,7 @@ fn eligible(store: &ObjectStore, owner: ObjectRef, desktop: ObjectRef) -> Result
|
||||
}
|
||||
return Ok(false);
|
||||
}
|
||||
// 窗口 owner 要求未 closed;Page owner 要求仍注册,普通非页面业务 owner 没有额外展示资格要求。
|
||||
if let Some(window) = payload.as_any().downcast_ref::<ManagedWindow>() {
|
||||
return Ok(!window.closed);
|
||||
}
|
||||
@@ -218,6 +230,7 @@ fn current(
|
||||
reference: ObjectRef,
|
||||
task: &Task,
|
||||
) -> Result<bool, VmError> {
|
||||
// key 指向的对象必须恰是当前 Task,替换后旧 Task 即使还被外部持有,也不能接收结果或执行回调。
|
||||
Ok(match scope(store, owner)? {
|
||||
Some(scope) => {
|
||||
store.dict_get(scope, &Value::Text(task.key.clone()))? == Some(Value::Object(reference))
|
||||
@@ -233,6 +246,7 @@ pub fn cancel_owner(cx: &mut NativeContext<'_>, owner: ObjectRef) -> Result<(),
|
||||
let Some(scope) = scope(cx.store(), owner)? else {
|
||||
return Ok(());
|
||||
};
|
||||
// 克隆条目后逐个变更字典;取消 pending/running 的执行边,再删除所有 owner/key 拥有边。
|
||||
for (key, value) in cx.store().dict_items(scope)?.to_vec() {
|
||||
let reference = object(&value)?;
|
||||
let mut task = cx.store().payload::<Task>(reference, "Task")?.clone();
|
||||
@@ -276,13 +290,14 @@ impl NativeCallable for After {
|
||||
fn signature(&self) -> Option<endor_core::CallSignature> {
|
||||
Some(self.signature.clone())
|
||||
}
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// 直接使用创建时准备的 after/sleep/readText/fetchText/spawn 签名数量,和具体 API 一致。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(self.signature.arity)
|
||||
}
|
||||
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 先拒绝退出或只读域的新工作,再解码 owner/key/提供方参数,取消同键旧任务并建立受作用域管理的新 Task。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
// 退出域先拒绝新任务;只读渲染也禁止异步提交,即使某构造操作只分配新的任务对象。
|
||||
if self.quiescing.load(Ordering::Acquire) {
|
||||
return Err(invalid("runtime is exiting; new tasks are forbidden"));
|
||||
}
|
||||
@@ -291,6 +306,7 @@ impl NativeCallable for After {
|
||||
"asynchronous work is forbidden during read-only rendering",
|
||||
));
|
||||
}
|
||||
// 统一解码不同 API 参数:spawn 用零延时与回调,sleep 无回调,I/O 第三个位置是路径/URL。
|
||||
let zero = Value::Number(0_i64.into());
|
||||
let (owner, key, delay, callback) = match args {
|
||||
[owner, key, callback] if self.operation == Operation::Spawn => {
|
||||
@@ -311,6 +327,7 @@ impl NativeCallable for After {
|
||||
};
|
||||
let owner = object(owner)?;
|
||||
let callback = callback.map(object).transpose()?;
|
||||
// 所有回调都按零显式参数执行,必须是 Function 且参数元数据确认接受零参,不能等执行时再碰运气。
|
||||
if let Some(callback) = callback {
|
||||
if !cx.store().object(callback)?.payload().is_function() {
|
||||
return Err(invalid("task callback must be a Function"));
|
||||
@@ -327,6 +344,7 @@ impl NativeCallable for After {
|
||||
let Value::Text(key) = key else {
|
||||
return Err(invalid("task key must be text"));
|
||||
};
|
||||
// key 是 owner 内替换/取消身份,限制 UTF-8 字节长度与控制字符,禁止空 key。
|
||||
if key.is_empty() || key.len() > 128 || key.chars().any(char::is_control) {
|
||||
return Err(invalid("invalid task key"));
|
||||
}
|
||||
@@ -359,6 +377,7 @@ impl NativeCallable for After {
|
||||
"task delay must be an integer number of milliseconds",
|
||||
));
|
||||
};
|
||||
// 定时延迟要求整数毫秒,范围为零到一天;spawn 复用零延迟路径,禁止小数/负数。
|
||||
let milliseconds = delay
|
||||
.as_i64()
|
||||
.filter(|delay| (0..=86_400_000).contains(delay))
|
||||
@@ -370,6 +389,7 @@ impl NativeCallable for After {
|
||||
if !eligible(cx.store(), owner, self.desktop)? {
|
||||
return Err(invalid("task owner is closed or detached"));
|
||||
}
|
||||
// 在分配新任务前找同 key 旧身份,额度检查允许替换但不允许借其他 key 继续增加总量。
|
||||
let existing_scope = scope(cx.store(), owner)?;
|
||||
let previous = existing_scope
|
||||
.map(|scope| cx.store().dict_get(scope, &Value::Text(key.clone())))
|
||||
@@ -381,6 +401,7 @@ impl NativeCallable for After {
|
||||
.index
|
||||
.lock()
|
||||
.map_err(|_| invalid("task index poisoned"))?;
|
||||
// 提交时压缩弱索引,只保留还存活且 pending/running 的任务;终态不占调度名额。
|
||||
let mut live = Vec::new();
|
||||
for weak in index.iter().copied() {
|
||||
if let Some(task) = cx.upgrade(weak)?
|
||||
@@ -401,6 +422,7 @@ impl NativeCallable for After {
|
||||
{
|
||||
return Err(VmError::ExecutionLimit("64 pending tasks per owner"));
|
||||
}
|
||||
// 先分配并设置截止点/状态,再安装 I/O 租约和 owner 字典边;弱 owner 不承担保活作用。
|
||||
let task = cx.allocate(Task {
|
||||
owner: cx.store().downgrade(owner)?,
|
||||
key: key.clone(),
|
||||
@@ -423,6 +445,7 @@ impl NativeCallable for After {
|
||||
})?;
|
||||
cx.attach_type(task, desktop_type(cx.store(), "Task", self.desktop)?)?;
|
||||
if let Some(request) = request {
|
||||
// 只有旧任务仍待完成且真的持有 I/O 租约才使用 provider 临时替换额度,不给普通任务扩大 I/O 上限。
|
||||
let replacing = previous
|
||||
.map(|previous| {
|
||||
cx.store().payload::<Task>(previous, "Task").map(|task| {
|
||||
@@ -440,6 +463,7 @@ impl NativeCallable for After {
|
||||
cx.replace_payload(task, value)?;
|
||||
}
|
||||
|
||||
// 首次提交才安装字典;以后同 owner 复用 scope,key 替换始终在同一拥有表中发生。
|
||||
let task_scope = if let Some(scope) = existing_scope {
|
||||
scope
|
||||
} else {
|
||||
@@ -450,10 +474,12 @@ impl NativeCallable for After {
|
||||
// Publish the new identity before invalidating the old result. Nothing
|
||||
// external is dispatched until the worker accepts this VM generation.
|
||||
let previous_weak = previous.map(|old| cx.store().downgrade(old)).transpose()?;
|
||||
// 替换字典边前临时升级旧任务,避免 dict_set 删除最后拥有边后,清理旧 payload 时对象已被回收。
|
||||
if let Some(weak) = previous_weak {
|
||||
cx.upgrade(weak)?;
|
||||
}
|
||||
cx.dict_set(task_scope, Value::Text(key.clone()), Value::Object(task))?;
|
||||
// 新 key 身份登记成功后再将旧任务取消,切断回调/协程/等待/结果与 I/O 租约,晚到结果不得复活。
|
||||
if let Some(previous) = previous {
|
||||
// The replacement above can release the old Task's last owner edge.
|
||||
// Preserve it through the replacement even without a saved token.
|
||||
@@ -472,6 +498,7 @@ impl NativeCallable for After {
|
||||
}
|
||||
}
|
||||
let weak = cx.store().downgrade(task)?;
|
||||
// 弱索引也移除被替换身份;spawn 额外设置 runnable 唤醒标志,使外层知道存在可推进协程。
|
||||
live.retain(|old| Some(*old) != previous_weak);
|
||||
live.push(weak);
|
||||
*index = live;
|
||||
@@ -503,13 +530,14 @@ impl NativeCallable for Method {
|
||||
fn signature(&self) -> Option<endor_core::CallSignature> {
|
||||
Some(self.signature.clone())
|
||||
}
|
||||
/// 声明宿主可调用对象接受的参数个数,供 VM 在执行前检查。
|
||||
/// Task 方法无显式参数;底层调用包含唯一一个隐式 Task 接收者。
|
||||
fn arity(&self) -> Option<endor_core::CallArity> {
|
||||
Some(endor_core::CallArity::exact(1))
|
||||
}
|
||||
|
||||
/// VM 调入本宿主能力的入口;按所属操作解码参数并返回托管值或后续调用。
|
||||
/// 验证 Task 接收者并按当前状态处理 status/active/result/error/cancel;仍活跃的任务需重新确认 owner/key 身份。
|
||||
fn invoke(&self, cx: &mut NativeContext<'_>, args: &[Value]) -> Result<NativeResult, VmError> {
|
||||
// 方法无显式参数,实际 args 唯一一项是隐式 Task 接收者。
|
||||
let [value] = args else {
|
||||
return Err(invalid("Task methods accept no arguments"));
|
||||
};
|
||||
@@ -528,6 +556,7 @@ impl NativeCallable for Method {
|
||||
false
|
||||
};
|
||||
let value = match self.query {
|
||||
// pending/running 但已失去 owner/key 资格时,对源报告 cancelled;只读查询不因此改写 payload。
|
||||
Query::Status => Value::Text(
|
||||
if pending && !active {
|
||||
State::Cancelled.name()
|
||||
@@ -537,6 +566,7 @@ impl NativeCallable for Method {
|
||||
.into(),
|
||||
),
|
||||
Query::Active => Value::Bool(active),
|
||||
// I/O 回调执行期间可读取已保存 text;协作任务必须真正 Completed 才能读最终 result,错误任务不返回成功值。
|
||||
Query::Result => {
|
||||
if !matches!(task.state, State::Running | State::Completed)
|
||||
|| task.error.is_some()
|
||||
@@ -570,6 +600,7 @@ impl NativeCallable for Method {
|
||||
if pending {
|
||||
task.state = State::Cancelled;
|
||||
task.callback = None;
|
||||
// 回调开始前释放 I/O 名额,已收到的 text 仍留在任务内,可由回调查询。
|
||||
task.lease = None;
|
||||
task.coroutine = None;
|
||||
task.waiting = None;
|
||||
@@ -583,6 +614,7 @@ impl NativeCallable for Method {
|
||||
cx.dict_delete(scope, &Value::Text(task.key))?;
|
||||
}
|
||||
}
|
||||
// 返回本次是否取消了仍有效的待执行任务;重复 cancel 或已失去资格的任务返回 false。
|
||||
Value::Bool(pending && active)
|
||||
}
|
||||
};
|
||||
@@ -601,6 +633,7 @@ pub struct Scheduler {
|
||||
impl Scheduler {
|
||||
/// 停止接收该域的新任务工作,为关闭/替换运行时做准备。
|
||||
pub fn quiesce(&self, vm: &mut Vm) -> Result<usize> {
|
||||
// 先设置不可逆的退出门闩再取消全部任务,防止清理回调间隙继续接收新工作。
|
||||
self.quiescing.store(true, Ordering::Release);
|
||||
self.cancel_all(vm)
|
||||
}
|
||||
@@ -615,6 +648,7 @@ impl Scheduler {
|
||||
anyhow::ensure!((1..=MAX_READY).contains(&limit), "invalid task poll limit");
|
||||
// Provider threads only fill the mailbox. Only this VM worker updates
|
||||
// managed payloads, and delivery below rechecks owner/key generation.
|
||||
// provider 只产纯数据,VM worker 此处重新核对任务 pending、租约仍在、owner 有效且 key 仍当前。
|
||||
for (weak, completion) in self.provider.take_ready()? {
|
||||
if let Some(reference) = vm.upgrade(weak)? {
|
||||
let result = (|| -> Result<()> {
|
||||
@@ -637,6 +671,7 @@ impl Scheduler {
|
||||
if !active? {
|
||||
return Ok(());
|
||||
}
|
||||
// 收到后台结果只设置 ready/text/error,实际终态与回调交付由下面的有界调度扫描决定。
|
||||
task.ready = true;
|
||||
match completion {
|
||||
Ok(text) => task.text = Some(text.into()),
|
||||
@@ -654,6 +689,7 @@ impl Scheduler {
|
||||
.lock()
|
||||
.map_err(|_| anyhow::anyhow!("task index poisoned"))?
|
||||
.clone();
|
||||
// 本轮使用同一 now 判断到期,避免长回调执行后在同轮无限吸收后来才到期的定时器。
|
||||
let now = Instant::now();
|
||||
let mut work = 0;
|
||||
let mut advanced = Vec::new();
|
||||
@@ -677,10 +713,12 @@ impl Scheduler {
|
||||
} else {
|
||||
false
|
||||
};
|
||||
// owner 回收、页面关闭或 key 被替换都使任务取消;取消也计一次 work,避免清理无限占用一轮。
|
||||
if !active {
|
||||
work += 1;
|
||||
return finish(vm, reference, State::Cancelled);
|
||||
}
|
||||
// 被等待对象未就绪则不消费量子;被取消会取消等待者,失败等其他结果交回协程 await 语义处理。
|
||||
if let Some(waiting) = task.waiting {
|
||||
match vm.object(waiting)?.payload().poll_await() {
|
||||
Ok(endor_core::AwaitPoll::Pending) => return Ok(()),
|
||||
@@ -695,7 +733,9 @@ impl Scheduler {
|
||||
return Ok(());
|
||||
}
|
||||
work += 1;
|
||||
// 记录实际推进的任务,轮末移到索引尾部,保证一回调空闲预算下仍能轮转多个协程。
|
||||
advanced.push(weak);
|
||||
// spawn 使用可恢复协程而非普通 call;其余 I/O/定时器回调作为一次完整有界调用执行。
|
||||
if task.cooperative {
|
||||
return advance_coroutine(vm, reference);
|
||||
}
|
||||
@@ -725,6 +765,7 @@ impl Scheduler {
|
||||
.map_err(|error| crate::endor_host::runtime_error(vm, error));
|
||||
match result {
|
||||
Ok(value) => {
|
||||
// 普通异步回调契约要求 Unit;先释放实际返回值,再把不符合契约的回调标失败并向外报告。
|
||||
let unit = matches!(value, Value::Nil);
|
||||
vm.release_value(value)?;
|
||||
if !unit {
|
||||
@@ -761,6 +802,7 @@ impl Scheduler {
|
||||
index.retain(|weak| !advanced.contains(weak));
|
||||
index.extend(advanced);
|
||||
}
|
||||
// 轮末移除终态/死亡任务并重算 runnable,等待外部 I/O 的任务不应导致忙轮询协程。
|
||||
self.prune(vm)?;
|
||||
Ok(work)
|
||||
}
|
||||
@@ -782,6 +824,7 @@ impl Scheduler {
|
||||
.index
|
||||
.lock()
|
||||
.map_err(|_| anyhow::anyhow!("task index poisoned"))?;
|
||||
// cancel_all 返回清空前索引项数,包含升级失败的弱项,不能等同于实际执行过的回调数。
|
||||
let count = index.len();
|
||||
index.clear();
|
||||
self.runnable.store(false, Ordering::Release);
|
||||
@@ -799,6 +842,7 @@ impl Scheduler {
|
||||
if let Some(task) = vm.upgrade(weak)? {
|
||||
let pending = vm.payload::<Task>(task, "Task").map(|task| {
|
||||
let pending = matches!(task.state, State::Pending | State::Running);
|
||||
// 可运行标记仅针对协作任务:无等待对象可立即推进,有等待对象则看其 poll_await 是否不再 Pending。
|
||||
runnable |= pending && task.cooperative && task.waiting.is_none();
|
||||
pending
|
||||
});
|
||||
@@ -828,6 +872,7 @@ impl Scheduler {
|
||||
fn cyclic_wait(vm: &Vm, source: ObjectRef, mut target: ObjectRef) -> Result<bool> {
|
||||
let mut seen = std::collections::HashSet::new();
|
||||
loop {
|
||||
// 等待链到达 source 或重复已有目标都表示环;不是 Task 或没有 waiting 边时链终止,不判环。
|
||||
if target == source || !seen.insert(target) {
|
||||
return Ok(true);
|
||||
}
|
||||
@@ -844,6 +889,7 @@ fn cyclic_wait(vm: &Vm, source: ObjectRef, mut target: ObjectRef) -> Result<bool
|
||||
/// 恢复已保存的源函数帧,处理完成、软量子挂起或 await 等待,保留已发生的副作用。
|
||||
fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
let mut task = vm.payload::<Task>(reference, "Task")?.clone();
|
||||
// 首次推进把 callback 转成托管 Coroutine,后续复用同一帧与预算,不重放已发生的源副作用。
|
||||
let coroutine = if let Some(coroutine) = task.coroutine {
|
||||
coroutine
|
||||
} else {
|
||||
@@ -868,6 +914,7 @@ fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
}
|
||||
};
|
||||
// Native preparation can itself cancel or replace this Task.
|
||||
// 准备 coroutine 期间原生代码也可能取消/替换任务,必须重读当前状态,不能把旧 task 副本覆盖回去。
|
||||
let current = vm.payload::<Task>(reference, "Task")?.state;
|
||||
if !matches!(current, State::Pending | State::Running) {
|
||||
vm.release(coroutine)?;
|
||||
@@ -876,16 +923,19 @@ fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
task.state = State::Running;
|
||||
task.callback = None;
|
||||
task.coroutine = Some(coroutine);
|
||||
// 先由 payload 建立协程拥有边,再释放创建根;安装失败也尝试释放局部根。
|
||||
let installed = vm.replace_payload(reference, task);
|
||||
vm.release(coroutine)?;
|
||||
installed?;
|
||||
coroutine
|
||||
};
|
||||
let result = vm
|
||||
// 每次最多执行 256 指令软量子,达到量子时仅挂起;总 100000 指令上限仍由协程本身累计。
|
||||
.resume_coroutine(coroutine, 256)
|
||||
.map_err(|error| crate::endor_host::runtime_error(vm, error));
|
||||
// Cancellation/replacement from the running function must win. A completed
|
||||
// value still owns a root and must be released even when delivery is stale.
|
||||
// 源代码运行期间可取消自身,取消/替换优先于本轮完成;完成值即使不交付也必须释放其拥有根。
|
||||
let active = matches!(
|
||||
vm.payload::<Task>(reference, "Task")?.state,
|
||||
State::Pending | State::Running
|
||||
@@ -895,6 +945,7 @@ fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
if !active {
|
||||
return Ok(());
|
||||
}
|
||||
// 登记等待强边前检查依赖环,拒绝永不满足的互相 await,并把当前任务转失败。
|
||||
if cyclic_wait(vm, reference, awaitable)? {
|
||||
fail_callback(vm, reference, "cyclic task await")?;
|
||||
bail!("cyclic task await");
|
||||
@@ -904,6 +955,7 @@ fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
vm.replace_payload(reference, task)?;
|
||||
Ok(())
|
||||
}
|
||||
// 软量子挂起不代表等待外部值,清 waiting 后下一轮仍视作可运行。
|
||||
Ok(endor_core::CoroutineProgress::Pending { .. }) => {
|
||||
if active {
|
||||
let mut task = vm.payload::<Task>(reference, "Task")?.clone();
|
||||
@@ -918,6 +970,7 @@ fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
return Ok(());
|
||||
}
|
||||
let mut task = vm.payload::<Task>(reference, "Task")?.clone();
|
||||
// 结果先写入受 trace 管理的 payload,再释放 VM 返回值根,最后 finish 清执行边但保留成功结果。
|
||||
task.result = Some(value.clone());
|
||||
task.waiting = None;
|
||||
let stored = vm.replace_payload(reference, task);
|
||||
@@ -937,6 +990,7 @@ fn advance_coroutine(vm: &mut Vm, reference: ObjectRef) -> Result<()> {
|
||||
/// 记录回调执行失败并沿运行时故障规则停止后续不安全的交付。
|
||||
fn fail_callback(vm: &mut Vm, reference: ObjectRef, message: &str) -> Result<()> {
|
||||
let mut task = vm.payload::<Task>(reference, "Task")?.clone();
|
||||
// 错误预览按字符限长,保证 UTF-8 完整;记录后统一走终态清理而非保留失败协程。
|
||||
task.error = Some(message.chars().take(1024).collect::<String>().into());
|
||||
vm.replace_payload(reference, task)?;
|
||||
finish(vm, reference, State::Failed)
|
||||
@@ -948,6 +1002,7 @@ fn finish(vm: &mut Vm, reference: ObjectRef, state: State) -> Result<()> {
|
||||
if !matches!(task.state, State::Pending | State::Running) {
|
||||
return Ok(());
|
||||
}
|
||||
// finish 只允许从 pending/running 转终态,重复调用不改变既有 Completed/Cancelled/Failed 的语义。
|
||||
task.state = state;
|
||||
task.callback = None;
|
||||
task.lease = None;
|
||||
@@ -960,6 +1015,7 @@ fn finish(vm: &mut Vm, reference: ObjectRef, state: State) -> Result<()> {
|
||||
vm.replace_payload(reference, task.clone())?;
|
||||
if let Some(owner) = vm.upgrade(task.owner)? {
|
||||
let result = (|| -> Result<()> {
|
||||
// 只有 key 仍指向本任务才删 owner 边,避免旧任务结束时误删同 key 新任务。
|
||||
if current(vm.object_store(), owner, reference, &task)?
|
||||
&& let Some(scope) = scope(vm.object_store(), owner)?
|
||||
{
|
||||
@@ -980,6 +1036,7 @@ pub fn install(
|
||||
ui: ObjectRef,
|
||||
desktop: ObjectRef,
|
||||
) -> Result<Scheduler> {
|
||||
// 每个运行时一份弱索引、可运行/退出标记和 provider mailbox,UI API 与 Scheduler 共享这些原生状态。
|
||||
let index = Arc::new(Mutex::new(Vec::new()));
|
||||
let runnable = Arc::new(AtomicBool::new(false));
|
||||
let quiescing = Arc::new(AtomicBool::new(false));
|
||||
@@ -1017,6 +1074,7 @@ pub fn install(
|
||||
endor_core::CallEffect::Mutating
|
||||
},
|
||||
);
|
||||
// I/O 可带可选回调;spawn 去掉延时参数,sleep 去掉回调参数,保留各自正确 arity。
|
||||
if external {
|
||||
signature.arity = endor_core::CallArity::range(3, 4);
|
||||
}
|
||||
@@ -1040,6 +1098,7 @@ pub fn install(
|
||||
roots.push(function);
|
||||
vm.set_attr(ui, name, Value::Object(function))?;
|
||||
}
|
||||
// Task 类型安装 cancel/status/active/result/error:只有 cancel 声明 Mutating,其余只读查询。
|
||||
for (name, query) in [
|
||||
("cancel", Query::Cancel),
|
||||
("status", Query::Status),
|
||||
|
||||
Reference in New Issue
Block a user