Skip to content

Commit

Permalink
make processing run on the main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
jupyterkat committed Jul 3, 2023
1 parent 851412b commit dabb9fe
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 224 deletions.
76 changes: 8 additions & 68 deletions src/turfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,6 @@ fn shutdown_turfs() {
*PLANETARY_ATMOS.write() = None;
}

fn set_turfs_dirty(b: bool) {
ANY_TURF_DIRTY.store(b, std::sync::atomic::Ordering::Relaxed);
}

fn check_turfs_dirty() -> bool {
ANY_TURF_DIRTY.load(std::sync::atomic::Ordering::Relaxed)
}
Expand All @@ -423,29 +419,13 @@ where
f(TURF_GASES.read().as_ref().unwrap())
}

fn with_turf_gases_read_fallible<T, F>(f: F) -> T
where
F: FnOnce(Option<&TurfGases>) -> T,
{
let turfs = TURF_GASES.try_read_for(std::time::Duration::from_millis(30));
f(turfs.as_ref().unwrap().as_ref())
}

fn with_turf_gases_write<T, F>(f: F) -> T
where
F: FnOnce(&mut TurfGases) -> T,
{
f(TURF_GASES.write().as_mut().unwrap())
}

fn with_dirty_turfs<T, F>(f: F) -> T
where
F: FnOnce(&mut IndexMap<TurfID, DirtyFlags, FxBuildHasher>) -> T,
{
set_turfs_dirty(true);
f(DIRTY_TURFS.lock().as_mut().unwrap())
}

fn with_planetary_atmos<T, F>(f: F) -> T
where
F: FnOnce(&IndexMap<u32, Mixture, FxBuildHasher>) -> T,
Expand All @@ -460,35 +440,16 @@ where
f(PLANETARY_ATMOS.upgradable_read())
}

fn rebuild_turf_graph() -> Result<(), Runtime> {
with_dirty_turfs(|dirty_turfs| {
for (&t, _) in dirty_turfs
.iter()
.filter(|&(_, &flags)| flags.contains(DirtyFlags::DIRTY_MIX_REF))
{
register_turf(t)?;
}
for (t, _) in dirty_turfs
.drain(..)
.filter(|&(_, flags)| flags.contains(DirtyFlags::DIRTY_ADJACENT))
{
update_adjacency_info(t)?;
}
Ok(())
})?;
set_turfs_dirty(false);
Ok(())
}

