Another way may be to use separate lockfiles to hand-roll your own lock - check for the existence of "myfile.lock" - if none, then create it, open "myfile", do stuff, close it, and delete "myfile.lock" all in one go. (Or maybe even just using chmod and some funky rw permission stuff on the file itself)
the monastery somewhere though, either providing code or trashing the idea (hopefully whilst providing some better solutions) :)