Author: timbo
Date: Tue Aug 28 08:11:53 2007
New Revision: 9894

Modified:
   dbi/trunk/Changes
   dbi/trunk/lib/DBI/ProfileData.pm
   dbi/trunk/lib/DBI/ProfileDumper.pm

Log:
Add DBI_PROFILE_FLOCK and LockFile attrib in DBI::ProfileData and 
DBI::ProfileDumper


Modified: dbi/trunk/Changes
==============================================================================
--- dbi/trunk/Changes   (original)
+++ dbi/trunk/Changes   Tue Aug 28 08:11:53 2007
@@ -15,6 +15,7 @@
 Or call _new_child and move to DBI::common?
 Add trace modules that just records the last N trace messages into an array
 and prepends them to any error message.
+Document DBI_PROFILE_FLOCK and LockFile attrib in DBI::ProfileData and 
DBI::ProfileDumper
 
 Gofo TODOs:
 

Modified: dbi/trunk/lib/DBI/ProfileData.pm
==============================================================================
--- dbi/trunk/lib/DBI/ProfileData.pm    (original)
+++ dbi/trunk/lib/DBI/ProfileData.pm    Tue Aug 28 08:11:53 2007
@@ -88,6 +88,12 @@
 sub LAST_AT   () { 6 };
 sub PATH      () { 7 };
 
+
+my $HAS_FLOCK = (defined $ENV{DBI_PROFILE_FLOCK})
+    ? $ENV{DBI_PROFILE_FLOCK}
+    : do { local $@; eval { flock STDOUT, 0; 1 } };
+
+
 =head2 $prof = DBI::ProfileData->new(File => "dbi.prof")
 
 =head2 $prof = DBI::ProfileData->new(File => "dbi.prof", Filter => sub { ... })
@@ -161,6 +167,7 @@
                 Files        => [ "dbi.prof" ],
                Filter       => undef,
                 DeleteFiles  => 0,
+                LockFile     => $HAS_FLOCK,
                 _header      => {},
                 _nodes       => [],
                 _node_lookup => {},
@@ -200,7 +207,7 @@
 
         # lock the file in case it's still being written to
         # (we'll be foced to wait till the write is complete)
-        flock($fh, LOCK_SH);
+        flock($fh, LOCK_SH) if $self->{LockFile};
 
         if (-s $fh) {   # not empty
             $self->_read_header($fh, $filename, $read_header ? 0 : 1);

Modified: dbi/trunk/lib/DBI/ProfileDumper.pm
==============================================================================
--- dbi/trunk/lib/DBI/ProfileDumper.pm  (original)
+++ dbi/trunk/lib/DBI/ProfileDumper.pm  Tue Aug 28 08:11:53 2007
@@ -183,13 +183,20 @@
 use Fcntl qw(:flock);
 use Symbol;
 
+my $HAS_FLOCK = (defined $ENV{DBI_PROFILE_FLOCK})
+    ? $ENV{DBI_PROFILE_FLOCK}
+    : do { local $@; eval { flock STDOUT, 0; 1 } };
+
 my $program_header;
 
 
 # validate params and setup default
 sub new {
     my $pkg = shift;
-    my $self = $pkg->SUPER::new(@_);
+    my $self = $pkg->SUPER::new(
+        LockFile => $HAS_FLOCK,
+        @_,
+    );
 
     # provide a default filename
     $self->filename("dbi.prof") unless $self->filename;
@@ -234,7 +241,7 @@
           or croak("Unable to open '$filename' for $class output: $!");
     }
     # lock the file (before checking size and writing the header)
-    flock($fh, LOCK_EX);
+    flock($fh, LOCK_EX) if $self->{LockFile};
     # write header if file is empty - typically because we just opened it
     # in '>' mode, or perhaps we used '>>' but the file had been truncated 
externally.
     if (-s $fh == 0) {

Reply via email to