fn register_turf(id: u32) -> Result<(), Runtime> {
let src = unsafe { Value::turf_by_id_unchecked(id) };
#[hook("/turf/proc/update_air_ref")]
fn hook_register_turf() {
let id = unsafe { src.raw.data.id };
let flag = determine_turf_flag(&src);
if let Ok(blocks) = src.get_number(byond_string!("blocks_air")) {
if blocks > 0.0 {
with_turf_gases_write(|arena| arena.remove_turf(id));
#[cfg(feature = "superconductivity")]
superconduct::supercond_update_ref(src)?;
return Ok(());
return Ok(Value::null());
}
}
if flag >= 0 {
Expand Down Expand Up @@ -543,18 +504,6 @@ fn register_turf(id: u32) -> Result<(), Runtime> {

#[cfg(feature = "superconductivity")]
superconduct::supercond_update_ref(src)?;

Ok(())
}

#[hook("/turf/proc/update_air_ref")]
fn hook_register_turf() {
with_dirty_turfs(|dirty_turfs| {
dirty_turfs
.entry(unsafe { src.raw.data.id })
.or_default()
.insert(DirtyFlags::DIRTY_MIX_REF);
});
Ok(Value::null())
}

Expand Down Expand Up @@ -583,10 +532,11 @@ fn determine_turf_flag(src: &Value) -> i32 {
}
}

fn update_adjacency_info(id: u32) -> Result<(), Runtime> {
let src_turf = unsafe { Value::turf_by_id_unchecked(id) };
#[hook("/turf/proc/__update_auxtools_turf_adjacency_info")]
fn hook_infos() {
let id = unsafe { src.raw.data.id };
with_turf_gases_write(|arena| -> Result<(), Runtime> {
if let Ok(adjacent_list) = src_turf.get_list(byond_string!("atmos_adjacent_turfs")) {
if let Ok(adjacent_list) = src.get_list(byond_string!("atmos_adjacent_turfs")) {
arena.update_adjacencies(id, adjacent_list)?;
} else if let Some(&idx) = arena.map.get(&id) {
arena.remove_adjacencies(idx);
Expand All @@ -596,16 +546,6 @@ fn update_adjacency_info(id: u32) -> Result<(), Runtime> {

#[cfg(feature = "superconductivity")]
superconduct::supercond_update_adjacencies(id)?;
Ok(())
}

#[hook("/turf/proc/__update_auxtools_turf_adjacency_info")]
fn hook_infos() {
with_dirty_turfs(|dirty_turfs| -> Result<(), Runtime> {
let e = dirty_turfs.entry(unsafe { src.raw.data.id }).or_default();
e.insert(DirtyFlags::DIRTY_ADJACENT);
Ok(())
})?;
Ok(Value::null())
}

Expand Down
1 change: 0 additions & 1 deletion src/turfs/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub fn send_to_groups(sent: BTreeSet<TurfID>) {

#[hook("/datum/controller/subsystem/air/proc/process_excited_groups_auxtools")]
fn groups_hook(remaining: Value) {
rebuild_turf_graph()?;
let group_pressure_goal = src
.get_number(byond_string!("excited_group_pressure_goal"))
.unwrap_or(0.5);
Expand Down
42 changes: 22 additions & 20 deletions src/turfs/katmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ fn explosively_depressurize(
turfs.insert(initial_index);
while cur_queue_idx < turfs.len() {
let cur_index = turfs[cur_queue_idx];
let mut had_firelock = false;
cur_queue_idx += 1;
let mut firelock_considerations = vec![];
with_turf_gases_read(|arena| -> Result<(), Runtime> {
let cur_mixture = {
let maybe = arena.get(cur_index);
Expand Down Expand Up @@ -368,19 +368,19 @@ fn explosively_depressurize(
if turfs.insert(adj_index)
&& flags.contains(AdjacentFlags::ATMOS_ADJACENT_FIRELOCK)
{
had_firelock = true;
unsafe { Value::turf_by_id_unchecked(cur_mixture.id) }.call(
"consider_firelocks",
&[&unsafe { Value::turf_by_id_unchecked(adj_mixture.id) }],
)?;
firelock_considerations.push((cur_mixture.id, adj_mixture.id));
}
}
}
Ok(())
})?;
if had_firelock {
rebuild_turf_graph()?; // consider_firelocks ought to dirtify it anyway
for (cur, adj) in firelock_considerations {
unsafe { Value::turf_by_id_unchecked(cur) }.call(
"consider_firelocks",
&[&unsafe { Value::turf_by_id_unchecked(adj) }],
)?;
}

if warned_about_planet_atmos {
break;
}
Expand All @@ -392,8 +392,9 @@ fn explosively_depressurize(
return Ok(()); // planet atmos > space
}

with_turf_gases_read(move |arena| {
let floor_rip_turfs = with_turf_gases_read(move |arena| {
let mut info: HashMap<NodeIndex, Cell<ReducedInfo>, FxBuildHasher> = Default::default();
let mut floor_rip_turfs = vec![];

let mut progression_order = space_turfs
.iter()
Expand Down Expand Up @@ -524,10 +525,13 @@ fn explosively_depressurize(
)?;
}

byond_turf.call("handle_decompression_floor_rip", &[&Value::from(sum)])?;
floor_rip_turfs.push((byond_turf, Value::from(sum)));
}
Ok(())
Ok(floor_rip_turfs)
})?;
for (turf, sum) in floor_rip_turfs {
turf.call("handle_decompression_floor_rip", &[&sum])?;
}

Ok(())
}
Expand Down Expand Up @@ -606,8 +610,8 @@ fn planet_equalize(
turfs.insert(initial_index);
while cur_queue_idx < turfs.len() {
let cur_index = turfs[cur_queue_idx];
let mut had_firelock = false;
cur_queue_idx += 1;
let mut firelock_considerations = vec![];
with_turf_gases_read(|arena| -> Result<(), Runtime> {
let cur_mixture = {
let maybe = arena.get(cur_index);
Expand Down Expand Up @@ -636,17 +640,16 @@ fn planet_equalize(
turfs.insert(*adj_index)
&& flags.contains(AdjacentFlags::ATMOS_ADJACENT_FIRELOCK)
}) {
had_firelock = true;
unsafe { Value::turf_by_id_unchecked(cur_mixture.id) }.call(
"consider_firelocks",
&[&unsafe { Value::turf_by_id_unchecked(adj_mixture.id) }],
)?;
firelock_considerations.push((cur_mixture.id, adj_mixture.id));
}
}
Ok(())
})?;
if had_firelock {
rebuild_turf_graph()?; // consider_firelocks ought to dirtify it anyway
for (cur, adj) in firelock_considerations {
unsafe { Value::turf_by_id_unchecked(cur) }.call(
"consider_firelocks",
&[&unsafe { Value::turf_by_id_unchecked(adj) }],
)?;
}
if warned_about_space {
break;
Expand Down Expand Up @@ -747,7 +750,6 @@ fn send_pressure_differences(

#[hook("/datum/controller/subsystem/air/proc/process_turf_equalize_auxtools")]
fn equalize_hook(remaining: Value) {
rebuild_turf_graph()?;
let equalize_hard_turf_limit = src
.get_number(byond_string!("equalize_hard_turf_limit"))
.unwrap_or(2000.0) as usize;
Expand Down
Loading

0 comments on commit dabb9fe

Please sign in to comment.