Quoth
(dan baker):
> Ben Morrow <> wrote in message
>
> > This is a perfectly sensible way to do the locking, but the
> > implementation is AWFUL. Do you understand the concept of an 'atomic
> > operation', and why it is relevant to locking?
> ---------
> I'm always open to learn... but remember that one of my conditions was
> to have this code work on a PC. atomic, flocking, and thread control
> is kinda out the window. unless you have better ways to do it in a
> Windows environment?
If your locking operation isn't atomic there is no point locking. You've
got a race condition anyway so you might as well go ahead and do
whatever without locks and pray.
> > Whitespace is free. Please use it. Especially newlines and decent
> > indentation.
> -------
> sorry, but posting source via yahoo does weird things to indents.
Get a decent newsreader then.
> > not sysopen $LCK, $LockFile, O_WRONLY | O_CREAT | O_EXCL
> > # O_EXCL is supposed to be atomic
> -------------
> I dont think this will work on ActiveState perl running on a PC, will
> it?
It will certainly work. Whether O_EXCL is properly atomic or not under
Win9x I don't know; you'd need to check MSDN.
> > Again, a race condition. Just try to unlink the lockfile: if it didn't
> > exist unlink will fail.
> > -------------
> I dont want it to fail.... I want it to try a couple times to skip
> over an execution lock that another user may have.
When I say 'fail' I mean 'unlink will return undef if the file was
already unlinked'. It may well anyway, as the code is written: there's
nothing to stop someone else unlinking the file between your -f test and
your unlink. This is what I mean by a 'race condition': you and another
process are racing to unlink the file, and whomever loses will get an
unexpected result (in this case, an error from unlink).
Don't bother with the -f, just do the unlink. If it's there, it'll
delete it; if it's not, it doesn't matter 'cos it's not there. To be
properly careful, you'd check, if unlink failed, that $! is ENOENT and
tell the user if it wasn't. That'll catch things like someone having the
lockfile open so you can't delete it (damnable win32 mandatory file
locks!).
Ben
--
Musica Dei donum optimi, trahit homines, trahit deos. |
Musica truces molit animos, tristesque mentes erigit. |
Musica vel ipsas arbores et horridas movet feras. |