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.

F.7. CHAPTER 8: DEFERRED PROCESSING 305possibility, perhaps, but best to consider it ahead oftime.)A third approach is to adminstratively abolishreal-time threads from the system in question. Inthis case, the preempted process will age up in priority,thus getting torunlongbeforethe counter hada chance to overflow. Of course, this approach is lessthan helpful for real-time applications.A final approach would be for rcu_read_lock()to recheck the value of the global rcu_gp_ctr afterstoringtoitsper-threadrcu_reader_gpcounter,retrying if the new value of the global rcu_gp_ctris inappropriate. This works, but introduces nondeterministicexecutiontimeintorcu_read_lock().On the other hand, if your application is being preemptedlong enough for the counter to overflow, youhave no hope of deterministic execution time in anycase!Quick Quiz 8.55:<strong>Do</strong>esn’t the additional memory barrier shown online 14 of Figure 8.44, greatly increase the overheadof rcu_quiescent_state?Answer:Indeed it does! An application using this implementationof RCU should therefore invokercu_quiescent_state sparingly, instead usingrcu_read_lock() and rcu_read_unlock() most ofthe time.However, this memory barrier is absolutely requiredso that other threads will see the store onlines 12-13 before any subsequent RCU read-sidecritical sections executed by the caller.Quick Quiz 8.56:Why are the two memory barriers on lines 19 and22 of Figure 8.44 needed?Answer:The memory barrier on line 19 prevents any RCUread-side critical sections that might precede thecall to rcu_thread_offline() won’t be reorderedby either the compiler or the CPU to follow theassignment on lines 20-21. The memory barrier online 22 is, strictly speaking, unnecessary, as it isillegal to have any RCU read-side critical sectionsfollowing the call to rcu_thread_offline().Quick Quiz 8.57:To be sure, the clock frequencies of ca-2008 Powersystems were quite high, but even a 5GHz clockfrequency is insufficent to allow loops to be executedin 50 picoseconds! <strong>What</strong> is going on here?Answer:Since the measurement loop contains a pair ofempty functions, the compiler optimizes it away.The measurement loop takes 1,000 passes betweeneach call to rcu_quiescent_state(), so this measurementis roughly one thousandth of the overheadof a single call to rcu_quiescent_state().Quick Quiz 8.58:Why would the fact that the code is in a librarymake any difference for how easy it is to use theRCU implementation shown in Figures 8.44 and8.45?Answer:A library function has absolutely no control overthe caller, and thus cannot force the caller to invokercu_quiescent_state() periodically. On the otherhand, a library function that made many referencesto a given RCU-protected data structure mightbe able to invoke rcu_thread_online() uponentry, rcu_quiescent_state() periodically, andrcu_thread_offline() upon exit.Quick Quiz 8.59:But what if you hold a lock across a call tosynchronize_rcu(), and then acquire that samelock within an RCU read-side critical section? Thisshould be a deadlock, but how can a primitive thatgenerates absolutely no code possibly participate ina deadlock cycle?Answer:Please note that the RCU read-side criticalsection is in effect extended beyondthe enclosing rcu_read_lock() andrcu_read_unlock(), out to the previous andnext call to rcu_quiescent_state(). Thisrcu_quiescent_state can be thought of as arcu_read_unlock() immediately followed by anrcu_read_lock().Even so, the actual deadlock itself will involvethe lock acquisition in the RCU read-side criticalsection and the synchronize_rcu(), never thercu_quiescent_state().Quick Quiz 8.60:

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

Saved successfully!

Ooh no, something went wrong!