Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark events as read during EventReader::par_read #13836

Merged
merged 8 commits into from
Jun 25, 2024
8 changes: 7 additions & 1 deletion crates/bevy_ecs/src/event/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub struct EventParIter<'a, E: Event> {
reader: &'a mut ManualEventReader<E>,
slices: [&'a [EventInstance<E>]; 2],
batching_strategy: BatchingStrategy,
unread: usize,
}

impl<'a, E: Event> EventParIter<'a, E> {
Expand All @@ -168,6 +169,7 @@ impl<'a, E: Event> EventParIter<'a, E> {
reader,
slices: [a, b],
batching_strategy: BatchingStrategy::default(),
unread: unread_count,
}
}

Expand Down Expand Up @@ -203,7 +205,7 @@ impl<'a, E: Event> EventParIter<'a, E> {
/// initialized and run from the ECS scheduler, this should never panic.
///
/// [`ComputeTaskPool`]: bevy_tasks::ComputeTaskPool
pub fn for_each_with_id<FN: Fn(&'a E, EventId<E>) + Send + Sync + Clone>(self, func: FN) {
pub fn for_each_with_id<FN: Fn(&'a E, EventId<E>) + Send + Sync + Clone>(mut self, func: FN) {
#[cfg(any(target_arch = "wasm32", not(feature = "multi_threaded")))]
{
self.into_iter().for_each(|(e, i)| func(e, i));
Expand Down Expand Up @@ -233,6 +235,10 @@ impl<'a, E: Event> EventParIter<'a, E> {
});
}
});

// If we get to hear we're guaranteed to have seen all events
BobG1983 marked this conversation as resolved.
Show resolved Hide resolved
self.reader.last_event_count += self.unread;
self.unread = 0;
}
}

Expand Down
37 changes: 20 additions & 17 deletions crates/bevy_ecs/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,30 +421,33 @@ mod tests {
#[cfg(feature = "multi_threaded")]
#[test]
fn test_events_par_iter() {
use std::{collections::HashSet, sync::mpsc};

use crate::prelude::*;
use std::sync::atomic::{AtomicUsize, Ordering};

#[derive(Resource)]
struct Counter(AtomicUsize);

let mut world = World::new();
world.init_resource::<Events<TestEvent>>();
BobG1983 marked this conversation as resolved.
Show resolved Hide resolved
for i in 0..100 {
world.send_event(TestEvent { i });
for _ in 0..100 {
world.send_event(TestEvent { i: 1 });
}

let mut schedule = Schedule::default();
schedule.add_systems(
|mut events: EventReader<TestEvent>, counter: ResMut<Counter>| {
events.par_read().for_each(|event| {
counter.0.fetch_add(event.i, Ordering::Relaxed);
});
},
);
world.insert_resource(Counter(AtomicUsize::new(0)));
schedule.run(&mut world);
let counter = world.remove_resource::<Counter>().unwrap();
assert_eq!(counter.0.into_inner(), 100);

schedule.add_systems(|mut events: EventReader<TestEvent>| {
let (tx, rx) = mpsc::channel();
events.par_read().for_each(|event| {
tx.send(event.i).unwrap();
});
drop(tx);

let observed: HashSet<_> = rx.into_iter().collect();
assert_eq!(observed, HashSet::from_iter(0..100));
});
world.insert_resource(Counter(AtomicUsize::new(0)));
schedule.run(&mut world);
let counter = world.remove_resource::<Counter>().unwrap();
assert_eq!(counter.0.into_inner(), 0);
}

// Peak tests
}