| @@ -28,7 +28,7 @@ use crate::heap::{drop_value, tl_alloc}; |
| 28 | 28 | use crate::sched::{Exit, REDUCTIONS, Sched, Wait, actor_of, exit_actor, park, this}; |
| 29 | 29 | |
| 30 | 30 | /// The operations of `std/process.proc`, in declaration order. |
| 31 | | −pub const PROC_OPS: [&str; 11] = [ |
| 31 | +pub const PROC_OPS: [&str; 12] = [ |
| 32 | 32 | "spawn", |
| 33 | 33 | "self", |
| 34 | 34 | "subject", |
| @@ -40,11 +40,12 @@ pub const PROC_OPS: [&str; 11] = [ |
| 40 | 40 | "trap_exits", |
| 41 | 41 | "exits", |
| 42 | 42 | "sleep", |
| 43 | + "receive_any", |
| 43 | 44 | ]; |
| 44 | 45 | |
| 45 | 46 | type Code = unsafe extern "C" fn(*mut Ctx, Value, Value, Value, Value, Value, Value) -> Value; |
| 46 | 47 | |
| 47 | | −const CLAUSES: [Code; 11] = [ |
| 48 | +const CLAUSES: [Code; 12] = [ |
| 48 | 49 | op_spawn, |
| 49 | 50 | op_self, |
| 50 | 51 | op_subject, |
| @@ -56,6 +57,7 @@ const CLAUSES: [Code; 11] = [ |
| 56 | 57 | op_trap_exits, |
| 57 | 58 | op_exits, |
| 58 | 59 | op_sleep, |
| 60 | + op_receive_any, |
| 59 | 61 | ]; |
| 60 | 62 | |
| 61 | 63 | /// A new root capability for `proc` (owned). |
| @@ -151,9 +153,15 @@ fn deliver(sched: &RefCell<Sched>, owner: i64, tag: i64, message: Value) -> Opti |
| 151 | 153 | let Some(a) = actor_of(&s, owner).filter(|a| !a.dead) else { |
| 152 | 154 | return Some(message); |
| 153 | 155 | }; |
| 154 | | − match a.wait { |
| 155 | | − Wait::Receive { tag: t, .. } if t == tag => s.wake(owner, some(message)), |
| 156 | | − _ => a.mailbox.push_back((tag, message)), |
| 156 | + if matches!(a.wait, Wait::Receive { .. }) && a.waiting_tags.contains(&tag) { |
| 157 | + let answer = if a.waiting_any { |
| 158 | + some(data(0, &[int(tag), message])) |
| 159 | + } else { |
| 160 | + some(message) |
| 161 | + }; |
| 162 | + s.wake(owner, answer); |
| 163 | + } else { |
| 164 | + a.mailbox.push_back((tag, message)); |
| 157 | 165 | } |
| 158 | 166 | None |
| 159 | 167 | } |
| @@ -318,8 +326,52 @@ unsafe extern "C" fn op_receive( |
| 318 | 326 | return done(ctx, none()); |
| 319 | 327 | } |
| 320 | 328 | let clock = sched.borrow().clock; |
| 329 | + a.waiting_tags = vec![tag]; |
| 330 | + a.waiting_any = false; |
| 331 | + a.wait = Wait::Receive { |
| 332 | + deadline: (timeout > 0).then(|| clock + timeout as u64), |
| 333 | + }; |
| 334 | + let answer = park(ctx); |
| 335 | + done(ctx, answer) |
| 336 | + } |
| 337 | +} |
| 338 | + |
| 339 | +/// `receive_any(tags, timeout)`: the oldest message on any of the tags, as |
| 340 | +/// `(tag, message)` (the subjects' owners are checked by |
| 341 | +/// `std/process.select`). |
| 342 | +unsafe extern "C" fn op_receive_any( |
| 343 | + ctx: *mut Ctx, |
| 344 | + this_: Value, |
| 345 | + tags: Value, |
| 346 | + timeout: Value, |
| 347 | + _: Value, |
| 348 | + _: Value, |
| 349 | + _: Value, |
| 350 | +) -> Value { |
| 351 | + // SAFETY: as above; `tags` is a `List<Int>`. |
| 352 | + unsafe { |
| 353 | + drop_value(ctx, this_); |
| 354 | + let mut wanted = Vec::new(); |
| 355 | + let mut cell = tags; |
| 356 | + while is_ptr(cell) { |
| 357 | + let f = Block::fields_ptr(cell as *mut Block); |
| 358 | + wanted.push(int_get(*f)); |
| 359 | + cell = *f.add(1); |
| 360 | + } |
| 361 | + drop_value(ctx, tags); |
| 362 | + let (a, sched) = this(ctx); |
| 363 | + if let Some(i) = a.mailbox.iter().position(|(t, _)| wanted.contains(t)) { |
| 364 | + let (tag, message) = a.mailbox.remove(i).expect("found"); |
| 365 | + return done(ctx, some(data(0, &[int(tag), message]))); |
| 366 | + } |
| 367 | + let timeout = int_get(timeout); |
| 368 | + if timeout == 0 { |
| 369 | + return done(ctx, none()); |
| 370 | + } |
| 371 | + let clock = sched.borrow().clock; |
| 372 | + a.waiting_tags = wanted; |
| 373 | + a.waiting_any = true; |
| 321 | 374 | a.wait = Wait::Receive { |
| 322 | | − tag, |
| 323 | 375 | deadline: (timeout > 0).then(|| clock + timeout as u64), |
| 324 | 376 | }; |
| 325 | 377 | let answer = park(ctx); |
| @@ -327,6 +379,14 @@ unsafe extern "C" fn op_receive( |
| 327 | 379 | } |
| 328 | 380 | } |
| 329 | 381 | |
| 382 | +/// `coerce` (std/process): the value as another type. Only |
| 383 | +/// `std/process.select` uses it, on a message whose type its subject |
| 384 | +/// guarantees. |
| 385 | +#[unsafe(no_mangle)] |
| 386 | +pub extern "C" fn tl_coerce(_ctx: *mut Ctx, v: Value) -> Value { |
| 387 | + crate::prims::dup(v) |
| 388 | +} |
| 389 | + |
| 330 | 390 | unsafe extern "C" fn op_link( |
| 331 | 391 | ctx: *mut Ctx, |
| 332 | 392 | this_: Value, |