1#![allow(unused_imports)]
4
5use super::atomic::*;
6use super::invariant::*;
7use super::modes::*;
8use super::prelude::*;
9
10verus! {
11
12pub trait AtomicInvariantPredicate<K, V, G> {
13 #[verifier::prophetic]
14 spec fn atomic_inv(k: K, v: V, g: G) -> bool;
15}
16
17pub open spec const ATOMIC_NS: int = 0;
18
19} macro_rules! declare_atomic_type {
21 ($at_ident:ident, $patomic_ty:ident, $perm_ty:ty, $value_ty: ty, $atomic_pred_ty: ident) => {
22 verus!{
23
24 pub struct $atomic_pred_ty<Pred> { p: Pred }
25
26 impl<K, G, Pred> InvariantPredicate<(K, int), ($perm_ty, G)> for $atomic_pred_ty<Pred>
27 where Pred: AtomicInvariantPredicate<K, $value_ty, G>
28 {
29 #[verifier::prophetic]
30 open spec fn inv(k_loc: (K, int), perm_g: ($perm_ty, G)) -> bool {
31 let (k, loc) = k_loc;
32 let (perm, g) = perm_g;
33
34 perm.view().patomic == loc
35 && Pred::atomic_inv(k, perm.view().value, g)
36 }
37 }
38
39 #[doc = concat!(
40 "Sequentially-consistent atomic memory location storing a `",
41 stringify!($value_ty),
42 "` and associated ghost state."
43 )]
44 pub struct $at_ident<K, G, Pred>
48 {
50 #[doc(hidden)]
51 pub patomic: $patomic_ty,
52
53 #[doc(hidden)]
54 pub atomic_inv: Tracked<AtomicInvariant<(K, int), ($perm_ty, G), $atomic_pred_ty<Pred>>>,
55 }
56
57 impl<K, G, Pred> $at_ident<K, G, Pred> {
58 pub open spec fn constant(&self) -> K {
59 self.atomic_inv@.constant().0
60 }
61
62 pub open spec fn well_formed(&self) -> bool {
63 self.atomic_inv@.constant().1 == self.patomic.id()
64 && self.atomic_inv@.namespace() == ATOMIC_NS
65 }
66 }
67
68 impl<K, G, Pred> $at_ident<K, G, Pred>
69 where Pred: AtomicInvariantPredicate<K, $value_ty, G>
70 {
71 #[inline(always)]
72 pub const fn new(Ghost(k): Ghost<K>, u: $value_ty, Tracked(g): Tracked<G>) -> (t: Self)
73 requires Pred::atomic_inv(k, u, g),
74 ensures t.well_formed() && t.constant() == k,
75 {
76 let (patomic, Tracked(perm)) = $patomic_ty::new(u);
77
78 let tracked pair = (perm, g);
79 assert(Pred::atomic_inv(k, u, g));
80 assert(perm.view().patomic == patomic.id());
81 let tracked atomic_inv = AtomicInvariant::new(
82 (k, patomic.id()), pair, 0);
83
84 $at_ident {
85 patomic,
86 atomic_inv: Tracked(atomic_inv),
87 }
88 }
89
90 #[inline(always)]
91 pub fn load(&self) -> $value_ty
92 requires self.well_formed(),
93 {
94 atomic_with_ghost!(self => load(); g => { })
95 }
96
97 #[inline(always)]
98 pub fn into_inner(self) -> (res: ($value_ty, Tracked<G>))
99 requires self.well_formed(),
100 ensures Pred::atomic_inv(self.constant(), res.0, res.1@),
101 {
102 let Self { patomic, atomic_inv: Tracked(atomic_inv) } = self;
103 let tracked (perm, g) = atomic_inv.into_inner();
104 let v = patomic.into_inner(Tracked(perm));
105 (v, Tracked(g))
106 }
107 }
108
109 }
110 };
111}
112macro_rules! declare_atomic_type_generic {
113 ($at_ident:ident, $patomic_ty:ident, $perm_ty:ty, $value_ty: ty, $atomic_pred_ty: ident) => {
114 verus!{
115
116 pub struct $atomic_pred_ty<T, Pred> { t: T, p: Pred }
117
118 impl<T, K, G, Pred> InvariantPredicate<(K, int), ($perm_ty, G)> for $atomic_pred_ty<T, Pred>
119 where Pred: AtomicInvariantPredicate<K, $value_ty, G>
120 {
121 #[verifier::prophetic]
122 open spec fn inv(k_loc: (K, int), perm_g: ($perm_ty, G)) -> bool {
123 let (k, loc) = k_loc;
124 let (perm, g) = perm_g;
125
126 perm.view().patomic == loc
127 && Pred::atomic_inv(k, perm.view().value, g)
128 }
129 }
130
131 #[doc = concat!(
132 "Sequentially-consistent atomic memory location storing a `",
133 stringify!($value_ty),
134 "` and associated ghost state."
135 )]
136 pub struct $at_ident<T, K, G, Pred>
140 {
142 #[doc(hidden)]
143 pub patomic: $patomic_ty<T>,
144
145 #[doc(hidden)]
146 pub atomic_inv: Tracked<AtomicInvariant<(K, int), ($perm_ty, G), $atomic_pred_ty<T, Pred>>>,
147 }
148
149 impl<T, K, G, Pred> $at_ident<T, K, G, Pred>
150 where Pred: AtomicInvariantPredicate<K, $value_ty, G>
151 {
152 pub open spec fn well_formed(&self) -> bool {
153 self.atomic_inv@.constant().1 == self.patomic.id()
154 }
155
156 pub open spec fn constant(&self) -> K {
157 self.atomic_inv@.constant().0
158 }
159
160 #[inline(always)]
161 pub const fn new(Ghost(k): Ghost<K>, u: $value_ty, Tracked(g): Tracked<G>) -> (t: Self)
162 requires Pred::atomic_inv(k, u, g),
163 ensures t.well_formed() && t.constant() == k,
164 {
165
166 let (patomic, Tracked(perm)) = $patomic_ty::<T>::new(u);
167
168 let tracked pair = (perm, g);
169 let tracked atomic_inv = AtomicInvariant::new(
170 (k, patomic.id()), pair, 0);
171
172 $at_ident {
173 patomic,
174 atomic_inv: Tracked(atomic_inv),
175 }
176 }
177
178 #[inline(always)]
179 pub fn load(&self) -> $value_ty
180 requires self.well_formed(),
181 {
182 atomic_with_ghost!(self => load(); g => { })
183 }
184
185 #[inline(always)]
186 pub fn into_inner(self) -> (res: ($value_ty, Tracked<G>))
187 requires self.well_formed(),
188 ensures Pred::atomic_inv(self.constant(), res.0, res.1@),
189 {
190 let Self { patomic, atomic_inv: Tracked(atomic_inv) } = self;
191 let tracked (perm, g) = atomic_inv.into_inner();
192 let v = patomic.into_inner(Tracked(perm));
193 (v, Tracked(g))
194 }
195 }
196
197 }
198 };
199}
200
201#[cfg(target_has_atomic = "64")]
202declare_atomic_type!(AtomicU64, PAtomicU64, PermissionU64, u64, AtomicPredU64);
203
204declare_atomic_type!(AtomicU32, PAtomicU32, PermissionU32, u32, AtomicPredU32);
205declare_atomic_type!(AtomicU16, PAtomicU16, PermissionU16, u16, AtomicPredU16);
206declare_atomic_type!(AtomicU8, PAtomicU8, PermissionU8, u8, AtomicPredU8);
207declare_atomic_type!(AtomicUsize, PAtomicUsize, PermissionUsize, usize, AtomicPredUsize);
208
209#[cfg(target_has_atomic = "64")]
210declare_atomic_type!(AtomicI64, PAtomicI64, PermissionI64, i64, AtomicPredI64);
211
212declare_atomic_type!(AtomicI32, PAtomicI32, PermissionI32, i32, AtomicPredI32);
213declare_atomic_type!(AtomicI16, PAtomicI16, PermissionI16, i16, AtomicPredI16);
214declare_atomic_type!(AtomicI8, PAtomicI8, PermissionI8, i8, AtomicPredI8);
215declare_atomic_type!(AtomicIsize, PAtomicIsize, PermissionIsize, isize, AtomicPredIsize);
216
217declare_atomic_type!(AtomicBool, PAtomicBool, PermissionBool, bool, AtomicPredBool);
218
219declare_atomic_type_generic!(AtomicPtr, PAtomicPtr, PermissionPtr<T>, *mut T, AtomicPredPtr);
220
221#[macro_export]
333macro_rules! atomic_with_ghost {
334 ($($tokens:tt)*) => {
335 $crate::vstd::prelude::atomic_with_ghost_helper!(
338 $crate::vstd::atomic_ghost::atomic_with_ghost_inner,
339 $($tokens)*)
340 }
341}
342
343pub use atomic_with_ghost;
344
345#[doc(hidden)]
346#[macro_export]
347macro_rules! atomic_with_ghost_inner {
348 (load, $e:expr, (), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
349 $crate::vstd::atomic_ghost::atomic_with_ghost_load!($e, $prev, $next, $ret, $g, $b)
350 };
351 (store, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
352 $crate::vstd::atomic_ghost::atomic_with_ghost_store!(
353 $e, $operand, $prev, $next, $ret, $g, $b
354 )
355 };
356 (swap, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
357 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
358 swap, $e, $operand, $prev, $next, $ret, $g, $b
359 )
360 };
361
362 (fetch_or, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
363 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
364 fetch_or, $e, $operand, $prev, $next, $ret, $g, $b
365 )
366 };
367 (fetch_and, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
368 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
369 fetch_and, $e, $operand, $prev, $next, $ret, $g, $b
370 )
371 };
372 (fetch_xor, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
373 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
374 fetch_xor, $e, $operand, $prev, $next, $ret, $g, $b
375 )
376 };
377 (fetch_nand, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
378 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
379 fetch_nand, $e, $operand, $prev, $next, $ret, $g, $b
380 )
381 };
382 (fetch_max, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
383 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
384 fetch_max, $e, $operand, $prev, $next, $ret, $g, $b
385 )
386 };
387 (fetch_min, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
388 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
389 fetch_min, $e, $operand, $prev, $next, $ret, $g, $b
390 )
391 };
392 (fetch_add_wrapping, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
393 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
394 fetch_add_wrapping,
395 $e,
396 $operand,
397 $prev,
398 $next,
399 $ret,
400 $g,
401 $b
402 )
403 };
404 (fetch_sub_wrapping, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
405 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_1_operand!(
406 fetch_sub_wrapping,
407 $e,
408 $operand,
409 $prev,
410 $next,
411 $ret,
412 $g,
413 $b
414 )
415 };
416
417 (fetch_add, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
418 $crate::vstd::atomic_ghost::atomic_with_ghost_update_fetch_add!(
419 $e, $operand, $prev, $next, $ret, $g, $b
420 )
421 };
422 (fetch_sub, $e:expr, ($operand:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
423 $crate::vstd::atomic_ghost::atomic_with_ghost_update_fetch_sub!(
424 $e, $operand, $prev, $next, $ret, $g, $b
425 )
426 };
427
428 (compare_exchange, $e:expr, ($operand1:expr, $operand2:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
429 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_2_operand!(
430 compare_exchange,
431 $e,
432 $operand1,
433 $operand2,
434 $prev,
435 $next,
436 $ret,
437 $g,
438 $b
439 )
440 };
441 (compare_exchange_weak, $e:expr, ($operand1:expr, $operand2:expr), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
442 $crate::vstd::atomic_ghost::atomic_with_ghost_update_with_2_operand!(
443 compare_exchange_weak,
444 $e,
445 $operand1,
446 $operand2,
447 $prev,
448 $next,
449 $ret,
450 $g,
451 $b
452 )
453 };
454 (no_op, $e:expr, (), $prev:pat, $next:pat, $ret:pat, $g:ident, $b:block) => {
455 $crate::vstd::atomic_ghost::atomic_with_ghost_no_op!($e, $prev, $next, $ret, $g, $b)
456 };
457}
458
459pub use atomic_with_ghost_inner;
460
461#[doc(hidden)]
462#[macro_export]
463macro_rules! atomic_with_ghost_store {
464 ($e:expr, $operand:expr, $prev:pat, $next:pat, $res:pat, $g:ident, $b:block) => {
465 $crate::vstd::prelude::verus_exec_expr! { {
466 let atomic = &($e);
467 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
468 #[allow(unused_mut)]
469 let tracked (mut perm, mut $g) = pair;
470 let ghost $prev = perm.view().value;
471 atomic.patomic.store(Tracked(&mut perm), $operand);
472 let ghost $next = perm.view().value;
473 let ghost $res = ();
474
475 proof { $b }
476
477 proof { pair = (perm, $g); }
478 });
479 } }
480 };
481}
482pub use atomic_with_ghost_store;
483
484#[doc(hidden)]
485#[macro_export]
486macro_rules! atomic_with_ghost_load {
487 ($e:expr, $prev:pat, $next: pat, $res: pat, $g:ident, $b:block) => {
488 $crate::vstd::prelude::verus_exec_expr! { {
489 let result;
490 let atomic = &($e);
491 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
492 #[allow(unused_mut)]
493 let tracked (perm, mut $g) = pair;
494 result = atomic.patomic.load(Tracked(&perm));
495 let ghost $res = result;
496 let ghost $prev = result;
497 let ghost $next = result;
498
499 proof { $b }
500
501 proof { pair = (perm, $g); }
502 });
503 result
504 } }
505 };
506}
507
508pub use atomic_with_ghost_load;
509
510#[doc(hidden)]
511#[macro_export]
512macro_rules! atomic_with_ghost_no_op {
513 ($e:expr, $prev:pat, $next: pat, $res: pat, $g:ident, $b:block) => {
514 $crate::vstd::prelude::verus_exec_expr! { {
515 let atomic = &($e);
516 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
517 #[allow(unused_mut)]
518 let tracked (perm, mut $g) = pair;
519 let ghost result = perm.view().value;
520 let ghost $res = result;
521 let ghost $prev = result;
522 let ghost $next = result;
523
524 proof { $b }
525
526 proof { pair = (perm, $g); }
527 });
528 } }
529 };
530}
531
532pub use atomic_with_ghost_no_op;
533
534#[doc(hidden)]
535#[macro_export]
536macro_rules! atomic_with_ghost_update_with_1_operand {
537 ($name:ident, $e:expr, $operand:expr, $prev:pat, $next:pat, $res: pat, $g:ident, $b:block) => {
538 $crate::vstd::prelude::verus_exec_expr! { {
539 let result;
540 let atomic = &($e);
541 let operand = $operand;
542 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
543 #[allow(unused_mut)]
544 let tracked (mut perm, mut $g) = pair;
545 let ghost $prev = perm.view().value;
546 result = atomic.patomic.$name(Tracked(&mut perm), operand);
547 let ghost $res = result;
548 let ghost $next = perm.view().value;
549
550 proof { $b }
551
552 proof { pair = (perm, $g); }
553 });
554 result
555 } }
556 };
557}
558
559pub use atomic_with_ghost_update_with_1_operand;
560
561#[doc(hidden)]
562#[macro_export]
563macro_rules! atomic_with_ghost_update_with_2_operand {
564 ($name:ident, $e:expr, $operand1:expr, $operand2:expr, $prev:pat, $next:pat, $res: pat, $g:ident, $b:block) => {
565 $crate::vstd::prelude::verus_exec_expr! { {
566 let result;
567 let atomic = &($e);
568 let operand1 = $operand1;
569 let operand2 = $operand2;
570 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
571 #[allow(unused_mut)]
572 let tracked (mut perm, mut $g) = pair;
573 let ghost $prev = perm.view().value;
574 result = atomic.patomic.$name(Tracked(&mut perm), operand1, operand2);
575 let ghost $res = result;
576 let ghost $next = perm.view().value;
577
578 proof { $b }
579
580 proof { pair = (perm, $g); }
581 });
582 result
583 } }
584 };
585}
586
587pub use atomic_with_ghost_update_with_2_operand;
588
589#[doc(hidden)]
590#[macro_export]
591macro_rules! atomic_with_ghost_update_fetch_add {
592 ($e:expr, $operand:expr, $prev:pat, $next:pat, $res: pat, $g:ident, $b:block) => {
593 ($crate::vstd::prelude::verus_exec_expr!( {
594 let result;
595 let atomic = &($e);
596 let operand = $operand;
597 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
598 #[allow(unused_mut)]
599 let tracked (mut perm, mut $g) = pair;
600
601 proof {
602 let $prev = perm.view().value as int;
603 let $res = perm.view().value as int;
604 let $next = perm.view().value as int + (operand as int);
605
606 { $b }
607 }
608
609 result = atomic.patomic.fetch_add(Tracked(&mut perm), operand);
610
611 proof { pair = (perm, $g); }
612 });
613 result
614 } ))
615 }
616}
617
618pub use atomic_with_ghost_update_fetch_add;
619
620#[doc(hidden)]
621#[macro_export]
622macro_rules! atomic_with_ghost_update_fetch_sub {
623 ($e:expr, $operand:expr, $prev:pat, $next:pat, $res: pat, $g:ident, $b:block) => {
624 $crate::vstd::prelude::verus_exec_expr! { {
625 let result;
626 let atomic = &($e);
627 let operand = $operand;
628 $crate::vstd::invariant::open_atomic_invariant!(atomic.atomic_inv.borrow() => pair => {
629 #[allow(unused_mut)]
630 let tracked (mut perm, mut $g) = pair;
631
632 proof {
633 let $prev = perm.view().value as int;
634 let $res = perm.view().value as int;
635 let $next = perm.view().value as int - (operand as int);
636
637 { $b }
638 }
639
640 result = atomic.patomic.fetch_sub(Tracked(&mut perm), operand);
641
642 proof { pair = (perm, $g); }
643 });
644 result
645 } }
646 };
647}
648
649pub use atomic_with_ghost_update_fetch_sub;