Mailing List Archive

some doc updates for lib/DB_File.pm
just added the locking code docs i wrote lately stuff.

one thing i found in trying to stick the module abuse section
from the modules file into perlmod is that nested lists and
=head3 are a problem in pod. sigh. too spoiled by html, whcih
i never thought that i would say.

--tom


530a531
> =head2 Locking Databases
532c533,537
< =head1 CHANGES
---
> Concurrent access of a read-write database by several parties requires
> them all to use some kind of locking. Here's an example of Tom's that
> uses the I<fd> method to get the file descriptor, and then a careful
> open() to give something Perl will flock() for you. Run this repeatedly
> in the background to watch the locks granted in proper order.
534c539,540
< =head2 0.1
---
> use Fcntl;
> use DB_File;
535a542,594
> use strict;
>
> sub LOCK_SH { 1 }
> sub LOCK_EX { 2 }
> sub LOCK_NB { 4 }
> sub LOCK_UN { 8 }
>
> my($oldval, $fd, $db, %db, $value, $key);
>
> $key = shift || 'default';
> $value = shift || 'magic';
>
> $value .= " $$";
>
> $db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0644)
> || die "dbcreat /tmp/foo.db $!";
> $fd = $db->fd;
> print "$$: db fd is $fd\n";
> open(DB_FH, "+<&=$fd") || die "dup $!";
>
>
> unless (flock (DB_FH, LOCK_SH | LOCK_NB)) {
> print "$$: CONTENTION; can't read during write update!
> Waiting for read lock ($!) ....";
> unless (flock (DB_FH, LOCK_SH)) { die "flock: $!" }
> }
> print "$$: Read lock granted\n";
>
> $oldval = $db{$key};
> print "$$: Old value was $oldval\n";
> flock(DB_FH, LOCK_UN);
>
> unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {
> print "$$: CONTENTION; must have exclusive lock!
> Waiting for write lock ($!) ....";
> unless (flock (DB_FH, LOCK_EX)) { die "flock: $!" }
> }
>
> print "$$: Write lock granted\n";
> $db{$key} = $value;
> sleep 10;
>
> flock(DB_FH, LOCK_UN);
> untie %db;
> close(DB_FH);
> print "$$: Updated db to $key=$value\n";
>
> =head1 HISTORY
>
> =over
>
> =item 0.1
>
538c597
< =head2 0.2
---
> =item 0.2
543c602
< <grass@cybercash.com> for spotting the bug.
---
> E<lt>grass@cybercash.comE<gt> for spotting the bug.
545c604
< =head2 0.3
---
> =item 0.3
549c608
< =head2 1.0
---
> =item 1.0
559c618
< =head2 1.01
---
> =item 1.01