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.

220 APPENDIX D. READ-COPY UPDATE IMPLEMENTATIONS1 void rcu_qsctr_inc(int cpu)2 {3 struct rcu_data *rdp = &per_cpu(rcu_data, cpu);4 rdp->passed_quiesc = 1;5 rdp->passed_quiesc_completed = rdp->completed;6 }78 void rcu_bh_qsctr_inc(int cpu)9 {10 struct rcu_data *rdp = &per_cpu(rcu_bh_data, cpu);11 rdp->passed_quiesc = 1;12 rdp->passed_quiesc_completed = rdp->completed;13 }1 static void2 rcu_check_quiescent_state(struct rcu_state *rsp,3 struct rcu_data *rdp)4 {5 if (check_for_new_grace_period(rsp, rdp))6 return;7 if (!rdp->qs_pending)8 return;9 if (!rdp->passed_quiesc)10 return;11 cpu_quiet(rdp->cpu, rsp, rdp,12 rdp->passed_quiesc_completed);13 }Figure D.38: Code for Recording Quiescent Statesquiescent state for “rcu” and “rcu bh”, respectively.Note that the dynticks-idle and CPUofflinequiescent states are handled specially,due to the fact that such a CPU is not executing,and thus is unable to report itself as beingin a quiescent state.2. rcu_check_quiescent_state()checkstoseeifthecurrentCPUhaspassedthroughaquiescentstate, invoking cpu_quiet() if so.3. cpu_quiet() reports the specified CPU as havingpassedthroughaquiescentstatebyinvokingcpu_quiet_msk(). The specified CPU must eitherbe the current CPU or an offline CPU.4. cpu_quiet_msk() reports the specified vectorof CPUs as having passed through a quiescentstate. The CPUs in the vector need not be thecurrent CPU, nor must they be offline.Each of these functions is described below.Figure D.38 shows the code for rcu_qsctr_inc()and rcu_bh_qsctr_inc(), which note the currentCPU’s passage through a quiescent state.Line 3 of rcu_qsctr_inc() obtains a pointer tothe specified CPU’s rcu_data structure (which correspondsto “rcu” as opposed to “rcu bh”). Line 4sets the ->passed_quiesc field, recording the quiescentstate. Line 5 sets the ->passed_quiesc_completed field to the number of the last completedgraceperiodthatthisCPUknowsof(whichisstoredinthe->completedfieldofthercu_datastructure).The rcu_bh_qsctr_inc() function operates inthe same manner, the only difference being thatline 10 obtains the rcu_data pointer from the rcu_bh_data per-CPU variable rather than the rcu_data per-CPU variable.Figure D.39 shows the code for rcu_check_quiescent_state(), which is invoked fromrcu_process_callbacks() (described in SectionD.3.2.4) in order to determine when otherFigure D.39: Code forrcu check quiescent state()CPUs have started a new grace period and toinform RCU of recent quiescent states for this CPU.Line 5 invokes check_for_new_grace_period()to check for a new grace period having been startedby some other CPU, and also updating this CPU’slocal state to account for that new grace period.<strong>If</strong> a new grace period has just started, line 6 returns.Line 7 checks to see if RCU is still expectinga quiescent state from the current CPU, andline 8 returns if not. Line 9 checks to see if thisCPU has passed through a quiescent state since thestart of the current grace period (in other words,if rcu_qsctr_inc() or rcu_bh_qsctr_inc() havebeen invoked for “rcu” and “rcu bh”, respectively),and line 10 returns if not.Therefore, execution reaches line 11 only if a previouslynoted grace period is still in effect, if thisCPU needs to pass through a quiescent state in orderto allow this grace period to end, and if thisCPU has passed through such a quiescent state. Inthis case, lines 11-12 invoke cpu_quiet() in orderto report this quiescent state to RCU.Quick Quiz D.46: <strong>What</strong> prevents lines 11-12 ofFigure D.39 from reporting a quiescent state from aprior grace period against the current grace period?Figure D.40 shows cpu_quiet, which is used toreport a quiescent state for the specified CPU. Asnoted earlier, this must either be the currently runningCPU or a CPU that is guaranteed to remainoffline throughout.Line 9 picks up a pointer to the leaf rcu_nodestructure responsible for this CPU. Line 10 acquiresthis leaf rcu_node structure’s lock and disables interrupts.Line 11 checks to make sure that the specifiedgrace period is still in effect, and, if not, line 11clears the indication that this CPU passed througha quiescent state (since it belongs to a defunct graceperiod), line 13 releases the lock and re-enables interrupts,and line 14 returns to the caller.

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

Saved successfully!

Ooh no, something went wrong!