欢迎来到天天文库
浏览记录
ID:40049610
大小:174.36 KB
页数:3页
时间:2019-07-18
《Beyond volatile》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、BeyondvolatileorHowtosavedaysofdebuggingtimeWhendebuggingembeddedapplications,intermittentproblemsareamongthemoredifficultones.Thistextdiscussesonesourceofsuchproblems:variablesthatareaccessedasynchronously,e.g.,byinterruptroutinesorbycoderunninginseparatethreads.Suchvariablesmu
2、stbeproperlydefinedandhaveadequateprotection.Thedefinitionmustincludethevolatilekeyword—thisinformsthecompilerthatthevariablecanbechangedfromotherplacesthanthecurrentlyexecutingthread.Thecompilerwillthenavoidcertainoptimizationsonthisvariable,obeyingtheaccessestothevariableasgiv
3、eninthesourcecodeandmakingsurethevariableisstableateach"sequencepoint".Thusthecompilerwillnotrelyonknowledgeofvaluesfrompreviousaccessestoreusethesameregister,moveoperationsacrosssequencepointsandsoon.Toprotectasharedvariable,eachaccesstothevariablemustalsobemadeatomicfromothera
4、ccesses.ReadingfromorwritingtoavariableinCisnotguaranteedtobeanatomicoperationassuch.Accessinga32-bitvariableonan8-bitarchitecturewouldtypicallybenon-atomic,aswouldanyobjectreadorwritethatrequiresmorethanoneinstruction.EvenifyouinspectthecompilergeneratedcodeandfindittobeOK,itis
5、notcertainthatthenextcompilationwillgivethesameresultifyouhavemadechangestothecodeortothecompilersettings.CExampleAtypicalexampleofasharedvariableiswhenaninterruptroutineupdatesatime-keepingvariablethatisreadfrommaincode.IftheInterruptServiceRoutine(ISR)runswithinterruptsdisable
6、d(whichoftenisthecase),theinterruptfunctionisatomic.Inotherwords,itwillnotbeinterruptedandthusthisaccessshouldnotneedfurtherprotection.NotethatforthebelowISRyouwillalsoneedtoenableandinitializetheinterrupt,butthatisnotofinterestforthisexample.volatilelongtick_count=0;__interrupt
7、voidtick_timer(){tick_count++;}Themaincodeontheotherhandmustpreventtheinterruptfromhappeningwhilethevariableisbeingaccessed.Asimplifiedunsafeexamplewouldbe:...if(tick_count>=next_stop)/*Unsafe!*/{next_stop+=100;do_stuff();}Thiscodeisunsafeasreadingtick_countisnotguaranteedtobean
8、atomicoperation.Atick_timerinterruptmayoccurwhe
此文档下载收益归作者所有