diff --git a/crates/bevy_ecs/src/system/exclusive_function_system.rs b/crates/bevy_ecs/src/system/exclusive_function_system.rs index 349f48af2e..08b4b678bd 100644 --- a/crates/bevy_ecs/src/system/exclusive_function_system.rs +++ b/crates/bevy_ecs/src/system/exclusive_function_system.rs @@ -124,9 +124,7 @@ where let out = self.func.run(world, input, params); world.flush(); - let change_tick = world.change_tick.get_mut(); - self.system_meta.last_run.set(*change_tick); - *change_tick = change_tick.wrapping_add(1); + self.system_meta.last_run = world.increment_change_tick(); out }) diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index f47aa55ae5..c7a66c86a7 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -2063,9 +2063,16 @@ impl World { } /// Increments the world's current change tick and returns the old value. + /// + /// If you need to call this method, but do not have `&mut` access to the world, + /// consider using [`as_unsafe_world_cell_readonly`](Self::as_unsafe_world_cell_readonly) + /// to obtain an [`UnsafeWorldCell`] and calling [`increment_change_tick`](UnsafeWorldCell::increment_change_tick) on that. + /// Note that this *can* be done in safe code, despite the name of the type. #[inline] - pub fn increment_change_tick(&self) -> Tick { - let prev_tick = self.change_tick.fetch_add(1, Ordering::AcqRel); + pub fn increment_change_tick(&mut self) -> Tick { + let change_tick = self.change_tick.get_mut(); + let prev_tick = *change_tick; + *change_tick = change_tick.wrapping_add(1); Tick::new(prev_tick) } diff --git a/crates/bevy_ecs/src/world/unsafe_world_cell.rs b/crates/bevy_ecs/src/world/unsafe_world_cell.rs index 8de3d3af3e..bbcc4b58ba 100644 --- a/crates/bevy_ecs/src/world/unsafe_world_cell.rs +++ b/crates/bevy_ecs/src/world/unsafe_world_cell.rs @@ -276,7 +276,10 @@ impl<'w> UnsafeWorldCell<'w> { pub fn increment_change_tick(self) -> Tick { // SAFETY: // - we only access world metadata - unsafe { self.world_metadata() }.increment_change_tick() + let change_tick = unsafe { &self.world_metadata().change_tick }; + // NOTE: We can used a relaxed memory ordering here, since nothing + // other than the atomic value itself is relying on atomic synchronization + Tick::new(change_tick.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) } /// Provides unchecked access to the internal data stores of the [`World`].