From: Romain Beauxis <romain.beauxis@gmail.com>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Problem with try_lock on win32.
Date: Wed, 27 Jan 2010 08:54:08 -0600 [thread overview]
Message-ID: <201001270854.09085.romain.beauxis@gmail.com> (raw)
In-Reply-To: <004001ca9f29$fffac320$fff04960$@romulus.metastack.com>
Hi !
Le mercredi 27 janvier 2010 02:23:25, David Allsopp a écrit :
> Romain Beauxis:
> > I have a problem with the following code under win32:
> >
> > let m = Mutex.create ()
> >
> > let () =
> > Mutex.lock m;
> > if Mutex.try_lock m then
> > Printf.printf "locked !\n"
> > else
> > Printf.printf "could not lock!\n"
>
> This code is behaving correctly for a Windows mutex (AFAIK - I can't find
> the relevant bit in the Synchronisation on MSDN atm) - once a thread has
> locked a Windows mutex, WaitForSingleObject will return WAIT_OBJECT_0 (i.e.
> success) because for that thread the mutex is signalled (it's only other
> threads which will see the object as non-signalled). I guess it's a
> philosophical discussion for whether it's useful for a thread to be able to
> block itself permanently by trying to lock a mutex which it has already
> locked (the POSIX way).
>
> One possible fix to make it behave like POSIX would be to patch
> otherlibs/systhreads/win32.c so that caml_mutex_lock records the thread ID
> of the thread when it locks the mutex. Some trick would be required to
> block the thread (permanently) if it calls Mutex.lock twice (to match the
> POSIX behaviour). caml_mutex_try_lock can check the thread ID before using
> WaitForSingleObject and return false if it shows that it's locked and
> caml_mutex_unlock would clear the thread ID to null on a successful
> release. Two potential problems (which I'm guessing other Windows users on
> the list may comment on, if relevant):
>
> a) The representation of a mutex internally (the abstract value) changes
> which means that any Windows C stubs which interoperate with OCaml mutexes
> would break.
> b) The behaviour of Mutex.lock and Mutex.try_lock under Windows would be
> altered to be non-Windows-like behaviour which may affect existing
> Windows-only programs which rely on it.
>
> But you could raise it as a bug in Mantis just for cross-platform
> consistency of the Mutex module (another option would be to have a separate
> module PosixMutex with the guaranteed consistent semantics and leave the
> Mutex module as behaving in an OS-specific way)
Thank you for your detailed and enlighting answer !
I have no problem with this behaviour for myself and I don't think we need to
change it: the case where a thread blocks itself should be a minotiry, if not
never exist.
For the record, we reached this issue with code like:
assert( not (Mutex.try_lock m))
In this case, the assert would fail, although the program should still behave
correctly under win32...
The only thing that would be nice is to have this written somewhere in the
Mutex module documentation..
Romain
prev parent reply other threads:[~2010-01-27 14:48 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-27 2:45 Romain Beauxis
2010-01-27 8:23 ` [Caml-list] " David Allsopp
2010-01-27 14:54 ` Romain Beauxis [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=201001270854.09085.romain.beauxis@gmail.com \
--to=romain.beauxis@gmail.com \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox