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.

236 APPENDIX D. READ-COPY UPDATE IMPLEMENTATIONSidlewaitackidle waitzerowaitack waitmbcompleted++CPU 2 ackCPU 0 ackCPU 1 ackCPU 3 ackOld counters zeroCPU 3 mbCPU 0 mbCPU 2 mbCPU 1 mbcompleted++Either counter mightbe incremented hereFigure D.63: Preemptable RCU State Machine2. A given CPU advances its callback lists just beforeacknowledging the counter increment.3. The blue oval represents the fact that memoryreordering might cause different CPUs tosee the increment at different times. Thismeans that a given CPU might believe thatsome other CPU has jumped the gun, usingthe new value of the counter before the counterwas actually incremented. In fact, in theory,a given CPU might see the next increment ofthe rcu_ctrlblk.completed counter as earlyas the last preceding memory barrier. (Notewell that this sentence is very imprecise. <strong>If</strong> youintend to do correctness proofs involving memorybarriers, please see Appendix D.4.3.3.4. Because rcu_read_lock() does not containany memory barriers, the corresponding RCUread-side critical sections might be reorderedby the CPU to follow the rcu_read_unlock().Therefore, the memory barriers are required toensure that the actions of the RCU read-sidecritical sections have in fact completed.5. As we will see, the fact that different CPUs cansee the counter flip happening at different timesmeans that a single trip through the state machineis not sufficient for a grace period: multipletrips are required.Figure D.64: Preemptable RCU State MachineTimeline1 void rcu_check_callbacks(int cpu, int user)2 {3 unsigned long flags;4 struct rcu_data *rdp = RCU_DATA_CPU(cpu);56 rcu_check_mb(cpu);7 if (rcu_ctrlblk.completed == rdp->completed)8 rcu_try_flip();9 spin_lock_irqsave(&rdp->lock, flags);10 RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);11 __rcu_advance_callbacks(rdp);12 spin_unlock_irqrestore(&rdp->lock, flags);13 }Figure D.65: rcu check callbacks() ImplementationGrace-Period State Machine WalkthroughThis section walks through the C code that implementsthe RCU grace-period state machine, which isinvoked from the scheduling-clock interrupt, whichinvokes rcu_check_callbacks() with irqs (andthus also preemption) disabled. This function is implementedas shown in Figure D.65. Line 4 selectsthe rcu_data structure corresponding to the currentCPU, and line 6 checks to see if this CPU needsto execute a memory barrier to advance the statemachine out of the rcu_try_flip_waitmb_statestate. Line 7 checks to see if this CPU is alreadyaware of the current grace-period stage number,and line 8 attempts to advance the state machineif so. Lines 9 and 12 hold the rcu_data’s

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

Saved successfully!

Ooh no, something went wrong!