aside.rs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //! Work for a list that a thread of ours does while the title records on:
  2. //! the pass over the heap's buffer views (`crate::draw`), which took 12 to 17
  3. //! ms of one recording thread at the frame's first list that needs it, with
  4. //! the submission of that list, and of those behind it, waiting.
  5. //! `VICTORY_HEAP_PASS_ASIDE=0`: done where it is needed, as before.
  6. //!
  7. //! What it finds are refreshes of host copies, which are steps of the list
  8. //! for its submission, and what it makes are descriptors, which the card
  9. //! reads when the list runs: both are there when the list is closed
  10. //! (`HostList::close` waits), and the refreshes stand at the place in the
  11. //! list's steps where the pass was begun.
  12. use super::*;
  13. /// A host copy, and the title memory of it (from, to).
  14. pub(crate) type Refresh = (std::sync::Arc<crate::shadow::Shadow>, u64, u64);
  15. /// What is being found, until it is.
  16. pub(crate) struct Coming(Mutex<Option<Vec<Refresh>>>, std::sync::Condvar);
  17. /// Since the last frame line: microseconds lists waited at their close.
  18. pub(crate) static WAITED: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
  19. impl Coming {
  20. /// What was found.
  21. pub(crate) fn wait(&self) -> Vec<Refresh> {
  22. let began = std::time::Instant::now();
  23. let mut found = self.0.lock().unwrap_or_else(|e| e.into_inner());
  24. loop {
  25. if let Some(found) = found.take() {
  26. WAITED.fetch_add(began.elapsed().as_micros() as u64, Ordering::Relaxed);
  27. return found;
  28. }
  29. found = self.1.wait(found).unwrap_or_else(|e| e.into_inner());
  30. }
  31. }
  32. fn found(&self, what: Vec<Refresh>) {
  33. *self.0.lock().unwrap_or_else(|e| e.into_inner()) = Some(what);
  34. self.1.notify_all();
  35. }
  36. }
  37. pub(crate) fn on() -> bool {
  38. static ON: OnceLock<bool> = OnceLock::new();
  39. *ON.get_or_init(|| std::env::var_os("VICTORY_HEAP_PASS_ASIDE").is_none_or(|v| v != "0"))
  40. }
  41. type Job = Box<dyn FnOnce() + Send>;
  42. /// `work` begins on the thread for it, one after another; here and now if
  43. /// there is no such thread.
  44. pub(crate) fn begin(work: impl FnOnce() -> Vec<Refresh> + Send + 'static) -> std::sync::Arc<Coming> {
  45. static THREAD: OnceLock<Option<Mutex<std::sync::mpsc::Sender<Job>>>> = OnceLock::new();
  46. let coming = std::sync::Arc::new(Coming(Mutex::new(None), std::sync::Condvar::new()));
  47. let told = coming.clone();
  48. let job: Job = Box::new(move || {
  49. // (However it ends, the list that waits goes on.)
  50. let found = std::panic::catch_unwind(std::panic::AssertUnwindSafe(work)).unwrap_or_default();
  51. told.found(found);
  52. });
  53. let thread = THREAD.get_or_init(|| {
  54. let (send, jobs) = std::sync::mpsc::channel::<Job>();
  55. std::thread::Builder::new()
  56. .name("victory-aside".into())
  57. .spawn(move || jobs.into_iter().for_each(|job| job()))
  58. .ok()
  59. .map(|_| Mutex::new(send))
  60. });
  61. match thread {
  62. Some(send) => {
  63. if let Err(job) = send.lock().unwrap_or_else(|e| e.into_inner()).send(job) {
  64. (job.0)();
  65. }
  66. }
  67. None => job(),
  68. }
  69. coming
  70. }
  71. #[cfg(test)]
  72. mod tests {
  73. use super::*;
  74. #[test]
  75. fn what_is_found_is_waited_for_and_a_panic_lets_the_list_go_on() {
  76. let (go, wait) = std::sync::mpsc::channel::<()>();
  77. let coming = begin(move || {
  78. let _ = wait.recv();
  79. Vec::new()
  80. });
  81. assert!(coming.0.lock().unwrap().is_none());
  82. go.send(()).unwrap();
  83. assert!(coming.wait().is_empty());
  84. assert!(begin(|| panic!("a panic of the work")).wait().is_empty());
  85. assert!(begin(Vec::new).wait().is_empty());
  86. }
  87. }