Texas Instruments TMS320 DSP Computer Hardware User Manual


 
www.ti.com
2.2.2Preemptivevs.Non-PreemptiveMultitasking
2.2.3Reentrancy
ThreadsandReentrancy
Non-preemptivemultitaskingreliesoneachthreadtovoluntarilyrelinquishcontroltotheoperatingsystem
beforelettinganotherthreadexecute.Thisisusuallydonebyrequiringthreadstoperiodicallycallan
operatingsystemfunction,sayyield(),toallowanotherthreadtotakecontroloftheCPUorbysimply
requiringallthreadstocompletewithinaspecifiedshortperiod.Inanon-preemptivemulti-threading
environment,theamountoftimeathreadisallowedtorunisdeterminedbythethread,whereasina
preemptiveenvironment,thetimeisdeterminedbytheoperatingsystemandtheentiresetoftasksthat
arereadytorun.
Notethatthedifferencebetweenthosetwoflavorsofmulti-threadingcanbeaverybigone;forexample,
underanon-preemptivesystem,youcansafelyassumethatnootherthreadexecuteswhileaparticular
algorithmprocessesdatausingon-chipdatamemory.Underpreemptiveexecution,thisisnottrue;a
threadmaybepreemptedwhileitisinthemiddleofprocessing.Thus,ifyourapplicationreliesonthe
assumptionthatthingsdonotchangeinthemiddleofprocessingsomedata,itmightbreakundera
preemptiveexecutionscheme.
Sincepreemptivesystemsaredesignedtopreservethestateofapreemptedthreadandrestoreitwhen
itsexecutioncontinues,threadscansafelyassumethatmostregistersandallofthethread'sdatamemory
remainunchanged.Whatwouldcauseanapplicationtofail?Anyassumptionsrelatedtothemaximum
amountoftimethatcanelapsebetweenanytwoinstructions,thestateofanyglobalsystemresource
suchasadatacache,orthestateofaglobalvariableaccessedbymultiplethreads,cancausean
applicationtofailinapreemptiveenvironment.
Non-preemptiveenvironmentsincurlessoverheadandoftenresultinhigherperformancesystems;for
example,datacachesaremuchmoreeffectiveinnon-preemptivesystemssinceeachthreadcancontrol
whenpreemption(andtherefore,cacheflushing)willoccur.
Ontheotherhand,non-preemptiveenvironmentsrequirethateithereachthreadcompletewithina
specifiedmaximumamountoftime,orexplicitlyrelinquishcontroloftheCPUtotheframework(or
operatingsystem)atsomeminimumperiodicrate.Byitself,thisisnotaproblemsincemostDSPthreads
areperiodicwithreal-timedeadlines.However,thisminimumrateisafunctionoftheotherthreadsinthe
systemand,consequently,non-preemptivethreadsarenotcompletelyindependentofoneanother;they
mustbesensitivetotheschedulingrequirementsoftheotherthreadsinthesystem.Thus,systemsthat
arebytheirnaturemultirateandmultichanneloftenrequirepreemption;otherwise,allofthealgorithms
usedwouldhavetoberewrittenwheneveranewalgorithmisaddedtothesystem.
Ifwewantallalgorithmstobeframework-independent,wemusteitherdefineaframework-neutralwayfor
algorithmstorelinquishcontrol,orassumethatalgorithmsusedinanon-preemptiveenvironmentalways
completeinlessthantherequiredmaximumschedulinglatencytime.Sincewerequiredocumentationof
worst-caseexecutiontimes,itispossibleforsystemintegratorstoquicklydetermineifanalgorithmwill
causeanon-preemptivesystemtoviolateitsschedulinglatencyrequirements.Thus,theTMS320DSP
AlgorithmStandarddoesnotdefineaframework-neutral"yield"operationforalgorithms.
Sincealgorithmscanbeusedinbothpreemptiveandnon-preemptiveenvironments,itisimportantthatall
algorithmsbedesignedtosupportboth.Thismeansthatalgorithmsshouldminimizethemaximumtime
thattheycandelayotheralgorithmsinanon-preemptivesystem.
Reentrancyistheattributeofaprogramorroutinethatallowsthesamecopyoftheprogramorroutineto
beusedconcurrentlybytwoormorethreads.
Reentrancyisanextremelyvaluablepropertyforfunctions.Inmultichannelsystems,forexample,any
functionthatcanbeinvokedaspartofonechannel'sprocessingmustbereentrant;otherwise,that
functionwouldnotbeusableforotherchannels.Insinglechannelmultiratesystems,anyfunctionthat
mustbeusedattwodifferentratesmustbereentrant;forexample,ageneraldigitalfilterfunctionusedfor
bothechocancellationandpre-emphasisforavocoder.Unfortunately,itisnotalwayseasytodetermineif
afunctionisreentrant.
SPRU352GJune2005RevisedFebruary2007GeneralProgrammingGuidelines17
SubmitDocumentationFeedback