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.

320 APPENDIX F. ANSWERS TO QUICK QUIZZESforce_quiescent_state() would report the extendedquiescent state on behalf of the offlinedCPU.Quick Quiz D.40:Given that an offline CPU is in an extended quiescentstate, why does line 28 of Figure D.32 need tocare which grace period it is dealing with?Answer:<strong>It</strong> really does not need to care in this case. However,because it does need to care in many other cases, thecpu_quiet() function does take the grace-periodnumber as an argument, so some value must besupplied.Quick Quiz D.41:But this list movement in Figure D.32 makes allof the going-offline CPU’s callbacks go throughanother grace period, even if they were ready toinvoke. <strong>Is</strong>n’t that inefficient? Furthermore, couldn’tan unfortunate pattern of CPUs going offline thencoming back online prevent a given callback fromever being invoked?Answer:<strong>It</strong> is inefficient, but it is simple. Given that thisis not a commonly executed code path, this is theright tradeoff. The starvation case would be aconcern, except that the online and offline processinvolves multiple grace periods.Quick Quiz D.42:Why not just expand note_new_gpnum() inline intocheck_for_new_grace_period() in Figure D.34?Answer:Because note_new_gpnum() must be called for eachnew grace period, including both those started bythis CPU and those started by other CPUs. Incontrast, check_for_new_grace_period() is calledonly for the case where some other CPU started thegrace period.Answer:There are two cases of interest.In the first case, there is only a single rcu_nodestructure in the hierarchy. Since the CPU executingin rcu_start_gp() is currently holding that rcu_node structure’s lock, the CPU attempting to reportthe quiescent state will not be able to acquire thislock until initialization is complete, at which pointthe quiescent state will be reported normally.In the second case, there are multiple rcu_nodestructures, and the leaf rcu_node structure correspondingto the CPU that is attempting to reportthe quiescent state already has that CPU’s->qsmask bit cleared. Therefore, the CPU attemptingto report the quiescent state will give up, andsome later quiescent state for that CPU will be appliedto the new grace period.Quick Quiz D.44:Hey!!! Shouldn’t we hold the non-leaf rcu_nodestructures’ locks when munging their state in line 37of Figure D.37???Answer:There is no need to hold their locks. The reasoningis as follows:1. The new grace period cannot end, because therunning CPU (which is initializing it) won’tpass through a quiescent state. Therefore,there is no race with another invocation ofrcu_start_gp().2. The running CPU holds ->onofflock, so thereis no race with CPU-hotplug operations.3. The leaf rcu_node structures are not yet initialized,so they have all of their ->qsmask bitscleared. This means that any other CPU attemptingto report a quiescent state will stopat the leaf level, and thus cannot race with thecurrent CPU for non-leaf rcu_node structures.4. The RCU tracing functions access, but do notmodify, the rcu_node structures’ fields. Raceswith these functions is therefore harmless.Quick Quiz D.43:But there has been no initialization yet at line 15of Figure D.37! <strong>What</strong> happens if a CPU noticesthe new grace period and immediately attempts toreport a quiescent state? Won’t it get confused?Quick Quiz D.45:Why can’t we merge the loop spanning lines 36-37with the loop spanning lines 40-44 in Figure D.37?

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

Saved successfully!

Ooh no, something went wrong!