| @@ -48,6 +48,31 @@ pub(crate) enum Wait { |
| 48 | 48 | Io, |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | +/// The tags a receive waits on: one for `receive`, several for |
| 52 | +/// `receive_any`. One is kept inline: a waiting actor holds its tags, and |
| 53 | +/// most waiting actors wait on one subject. |
| 54 | +pub(crate) enum Tags { |
| 55 | + One(i64), |
| 56 | + Many(Vec<i64>), |
| 57 | +} |
| 58 | + |
| 59 | +impl Tags { |
| 60 | + #[cfg(test)] |
| 61 | + fn of(tags: &[i64]) -> Tags { |
| 62 | + match tags { |
| 63 | + [one] => Tags::One(*one), |
| 64 | + many => Tags::Many(many.to_vec()), |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + fn as_slice(&self) -> &[i64] { |
| 69 | + match self { |
| 70 | + Tags::One(tag) => std::slice::from_ref(tag), |
| 71 | + Tags::Many(tags) => tags, |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 51 | 76 | /// How long a receive waits. |
| 52 | 77 | #[derive(Clone, Copy)] |
| 53 | 78 | pub(crate) enum Timeout { |
| @@ -76,6 +101,16 @@ pub(crate) struct Exited { |
| 76 | 101 | pub(crate) wake: bool, |
| 77 | 102 | } |
| 78 | 103 | |
| 104 | +/// An actor's links and monitors, and how it takes exit signals. |
| 105 | +#[derive(Default)] |
| 106 | +pub(crate) struct Ties { |
| 107 | + pub(crate) links: Links, |
| 108 | + /// (watcher, tag): who receives `Down` when this actor exits. |
| 109 | + pub(crate) monitors: Vec<(i64, i64)>, |
| 110 | + pub(crate) trap: bool, |
| 111 | + pub(crate) exits_tag: Option<i64>, |
| 112 | +} |
| 113 | + |
| 79 | 114 | /// The actors an actor is linked to, in the order they were linked (the |
| 80 | 115 | /// order exits propagate in). Membership and removal take constant time: |
| 81 | 116 | /// an actor linked to each of 100,000 short-lived children removed each |
| @@ -90,6 +125,10 @@ pub(crate) struct Links { |
| 90 | 125 | } |
| 91 | 126 | |
| 92 | 127 | impl Links { |
| 128 | + fn is_empty(&self) -> bool { |
| 129 | + self.live.is_empty() |
| 130 | + } |
| 131 | + |
| 93 | 132 | /// Link `pid`, unless it already is. |
| 94 | 133 | pub(crate) fn insert(&mut self, pid: i64) { |
| 95 | 134 | if self.live.insert(pid) { |
| @@ -193,14 +232,11 @@ pub(crate) struct Proc<V, F> { |
| 193 | 232 | pub(crate) wait: Wait, |
| 194 | 233 | /// What a parked operation returns when woken. |
| 195 | 234 | wake: V, |
| 196 | | − pub(crate) links: Links, |
| 197 | | − /// (watcher, tag): who receives `Down` when this actor exits. |
| 198 | | − pub(crate) monitors: Vec<(i64, i64)>, |
| 199 | | − pub(crate) trap: bool, |
| 200 | | − pub(crate) exits_tag: Option<i64>, |
| 235 | + /// Allocated when first used: most actors have none (ADR 0028). |
| 236 | + ties: Option<Box<Ties>>, |
| 201 | 237 | /// While `wait` is `Receive`: the tags waited on, and whether the |
| 202 | 238 | /// answer carries the tag (`receive_any`). |
| 203 | | − waiting_tags: Vec<i64>, |
| 239 | + waiting_tags: Tags, |
| 204 | 240 | waiting_any: bool, |
| 205 | 241 | /// Counts waits, so a timer set for an earlier one is ignored. |
| 206 | 242 | wait_gen: u64, |
| @@ -220,11 +256,14 @@ impl<V: Answer, F> Proc<V, F> { |
| 220 | 256 | mailbox: Mailbox::default(), |
| 221 | 257 | wait: Wait::Ready, |
| 222 | 258 | wake: V::unit(), |
| 223 | | − links, |
| 224 | | − monitors, |
| 225 | | − trap: false, |
| 226 | | − exits_tag: None, |
| 227 | | − waiting_tags: Vec::new(), |
| 259 | + ties: (!links.is_empty() || !monitors.is_empty()).then(|| { |
| 260 | + Box::new(Ties { |
| 261 | + links, |
| 262 | + monitors, |
| 263 | + ..Ties::default() |
| 264 | + }) |
| 265 | + }), |
| 266 | + waiting_tags: Tags::Many(Vec::new()), |
| 228 | 267 | waiting_any: false, |
| 229 | 268 | wait_gen: 0, |
| 230 | 269 | timed_out: false, |
| @@ -242,7 +281,21 @@ impl<V: Answer, F> Proc<V, F> { |
| 242 | 281 | |
| 243 | 282 | /// Whether the actor waits in a receive on `tag`. |
| 244 | 283 | pub(crate) fn waits_for(&self, tag: i64) -> bool { |
| 245 | | − matches!(self.wait, Wait::Receive { .. }) && self.waiting_tags.contains(&tag) |
| 284 | + matches!(self.wait, Wait::Receive { .. }) && self.waiting_tags.as_slice().contains(&tag) |
| 285 | + } |
| 286 | + |
| 287 | + /// Its links, monitors and exit-signal settings, allocated now if they |
| 288 | + /// were not. |
| 289 | + pub(crate) fn ties(&mut self) -> &mut Ties { |
| 290 | + self.ties.get_or_insert_with(Box::default) |
| 291 | + } |
| 292 | + |
| 293 | + /// `pid`'s exit unlinks it; returns whether this actor traps exits. |
| 294 | + pub(crate) fn unlink(&mut self, pid: i64) -> bool { |
| 295 | + self.ties.as_mut().is_some_and(|t| { |
| 296 | + t.links.remove(pid); |
| 297 | + t.trap |
| 298 | + }) |
| 246 | 299 | } |
| 247 | 300 | |
| 248 | 301 | /// A sender that claimed the actor's wait ([`Inbox::claim_waiting`], |
| @@ -266,11 +319,11 @@ impl<V: Answer, F> Proc<V, F> { |
| 266 | 319 | pub(crate) fn receive( |
| 267 | 320 | &mut self, |
| 268 | 321 | inbox: &Inbox<V>, |
| 269 | | − tags: Vec<i64>, |
| 322 | + tags: Tags, |
| 270 | 323 | any: bool, |
| 271 | 324 | timeout: Timeout, |
| 272 | 325 | ) -> Received<V> { |
| 273 | | − if let Some((tag, message)) = take_wanted(&mut self.mailbox, inbox, &tags) { |
| 326 | + if let Some((tag, message)) = take_wanted(&mut self.mailbox, inbox, tags.as_slice()) { |
| 274 | 327 | return Received::Now(V::received(message, any.then_some(tag))); |
| 275 | 328 | } |
| 276 | 329 | let deadline = match timeout { |
| @@ -294,7 +347,9 @@ impl<V: Answer, F> Proc<V, F> { |
| 294 | 347 | // received message comes wrapped (`Answer::received`). |
| 295 | 348 | return Some(handed); |
| 296 | 349 | } |
| 297 | | − if let Some((tag, message)) = take_wanted(&mut self.mailbox, inbox, &self.waiting_tags) { |
| 350 | + if let Some((tag, message)) = |
| 351 | + take_wanted(&mut self.mailbox, inbox, self.waiting_tags.as_slice()) |
| 352 | + { |
| 298 | 353 | self.wait = Wait::Ready; |
| 299 | 354 | return Some(V::received(message, self.waiting_any.then_some(tag))); |
| 300 | 355 | } |
| @@ -376,9 +431,10 @@ impl<V: Answer, F> Proc<V, F> { |
| 376 | 431 | self.dead = true; |
| 377 | 432 | // A receive it waits in is interrupted below, like any other wait. |
| 378 | 433 | inbox.close(); |
| 434 | + let ties = self.ties.take().map(|t| *t).unwrap_or_default(); |
| 379 | 435 | Some(Exited { |
| 380 | | − links: std::mem::take(&mut self.links).into_vec(), |
| 381 | | − monitors: std::mem::take(&mut self.monitors), |
| 436 | + links: ties.links.into_vec(), |
| 437 | + monitors: ties.monitors, |
| 382 | 438 | wake: self.interrupt(), |
| 383 | 439 | }) |
| 384 | 440 | } |
| @@ -546,7 +602,7 @@ mod loom_models { |
| 546 | 602 | /// What is left in the mailbox on `tag` (checked without waiting). |
| 547 | 603 | fn left(&self, tag: i64) -> Val { |
| 548 | 604 | let mut p = self.proc.lock().unwrap(); |
| 549 | | − match p.receive(&self.inbox, vec![tag], false, Timeout::Zero) { |
| 605 | + match p.receive(&self.inbox, Tags::One(tag), false, Timeout::Zero) { |
| 550 | 606 | Received::Now(v) => v, |
| 551 | 607 | Received::Parked(_) => unreachable!("a zero timeout never waits"), |
| 552 | 608 | } |
| @@ -566,7 +622,7 @@ mod loom_models { |
| 566 | 622 | cell.proc |
| 567 | 623 | .lock() |
| 568 | 624 | .unwrap() |
| 569 | | − .receive(&cell.inbox, tags.to_vec(), false, timeout); |
| 625 | + .receive(&cell.inbox, Tags::of(tags), false, timeout); |
| 570 | 626 | let wait = match received { |
| 571 | 627 | Received::Now(v) => return (Some(v), None), |
| 572 | 628 | Received::Parked(wait) => wait, |
| @@ -702,11 +758,11 @@ mod tests { |
| 702 | 758 | assert_eq!(inbox.push(tag, Val::Message(n)), Ok(false)); |
| 703 | 759 | } |
| 704 | 760 | assert!(matches!( |
| 705 | | − p.receive(&inbox, vec![1, 2], true, Timeout::Zero), |
| 761 | + p.receive(&inbox, Tags::of(&[1, 2]), true, Timeout::Zero), |
| 706 | 762 | Received::Now(Val::Received(1, Some(2))) |
| 707 | 763 | )); |
| 708 | 764 | assert!(matches!( |
| 709 | | − p.receive(&inbox, vec![1], false, Timeout::Zero), |
| 765 | + p.receive(&inbox, Tags::of(&[1]), false, Timeout::Zero), |
| 710 | 766 | Received::Now(Val::Received(2, None)) |
| 711 | 767 | )); |
| 712 | 768 | } |
| @@ -714,7 +770,7 @@ mod tests { |
| 714 | 770 | #[test] |
| 715 | 771 | fn a_message_on_another_tag_wakes_a_receive_which_waits_again() { |
| 716 | 772 | let (mut p, inbox) = actor(); |
| 717 | | − parked(p.receive(&inbox, vec![1], false, Timeout::Never)); |
| 773 | + parked(p.receive(&inbox, Tags::of(&[1]), false, Timeout::Never)); |
| 718 | 774 | assert!(!p.stop(&inbox, ()), "it waits"); |
| 719 | 775 | assert_eq!(inbox.push(2, Val::Message(1)), Ok(true)); |
| 720 | 776 | assert!(p.woken_by_message()); |
| @@ -730,14 +786,14 @@ mod tests { |
| 730 | 786 | #[test] |
| 731 | 787 | fn a_timer_for_an_earlier_wait_is_ignored() { |
| 732 | 788 | let (mut p, inbox) = actor(); |
| 733 | | − let first = parked(p.receive(&inbox, vec![1], false, Timeout::At(10))); |
| 789 | + let first = parked(p.receive(&inbox, Tags::of(&[1]), false, Timeout::At(10))); |
| 734 | 790 | assert!(!p.stop(&inbox, ())); |
| 735 | 791 | assert_eq!(inbox.push(1, Val::Message(1)), Ok(true)); |
| 736 | 792 | assert!(p.woken_by_message()); |
| 737 | 793 | assert!(p.run.start().is_some()); |
| 738 | 794 | assert_eq!(p.receive_woken(&inbox), Some(Val::Received(1, None))); |
| 739 | 795 | // Waiting again, with the same deadline. |
| 740 | | − let second = parked(p.receive(&inbox, vec![1], false, Timeout::At(10))); |
| 796 | + let second = parked(p.receive(&inbox, Tags::of(&[1]), false, Timeout::At(10))); |
| 741 | 797 | assert!(!p.stop(&inbox, ())); |
| 742 | 798 | assert!(!p.time_out(first, 10), "the first wait's timer"); |
| 743 | 799 | assert!(p.time_out(second, 10)); |
| @@ -748,7 +804,7 @@ mod tests { |
| 748 | 804 | #[test] |
| 749 | 805 | fn a_timeout_survives_a_wake_up_for_another_tag() { |
| 750 | 806 | let (mut p, inbox) = actor(); |
| 751 | | − let wait = parked(p.receive(&inbox, vec![1], false, Timeout::At(10))); |
| 807 | + let wait = parked(p.receive(&inbox, Tags::of(&[1]), false, Timeout::At(10))); |
| 752 | 808 | assert!(!p.stop(&inbox, ())); |
| 753 | 809 | assert_eq!(inbox.push(2, Val::Message(1)), Ok(true)); |
| 754 | 810 | assert!(p.woken_by_message()); |
| @@ -764,7 +820,7 @@ mod tests { |
| 764 | 820 | fn a_message_does_not_wake_a_sleep() { |
| 765 | 821 | let (mut p, inbox) = actor(); |
| 766 | 822 | // A wait flag left by an earlier receive that a timer ended. |
| 767 | | − parked(p.receive(&inbox, vec![1], false, Timeout::Never)); |
| 823 | + parked(p.receive(&inbox, Tags::of(&[1]), false, Timeout::Never)); |
| 768 | 824 | assert!(!p.stop(&inbox, ())); |
| 769 | 825 | assert!(p.interrupt()); |
| 770 | 826 | assert!(p.run.start().is_some()); |
| @@ -790,7 +846,7 @@ mod tests { |
| 790 | 846 | } |
| 791 | 847 | let (mut p, inbox) = actor(); |
| 792 | 848 | p.exit(&inbox); |
| 793 | | − parked(p.receive(&inbox, vec![1], false, Timeout::Never)); |
| 849 | + parked(p.receive(&inbox, Tags::of(&[1]), false, Timeout::Never)); |
| 794 | 850 | assert!(p.stop(&inbox, ())); |
| 795 | 851 | } |
| 796 | 852 | |