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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

82 CHAPTER 8. DEFERRED PROCESSINGNow, line 5 does the replacement, so that the newelement is finally visible to readers, and hence isshaded red, as shown on the fifth row of Figure 8.14.At this point, as shown below, we have two versionsof the list. Pre-existing readers might see the5,6,7 element (which is therefore now shaded yellow),but new readers will instead see the 5,2,3element. But any given reader is guaranteed to seesome well-defined list.After the synchronize_rcu() on line 6 returns, agrace period will have elapsed, and so all reads thatstarted before the list_replace_rcu() will havecompleted. In particular, any readers that mighthave been holding references to the 5,6,7 elementare guaranteed to have exited their RCU read-sidecritical sections, and are thus prohibited from continuingto hold a reference. Therefore, there can nolonger be any readers holding references to the oldelement, as indicated its green shading in the sixthrow of Figure 8.14. As far as the readers are concerned,we are back to having a single version of thelist, but with the new element in place of the old.After thekfree() on line 7 completes, the list willappear as shown on the final row of Figure 8.14.Despite the fact that RCU was named after thereplacement case, the vast majority of RCU usagewithin the Linux kernel relies on the simple deletioncase shown in Section 8.3.1.3.Discussion These examples assumed that a mutexwas held across the entire update operation,which would mean that there could be at most twoversions of the list active at a given time.Quick Quiz 8.9: How would you modify thedeletion example to permit more than two versionsof the list to be active?Quick Quiz 8.10: How many RCU versions of agiven list can be active at any given time?This sequence of events shows how RCU updatesuse multiple versions to safely carry out changes inpresence of concurrent readers. Of course, some algorithmscannot gracefully handle multiple versions.There are techniques for adapting such algorithmsto RCU [McK04], but these are beyond the scope ofthis section.8.3.1.4 Summary of RCU FundamentalsThis section has described the three fundamentalcomponents of RCU-based algorithms:1. a publish-subscribe mechanism for adding newdata,Mechanism RCU ReplacesSectionReader-writer locking Section 8.3.2.1Restricted reference-counting mechanism Section 8.3.2.2Bulk reference-counting mechanism Section 8.3.2.3Poor man’s garbage collector Section 8.3.2.4Existence Guarantees Section 8.3.2.5Type-Safe Memory Section 8.3.2.6Wait for things to finish Section 8.3.2.7Table 8.3: RCU Usage2. a way of waiting for pre-existing RCU readersto finish, and3. a discipline of maintaining multiple versions topermit change without harming or unduly delayingconcurrent RCU readers.Quick Quiz 8.11: How can RCU updaterspossibly delay RCU readers, given that thercu read lock() and rcu read unlock() primitivesneither spin nor block?These three RCU components allow data to beupdated in face of concurrent readers, and can becombinedindifferentwaystoimplementasurprisingvariety of different types of RCU-based algorithms,someofwhicharedescribedinthefollowingsection.8.3.2 RCU UsageThis section answers the question ”what is RCU?”from the viewpoint of the uses to which RCU can beput. BecauseRCUismostfrequentlyusedtoreplacesome existing mechanism, we look at it primarilyin terms of its relationship to such mechanisms, aslisted in Table 8.3. Following the sections listed inthis table, Section 8.3.2.8 provides a summary.8.3.2.1 RCU is a Reader-Writer Lock ReplacementPerhaps the most common use of RCU within theLinux kernel is as a replacement for reader-writerlocking in read-intensive situations. Nevertheless,this use of RCU was not immediately apparent tome at the outset, in fact, I chose to implementsomething similar to brlock before implementinga general-purpose RCU implementation back in theearly 1990s. Each and every one of the uses I envisionedfor the proto-brlock primitive was insteadimplemented using RCU. In fact, it was more thanthree years before the proto-brlock primitive sawits first use. Boy, did I feel foolish!The key similarity between RCU and readerwriterlocking is that both have read-side criticalsections that can execute in parallel. In fact, in

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

Saved successfully!

Ooh no, something went wrong!