Skip to main content

vstd/
atomic_ghost.rs

1//! Provides sequentially-consistent atomic memory locations with associated ghost state.
2//! See the [`atomic_with_ghost!`] documentation for more information.
3#![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} // verus!
20macro_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        ///
45        /// See the [`atomic_with_ghost!`] documentation for usage information.
46
47        pub struct $at_ident<K, G, Pred>
48            //where Pred: AtomicInvariantPredicate<K, $value_ty, G>
49        {
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        ///
137        /// See the [`atomic_with_ghost!`] documentation for usage information.
138
139        pub struct $at_ident<T, K, G, Pred>
140            //where Pred: AtomicInvariantPredicate<K, $value_ty, G>
141        {
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/// Performs a given atomic operation on a given atomic
222/// while providing access to its ghost state.
223///
224/// `atomic_with_ghost!` supports the types
225/// [`AtomicU64`] [`AtomicU32`], [`AtomicU16`], [`AtomicU8`],
226/// [`AtomicI64`], [`AtomicI32`], [`AtomicI16`], [`AtomicI8`], and [`AtomicBool`].
227///
228/// For each type, it supports all applicable atomic operations among
229/// `load`, `store`, `swap`, `compare_exchange`, `compare_exchange_weak`,
230/// `fetch_add`, `fetch_add_wrapping`, `fetch_sub`, `fetch_sub_wrapping`,
231/// `fetch_or`, `fetch_and`, `fetch_xor`, `fetch_nand`, `fetch_max`, and `fetch_min`.
232///
233/// Naturally, `AtomicBool` does not support the arithmetic-specific operations.
234///
235/// In general, the syntax is:
236///
237///     let result = atomic_with_ghost!(
238///         $atomic => $operation_name($operands...);
239///         update $prev -> $next;         // `update` line is optional
240///         returning $ret;                // `returning` line is optional
241///         ghost $g => {
242///             /* Proof code with access to `tracked` variable `g: G` */
243///         }
244///     );
245///
246/// Here, the `$operation_name` is one of `load`, `store`, etc. Meanwhile,
247/// `$prev`, `$next`, and `$ret` are all identifiers which
248/// will be available as spec variable inside the block to describe the
249/// atomic action which is performed.
250///
251/// For example, suppose the user performs `fetch_add(1)`. The atomic
252/// operation might load the value 5, add 1, store the value 6,
253/// and return the original value, 5. In that case, we would have
254/// `prev == 5`, `next == 6`, and `ret == 5`.
255///
256/// The specification for a given operation is given as a relation between
257/// `prev`, `next`, and `ret`; that is, at the beginning of the proof block,
258/// the user may assume the given specification holds:
259///
260/// | operation                     | specification                                                                                                              |
261/// |-------------------------------|----------------------------------------------------------------------------------------------------------------------------|
262/// | `load()`                      | `next == prev && rev == prev`                                                                                              |
263/// | `store(x)`                    | `next == x && ret == ()`                                                                                                   |
264/// | `swap(x)`                     | `next == x && ret == prev`                                                                                                 |
265/// | `compare_exchange(x, y)`      | `prev == x && next == y && ret == Ok(prev)` ("success") OR<br> `prev != x && next == prev && ret == Err(prev)` ("failure") |
266/// | `compare_exchange_weak(x, y)` | `prev == x && next == y && ret == Ok(prev)` ("success") OR<br> `next == prev && ret == Err(prev)` ("failure")              |
267/// | `fetch_add(x)` (*)            | `next == prev + x && ret == prev`                                                                                          |
268/// | `fetch_add_wrapping(x)`       | `next == wrapping_add(prev, x) && ret == prev`                                                                             |
269/// | `fetch_sub(x)` (*)            | `next == prev - x && ret == prev`                                                                                          |
270/// | `fetch_sub_wrapping(x)`       | `next == wrapping_sub(prev, x) && ret == prev`                                                                             |
271/// | `fetch_or(x)`                 | <code>next == prev \| x && ret == prev</code>                                                                              |
272/// | `fetch_and(x)`                | `next == prev & x && ret == prev`                                                                                          |
273/// | `fetch_xor(x)`                | `next == prev ^ x && ret == prev`                                                                                          |
274/// | `fetch_nand(x)`               | `next == !(prev & x) && ret == prev`                                                                                       |
275/// | `fetch_max(x)`                | `next == max(prev, x) && ret == prev`                                                                                      |
276/// | `fetch_min(x)`                | `next == min(prev, x) && ret == prev`                                                                                      |
277/// | `no_op()` (**)                | `next == prev && ret == ()`                                                                                                |
278///
279/// (*) Note that `fetch_add` and `fetch_sub` do not specify
280/// wrapping-on-overflow; instead, they require the user to
281/// prove that overflow _does not occur_, i.e., the user must show
282/// that `next` is in bounds for the integer type in question.
283/// Furthermore, for `fetch_add` and `fetch_sub`, the spec values of
284/// `prev`, `next`, and `ret` are all given with type `int`, so the
285/// user may reason about boundedness within the proof block.
286///
287/// (As executable code, `fetch_add` is equivalent to `fetch_add_wrapping`,
288/// and likewise for `fetch_sub` and `fetch_sub_wrapping`.
289/// We have both because it's frequently the case that the user needs to verify
290/// lack-of-overflow _anyway_, and having it as an explicit precondition by default
291/// then makes verification errors easier to diagnose. Furthermore, when overflow is
292/// intended, the wrapping operations document that intent.)
293///
294/// (**) `no_op` is entirely a ghost operation and doesn't emit any actual instruction.
295/// This allows the user to access the ghost state and the stored value (as `spec` data)
296/// without actually performing a load.
297///
298/// ---
299///
300/// At the beginning of the proof block, the user may assume, in addition
301/// to the specified relation between `prev`, `next`, and `ret`, that
302/// `atomic.inv(prev, g)` holds. The user is required to update `g` such that
303/// `atomic.inv(next, g)` holds at the end of the block.
304/// In other words, the ghost block has the implicit pre- and post-conditions:
305///
306///     let result = atomic_with_ghost!(
307///         $atomic => $operation_name($operands...);
308///         update $prev -> $next;
309///         returning $ret;
310///         ghost $g => {
311///             assume(specified relation on (prev, next, ret));
312///             assume(atomic.inv(prev, g));
313///
314///             // User code here; may update variable `g` with full
315///             // access to variables in the outer context.
316///
317///             assert(atomic.inv(next, g));
318///         }
319///     );
320///
321/// Note that the necessary action on ghost state might depend
322/// on the result of the operation; for example, if the user performs a
323/// compare-and-swap, then the ghost action that they then need to do
324/// will probably depend on whether the operation succeeded or not.
325///
326/// The value returned by the `atomic_with_ghost!(...)` expression will be equal
327/// to `ret`, although the return value is an `exec` value (the actual result of
328/// the operation) while `ret` is a `spec` value.
329///
330/// ### Example (TODO)
331
332#[macro_export]
333macro_rules! atomic_with_ghost {
334    ($($tokens:tt)*) => {
335        // The helper is used to parse things using Verus syntax
336        // The helper then calls atomic_with_ghost_inner, below:
337        $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;