A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 85 lines 1.7 kB view raw
1#!/usr/bin/perl 2use List::Util 'shuffle'; # standard from Perl 5.8 and later 3 4my $tempfile = "multigcc.out"; 5my @params; 6my @files; 7my $list = \@params; 8 9# parse command line arguments 10for my $a (@ARGV) { 11 if ($a eq "--") { 12 $list = \@files; 13 next; 14 } 15 16 push @{$list}, $a; 17} 18 19exit if (not @files); 20 21my $command = join " ", @params; 22 23# shuffle the file list to spread the load as evenly as we can 24@files = shuffle(@files); 25 26# count number of cores 27my $cores; 28# Don't use given/when here - it's not compatible with old perl versions 29if ($^O eq 'darwin') { 30 chomp($cores = `sysctl -n hw.ncpu`); 31 $cores = 1 if ($?); 32} 33elsif ($^O eq 'solaris') { 34 $cores = scalar grep /on-line/i, `psrinfo`; 35 $cores = 1 if ($?); 36} 37else { 38 chomp($cores = `/usr/bin/nproc`); 39 $cores = 1 if ($?); 40# if (open CPUINFO, "</proc/cpuinfo") { 41# $cores = scalar grep /^processor/i, <CPUINFO>; 42# close CPUINFO; 43# } 44} 45 46# fork children 47my @pids; 48my $slice = int((scalar @files + $cores) / $cores); 49 50# reset $cores to 0 so we can count the number of actually used cores 51$cores=0; 52 53for (my $i=0;$i<scalar @files;$i += $slice) 54{ 55 my $pid = fork; 56 if ($pid) 57 { 58 # mother 59 $pids[$cores++] = $pid; 60 } 61 else 62 { 63 # get my slice of the files 64 my @list = @files[$i .. $i + $slice - 1]; 65 66 # run command 67 system("$command @list > $tempfile.$$"); 68 69 exit; 70 } 71} 72 73for my $i (0 .. $cores - 1) 74{ 75 # wait for child to complete 76 waitpid $pids[$i], 0; 77 78 # read & print result 79 if (open F, "<$tempfile.$pids[$i]") 80 { 81 print <F>; 82 close F; 83 unlink "$tempfile.$pids[$i]"; 84 } 85}