Re: WaitForSingleObject() will not deadlock



Actually, all I found in the Butenhof reference was a quote from Alice in Wonderland, and
the next page is 97, thus skipping everything in between.

Memory ordering (which I would not have thought of as "memory visibility guarantees") is
an ancient topic. The DEC Alpha was a driver writer's nightmare because it had no
guarantees for write orderings, and it took several releases of the chip before they got
smart enough to put a memory fence instruction in. The nightmares this gave driver
writers was substantial, since you couldn't be guaranteed what order data would hit the
control registers, and the trick was to write out N (or N+1, I have thankfully forgotten)
values, thus forcing the value in the write pipe to be flushed. N was initially 4, and
when the next chip came out with a 7-deep write-pipe, every device driver for the Alpha
stopped working correctly.

In addition to the opportunistic scheduling of the write pipe, if a second value was
written to the a given memory address, and that address had a value pending in the write
pipe, the Alpha simply replaced the value in the write pipe without bothering to write the
current value back to memory. You can see what a disaster this was for doing things like
writing to control registers such as a FIFO buffer register!

The x86 architecture explicitly guarantees that data values in the write pipe are written
out "in the order they were generated by the CPU core" (FIFO ordering), and the only issue
then is making sure that there is a memory fence that forces the data out from the write
pipe to memory. The lock prefix forces a memory fence and causes the write pipe to be
flushed to memory. The x64 has explicit SFENCE (Store-fence), LFENCE (Load-fence) and
MFENCE (memory-fence, both store-and-load) instructions.
joe

On Tue, 03 Jul 2007 18:49:15 -0500, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:

On Tue, 03 Jul 2007 17:18:24 -0400, Joseph M. Newcomer
<newcomer@xxxxxxxxxxxx> wrote:

I agree. It makes no sense, athough the term "memory visibility guarantees" is not
actually defined anywhere I can find; the apparent interpretation that it guarantees that
all memory is consistent seems fundamental to any kind of locking structure. Otherwise,
the locks cannot possibly work.

See "Memory Ordering" here:

http://msdn2.microsoft.com/en-us/library/ms686355.aspx

In "Programming with POSIX Threads", Butenhof devotes a number of pages to
"Memory visibility between threads"; here's a link to page 89 where he
gives four rules POSIX obeys:

http://books.google.com/books?id=_xvnuFzo7q0C&pg=PA88&ots=Kl5R_S-7Y9&dq=%22memory+visibility%22+isbn:0201633922&num=50&sig=2L0dMbDOavxA9VVnDA2Ai28gIF8#PPA89,M1
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Pthread fd and memory leak
    ... and closes the socket file descriptor. ... When you send an fd over a pipe, the fd is still open in the sending ... memory at the pointer value and free it without errors, ...
    (comp.programming.threads)
  • Re: using pipe
    ... to another?Yes, pipe can be used as Inter-Process Communication between ... related processes. ... file system).Going through the file system (and I guess what you were looking for ... Why not use memory, obtained by e.g. ...
    (comp.unix.programmer)
  • Re: Your Greatest ASP Memory
    ... memory from ASP, but many many memories. ... few pipe shows I am able to attend (it looks like DC no longer hosts a pipe ... Because of ASP, I no longer smoke only Borkum ...
    (alt.smokers.pipes)
  • Re: Syslets, Threadlets, generic AIO support, v6
    ... * For lack of a better implementation, implement vmsplice() to userspace ... In both cases the output is a pipe, ... Splicing from user memory is a simple operation ... unsigned long nr_segs, unsigned int flags) ...
    (Linux-Kernel)
  • Re: How to reinvent grep in perl?
    ... that you want to grep into it, and pipe its output into grep. ... Now I could do this with pipes and 3 different instances of perl ... So am I concerned about memory problems? ... string (that becomes a 3MB string after I prepend the file name and ...
    (perl.beginners)