10.07.2015 Views

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

Is Parallel Programming Hard, And, If So, What Can You Do About It?

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8.3. READ-COPY UPDATE (RCU) 81oldreadersmightstillbereferencingit, butthatnewreaders cannot obtain a reference to it.Please note that readers are not permitted tomaintain references to element 5,6,7 after exitingfrom their RCU read-side critical sections. Therefore,once the synchronize_rcu() on line 4 completes,so that all pre-existing readers are guaranteedto have completed, there can be no more readersreferencingthiselement,asindicatedbyitsgreenshading on the third row of Figure 8.13. We are thusback to a single version of the list.At this point, the 5,6,7 element may safely befreed, as shown on the final row of Figure 8.13. Atthis point, we have completed the deletion of element5,6,7. The following section covers replacement.Example 2: Maintaining Multiple VersionsDuring Replacement To start the replacementexample, here are the last few lines of the exampleshown in Figure 8.12:1 q = kmalloc(sizeof(*p), GFP_KERNEL);2 *q = *p;3 q->b = 2;4 q->c = 3;5 list_replace_rcu(&p->list, &q->list);6 synchronize_rcu();7 kfree(p);The initial state of the list, including the pointerp, is the same as for the deletion example, as shownon the first row of Figure 8.14.As before, the triples in each element representthe values of fields a, b, and c, respectively. Thered-shaded elements might be referenced by readers,and because readers do not synchronize directlywith updaters, readers might run concurrently withthis entire replacement process. Please note that weagain omit the backwards pointers and the link fromthe tail of the list to the head for clarity.Line 1 kmalloc()s a replacement element, as follows,resulting in the state as shown in the secondrow of Figure 8.14. At this point, no reader can holda reference to the newly allocated element (as indicatedby its green shading), and it is uninitialized(as indicated by the question marks).Line 2 copies the old element to the new one, resultingin the state as shown in the third row of Figure8.14. The newly allocated element still cannotbe referenced by readers, but it is now initialized.Line 3 updates q->b to the value “2”, and line 4updates q->c to the value “3”, as shown on thefourth row of Figure 8.14.1,2,3 5,6,7 11,4,8Allocate?,?,?1,2,3 5,6,711,4,8Copy5,6,71,2,3 5,6,75,6,7Update5,2,31,2,3 5,6,711,4,8list_replace_rcu()5,2,31,2,3 5,6,711,4,8synchronize_rcu()5,2,31,2,3 5,6,711,4,8kfree()1,2,3 5,6,7 11,4,8Figure 8.14: RCU Replacement in Linked List

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!