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.

Appendix BSynchronization PrimitivesAllbutthesimplestparallelprogramsrequiresynchronizationprimitives. This appendix gives a quickoverview of a set of primitives based loosely on thosein the Linux kernel.Why Linux? Because it is one of the well-known,largest, and easily obtained bodies of parallel codeavailable. We believe that reading code is, if anything,more important to learning than is writingcode, so by using examples similar to real code inthe Linux kernel, we are enabling you to use Linuxto continue your learning as you progress beyond theconfines of this book.WhybasedlooselyratherthanfollowingtheLinuxkernel API exactly? First, the Linux API changeswith time, so any attempt to track it exactly wouldlikely end in total frustration for all involved. Second,many of the members of the Linux kernelAPI are specialized for use in a production-qualityoperating-system kernel. This specialization introducescomplexitiesthat,thoughabsolutelynecessaryintheLinuxkernelitself,areoftenmoretroublethanthey are worth in the “toy” programs that we will beusing to demonstrate SMP and realtime design principlesand practices. For example, properly checkingfor error conditions such as memory exhaustion is a“must” in the Linux kernel, however, in “toy” programsit is perfectly acceptable to simply abort()the program, correct the problem, and rerun.Finally, it should be possible to implement atrivial mapping layer between this API and mostproduction-level AP<strong>Is</strong>. A pthreads implementationis available (CodeSamples/api-pthreads/api-pthreads.h), and a Linux-kernel-module APIwould not be difficult to create.Quick Quiz B.1: Give an example of a parallelprogram that could be written without synchronizationprimitives.The following sections describe commonly usedclasses of synchronization primitives. @@@ Moreesoteric primitives will be introduced in later revision.Section B.1 covers organization/initializationprimitives; Section B.2 presents thread creation,destruction, and control primitives; Section B.3presentslockingprimitives; SectionB.4presentsperthreadand per-CPU variable primitives; and SectionB.5 gives an overview of the relative performanceof the various primitives.B.1 Organization and Initialization@@@ currently include ../api.h, and there is onlypthreads. Expand and complete once the CodeSamplesstructure settles down.B.1.1 smp init():<strong>You</strong> must invoke smp_init() before invoking anyother primitives.B.2 Thread Creation, Destruction,and ControlThis API focuses on “threads”, which are a locusof control. 1 Each such thread has an identifier oftype thread_id_t, and no two threads running ata given time will have the same identifier. Threadsshare everything except for per-thread local state, 2which includes program counter and stack.The thread API is shown in Figure B.1, and membersare described in the following sections.1 There are many other names for similar software constructs,including “process”, “task”, “fiber”, “event”, and soon. Similar design principles apply to all of them.2 How is that for a circular definition?

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

Saved successfully!

Ooh no, something went wrong!