From: skaller <skaller@users.sourceforge.net>
To: David Brown <caml-list@davidb.org>
Cc: Ker Lutyn <ker527mail@yahoo.com>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] atomicity guarantees for threaded code
Date: 30 Oct 2004 09:54:37 +1000 [thread overview]
Message-ID: <1099094075.11063.215.camel@pelican.wigram> (raw)
In-Reply-To: <20041029215027.GA9504@old.davidb.org>
On Sat, 2004-10-30 at 07:50, David Brown wrote:
> On Fri, Oct 29, 2004 at 09:39:07AM -0700, Ker Lutyn wrote:
>
> > thread 1:
> >
> > mapref := StringMap.add key value !mapref
> >
> > thread 2 through thread N:
> >
> > let value = StringMap.find key !mapref
> >
> > Assume only thread 1 can update the reference. Is this code safe?
>
> As long as the reference write is atomic, which it is going to be, I would
> suspect this is safe. It probably would even be safe if multiple threads
> updated the reference, but you might just drop entries.
It isn't clear though. On x86 you can have 1 byte alignment
for an address. What happens if:
(a) thread 2 reads half an address, then gets a page fault
(b) thread 1 writes half an address, then gets a page fault,
(c) thread 1 writes the second half of the address
(d) thread 2 reads the second half of the WRONG address
The x86 can interrupt instructions in the middle ***
Actually, the 68K can do this too. On the x86 the
only way to get an atomic operation is on physical
memory, and by also locking the bus. CISC machines
get rather nasty ..
Bottom line: you have to use a specified atomic
access technique (eg sig_atomic_t, locks, etc) if you
want to be sure an access is atomic.
It is likely as you say that there won't be a problem
on most processors (by which I mean the code
Ocaml generates will be effectively atomic wrt. threads)
and Ocaml could even guarrantee it, but there is a risk
for some processor extra code would have to generated
to ensure the guarrantee.
*** And I mean even with IRQs. In particular
I found a bug in the early 80186 hardware,
where if you used two prefixes such as with
ES REP MOVB
then RTTI failed to restart the instruction correctly,
setting the PC to the REP instead of the ES prefix.
The MOVB instruction moves many bytes and must
be interruptible, even on the 186 which has no VM.
--
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850,
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net
next prev parent reply other threads:[~2004-10-29 23:54 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-10-29 16:39 Ker Lutyn
2004-10-29 21:50 ` [Caml-list] " David Brown
2004-10-29 23:54 ` skaller [this message]
2004-10-30 0:32 ` David Brown
2004-10-30 1:07 ` skaller
2004-10-30 2:07 ` David Brown
2004-10-30 2:36 ` skaller
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=1099094075.11063.215.camel@pelican.wigram \
--to=skaller@users.sourceforge.net \
--cc=caml-list@davidb.org \
--cc=caml-list@inria.fr \
--cc=ker527mail@yahoo.com \
/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