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.

D.3. HIERARCHICAL RCU CODE WALKTHROUGH 2171 long rcu_batches_completed(void)2 {3 return rcu_state.completed;4 }56 long rcu_batches_completed_bh(void)7 {8 return rcu_bh_state.completed;9 }1011 static int12 cpu_has_callbacks_ready_to_invoke(struct rcu_data *rdp)13 {14 return &rdp->nxtlist != rdp->nxttail[RCU_DONE_TAIL];15 }1617 static int18 cpu_needs_another_gp(struct rcu_state *rsp,19 struct rcu_data *rdp)20 {21 return *rdp->nxttail[RCU_DONE_TAIL] &&22 ACCESS_ONCE(rsp->completed) ==23 ACCESS_ONCE(rsp->gpnum);24 }2526 static struct rcu_node27 *rcu_get_root(struct rcu_state *rsp)28 {29 return &rsp->node[0];30 }1 static void note_new_gpnum(struct rcu_state *rsp,2 struct rcu_data *rdp)3 {4 rdp->qs_pending = 1;5 rdp->passed_quiesc = 0;6 rdp->gpnum = rsp->gpnum;7 rdp->n_rcu_pending_force_qs = rdp->n_rcu_pending +8 RCU_JIFFIES_TILL_FORCE_QS;9 }1011 static int12 check_for_new_grace_period(struct rcu_state *rsp,13 struct rcu_data *rdp)14 {15 unsigned long flags;16 int ret = 0;1718 local_irq_save(flags);19 if (rdp->gpnum != rsp->gpnum) {20 note_new_gpnum(rsp, rdp);21 ret = 1;22 }23 local_irq_restore(flags);24 return ret;25 }Figure D.34: Noting New Grace PeriodsFigure D.33: Miscellaneous FunctionsD.3.6 Grace-Period-Detection FunctionsThis section covers functions that are directly involvedin detecting beginnings and ends of grace periods.This of course includes actually starting andending grace periods, but also includes noting whenother CPUs have started or ended grace periods.D.3.6.1 Noting New Grace PeriodsThe main purpose of Hierarchical RCU is to detectgrace periods, and the functions more directlyinvolved in this task are described in this section.Section D.3.6.1 covers functions that allow CPUsto note that a new grace period has begun, SectionD.3.6.2 covers functions that allow CPUs tonote that an existing grace period has ended, SectionD.3.6.3 covers rcu_start_gp(), which starts anew grace period, and Section D.3.6.4 covers functionsinvolved in reporting CPUs’ quiescent statesto the RCU core.Figure D.34 shows the code for note_new_gpnum(), which updates state to reflect a new graceperiod, as well as check_for_new_grace_period(),which is used by CPUs to detect when other CPUshave started a new grace period.Line 4 of note_new_gpnum() sets the ->qs_pending flag is the current CPU’s rcu_data structureto indicate that RCU needs a quiescent statefrom this CPU, line 5 clears the ->passed_quiescflag to indicate that this CPU has not yet passedthrough such a quiescent state, line 6 copies thegrace-period number from the global rcu_statestructure to this CPU’s rcu_data structure so thatthis CPU will remember that it has already notedthe beginning of this new grace period. Finally,lines 7-8 record the time in jiffies at which this CPUwill attempt to force holdout CPUs to pass throughquiescent states (by invoking force_quiescent_state()onorafter thatfuturetime), assumingthatthe grace period does not end beforehand.Lines 18 and 23 of check_for_new_grace_period() disable and re-enable interrupts, respectively.Line 19 checks to see if there is a new graceperiod that the current CPU has not yet noted, and,if so, line 20 invokes note_new_gpnum() in order tonote the new grace period, and line 21 sets the returnvalue accordingly. Either way, line 24 returnsstatus: non-zero if a new grace period has started,and zero otherwise.Quick Quiz D.42: Why not just expand note_new_gpnum() inline into check_for_new_grace_period() in Figure D.34?D.3.6.2 Noting End of Old Grace PeriodsFigure D.35 shows rcu_process_gp_end(), whichis invoked when a CPU suspects that a grace periodmight have ended (possibly because the CPUin question in fact ended the grace period). <strong>If</strong> agrace period really has ended, then this function advancesthe current CPU’s RCU callbacks, which are

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

Saved successfully!

Ooh no, something went wrong!