lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Jul 3, 2009 at 9:14 AM, David Given <dg@cowlark.com> wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Fabio Mascarenhas wrote:
[...]
> Obviously I am not married to this solution, so if you have a suggestion
> on how to implement exclusive locks that works both inside the same
> process (even in the same thread) and across processes, for both POSIX
> and Windows, I am all ears. :-)

We did this at work. On Unix our solution was to create a dummy file and
then use advisory locks with fcntl(_, F_SETLKW, _) locking across the
whole file. These have the nice behaviour of being automatically
unlocked if the app terminates unexpectedly. Allegedly these even work
across NFS these days.

I thought about using file-level locks (lfs already has those), but they do let the process that acquired the lock acquire it again, which is quite common if you are using coroutines for cooperative threading (using the Xavante web server, for example). Lockfiles don't have this problem.


For Windows we used named semaphores --- there's an atomic
lookup-by-name-or-create-if-not-found function call. Then you just use
them like an ordinary mutex.

I haven't tested, but mutexes probably have the same problem as above. In Windows I use a call to CreateFile that always fails if the file already exists, and set the file to be automatically deleted when its fd is closed (or the process is killed).
 

If you want to lock *between* Windows and a Posix subsystem? Pass, I'm
afraid...
 
--
Fabio Mascarenhas