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.

Chapter 8Deferred ProcessingThe strategy of deferring work probably predatesmankind, but only in the last few decades haveworkers recognized this strategy’s value in simplifyingparallel algorithms [KL80, Mas92]. General approachesto work deferral in parallel programminginclude queuing, reference counting, and RCU.8.1 BarriersHPC-style barriers.8.2 Reference CountingReference counting tracks the number of referencesto a given object in order to prevent that objectfrom being prematurely freed. Although this is aconceptually simple technique, many devils hide inthe details. After all, if the object was not subject tobeing prematurely freed, there would be no need forthe reference counter. But if the object is subjectto being prematurely freed, what prevents that objectfrombeingfreedduringthereference-acquisitionprocess itself?There are a number of possible answers to thisquestion, including:1. A lock residing outside of the object must beheld while manipulating the reference count.Note that there are a wide variety of types oflocks, however, pretty much any type will suffice.2. The object is created with a non-zero referencecount, and new references may be acquired onlywhen the current value of the reference counteris non-zero. Once acquired, a reference may behanded off to some other entity.3. An existence guarantee is provided for the object,so that it cannot be freed during any timeinterval when some entity might be attemptingRelease SynchronizationAcquisition ReferenceSynchronization Locking Counting RCULocking - CAM CAReference A AM ACountingRCU CA MCA CATable 8.1: Reference Counting and SynchronizationMechanismsto acquire a reference. Existence guarantees areoften provided by automatic garbage collectors,and, as will be seen in Section 8.3, they can alsobe provided by RCU.4. A type-safety guarantee is provided for the object,andthereisinadditionsomeidentitycheckthat can be performed once the reference is acquired.Type-safety guarantees can be providedby special-purpose memory allocators, and canalso be provided by the SLAB_DESTROY_BY_RCUfeature within the Linux kernel, again, as willbe seen in Section 8.3.Of course, any mechanism that provides existenceguarantees by definition also provides type-safetyguarantees. This section will therefore group thelast two answers together under the rubric of RCU,leaving us with three general categories of referenceacquisitionprotection, namely, locking, referencecounting, and RCU.Quick Quiz 8.1: Why not implement referenceacquisitionusing a simple compare-and-swap operationthat only acquires a reference if the referencecounter is non-zero?Given that the key reference-counting issue is synchronizationbetween acquisition of a reference andfreeing of the object, we have nine possible combinationsof mechanisms, as shown in Table 8.1. Thistabledividesreference-countingmechanismsintothe

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

Saved successfully!

Ooh no, something went wrong!