www.ti.com
2.1UseofCLanguage
2.2ThreadsandReentrancy
2.2.1Threads
UseofCLanguage
Almostallrecentlydevelopedsoftwaremodulesfollowthesecommonsenseguidelinesalready,sothis
chapterjustformalizesthem.Inadditiontotheseguidelines,wealsodevelopageneralmodelofdata
memorythatenablesapplicationstoefficientlymanageanalgorithm'smemoryrequirements.
Allalgorithmswillfollowtherun-timeconventionsimposedbytheCprogramminglanguage.Thisensures
thatthesystemintegratorisfreetouseCto"bind"variousalgorithmstogether,controltheflowofdata
betweenalgorithms,andinteractwithotherprocessorsinthesystemeasily.
Rule1
Allalgorithmsmustfollowtherun-timeconventionsimposedbyTI’simplementationoftheC
programminglanguage.
ItisveryimportanttonotethatthisdoesnotmeanthatalgorithmsmustbewrittenintheClanguage.
Algorithmsmaybeimplementedentirelyinassemblylanguage.Theymust,however,becallablefromthe
ClanguageandrespecttheClanguagerun-timeconventions.Mostsignificantalgorithmsarenot
implementedasasinglefunction;likeanysophisticatedsoftware,theyarecomposedofmanyinterrelated
internalfunctions.Again,itisimportanttonotethattheseinternalfunctionsdonotneedtofollowtheC
languageconventions;onlythetop-mostinterfacesmustobeytheClanguageconventions.Ontheother
hand,theseinternalfunctionsmustbecarefulnottocausethetop-mostfunctiontoviolatetheCrun-time
conventions;e.g.,nocalledfunctionmayuseawordonthestackwithinterruptsenabledwithoutfirst
updatingthestackpointer.
BecauseofthevarietyofframeworksavailableforDSPsystems,therearemanydifferingtypesof
threads,andtherefore,reentrancyrequirements.Inthissection,wetrytopreciselydefinethetypesof
threadssupportedbythisstandardandthereentrancyrequirementsofalgorithms.
Athreadisanencapsulationoftheflowofcontrolinaprogram.Mostpeopleareaccustomedtowriting
single-threadedprograms;i.e.,programsthatonlyexecuteonepaththroughtheircode"atatime."
Multi-threadedprogramsmayhaveseveralthreadsrunningthroughdifferentcodepaths"simultaneously."
Whyaresomephrasesaboveinquotes?Inatypicalmulti-threadedprogram,zeroormorethreadsmay
actuallyberunningatanyonetime.ThisdependsonthenumberofCPUsinthesysteminwhichthe
processisrunning,andonhowthethreadsystemisimplemented.AsystemwithnCPUscan,intuitively
runnomorethannthreadsinparallel,butitmaygivetheappearanceofrunningmanymorethann
"simultaneously,"bysharingtheCPUsamongthreads.Themostcommoncaseisthatofnequaltoone;
i.e.,asingleCPUrunningallthethreadsofanapplication.
Whyarethreadsinteresting?AnOSorframeworkcanschedulethem,relievingthedeveloperofan
individualthreadfromhavingtoknowaboutalltheotherthreadsinthesystem.Inamulti-CPUsystem,
communicatingthreadscanbemovedamongtheCPUstomaximizesystemperformancewithouthaving
tomodifytheapplicationcode.InthemorecommoncaseofasingleCPU,theabilitytocreate
multi-threadedapplicationsallowstheCPUtobeusedmoreeffectively;whileonethreadiswaitingfor
data,anothercanbeprocessingdata.
VirtuallyallDSPsystemsaremulti-threaded;eventhesimplestsystemsconsistofamainprogramand
oneormorehardwareinterruptserviceroutines.Additionally,manyDSPsystemsaredesignedtomanage
multiple"channels"or"ports,"i.e.,theyperformthesameprocessingfortwoormoreindependentdata
streams.
GeneralProgrammingGuidelines 16SPRU352G–June2005–RevisedFebruary2007
SubmitDocumentationFeedback