1#![allow(clippy::arc_with_non_send_sync)]
22
23use std::{
24 cell::{Cell, RefCell},
25 hash::{Hash, Hasher},
26 rc::Rc,
27 sync::{Arc, Weak},
28};
29
30use crate::{
31 collections::map::{HashMap, HashSet},
32 snapshot_id_set::{SnapshotId, SnapshotIdSet},
33 snapshot_pinning::{self, PinHandle},
34 snapshot_weak_set::SnapshotWeakSetDebugStats,
35 state::{StateObject, StateRecord},
36};
37
38mod global;
39mod mutable;
40mod nested;
41mod readonly;
42mod runtime;
43mod transparent;
44
45#[cfg(test)]
46mod integration_tests;
47
48pub use global::{GlobalSnapshot, advance_global_snapshot};
49pub use mutable::MutableSnapshot;
50pub use nested::{NestedMutableSnapshot, NestedReadonlySnapshot};
51pub use readonly::ReadonlySnapshot;
52#[cfg(test)]
53pub(crate) use runtime::{TestRuntimeGuard, reset_runtime_for_tests};
54pub(crate) use runtime::{allocate_snapshot, close_snapshot, with_runtime};
55pub use transparent::{TransparentObserverMutableSnapshot, TransparentObserverSnapshot};
56
57pub type ReadObserver = Arc<dyn Fn(&dyn StateObject) + 'static>;
59
60pub type WriteObserver = Arc<dyn Fn(&dyn StateObject) + 'static>;
62
63pub type ApplyObserver = Rc<dyn Fn(&[Arc<dyn StateObject>], SnapshotId) + 'static>;
65
66#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum SnapshotApplyResult {
69 Success,
71 Failure,
73}
74
75impl SnapshotApplyResult {
76 pub fn is_success(&self) -> bool {
78 matches!(self, SnapshotApplyResult::Success)
79 }
80
81 pub fn is_failure(&self) -> bool {
83 matches!(self, SnapshotApplyResult::Failure)
84 }
85
86 #[track_caller]
88 pub fn check(&self) {
89 if self.is_failure() {
90 panic!("Snapshot apply failed");
91 }
92 }
93}
94
95pub type StateObjectId = usize;
97
98#[derive(Clone)]
103pub enum AnySnapshot {
104 Readonly(Arc<ReadonlySnapshot>),
105 Mutable(Arc<MutableSnapshot>),
106 NestedReadonly(Arc<NestedReadonlySnapshot>),
107 NestedMutable(Arc<NestedMutableSnapshot>),
108 Global(Arc<GlobalSnapshot>),
109 TransparentMutable(Arc<TransparentObserverMutableSnapshot>),
110 TransparentReadonly(Arc<TransparentObserverSnapshot>),
111}
112
113#[derive(Clone)]
120pub enum AnyMutableSnapshot {
121 Root(Arc<MutableSnapshot>),
122 Nested(Arc<NestedMutableSnapshot>),
123}
124
125impl AnyMutableSnapshot {
126 pub fn snapshot_id(&self) -> SnapshotId {
128 match self {
129 AnyMutableSnapshot::Root(s) => s.snapshot_id(),
130 AnyMutableSnapshot::Nested(s) => s.snapshot_id(),
131 }
132 }
133
134 pub fn invalid(&self) -> SnapshotIdSet {
136 match self {
137 AnyMutableSnapshot::Root(s) => s.invalid(),
138 AnyMutableSnapshot::Nested(s) => s.invalid(),
139 }
140 }
141
142 pub fn enter<T>(&self, f: impl FnOnce() -> T) -> T {
144 match self {
145 AnyMutableSnapshot::Root(s) => s.enter(f),
146 AnyMutableSnapshot::Nested(s) => s.enter(f),
147 }
148 }
149
150 pub fn apply(&self) -> SnapshotApplyResult {
152 match self {
153 AnyMutableSnapshot::Root(s) => s.apply(),
154 AnyMutableSnapshot::Nested(s) => s.apply(),
155 }
156 }
157
158 pub fn dispose(&self) {
160 match self {
161 AnyMutableSnapshot::Root(s) => s.dispose(),
162 AnyMutableSnapshot::Nested(s) => s.dispose(),
163 }
164 }
165}
166
167impl AnySnapshot {
168 pub fn snapshot_id(&self) -> SnapshotId {
170 match self {
171 AnySnapshot::Readonly(s) => s.snapshot_id(),
172 AnySnapshot::Mutable(s) => s.snapshot_id(),
173 AnySnapshot::NestedReadonly(s) => s.snapshot_id(),
174 AnySnapshot::NestedMutable(s) => s.snapshot_id(),
175 AnySnapshot::Global(s) => s.snapshot_id(),
176 AnySnapshot::TransparentMutable(s) => s.snapshot_id(),
177 AnySnapshot::TransparentReadonly(s) => s.snapshot_id(),
178 }
179 }
180
181 pub fn invalid(&self) -> SnapshotIdSet {
183 match self {
184 AnySnapshot::Readonly(s) => s.invalid(),
185 AnySnapshot::Mutable(s) => s.invalid(),
186 AnySnapshot::NestedReadonly(s) => s.invalid(),
187 AnySnapshot::NestedMutable(s) => s.invalid(),
188 AnySnapshot::Global(s) => s.invalid(),
189 AnySnapshot::TransparentMutable(s) => s.invalid(),
190 AnySnapshot::TransparentReadonly(s) => s.invalid(),
191 }
192 }
193
194 pub fn is_valid(&self, id: SnapshotId) -> bool {
196 let snapshot_id = self.snapshot_id();
197 id <= snapshot_id && !self.invalid().get(id)
198 }
199
200 pub fn read_only(&self) -> bool {
202 match self {
203 AnySnapshot::Readonly(_) => true,
204 AnySnapshot::Mutable(_) => false,
205 AnySnapshot::NestedReadonly(_) => true,
206 AnySnapshot::NestedMutable(_) => false,
207 AnySnapshot::Global(_) => false,
208 AnySnapshot::TransparentMutable(_) => false,
209 AnySnapshot::TransparentReadonly(_) => true,
210 }
211 }
212
213 pub fn root(&self) -> AnySnapshot {
215 match self {
216 AnySnapshot::Readonly(s) => AnySnapshot::Readonly(s.root_readonly()),
217 AnySnapshot::Mutable(s) => AnySnapshot::Mutable(s.root_mutable()),
218 AnySnapshot::NestedReadonly(s) => AnySnapshot::NestedReadonly(s.root_nested_readonly()),
219 AnySnapshot::NestedMutable(s) => AnySnapshot::Mutable(s.root_mutable()),
220 AnySnapshot::Global(s) => AnySnapshot::Global(s.root_global()),
221 AnySnapshot::TransparentMutable(s) => {
222 AnySnapshot::TransparentMutable(s.root_transparent_mutable())
223 }
224 AnySnapshot::TransparentReadonly(s) => {
225 AnySnapshot::TransparentReadonly(s.root_transparent_readonly())
226 }
227 }
228 }
229
230 pub fn is_same_transparent(&self, other: &Arc<TransparentObserverMutableSnapshot>) -> bool {
232 matches!(self, AnySnapshot::TransparentMutable(snapshot) if Arc::ptr_eq(snapshot, other))
233 }
234
235 pub fn is_same_transparent_mutable(
237 &self,
238 other: &Arc<TransparentObserverMutableSnapshot>,
239 ) -> bool {
240 self.is_same_transparent(other)
241 }
242
243 pub fn is_same_transparent_readonly(&self, other: &Arc<TransparentObserverSnapshot>) -> bool {
245 matches!(self, AnySnapshot::TransparentReadonly(snapshot) if Arc::ptr_eq(snapshot, other))
246 }
247
248 pub fn enter<T>(&self, f: impl FnOnce() -> T) -> T {
250 match self {
251 AnySnapshot::Readonly(s) => s.enter(f),
252 AnySnapshot::Mutable(s) => s.enter(f),
253 AnySnapshot::NestedReadonly(s) => s.enter(f),
254 AnySnapshot::NestedMutable(s) => s.enter(f),
255 AnySnapshot::Global(s) => s.enter(f),
256 AnySnapshot::TransparentMutable(s) => s.enter(f),
257 AnySnapshot::TransparentReadonly(s) => s.enter(f),
258 }
259 }
260
261 pub fn take_nested_snapshot(&self, read_observer: Option<ReadObserver>) -> AnySnapshot {
263 match self {
264 AnySnapshot::Readonly(s) => {
265 AnySnapshot::Readonly(s.take_nested_snapshot(read_observer))
266 }
267 AnySnapshot::Mutable(s) => AnySnapshot::Readonly(s.take_nested_snapshot(read_observer)),
268 AnySnapshot::NestedReadonly(s) => {
269 AnySnapshot::NestedReadonly(s.take_nested_snapshot(read_observer))
270 }
271 AnySnapshot::NestedMutable(s) => {
272 AnySnapshot::Readonly(s.take_nested_snapshot(read_observer))
273 }
274 AnySnapshot::Global(s) => AnySnapshot::Readonly(s.take_nested_snapshot(read_observer)),
275 AnySnapshot::TransparentMutable(s) => {
276 AnySnapshot::Readonly(s.take_nested_snapshot(read_observer))
277 }
278 AnySnapshot::TransparentReadonly(s) => {
279 AnySnapshot::TransparentReadonly(s.take_nested_snapshot(read_observer))
280 }
281 }
282 }
283
284 pub fn has_pending_changes(&self) -> bool {
286 match self {
287 AnySnapshot::Readonly(s) => s.has_pending_changes(),
288 AnySnapshot::Mutable(s) => s.has_pending_changes(),
289 AnySnapshot::NestedReadonly(s) => s.has_pending_changes(),
290 AnySnapshot::NestedMutable(s) => s.has_pending_changes(),
291 AnySnapshot::Global(s) => s.has_pending_changes(),
292 AnySnapshot::TransparentMutable(s) => s.has_pending_changes(),
293 AnySnapshot::TransparentReadonly(s) => s.has_pending_changes(),
294 }
295 }
296
297 pub fn dispose(&self) {
299 match self {
300 AnySnapshot::Readonly(s) => s.dispose(),
301 AnySnapshot::Mutable(s) => s.dispose(),
302 AnySnapshot::NestedReadonly(s) => s.dispose(),
303 AnySnapshot::NestedMutable(s) => s.dispose(),
304 AnySnapshot::Global(s) => s.dispose(),
305 AnySnapshot::TransparentMutable(s) => s.dispose(),
306 AnySnapshot::TransparentReadonly(s) => s.dispose(),
307 }
308 }
309
310 pub fn is_disposed(&self) -> bool {
312 match self {
313 AnySnapshot::Readonly(s) => s.is_disposed(),
314 AnySnapshot::Mutable(s) => s.is_disposed(),
315 AnySnapshot::NestedReadonly(s) => s.is_disposed(),
316 AnySnapshot::NestedMutable(s) => s.is_disposed(),
317 AnySnapshot::Global(s) => s.is_disposed(),
318 AnySnapshot::TransparentMutable(s) => s.is_disposed(),
319 AnySnapshot::TransparentReadonly(s) => s.is_disposed(),
320 }
321 }
322
323 pub fn record_read(&self, state: &dyn StateObject) {
325 match self {
326 AnySnapshot::Readonly(s) => s.record_read(state),
327 AnySnapshot::Mutable(s) => s.record_read(state),
328 AnySnapshot::NestedReadonly(s) => s.record_read(state),
329 AnySnapshot::NestedMutable(s) => s.record_read(state),
330 AnySnapshot::Global(s) => s.record_read(state),
331 AnySnapshot::TransparentMutable(s) => s.record_read(state),
332 AnySnapshot::TransparentReadonly(s) => s.record_read(state),
333 }
334 }
335
336 pub fn record_write(&self, state: Arc<dyn StateObject>) {
338 match self {
339 AnySnapshot::Readonly(s) => s.record_write(state),
340 AnySnapshot::Mutable(s) => s.record_write(state),
341 AnySnapshot::NestedReadonly(s) => s.record_write(state),
342 AnySnapshot::NestedMutable(s) => s.record_write(state),
343 AnySnapshot::Global(s) => s.record_write(state),
344 AnySnapshot::TransparentMutable(s) => s.record_write(state),
345 AnySnapshot::TransparentReadonly(s) => s.record_write(state),
346 }
347 }
348
349 pub fn apply(&self) -> SnapshotApplyResult {
351 match self {
352 AnySnapshot::Mutable(s) => s.apply(),
353 AnySnapshot::NestedMutable(s) => s.apply(),
354 AnySnapshot::Global(s) => s.apply(),
355 AnySnapshot::TransparentMutable(s) => s.apply(),
356 _ => panic!("Cannot apply a read-only snapshot"),
357 }
358 }
359
360 pub fn take_nested_mutable_snapshot(
362 &self,
363 read_observer: Option<ReadObserver>,
364 write_observer: Option<WriteObserver>,
365 ) -> AnySnapshot {
366 match self {
367 AnySnapshot::Mutable(s) => AnySnapshot::NestedMutable(
368 s.take_nested_mutable_snapshot(read_observer, write_observer),
369 ),
370 AnySnapshot::NestedMutable(s) => AnySnapshot::NestedMutable(
371 s.take_nested_mutable_snapshot(read_observer, write_observer),
372 ),
373 AnySnapshot::Global(s) => {
374 AnySnapshot::Mutable(s.take_nested_mutable_snapshot(read_observer, write_observer))
375 }
376 AnySnapshot::TransparentMutable(s) => AnySnapshot::TransparentMutable(
377 s.take_nested_mutable_snapshot(read_observer, write_observer),
378 ),
379 _ => panic!("Cannot take nested mutable snapshot from read-only snapshot"),
380 }
381 }
382}
383
384thread_local! {
385 static CURRENT_SNAPSHOT: RefCell<Option<AnySnapshot>> = const { RefCell::new(None) };
386}
387
388pub fn current_snapshot() -> Option<AnySnapshot> {
390 CURRENT_SNAPSHOT
391 .try_with(|cell| cell.borrow().clone())
392 .unwrap_or(None)
393}
394
395pub(crate) fn set_current_snapshot(snapshot: Option<AnySnapshot>) {
396 let _ = CURRENT_SNAPSHOT.try_with(|cell| {
397 *cell.borrow_mut() = snapshot;
398 });
399}
400
401struct CurrentSnapshotGuard {
402 previous: Option<AnySnapshot>,
403}
404
405impl CurrentSnapshotGuard {
406 fn enter(snapshot: AnySnapshot) -> Self {
407 let previous = current_snapshot();
408 set_current_snapshot(Some(snapshot));
409 Self { previous }
410 }
411}
412
413impl Drop for CurrentSnapshotGuard {
414 fn drop(&mut self) {
415 set_current_snapshot(self.previous.take());
416 }
417}
418
419pub(crate) fn enter_snapshot_scope<T>(snapshot: AnySnapshot, f: impl FnOnce() -> T) -> T {
420 let _guard = CurrentSnapshotGuard::enter(snapshot);
421 f()
422}
423
424pub fn take_mutable_snapshot(
433 read_observer: Option<ReadObserver>,
434 write_observer: Option<WriteObserver>,
435) -> AnyMutableSnapshot {
436 match current_snapshot() {
437 Some(AnySnapshot::Mutable(parent)) => AnyMutableSnapshot::Nested(
438 parent.take_nested_mutable_snapshot(read_observer, write_observer),
439 ),
440 Some(AnySnapshot::NestedMutable(parent)) => AnyMutableSnapshot::Nested(
441 parent.take_nested_mutable_snapshot(read_observer, write_observer),
442 ),
443 _ => AnyMutableSnapshot::Root(
444 GlobalSnapshot::get_or_create()
445 .take_nested_mutable_snapshot(read_observer, write_observer),
446 ),
447 }
448}
449
450pub fn take_transparent_observer_mutable_snapshot(
459 read_observer: Option<ReadObserver>,
460 write_observer: Option<WriteObserver>,
461) -> Arc<TransparentObserverMutableSnapshot> {
462 let parent = current_snapshot();
463 match parent {
464 Some(AnySnapshot::TransparentMutable(transparent)) if transparent.can_reuse() => {
465 transparent
466 }
467 _ => {
468 let current = current_snapshot()
469 .unwrap_or_else(|| AnySnapshot::Global(GlobalSnapshot::get_or_create()));
470 let id = current.snapshot_id();
471 let invalid = current.invalid();
472 TransparentObserverMutableSnapshot::new(
473 id,
474 invalid,
475 read_observer,
476 write_observer,
477 None,
478 )
479 }
480 }
481}
482
483pub fn allocate_record_id() -> SnapshotId {
485 runtime::allocate_record_id()
486}
487
488pub(crate) fn peek_next_snapshot_id() -> SnapshotId {
489 runtime::peek_next_snapshot_id()
490}
491
492#[derive(Clone)]
493struct ObserverId(Rc<()>);
494
495impl ObserverId {
496 fn new() -> Self {
497 Self(Rc::new(()))
498 }
499}
500
501impl PartialEq for ObserverId {
502 fn eq(&self, other: &Self) -> bool {
503 Rc::ptr_eq(&self.0, &other.0)
504 }
505}
506
507impl Eq for ObserverId {}
508
509impl Hash for ObserverId {
510 fn hash<H: Hasher>(&self, state: &mut H) {
511 Rc::as_ptr(&self.0).hash(state);
512 }
513}
514
515thread_local! {
516 static APPLY_OBSERVERS: RefCell<HashMap<ObserverId, ApplyObserver>> = RefCell::new(HashMap::default());
517}
518
519thread_local! {
520 static LAST_WRITES: RefCell<HashMap<StateObjectId, SnapshotId>> = RefCell::new(HashMap::default());
521}
522
523thread_local! {
524 static EXTRA_STATE_OBJECTS: RefCell<crate::snapshot_weak_set::SnapshotWeakSet> = RefCell::new(crate::snapshot_weak_set::SnapshotWeakSet::new());
525}
526
527const UNUSED_RECORD_CLEANUP_INTERVAL: SnapshotId = 2;
528const UNUSED_RECORD_CLEANUP_BUSY_INTERVAL: SnapshotId = 1;
529const UNUSED_RECORD_CLEANUP_MIN_SIZE: usize = 64;
530
531thread_local! {
532 static LAST_UNUSED_RECORD_CLEANUP: Cell<SnapshotId> = const { Cell::new(0) };
533}
534
535#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
536pub struct SnapshotV2DebugStats {
537 pub apply_observers_len: usize,
538 pub apply_observers_cap: usize,
539 pub last_writes_len: usize,
540 pub last_writes_cap: usize,
541 pub extra_state_objects_len: usize,
542 pub extra_state_objects_cap: usize,
543 pub last_unused_record_cleanup: SnapshotId,
544}
545
546pub fn debug_snapshot_v2_stats() -> SnapshotV2DebugStats {
547 let (apply_observers_len, apply_observers_cap) = APPLY_OBSERVERS.with(|cell| {
548 let observers = cell.borrow();
549 (observers.len(), observers.capacity())
550 });
551 let (last_writes_len, last_writes_cap) = LAST_WRITES.with(|cell| {
552 let writes = cell.borrow();
553 (writes.len(), writes.capacity())
554 });
555 let SnapshotWeakSetDebugStats {
556 len: extra_state_objects_len,
557 capacity: extra_state_objects_cap,
558 } = EXTRA_STATE_OBJECTS.with(|cell| cell.borrow().debug_stats());
559 let last_unused_record_cleanup = LAST_UNUSED_RECORD_CLEANUP.with(|cell| cell.get());
560
561 SnapshotV2DebugStats {
562 apply_observers_len,
563 apply_observers_cap,
564 last_writes_len,
565 last_writes_cap,
566 extra_state_objects_len,
567 extra_state_objects_cap,
568 last_unused_record_cleanup,
569 }
570}
571
572pub fn register_apply_observer(observer: ApplyObserver) -> ObserverHandle {
576 let id = ObserverId::new();
577 APPLY_OBSERVERS.with(|cell| {
578 cell.borrow_mut().insert(id.clone(), observer);
579 });
580 ObserverHandle {
581 kind: ObserverKind::Apply,
582 id,
583 }
584}
585
586pub struct ObserverHandle {
590 kind: ObserverKind,
591 id: ObserverId,
592}
593
594enum ObserverKind {
595 Apply,
596}
597
598impl Drop for ObserverHandle {
599 fn drop(&mut self) {
600 match self.kind {
601 ObserverKind::Apply => {
602 APPLY_OBSERVERS.with(|cell| {
603 cell.borrow_mut().remove(&self.id);
604 });
605 }
606 }
607 }
608}
609
610pub(crate) fn notify_apply_observers(modified: &[Arc<dyn StateObject>], snapshot_id: SnapshotId) {
611 APPLY_OBSERVERS.with(|cell| {
612 let observers: Vec<ApplyObserver> = cell.borrow().values().cloned().collect();
613 for observer in observers.into_iter() {
614 observer(modified, snapshot_id);
615 }
616 });
617}
618
619pub(crate) fn set_last_write(id: StateObjectId, snapshot_id: SnapshotId) {
620 LAST_WRITES.with(|cell| {
621 cell.borrow_mut().insert(id, snapshot_id);
622 });
623}
624
625#[cfg(test)]
626pub(crate) fn clear_last_writes() {
627 LAST_WRITES.with(|cell| {
628 cell.borrow_mut().clear();
629 });
630}
631
632pub(crate) fn check_and_overwrite_unused_records_locked() {
633 EXTRA_STATE_OBJECTS.with(|cell| {
634 cell.borrow_mut()
635 .remove_if(|state| state.overwrite_unused_records());
636 });
637}
638
639pub(crate) fn maybe_check_and_overwrite_unused_records_locked(current_snapshot_id: SnapshotId) {
640 let should_run = EXTRA_STATE_OBJECTS.with(|cell| {
641 let set = cell.borrow();
642 if set.is_empty() {
643 return false;
644 }
645 let last_cleanup = LAST_UNUSED_RECORD_CLEANUP.with(|last| last.get());
646 let interval = if set.len() >= UNUSED_RECORD_CLEANUP_MIN_SIZE {
647 UNUSED_RECORD_CLEANUP_BUSY_INTERVAL
648 } else {
649 UNUSED_RECORD_CLEANUP_INTERVAL
650 };
651 current_snapshot_id.saturating_sub(last_cleanup) >= interval
652 });
653
654 if should_run {
655 LAST_UNUSED_RECORD_CLEANUP.with(|cell| cell.set(current_snapshot_id));
656 check_and_overwrite_unused_records_locked();
657 }
658}
659
660#[cfg(test)]
661pub(crate) fn clear_unused_record_cleanup_for_tests() {
662 LAST_UNUSED_RECORD_CLEANUP.with(|cell| cell.set(0));
663}
664
665pub(crate) fn optimistic_merges(
666 current_snapshot_id: SnapshotId,
667 base_parent_id: SnapshotId,
668 modified_objects: &[(StateObjectId, Arc<dyn StateObject>, SnapshotId)],
669 invalid_snapshots: &SnapshotIdSet,
670 applying_invalid: &SnapshotIdSet,
671) -> Option<HashMap<usize, Rc<StateRecord>>> {
672 if modified_objects.is_empty() {
673 return None;
674 }
675
676 let mut result: Option<HashMap<usize, Rc<StateRecord>>> = None;
677
678 for (_, state, writer_id) in modified_objects.iter() {
679 let head = state.first_record();
680
681 let current = match crate::state::readable_record_for(
682 &head,
683 current_snapshot_id,
684 invalid_snapshots,
685 ) {
686 Some(record) => record,
687 None => continue,
688 };
689
690 let (previous_opt, found_base) =
691 mutable::find_previous_record(&head, base_parent_id, applying_invalid);
692 let previous = previous_opt?;
693
694 if !found_base || previous.snapshot_id() == crate::state::PREEXISTING_SNAPSHOT_ID {
695 continue;
696 }
697
698 if Rc::ptr_eq(¤t, &previous) {
699 continue;
700 }
701
702 let applied = mutable::find_record_by_id(&head, *writer_id)?;
703
704 let merged = state.merge_records(
705 Rc::clone(&previous),
706 Rc::clone(¤t),
707 Rc::clone(&applied),
708 )?;
709
710 result
711 .get_or_insert_with(HashMap::default)
712 .insert(Rc::as_ptr(¤t) as usize, merged);
713 }
714
715 result
716}
717
718#[allow(clippy::arc_with_non_send_sync)]
719fn merge_observers(a: Option<ReadObserver>, b: Option<ReadObserver>) -> Option<ReadObserver> {
720 match (a, b) {
721 (None, None) => None,
722 (Some(a), None) => Some(a),
723 (None, Some(b)) => Some(b),
724 (Some(a), Some(b)) => Some(Arc::new(move |state: &dyn StateObject| {
725 a(state);
726 b(state);
727 })),
728 }
729}
730
731pub fn merge_read_observers(
737 a: Option<ReadObserver>,
738 b: Option<ReadObserver>,
739) -> Option<ReadObserver> {
740 merge_observers(a, b)
741}
742
743pub fn merge_write_observers(
749 a: Option<WriteObserver>,
750 b: Option<WriteObserver>,
751) -> Option<WriteObserver> {
752 merge_observers(a, b)
753}
754
755pub(crate) struct SnapshotState {
756 pub(crate) id: Cell<SnapshotId>,
757 pub(crate) invalid: RefCell<SnapshotIdSet>,
758 pub(crate) pin_handle: Cell<PinHandle>,
759 pub(crate) disposed: Cell<bool>,
760 pub(crate) read_observer: RefCell<Option<ReadObserver>>,
761 pub(crate) write_observer: RefCell<Option<WriteObserver>>,
762 #[allow(clippy::type_complexity)]
763 pub(crate) modified: RefCell<HashMap<StateObjectId, (Arc<dyn StateObject>, SnapshotId)>>,
764 on_dispose: RefCell<Option<Box<dyn FnOnce()>>>,
765 runtime_tracked: bool,
766 pending_children: RefCell<HashSet<SnapshotId>>,
767}
768
769impl SnapshotState {
770 pub(crate) fn new(
771 id: SnapshotId,
772 invalid: SnapshotIdSet,
773 read_observer: Option<ReadObserver>,
774 write_observer: Option<WriteObserver>,
775 runtime_tracked: bool,
776 ) -> Self {
777 Self::new_with_pinning(
778 id,
779 invalid,
780 read_observer,
781 write_observer,
782 runtime_tracked,
783 true,
784 )
785 }
786
787 pub(crate) fn new_with_pinning(
788 id: SnapshotId,
789 invalid: SnapshotIdSet,
790 read_observer: Option<ReadObserver>,
791 write_observer: Option<WriteObserver>,
792 runtime_tracked: bool,
793 should_pin: bool,
794 ) -> Self {
795 let pin_handle = if should_pin {
796 snapshot_pinning::track_pinning(id, &invalid)
797 } else {
798 snapshot_pinning::PinHandle::INVALID
799 };
800 Self {
801 id: Cell::new(id),
802 invalid: RefCell::new(invalid),
803 pin_handle: Cell::new(pin_handle),
804 disposed: Cell::new(false),
805 read_observer: RefCell::new(read_observer),
806 write_observer: RefCell::new(write_observer),
807 modified: RefCell::new(HashMap::default()),
808 on_dispose: RefCell::new(None),
809 runtime_tracked,
810 pending_children: RefCell::new(HashSet::default()),
811 }
812 }
813
814 pub(crate) fn record_read(&self, state: &dyn StateObject) {
815 if let Some(observer) = self.read_observer.borrow().as_ref() {
816 observer(state);
817 }
818 }
819
820 pub(crate) fn record_write(&self, state: Arc<dyn StateObject>, writer_id: SnapshotId) {
821 let state_id = state.object_id().as_usize();
822
823 let mut modified = self.modified.borrow_mut();
824
825 match modified.entry(state_id) {
826 std::collections::hash_map::Entry::Vacant(e) => {
827 if let Some(observer) = self.write_observer.borrow().as_ref() {
828 observer(&*state);
829 }
830 e.insert((state, writer_id));
831 }
832 std::collections::hash_map::Entry::Occupied(mut e) => {
833 e.insert((state, writer_id));
834 }
835 }
836 }
837
838 pub(crate) fn dispose(&self) {
839 if !self.disposed.replace(true) {
840 let pin_handle = self.pin_handle.get();
841 snapshot_pinning::release_pinning(pin_handle);
842 if let Some(cb) = self.on_dispose.borrow_mut().take() {
843 cb();
844 }
845 if self.runtime_tracked {
846 close_snapshot(self.id.get());
847 }
848 }
849 }
850
851 pub(crate) fn add_pending_child(&self, id: SnapshotId) {
852 self.pending_children.borrow_mut().insert(id);
853 }
854
855 pub(crate) fn remove_pending_child(&self, id: SnapshotId) {
856 self.pending_children.borrow_mut().remove(&id);
857 }
858
859 pub(crate) fn has_pending_children(&self) -> bool {
860 !self.pending_children.borrow().is_empty()
861 }
862
863 pub(crate) fn pending_children(&self) -> Vec<SnapshotId> {
864 self.pending_children.borrow().iter().copied().collect()
865 }
866
867 pub(crate) fn set_on_dispose<F>(&self, f: F)
868 where
869 F: FnOnce() + 'static,
870 {
871 *self.on_dispose.borrow_mut() = Some(Box::new(f));
872 }
873}
874
875pub(crate) trait NestedMutableHost {
876 fn snapshot_state(&self) -> &SnapshotState;
877 fn nested_count(&self) -> &Cell<usize>;
878}
879
880pub(crate) fn clear_nested_child_on_dispose<P>(
881 parent: &Arc<P>,
882 child_id: SnapshotId,
883) -> impl FnOnce() + 'static
884where
885 P: NestedMutableHost + 'static,
886{
887 let weak = Arc::downgrade(parent);
888 move || {
889 if let Some(parent) = weak.upgrade() {
890 let nested_count = parent.nested_count();
891 if nested_count.get() > 0 {
892 nested_count.set(nested_count.get().saturating_sub(1));
893 }
894 let state = parent.snapshot_state();
895 let new_invalid = state.invalid.borrow().clone().clear(child_id);
896 state.invalid.replace(new_invalid);
897 state.remove_pending_child(child_id);
898 }
899 }
900}
901
902pub(crate) fn allocate_nested_mutable_snapshot<P>(
903 parent: &Arc<P>,
904 root: Weak<MutableSnapshot>,
905 read_observer: Option<ReadObserver>,
906 write_observer: Option<WriteObserver>,
907) -> Arc<NestedMutableSnapshot>
908where
909 P: NestedMutableHost + 'static,
910{
911 let state = parent.snapshot_state();
912 let merged_read = merge_read_observers(read_observer, state.read_observer.borrow().clone());
913 let merged_write = merge_write_observers(write_observer, state.write_observer.borrow().clone());
914
915 let parent_id = state.id.get();
916 let current_invalid = state.invalid.borrow().clone();
917
918 let (new_id, _runtime_invalid) = allocate_snapshot();
919
920 let parent_invalid_with_child = current_invalid.set(new_id);
921 state.invalid.replace(parent_invalid_with_child);
922
923 let invalid = current_invalid.add_range(parent_id + 1, new_id);
924
925 let nested = NestedMutableSnapshot::new(
926 new_id,
927 invalid,
928 merged_read,
929 merged_write,
930 root,
931 state.id.get(),
932 );
933
934 let nested_count = parent.nested_count();
935 nested_count.set(nested_count.get() + 1);
936 state.add_pending_child(new_id);
937
938 nested.set_on_dispose(clear_nested_child_on_dispose(parent, new_id));
939
940 nested
941}
942
943#[cfg(test)]
944mod tests {
945 use std::sync::Mutex;
946
947 use super::*;
948
949 #[test]
950 fn apply_observer_ids_do_not_use_process_global_counter() {
951 let source = include_str!("mod.rs");
952 assert!(!source.contains(concat!("NEXT_", "OBSERVER_ID")));
953 assert!(!source.contains(concat!("Atomic", "Usize")));
954 }
955
956 #[test]
957 fn test_apply_result_is_success() {
958 assert!(SnapshotApplyResult::Success.is_success());
959 assert!(!SnapshotApplyResult::Failure.is_success());
960 }
961
962 #[test]
963 fn test_apply_result_is_failure() {
964 assert!(!SnapshotApplyResult::Success.is_failure());
965 assert!(SnapshotApplyResult::Failure.is_failure());
966 }
967
968 #[test]
969 fn test_apply_result_check_success() {
970 SnapshotApplyResult::Success.check();
971 }
972
973 #[test]
974 #[should_panic(expected = "Snapshot apply failed")]
975 fn test_apply_result_check_failure() {
976 SnapshotApplyResult::Failure.check();
977 }
978
979 #[test]
980 fn snapshot_enter_restores_current_snapshot_after_panic() {
981 let _guard = reset_runtime_for_tests();
982 let snapshot = take_mutable_snapshot(None, None);
983
984 let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
985 snapshot.enter(|| panic!("snapshot body panic"));
986 }));
987
988 assert!(result.is_err());
989 assert!(
990 current_snapshot().is_none(),
991 "snapshot enter must restore the previous current snapshot while unwinding"
992 );
993 }
994
995 #[test]
996 fn test_merge_read_observers_both_none() {
997 let result = merge_read_observers(None, None);
998 assert!(result.is_none());
999 }
1000
1001 #[test]
1002 fn test_merge_read_observers_one_some() {
1003 let observer = Arc::new(|_: &dyn StateObject| {});
1004 let result = merge_read_observers(Some(observer.clone()), None);
1005 assert!(result.is_some());
1006
1007 let result = merge_read_observers(None, Some(observer));
1008 assert!(result.is_some());
1009 }
1010
1011 #[test]
1012 fn test_merge_write_observers_both_none() {
1013 let result = merge_write_observers(None, None);
1014 assert!(result.is_none());
1015 }
1016
1017 #[test]
1018 fn test_merge_write_observers_one_some() {
1019 let observer = Arc::new(|_: &dyn StateObject| {});
1020 let result = merge_write_observers(Some(observer.clone()), None);
1021 assert!(result.is_some());
1022
1023 let result = merge_write_observers(None, Some(observer));
1024 assert!(result.is_some());
1025 }
1026
1027 #[test]
1028 fn test_current_snapshot_none_initially() {
1029 set_current_snapshot(None);
1030 assert!(current_snapshot().is_none());
1031 }
1032
1033 struct TestStateObject {
1034 id: usize,
1035 }
1036
1037 impl TestStateObject {
1038 fn new(id: usize) -> Arc<Self> {
1039 Arc::new(Self { id })
1040 }
1041 }
1042
1043 impl StateObject for TestStateObject {
1044 fn object_id(&self) -> crate::state::ObjectId {
1045 crate::state::ObjectId(self.id)
1046 }
1047
1048 fn first_record(&self) -> Rc<crate::state::StateRecord> {
1049 unimplemented!("Not needed for observer tests")
1050 }
1051
1052 fn try_readable_record(
1053 &self,
1054 _snapshot_id: SnapshotId,
1055 _invalid: &SnapshotIdSet,
1056 ) -> Option<Rc<crate::state::StateRecord>> {
1057 None
1058 }
1059
1060 fn readable_record(
1061 &self,
1062 _snapshot_id: SnapshotId,
1063 _invalid: &SnapshotIdSet,
1064 ) -> Rc<crate::state::StateRecord> {
1065 unimplemented!("Not needed for observer tests")
1066 }
1067
1068 fn prepend_state_record(&self, _record: Rc<crate::state::StateRecord>) {
1069 unimplemented!("Not needed for observer tests")
1070 }
1071
1072 fn promote_record(&self, _child_id: SnapshotId) -> Result<(), &'static str> {
1073 unimplemented!("Not needed for observer tests")
1074 }
1075
1076 fn as_any(&self) -> &dyn std::any::Any {
1077 self
1078 }
1079 }
1080
1081 #[test]
1082 fn test_apply_observer_receives_correct_modified_objects() {
1083 use std::sync::Mutex;
1084
1085 let received_count = Arc::new(Mutex::new(0));
1086 let received_snapshot_id = Arc::new(Mutex::new(0));
1087
1088 let received_count_clone = received_count.clone();
1089 let received_snapshot_id_clone = received_snapshot_id.clone();
1090
1091 let _handle = register_apply_observer(Rc::new(move |modified, snapshot_id| {
1092 *received_snapshot_id_clone.lock().unwrap() = snapshot_id;
1093 *received_count_clone.lock().unwrap() = modified.len();
1094 }));
1095
1096 let obj1: Arc<dyn StateObject> = TestStateObject::new(42);
1097 let obj2: Arc<dyn StateObject> = TestStateObject::new(99);
1098 let modified = vec![obj1, obj2];
1099
1100 notify_apply_observers(&modified, 123);
1101
1102 assert_eq!(*received_snapshot_id.lock().unwrap(), 123);
1103 assert_eq!(*received_count.lock().unwrap(), 2);
1104 }
1105
1106 #[test]
1107 fn test_apply_observer_receives_correct_snapshot_id() {
1108 use std::sync::Mutex;
1109
1110 let received_id = Arc::new(Mutex::new(0));
1111 let received_id_clone = received_id.clone();
1112
1113 let _handle = register_apply_observer(Rc::new(move |_, snapshot_id| {
1114 *received_id_clone.lock().unwrap() = snapshot_id;
1115 }));
1116
1117 notify_apply_observers(&[], 456);
1118
1119 assert_eq!(*received_id.lock().unwrap(), 456);
1120 }
1121
1122 #[test]
1123 fn test_multiple_apply_observers_all_called() {
1124 use std::sync::Mutex;
1125
1126 let call_count1 = Arc::new(Mutex::new(0));
1127 let call_count2 = Arc::new(Mutex::new(0));
1128 let call_count3 = Arc::new(Mutex::new(0));
1129
1130 let call_count1_clone = call_count1.clone();
1131 let call_count2_clone = call_count2.clone();
1132 let call_count3_clone = call_count3.clone();
1133
1134 let _handle1 = register_apply_observer(Rc::new(move |_, _| {
1135 *call_count1_clone.lock().unwrap() += 1;
1136 }));
1137
1138 let _handle2 = register_apply_observer(Rc::new(move |_, _| {
1139 *call_count2_clone.lock().unwrap() += 1;
1140 }));
1141
1142 let _handle3 = register_apply_observer(Rc::new(move |_, _| {
1143 *call_count3_clone.lock().unwrap() += 1;
1144 }));
1145
1146 notify_apply_observers(&[], 1);
1147
1148 assert_eq!(*call_count1.lock().unwrap(), 1);
1149 assert_eq!(*call_count2.lock().unwrap(), 1);
1150 assert_eq!(*call_count3.lock().unwrap(), 1);
1151
1152 notify_apply_observers(&[], 2);
1153
1154 assert_eq!(*call_count1.lock().unwrap(), 2);
1155 assert_eq!(*call_count2.lock().unwrap(), 2);
1156 assert_eq!(*call_count3.lock().unwrap(), 2);
1157 }
1158
1159 #[test]
1160 fn test_apply_observer_not_called_for_empty_modifications() {
1161 use std::sync::Mutex;
1162
1163 let call_count = Arc::new(Mutex::new(0));
1164 let call_count_clone = call_count.clone();
1165
1166 let _handle = register_apply_observer(Rc::new(move |modified, _| {
1167 *call_count_clone.lock().unwrap() += 1;
1168 assert_eq!(modified.len(), 0);
1169 }));
1170
1171 notify_apply_observers(&[], 1);
1172
1173 assert_eq!(*call_count.lock().unwrap(), 1);
1174 }
1175
1176 fn register_counting_observer(calls: &Arc<Mutex<Vec<i32>>>, tag: i32) -> ObserverHandle {
1177 let calls = calls.clone();
1178 register_apply_observer(Rc::new(move |_, _| {
1179 calls.lock().unwrap().push(tag);
1180 }))
1181 }
1182
1183 #[test]
1184 fn test_observer_handle_drop_removes_correct_observer() {
1185 let calls = Arc::new(Mutex::new(Vec::new()));
1186
1187 let handle1 = register_counting_observer(&calls, 1);
1188 let handle2 = register_counting_observer(&calls, 2);
1189 let handle3 = register_counting_observer(&calls, 3);
1190
1191 notify_apply_observers(&[], 1);
1192 let result = calls.lock().unwrap().clone();
1193 assert_eq!(result.len(), 3);
1194 assert!(result.contains(&1));
1195 assert!(result.contains(&2));
1196 assert!(result.contains(&3));
1197 calls.lock().unwrap().clear();
1198
1199 drop(handle2);
1200
1201 notify_apply_observers(&[], 2);
1202 let result = calls.lock().unwrap().clone();
1203 assert_eq!(result.len(), 2);
1204 assert!(result.contains(&1));
1205 assert!(result.contains(&3));
1206 assert!(!result.contains(&2));
1207 calls.lock().unwrap().clear();
1208
1209 drop(handle1);
1210
1211 notify_apply_observers(&[], 3);
1212 let result = calls.lock().unwrap().clone();
1213 assert_eq!(result.len(), 1);
1214 assert!(result.contains(&3));
1215 calls.lock().unwrap().clear();
1216
1217 drop(handle3);
1218
1219 notify_apply_observers(&[], 4);
1220 assert_eq!(calls.lock().unwrap().len(), 0);
1221 }
1222
1223 #[test]
1224 fn test_observer_handle_drop_in_different_orders() {
1225 {
1226 let calls = Arc::new(Mutex::new(Vec::new()));
1227
1228 let h1 = register_counting_observer(&calls, 1);
1229 let h2 = register_counting_observer(&calls, 2);
1230 let h3 = register_counting_observer(&calls, 3);
1231
1232 drop(h3);
1233 notify_apply_observers(&[], 1);
1234 let result = calls.lock().unwrap().clone();
1235 assert!(result.contains(&1) && result.contains(&2) && !result.contains(&3));
1236 calls.lock().unwrap().clear();
1237
1238 drop(h2);
1239 notify_apply_observers(&[], 2);
1240 let result = calls.lock().unwrap().clone();
1241 assert_eq!(result.len(), 1);
1242 assert!(result.contains(&1));
1243 calls.lock().unwrap().clear();
1244
1245 drop(h1);
1246 notify_apply_observers(&[], 3);
1247 assert_eq!(calls.lock().unwrap().len(), 0);
1248 }
1249
1250 {
1251 let calls = Arc::new(Mutex::new(Vec::new()));
1252
1253 let h1 = register_counting_observer(&calls, 1);
1254 let h2 = register_counting_observer(&calls, 2);
1255 let h3 = register_counting_observer(&calls, 3);
1256
1257 drop(h1);
1258 notify_apply_observers(&[], 1);
1259 let result = calls.lock().unwrap().clone();
1260 assert!(!result.contains(&1) && result.contains(&2) && result.contains(&3));
1261 calls.lock().unwrap().clear();
1262
1263 drop(h2);
1264 notify_apply_observers(&[], 2);
1265 let result = calls.lock().unwrap().clone();
1266 assert_eq!(result.len(), 1);
1267 assert!(result.contains(&3));
1268 calls.lock().unwrap().clear();
1269
1270 drop(h3);
1271 notify_apply_observers(&[], 3);
1272 assert_eq!(calls.lock().unwrap().len(), 0);
1273 }
1274 }
1275
1276 #[test]
1277 fn test_remaining_observers_still_work_after_drop() {
1278 use std::sync::Mutex;
1279
1280 let calls = Arc::new(Mutex::new(Vec::new()));
1281
1282 let calls1 = calls.clone();
1283 let handle1 = register_apply_observer(Rc::new(move |_, snapshot_id| {
1284 calls1.lock().unwrap().push((1, snapshot_id));
1285 }));
1286
1287 let calls2 = calls.clone();
1288 let handle2 = register_apply_observer(Rc::new(move |_, snapshot_id| {
1289 calls2.lock().unwrap().push((2, snapshot_id));
1290 }));
1291
1292 notify_apply_observers(&[], 100);
1293 assert_eq!(calls.lock().unwrap().len(), 2);
1294 calls.lock().unwrap().clear();
1295
1296 drop(handle1);
1297
1298 notify_apply_observers(&[], 200);
1299 assert_eq!(*calls.lock().unwrap(), vec![(2, 200)]);
1300 calls.lock().unwrap().clear();
1301
1302 let calls3 = calls.clone();
1303 let _handle3 = register_apply_observer(Rc::new(move |_, snapshot_id| {
1304 calls3.lock().unwrap().push((3, snapshot_id));
1305 }));
1306
1307 notify_apply_observers(&[], 300);
1308 let result = calls.lock().unwrap().clone();
1309 assert_eq!(result.len(), 2);
1310 assert!(result.contains(&(2, 300)));
1311 assert!(result.contains(&(3, 300)));
1312
1313 drop(handle2);
1314 }
1315
1316 #[test]
1317 fn test_observer_ids_are_unique() {
1318 use std::sync::Mutex;
1319
1320 let ids = Arc::new(Mutex::new(std::collections::HashSet::new()));
1321
1322 let mut handles = Vec::new();
1323
1324 for i in 0..100 {
1325 let ids_clone = ids.clone();
1326 let handle = register_apply_observer(Rc::new(move |_, _| {
1327 ids_clone.lock().unwrap().insert(i);
1328 }));
1329 handles.push(handle);
1330 }
1331
1332 notify_apply_observers(&[], 1);
1333 assert_eq!(ids.lock().unwrap().len(), 100);
1334
1335 for i in (0..100).step_by(2) {
1336 handles.remove(i / 2);
1337 }
1338
1339 ids.lock().unwrap().clear();
1340 notify_apply_observers(&[], 2);
1341 assert_eq!(ids.lock().unwrap().len(), 50);
1342 }
1343
1344 #[test]
1345 fn test_state_object_storage_in_modified_set() {
1346 let state = SnapshotState::new(1, SnapshotIdSet::new(), None, None, false);
1347
1348 let state_obj = TestStateObject::new(12345) as Arc<dyn StateObject>;
1349
1350 state.record_write(state_obj.clone(), 1);
1351
1352 let modified = state.modified.borrow();
1353 assert_eq!(modified.len(), 1);
1354 assert!(modified.contains_key(&12345));
1355
1356 let (stored, writer_id) = modified.get(&12345).unwrap();
1357 assert_eq!(stored.object_id().as_usize(), 12345);
1358 assert_eq!(*writer_id, 1);
1359 }
1360
1361 #[test]
1362 fn test_multiple_writes_to_same_state_object() {
1363 let state = SnapshotState::new(1, SnapshotIdSet::new(), None, None, false);
1364 let state_obj = TestStateObject::new(99999) as Arc<dyn StateObject>;
1365
1366 state.record_write(state_obj.clone(), 1);
1367 assert_eq!(state.modified.borrow().len(), 1);
1368
1369 state.record_write(state_obj.clone(), 2);
1370 let modified = state.modified.borrow();
1371 assert_eq!(modified.len(), 1);
1372 assert!(modified.contains_key(&99999));
1373 let (_, writer_id) = modified.get(&99999).unwrap();
1374 assert_eq!(*writer_id, 2);
1375 }
1376}