in reply to Fcntl() and multiple filehandles
our @lockstat; sub readlock { my($fh)=@_; my $fileno = fileno($fh); defined($fileno) or die "Bad filehandle"; if ($lockstat[$fileno] && $lockstat[$fileno]{count}) { $lockstat[$fileno]{count}++; } else { fcntl_lock($fh,F_RDLCK); $lockstat[$fileno]{count}=1; } } sub readunlock { my($fh)=@_; my $fileno = fileno($fh); defined($fileno) or die "Bad filehandle"; return unless $lockstat[$fileno] and $lockstat[$fileno]{count}; if (--$lockstat[$fileno]{count} == 0) { fcntl_lock($fh,F_UNLCK); } }
You can expand this to cover write locks in a similar way (or write a general myflock that does both read, write, and unlocks). If you're using multiple threads, you'll need to use mutexes to protect the data structures from the different threads. You'll also have to think about how to deal with close.
I seem to recall that Perl has a compile-time option to use its own flock implementation based on fcntl; you may want to look into that as well.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Fcntl() and multiple filehandles
by Anonymous Monk on Sep 20, 2004 at 23:58 UTC | |
by sgifford (Prior) on Sep 21, 2004 at 02:35 UTC | |
by Anonymous Monk on Sep 21, 2004 at 11:26 UTC | |
by Anonymous Monk on Sep 21, 2004 at 12:52 UTC | |
by sgifford (Prior) on Sep 21, 2004 at 20:46 UTC |