- #!/usr/bin/perl
- use 5.14.0;
- use strict;
- use warnings;
- use threads::shared;
- use Thread::Queue;
- use Thread::Semaphore;
- my $clear = `clear && echo`;
- my $library = "/home/sir/Music/Songbird Music";
- my $logf = "$ENV{HOME}/flac_test.log";
- my $n :shared = 0;
- my @err :shared;
- my $end :shared;
- my %flac :shared;
- my %done :shared;
- my $total :shared = 0;
- my $disk_size = 1000000000;
- # This will be used to control access to the logger subroutine.
- my $semaphore = Thread::Semaphore->new();
- my $semaphore2 = Thread::Semaphore->new();
- my $semaphore3 = Thread::Semaphore->new();
- # Creating the thread queue.
- my $q = Thread::Queue->new();
- $LOG->autoflush(1); # Make the $LOG file handle unbuffered for instant logging.
- #sub msay {
- # my @fh;
- # push(@fh, shift) while (ref($_[0]) eq 'GLOB');
- # say $_ @_ foreach (@fh);
- #}
- sub getdirs {
- my $dn = $library;
- my @dirs;
- }
- sub getfiles {
- my (@files, $fn);
- if (/.flac$/) {
- $fn = $dn . '/' . $_;
- }
- }
- }
- #### Subroutine for controlling the log file
- sub logger {
- $semaphore->down();
- my($arg, $sw, $fn, $n);
- #### Loop through all the arguments passed to this subroutine
- #### Perform checks that decide which variable the arguments are to be assigned to
- while (@_) {
- foreach (@larg) {
- if ($_ eq $arg) { $sw = $arg }
- }
- #### If $arg is a number assign it to $n, if it's a file assign it to $fn
- if ($arg =~ /^[0-9]*$/) { $n = $arg; }
- else { $fn = $arg; }
- }
- given ($sw) {
- #### Starts writing the log
- when ('start') {
- say $LOG "\n**** Logging started on $now ****\n";
- }
- #### Called when file has been changed
- when ('change') {
- say $LOG $fn . "\n\t" . "has been corrupted.\n";
- lock(@err);
- }
- #### Called when done, and to close the log
- #### If no errors occured write "Everything is OK!" to the log
- #### If errors occurred print the @err array
- #### Either way, print number of files processed
- when ('end') {
- if (!@err) {
- say $LOG "\nEverything is OK!\n";
- } else {
- foreach my $fn (@err) {
- say $SE "ERROR: " . $fn;
- }
- }
- say $LOG $n . " file(s) were tested.\n" if ($n);
- say $LOG "\n**** Logging ended on $now ****\n";
- }
- }
- $semaphore->up();
- }
- sub ram {
- $semaphore2->down();
- $done{$fn} = 1;
- $semaphore2->up();
- }
- sub test {
- while (!$q->pending() && !$end) {
- yield();
- }
- my $tid = threads->tid();
- LOOP: while (my $fn = $q->dequeue_nb()) {
- say "thread${tid} $fn: testing...";
- my $status = ${^CHILD_ERROR_NATIVE};
- if ($status != 0 && $status != 2) {
- say $pid;
- logger('change', $fn);
- }
- $semaphore3->down();
- $n++;
- $semaphore3->up();
- }
- if (!$q->pending() && !$end) {
- say "thread${tid}: yielding!";
- yield();
- }
- }
- say "Starting threads";
- my @threads;
- foreach (1 .. $cores) {
- }
- logger('start');
- foreach my $dn (getdirs) {
- my @files = getfiles($dn);
- foreach my $fn (@files) {
- my $running_threads = threads->list(threads::running);
- my $pending = $q->pending();
- say "$running_threads running threads : $pending pending files : $total bytes in RAM";
- ram($fn);
- # If the RAM disk is full, put decoded files in the queue,
- # and then yield until the LAME threads have cleared some RAM.
- while ($total > $disk_size) {
- $q->enqueue($fn);
- lock(%done);
- }
- yield();
- }
- }
- $q->enqueue($fn);
- lock(%done);
- }
- }
- $end = 1;
- say("All threads joined");
- logger('end', $n);
flac test threaded
Posted by Anonymous on Mon 17th Sep 2012 21:59
raw | new post
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.