From 7b76d3c7d843cc2e7bc2f026db46bec7242221dc Mon Sep 17 00:00:00 2001 From: icenine451 Date: Fri, 4 Apr 2025 10:20:00 -0400 Subject: [PATCH] Multi-threaded find_compatible_games function for game compression process --- functions/compression.sh | 24 ++++++++++++++++++++++++ functions/global.sh | 3 +++ 2 files changed, 27 insertions(+) diff --git a/functions/compression.sh b/functions/compression.sh index e755ffb0..8dadc716 100644 --- a/functions/compression.sh +++ b/functions/compression.sh @@ -163,12 +163,20 @@ find_compatible_games() { log d "compression_targets: $compression_targets" while IFS= read -r system; do + while (( $(jobs -p | wc -l) >= $max_threads )); do # Wait for a background task to finish if max_threads has been hit + sleep 0.1 + done + ( log d "Checking system: $system" if [[ -d "$roms_folder/$system" ]]; then local compression_candidates compression_candidates=$(find "$roms_folder/$system" -type f -not -iname "*.txt") if [[ -n "$compression_candidates" ]]; then while IFS= read -r game; do + while (( $(jobs -p | wc -l) >= $max_threads )); do # Wait for a background task to finish if max_threads has been hit + sleep 0.1 + done + ( log d "Checking game: $game" local compatible_compression_format compatible_compression_format=$(find_compatible_compression_format "$game") @@ -181,34 +189,50 @@ find_compatible_games() { "chd") if [[ "$compatible_compression_format" == "chd" ]]; then log d "Game $game is compatible with CHD compression" + ( + flock -x 200 echo "${game}^chd" >> "$output_file" + ) 200>"$RD_FILE_LOCK" fi ;; "zip") if [[ "$compatible_compression_format" == "zip" ]]; then log d "Game $game is compatible with ZIP compression" + ( + flock -x 200 echo "${game}^zip" >> "$output_file" + ) 200>"$RD_FILE_LOCK" fi ;; "rvz") if [[ "$compatible_compression_format" == "rvz" ]]; then log d "Game $game is compatible with RVZ compression" + ( + flock -x 200 echo "${game}^rvz" >> "$output_file" + ) 200>"$RD_FILE_LOCK" fi ;; "all") if [[ "$compatible_compression_format" != "none" ]]; then log d "Game $game is compatible with $compatible_compression_format compression" + ( + flock -x 200 echo "${game}^${compatible_compression_format}" >> "$output_file" + ) 200>"$RD_FILE_LOCK" fi ;; esac + ) & done < <(printf '%s\n' "$compression_candidates") + wait # wait for background tasks to finish fi else log d "Rom folder for $system is missing, skipping" fi + ) & done < <(printf '%s\n' "$compressible_systems_list") + wait # wait for background tasks to finish log d "Compatible games have been written to $output_file" cat "$output_file" diff --git a/functions/global.sh b/functions/global.sh index bb2d7368..07ad6aec 100644 --- a/functions/global.sh +++ b/functions/global.sh @@ -92,6 +92,9 @@ features="$config/retrodeck/reference_lists/features.json" es_systems="/app/share/es-de/resources/systems/linux/es_systems.xml" # ES-DE supported system list es_find_rules="/app/share/es-de/resources/systems/linux/es_find_rules.xml" # ES-DE emulator find rules +# File lock file for multi-threaded write operations to the same file +RD_FILE_LOCK="/tmp/retrodeck_file_lock" + # Godot data transfer temp files godot_bios_files_checked="$XDG_CONFIG_HOME/retrodeck/godot/godot_bios_files_checked.tmp"