Author: paul
Date: 2007-05-16 06:59:34 +0200 (Wed, 16 May 2007)
New Revision: 6487
Log:
r20440@frost: paul | 2007-05-15 23:59:21 -0500
Add locking to idmap file generation so that multiple idmap.pl
processes invoked in parallel don't stomp on each other's work.
(WL#3801)
Modified:
trunk/tools/idmap.pl
Property changes on: trunk
___________________________________________________________________
Name: svk:merge
- 4767c598-dc10-0410-bea0-d01b485662eb:/mysqldoc-local/mysqldoc/trunk:25025
7d8d2c4e-af1d-0410-ab9f-b038ce55645b:/mysqldoc-local/mysqldoc:20438
b5ec3a16-e900-0410-9ad2-d183a3acac99:/mysqldoc-local/mysqldoc/trunk:14218
bf112a9c-6c03-0410-a055-ad865cd57414:/mysqldoc-local/mysqldoc/trunk:17229
+ 4767c598-dc10-0410-bea0-d01b485662eb:/mysqldoc-local/mysqldoc/trunk:25025
7d8d2c4e-af1d-0410-ab9f-b038ce55645b:/mysqldoc-local/mysqldoc:20440
b5ec3a16-e900-0410-9ad2-d183a3acac99:/mysqldoc-local/mysqldoc/trunk:14218
bf112a9c-6c03-0410-a055-ad865cd57414:/mysqldoc-local/mysqldoc/trunk:17229
Modified: trunk/tools/idmap.pl
===================================================================
--- trunk/tools/idmap.pl 2007-05-16 04:57:47 UTC (rev 6486)
+++ trunk/tools/idmap.pl 2007-05-16 04:59:34 UTC (rev 6487)
Changed blocks: 3, Lines Added: 52, Lines Deleted: 0; 2146 bytes
@@ -115,6 +115,7 @@
XML::Parser::PerlSAX->new->parse(Source => { SystemId => $file},
Handler => $my_handler);
+ lockfile ($maptempfile);
my $dest = IO::File->new($maptempfile,'w');
die "Couldn't open map destination file ($maptempfile): $!" unless (defined($dest));
@@ -146,6 +147,7 @@
unlink($mapfile);
rename($maptempfile,$mapfile);
+ unlockfile ($maptempfile);
delete($filemap->{$file});
}
@@ -176,6 +178,56 @@
}
}
+# ----------------------------------------------------------------------
+
+use Fcntl qw(:flock);
+
+# lockfile ($filename)
+
+# Acquires a lockfile for the file passed as an argument.
+# (For argument "abc", uses a lockfile "abc.lock".)
+# If another process already holds the lock, lockfile() blocks
+# until the lock is available.
+
+# unlockfile ($filename)
+
+# Removes the lockfile associated with $filename and unlocks its file
+# handle. It is possible for another process to get a lock on the
+# lockfile between the unlink and the unlock, but that doesn't matter
+# if the current program has closed $filename (the file for which it
+# obtained the lock).
+
+# Intended use for locking a given ID map file:
+
+# $mapfile = "xxx.idmap"; # some ID map file name
+# lockfile ($mapfile)
+# <open $mapfile for writing>
+# <write contents of $mapfile>
+# <close $mapfile>
+# unlockfile ($mapfile);
+
+# Assumptions:
+# lockfile() and unlockfile() use a filehandle named LOCK, so it is
+# assumed that:
+# - The program will not use this filehandle elsewhere
+# - The program will lock ONLY ONE file at a time
+
+sub lockfile
+{
+my $lockfile = $_[0] . ".lock";
+
+ open (LOCK, ">$lockfile") or die "Cannot open $lockfile: $!\n";
+ flock (LOCK, LOCK_EX) or die "Cannot lock $lockfile: $!\n";
+}
+
+sub unlockfile
+{
+my $lockfile = $_[0] . ".lock";
+
+ unlink ($lockfile);
+ flock (LOCK, LOCK_UN);
+}
+
# This is not used, but retained here for reference
package MyDTDHandler;
| Thread |
|---|
| • svn commit - mysqldoc@docsrva: r6487 - in trunk: . tools | paul | 16 May |