Re: memory barriers (was: Yes, WaitLatch is vulnerable to weak-memory-ordering bugs) - Mailing list pgsql-hackers

From Jeff Davis
Subject Re: memory barriers (was: Yes, WaitLatch is vulnerable to weak-memory-ordering bugs)
Date
Msg-id 1316727950.12660.2.camel@sussancws0025
Whole thread Raw
In response to Re: memory barriers (was: Yes, WaitLatch is vulnerable to weak-memory-ordering bugs)  (Robert Haas <robertmhaas@gmail.com>)
Responses Re: memory barriers (was: Yes, WaitLatch is vulnerable to weak-memory-ordering bugs)
Re: memory barriers (was: Yes, WaitLatch is vulnerable to weak-memory-ordering bugs)
List pgsql-hackers
On Thu, 2011-09-22 at 11:31 -0400, Robert Haas wrote:
> On Thu, Sep 22, 2011 at 11:25 AM, Thom Brown <thom@linux.com> wrote:
> > s/visca-versa/vice-versa/
> > s/laods/loads/
> 
> Fixed.  v4 attached.
> 

Can you please explain the "more subtly" part below?

+A common pattern where this actually does result in a bug is when
adding items
+onto a queue.  The writer does this:
+
+    q->items[q->num_items] = new_item;
+    ++q->num_items;
+
+The reader does this:
+
+    num_items = q->num_items;
+    for (i = 0; i < num_items; ++i)
+        /* do something with q->items[i] */
+
+This code turns out to be unsafe, because the writer might increment
+q->num_items before it finishes storing the new item into the
appropriate slot.
+More subtly, the reader might prefetch the contents of the q->items
array
+before reading q->num_items.

How would the reader prefetch the contents of the items array, without
knowing how big it is?

Regards,Jeff Davis



pgsql-hackers by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: patch: plpgsql - remove unnecessary ccache search when a array variable is updated
Next
From: Robert Haas
Date:
Subject: Re: memory barriers (was: Yes, WaitLatch is vulnerable to weak-memory-ordering bugs)