tasks.c 354 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873
  1. /*
  2. * FreeRTOS Kernel V11.3.0
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. * Copyright 2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
  5. *
  6. * SPDX-License-Identifier: MIT
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  9. * this software and associated documentation files (the "Software"), to deal in
  10. * the Software without restriction, including without limitation the rights to
  11. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  12. * the Software, and to permit persons to whom the Software is furnished to do so,
  13. * subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  20. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  21. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  22. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. * https://www.FreeRTOS.org
  26. * https://github.com/FreeRTOS
  27. *
  28. */
  29. /* Standard includes. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  33. * all the API functions to use the MPU wrappers. That should only be done when
  34. * task.h is included from an application file. */
  35. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  36. /* FreeRTOS includes. */
  37. #include "FreeRTOS.h"
  38. #include "task.h"
  39. #include "timers.h"
  40. #include "stack_macros.h"
  41. /* The default definitions are only available for non-MPU ports. The
  42. * reason is that the stack alignment requirements vary for different
  43. * architectures.*/
  44. #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS != 0 ) )
  45. #error configKERNEL_PROVIDED_STATIC_MEMORY cannot be set to 1 when using an MPU port. The vApplicationGet*TaskMemory() functions must be provided manually.
  46. #endif
  47. /* The MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  48. * for the header files above, but not in this file, in order to generate the
  49. * correct privileged Vs unprivileged linkage and placement. */
  50. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  51. /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
  52. * functions but without including stdio.h here. */
  53. #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
  54. /* At the bottom of this file are two optional functions that can be used
  55. * to generate human readable text from the raw data generated by the
  56. * uxTaskGetSystemState() function. Note the formatting functions are provided
  57. * for convenience only, and are NOT considered part of the kernel. */
  58. #include <stdio.h>
  59. #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
  60. #if ( configUSE_PREEMPTION == 0 )
  61. /* If the cooperative scheduler is being used then a yield should not be
  62. * performed just because a higher priority task has been woken. */
  63. #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
  64. #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
  65. #else
  66. #if ( configNUMBER_OF_CORES == 1 )
  67. /* This macro requests the running task pxTCB to yield. In single core
  68. * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
  69. * can be used to request the task running on core 0 to yield. Therefore, pxTCB
  70. * is not used in this macro. */
  71. #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
  72. do { \
  73. ( void ) ( pxTCB ); \
  74. portYIELD_WITHIN_API(); \
  75. } while( 0 )
  76. #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
  77. do { \
  78. if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority ) \
  79. { \
  80. portYIELD_WITHIN_API(); \
  81. } \
  82. else \
  83. { \
  84. mtCOVERAGE_TEST_MARKER(); \
  85. } \
  86. } while( 0 )
  87. #else /* if ( configNUMBER_OF_CORES == 1 ) */
  88. /* Yield the core on which this task is running. */
  89. #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldCore( ( pxTCB )->xTaskRunState )
  90. /* Yield for the task if a running task has priority lower than this task. */
  91. #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) prvYieldForTask( pxTCB )
  92. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  93. #endif /* if ( configUSE_PREEMPTION == 0 ) */
  94. /* Values that can be assigned to the ucNotifyState member of the TCB. */
  95. #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
  96. #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
  97. #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
  98. /*
  99. * The value used to fill the stack of a task when the task is created. This
  100. * is used purely for checking the high water mark for tasks.
  101. */
  102. #define tskSTACK_FILL_BYTE ( 0xa5U )
  103. /* Bits used to record how a task's stack and TCB were allocated. */
  104. #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
  105. #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
  106. #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
  107. /* If any of the following are set then task stacks are filled with a known
  108. * value so the high water mark can be determined. If none of the following are
  109. * set then don't fill the stack so there is no unnecessary dependency on memset. */
  110. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  111. #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
  112. #else
  113. #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
  114. #endif
  115. /*
  116. * Macros used by vListTask to indicate which state a task is in.
  117. */
  118. #define tskRUNNING_CHAR ( 'X' )
  119. #define tskBLOCKED_CHAR ( 'B' )
  120. #define tskREADY_CHAR ( 'R' )
  121. #define tskDELETED_CHAR ( 'D' )
  122. #define tskSUSPENDED_CHAR ( 'S' )
  123. /*
  124. * Some kernel aware debuggers require the data the debugger needs access to be
  125. * global, rather than file scope.
  126. */
  127. #ifdef portREMOVE_STATIC_QUALIFIER
  128. #define static
  129. #endif
  130. /* The name allocated to the Idle task. This can be overridden by defining
  131. * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
  132. #ifndef configIDLE_TASK_NAME
  133. #define configIDLE_TASK_NAME "IDLE"
  134. #endif
  135. /* Reserve space for Core ID and null termination. */
  136. #if ( configNUMBER_OF_CORES > 1 )
  137. /* Multi-core systems with up to 9 cores require 1 character for core ID and 1 for null termination. */
  138. #if ( configMAX_TASK_NAME_LEN < 2U )
  139. #error Minimum required task name length is 2. Please increase configMAX_TASK_NAME_LEN.
  140. #endif
  141. #define taskRESERVED_TASK_NAME_LENGTH 2U
  142. #else /* if ( configNUMBER_OF_CORES > 1 ) */
  143. /* Reserve space for null termination. */
  144. #if ( configMAX_TASK_NAME_LEN < 1U )
  145. #error Minimum required task name length is 1. Please increase configMAX_TASK_NAME_LEN.
  146. #endif
  147. #define taskRESERVED_TASK_NAME_LENGTH 1U
  148. #endif /* if ( ( configNUMBER_OF_CORES > 1 ) */
  149. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  150. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
  151. * performed in a generic way that is not optimised to any particular
  152. * microcontroller architecture. */
  153. /* uxTopReadyPriority holds the priority of the highest priority ready
  154. * state task. */
  155. #define taskRECORD_READY_PRIORITY( uxPriority ) \
  156. do { \
  157. if( ( uxPriority ) > uxTopReadyPriority ) \
  158. { \
  159. uxTopReadyPriority = ( uxPriority ); \
  160. } \
  161. } while( 0 ) /* taskRECORD_READY_PRIORITY */
  162. /*-----------------------------------------------------------*/
  163. #if ( configNUMBER_OF_CORES == 1 )
  164. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  165. do { \
  166. UBaseType_t uxTopPriority = uxTopReadyPriority; \
  167. \
  168. /* Find the highest priority queue that contains ready tasks. */ \
  169. while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) != pdFALSE ) \
  170. { \
  171. configASSERT( uxTopPriority ); \
  172. --uxTopPriority; \
  173. } \
  174. \
  175. /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
  176. * the same priority get an equal share of the processor time. */ \
  177. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  178. uxTopReadyPriority = uxTopPriority; \
  179. } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
  180. #else /* if ( configNUMBER_OF_CORES == 1 ) */
  181. #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID ) prvSelectHighestPriorityTask( xCoreID )
  182. #endif /* if ( configNUMBER_OF_CORES == 1 ) */
  183. /*-----------------------------------------------------------*/
  184. /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
  185. * they are only required when a port optimised method of task selection is
  186. * being used. */
  187. #define taskRESET_READY_PRIORITY( uxPriority )
  188. #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  189. #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  190. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
  191. * performed in a way that is tailored to the particular microcontroller
  192. * architecture being used. */
  193. /* A port optimised version is provided. Call the port defined macros. */
  194. #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
  195. /*-----------------------------------------------------------*/
  196. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  197. do { \
  198. UBaseType_t uxTopPriority; \
  199. \
  200. /* Find the highest priority list that contains ready tasks. */ \
  201. portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
  202. configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
  203. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  204. } while( 0 )
  205. /*-----------------------------------------------------------*/
  206. /* A port optimised version is provided, call it only if the TCB being reset
  207. * is being referenced from a ready list. If it is referenced from a delayed
  208. * or suspended list then it won't be in a ready list. */
  209. #define taskRESET_READY_PRIORITY( uxPriority ) \
  210. do { \
  211. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
  212. { \
  213. portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
  214. } \
  215. } while( 0 )
  216. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  217. /*-----------------------------------------------------------*/
  218. /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
  219. * count overflows. */
  220. #define taskSWITCH_DELAYED_LISTS() \
  221. do { \
  222. List_t * pxTemp; \
  223. \
  224. /* The delayed tasks list should be empty when the lists are switched. */ \
  225. configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
  226. \
  227. pxTemp = pxDelayedTaskList; \
  228. pxDelayedTaskList = pxOverflowDelayedTaskList; \
  229. pxOverflowDelayedTaskList = pxTemp; \
  230. xNumOfOverflows = ( BaseType_t ) ( xNumOfOverflows + 1 ); \
  231. prvResetNextTaskUnblockTime(); \
  232. } while( 0 )
  233. /*-----------------------------------------------------------*/
  234. /*
  235. * Place the task represented by pxTCB into the appropriate ready list for
  236. * the task. It is inserted at the end of the list.
  237. */
  238. #define prvAddTaskToReadyList( pxTCB ) \
  239. do { \
  240. traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
  241. taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
  242. listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
  243. tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB ); \
  244. } while( 0 )
  245. /*-----------------------------------------------------------*/
  246. /*
  247. * Several functions take a TaskHandle_t parameter that can optionally be NULL,
  248. * where NULL is used to indicate that the handle of the currently executing
  249. * task should be used in place of the parameter. This macro simply checks to
  250. * see if the parameter is NULL and returns a pointer to the appropriate TCB.
  251. */
  252. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
  253. /* The item value of the event list item is normally used to hold the priority
  254. * of the task to which it belongs (coded to allow it to be held in reverse
  255. * priority order). However, it is occasionally borrowed for other purposes. It
  256. * is important its value is not updated due to a task priority change while it is
  257. * being used for another purpose. The following bit definition is used to inform
  258. * the scheduler that the value should not be changed - in which case it is the
  259. * responsibility of whichever module is using the value to ensure it gets set back
  260. * to its original value when it is released. */
  261. #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
  262. #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint16_t ) 0x8000U )
  263. #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
  264. #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint32_t ) 0x80000000U )
  265. #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
  266. #define taskEVENT_LIST_ITEM_VALUE_IN_USE ( ( uint64_t ) 0x8000000000000000U )
  267. #endif
  268. /* Indicates that the task is not actively running on any core. */
  269. #define taskTASK_NOT_RUNNING ( ( BaseType_t ) ( -1 ) )
  270. /* Indicates that the task is actively running but scheduled to yield. */
  271. #define taskTASK_SCHEDULED_TO_YIELD ( ( BaseType_t ) ( -2 ) )
  272. /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
  273. #if ( configNUMBER_OF_CORES == 1 )
  274. #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
  275. #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
  276. #else
  277. #define taskTASK_IS_RUNNING( pxTCB ) ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
  278. #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
  279. #endif
  280. /* Indicates that the task is an Idle task. */
  281. #define taskATTRIBUTE_IS_IDLE ( UBaseType_t ) ( 1U << 0U )
  282. #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
  283. #define portGET_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting )
  284. #define portSET_CRITICAL_NESTING_COUNT( xCoreID, x ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting = ( x ) )
  285. #define portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting++ )
  286. #define portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID ) ( pxCurrentTCBs[ ( xCoreID ) ]->uxCriticalNesting-- )
  287. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
  288. #define taskBITS_PER_BYTE ( ( size_t ) 8 )
  289. #if ( configNUMBER_OF_CORES > 1 )
  290. /* Yields the given core. This must be called from a critical section and xCoreID
  291. * must be valid. This macro is not required in single core since there is only
  292. * one core to yield. */
  293. #define prvYieldCore( xCoreID ) \
  294. do { \
  295. if( ( xCoreID ) == ( BaseType_t ) portGET_CORE_ID() ) \
  296. { \
  297. /* Pending a yield for this core since it is in the critical section. */ \
  298. xYieldPendings[ ( xCoreID ) ] = pdTRUE; \
  299. } \
  300. else \
  301. { \
  302. /* Request other core to yield if it is not requested before. */ \
  303. if( pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
  304. { \
  305. portYIELD_CORE( xCoreID ); \
  306. pxCurrentTCBs[ ( xCoreID ) ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD; \
  307. } \
  308. } \
  309. } while( 0 )
  310. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  311. /*-----------------------------------------------------------*/
  312. /*
  313. * Task control block. A task control block (TCB) is allocated for each task,
  314. * and stores task state information, including a pointer to the task's context
  315. * (the task's run time environment, including register values)
  316. */
  317. typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  318. {
  319. volatile StackType_t * pxTopOfStack; /**< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */
  320. #if ( portUSING_MPU_WRAPPERS == 1 )
  321. xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  322. #endif
  323. #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
  324. UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores. UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
  325. #endif
  326. ListItem_t xStateListItem; /**< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
  327. ListItem_t xEventListItem; /**< Used to reference a task from an event list. */
  328. UBaseType_t uxPriority; /**< The priority of the task. 0 is the lowest priority. */
  329. StackType_t * pxStack; /**< Points to the start of the stack. */
  330. #if ( configNUMBER_OF_CORES > 1 )
  331. volatile BaseType_t xTaskRunState; /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
  332. UBaseType_t uxTaskAttributes; /**< Task's attributes - currently used to identify the idle tasks. */
  333. #endif
  334. char pcTaskName[ configMAX_TASK_NAME_LEN ]; /**< Descriptive name given to the task when created. Facilitates debugging only. */
  335. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  336. BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
  337. #endif
  338. #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
  339. StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
  340. #endif
  341. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  342. UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
  343. #endif
  344. #if ( configUSE_TRACE_FACILITY == 1 )
  345. UBaseType_t uxTCBNumber; /**< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */
  346. UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
  347. #endif
  348. #if ( configUSE_MUTEXES == 1 )
  349. UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
  350. UBaseType_t uxMutexesHeld;
  351. #endif
  352. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  353. TaskHookFunction_t pxTaskTag;
  354. #endif
  355. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
  356. void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
  357. #endif
  358. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  359. configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
  360. #endif
  361. #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
  362. configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
  363. #endif
  364. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  365. volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
  366. volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
  367. #endif
  368. /* See the comments in FreeRTOS.h with the definition of
  369. * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
  370. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  371. uint8_t ucStaticallyAllocated; /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
  372. #endif
  373. #if ( INCLUDE_xTaskAbortDelay == 1 )
  374. uint8_t ucDelayAborted;
  375. #endif
  376. #if ( configUSE_POSIX_ERRNO == 1 )
  377. int iTaskErrno;
  378. #endif
  379. } tskTCB;
  380. /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
  381. * below to enable the use of older kernel aware debuggers. */
  382. typedef tskTCB TCB_t;
  383. #if ( configNUMBER_OF_CORES == 1 )
  384. /* MISRA Ref 8.4.1 [Declaration shall be visible] */
  385. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
  386. /* coverity[misra_c_2012_rule_8_4_violation] */
  387. portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
  388. #else
  389. /* MISRA Ref 8.4.1 [Declaration shall be visible] */
  390. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
  391. /* coverity[misra_c_2012_rule_8_4_violation] */
  392. portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
  393. #define pxCurrentTCB xTaskGetCurrentTaskHandle()
  394. #endif
  395. /* Lists for ready and blocked tasks. --------------------
  396. * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
  397. * doing so breaks some kernel aware debuggers and debuggers that rely on removing
  398. * the static qualifier. */
  399. PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
  400. PRIVILEGED_DATA static List_t xDelayedTaskList1; /**< Delayed tasks. */
  401. PRIVILEGED_DATA static List_t xDelayedTaskList2; /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
  402. PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /**< Points to the delayed task list currently being used. */
  403. PRIVILEGED_DATA static List_t * volatile pxOverflowDelayedTaskList; /**< Points to the delayed task list currently being used to hold tasks that have overflowed the current tick count. */
  404. PRIVILEGED_DATA static List_t xPendingReadyList; /**< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
  405. #if ( INCLUDE_vTaskDelete == 1 )
  406. PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
  407. PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
  408. #endif
  409. #if ( INCLUDE_vTaskSuspend == 1 )
  410. PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
  411. #endif
  412. /* Global POSIX errno. Its value is changed upon context switching to match
  413. * the errno of the currently running task. */
  414. #if ( configUSE_POSIX_ERRNO == 1 )
  415. int FreeRTOS_errno = 0;
  416. #endif
  417. /* Other file private variables. --------------------------------*/
  418. PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
  419. PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
  420. PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
  421. PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
  422. PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
  423. PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
  424. PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
  425. PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
  426. PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
  427. PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ]; /**< Holds the handles of the idle tasks. The idle tasks are created automatically when the scheduler is started. */
  428. /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
  429. * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
  430. * to determine the number of priority lists to read back from the remote target. */
  431. static const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
  432. /* Context switches are held pending while the scheduler is suspended. Also,
  433. * interrupts must not manipulate the xStateListItem of a TCB, or any of the
  434. * lists the xStateListItem can be referenced from, if the scheduler is suspended.
  435. * If an interrupt needs to unblock a task while the scheduler is suspended then it
  436. * moves the task's event list item into the xPendingReadyList, ready for the
  437. * kernel to move the task from the pending ready list into the real ready list
  438. * when the scheduler is unsuspended. The pending ready list itself can only be
  439. * accessed from a critical section.
  440. *
  441. * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
  442. * and must not be done from an ISR. Reads must be protected by either lock and may be done
  443. * from either an ISR or a task. */
  444. PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
  445. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  446. /* Do not move these variables to function scope as doing so prevents the
  447. * code working with debuggers that need to remove the static qualifier. */
  448. PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the value of a timer/counter the last time a task was switched in. */
  449. PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
  450. #endif
  451. /*-----------------------------------------------------------*/
  452. /* File private functions. --------------------------------*/
  453. /*
  454. * Creates the idle tasks during scheduler start.
  455. */
  456. static BaseType_t prvCreateIdleTasks( void );
  457. #if ( configNUMBER_OF_CORES > 1 )
  458. /*
  459. * Checks to see if another task moved the current task out of the ready
  460. * list while it was waiting to enter a critical section and yields, if so.
  461. */
  462. static void prvCheckForRunStateChange( void );
  463. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  464. #if ( configNUMBER_OF_CORES > 1 )
  465. /*
  466. * Yields a core, or cores if multiple priorities are not allowed to run
  467. * simultaneously, to allow the task pxTCB to run.
  468. */
  469. static void prvYieldForTask( const TCB_t * pxTCB );
  470. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  471. #if ( configNUMBER_OF_CORES > 1 )
  472. /*
  473. * Selects the highest priority available task for the given core.
  474. */
  475. static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
  476. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  477. /**
  478. * Utility task that simply returns pdTRUE if the task referenced by xTask is
  479. * currently in the Suspended state, or pdFALSE if the task referenced by xTask
  480. * is in any other state.
  481. */
  482. #if ( INCLUDE_vTaskSuspend == 1 )
  483. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
  484. #endif /* INCLUDE_vTaskSuspend */
  485. /*
  486. * Utility to ready all the lists used by the scheduler. This is called
  487. * automatically upon the creation of the first task.
  488. */
  489. static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
  490. /*
  491. * The idle task, which as all tasks is implemented as a never ending loop.
  492. * The idle task is automatically created and added to the ready lists upon
  493. * creation of the first user task.
  494. *
  495. * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks are also
  496. * created to ensure that each core has an idle task to run when no other
  497. * task is available to run.
  498. *
  499. * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
  500. * language extensions. The equivalent prototype for these functions are:
  501. *
  502. * void prvIdleTask( void *pvParameters );
  503. * void prvPassiveIdleTask( void *pvParameters );
  504. *
  505. */
  506. static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
  507. #if ( configNUMBER_OF_CORES > 1 )
  508. static portTASK_FUNCTION_PROTO( prvPassiveIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
  509. #endif
  510. /*
  511. * Utility to free all memory allocated by the scheduler to hold a TCB,
  512. * including the stack pointed to by the TCB.
  513. *
  514. * This does not free memory allocated by the task itself (i.e. memory
  515. * allocated by calls to pvPortMalloc from within the tasks application code).
  516. */
  517. #if ( INCLUDE_vTaskDelete == 1 )
  518. static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
  519. #endif
  520. /*
  521. * Used only by the idle task. This checks to see if anything has been placed
  522. * in the list of tasks waiting to be deleted. If so the task is cleaned up
  523. * and its TCB deleted.
  524. */
  525. static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
  526. /*
  527. * The currently executing task is entering the Blocked state. Add the task to
  528. * either the current or the overflow delayed task list.
  529. */
  530. static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
  531. const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
  532. /*
  533. * Fills an TaskStatus_t structure with information on each task that is
  534. * referenced from the pxList list (which may be a ready list, a delayed list,
  535. * a suspended list, etc.).
  536. *
  537. * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
  538. * NORMAL APPLICATION CODE.
  539. */
  540. #if ( configUSE_TRACE_FACILITY == 1 )
  541. static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
  542. List_t * pxList,
  543. eTaskState eState ) PRIVILEGED_FUNCTION;
  544. #endif
  545. /*
  546. * Searches pxList for a task with name pcNameToQuery - returning a handle to
  547. * the task if it is found, or NULL if the task is not found.
  548. */
  549. #if ( INCLUDE_xTaskGetHandle == 1 )
  550. static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
  551. const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
  552. #endif
  553. /*
  554. * When a task is created, the stack of the task is filled with a known value.
  555. * This function determines the 'high water mark' of the task stack by
  556. * determining how much of the stack remains at the original preset value.
  557. */
  558. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  559. static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
  560. #endif
  561. /*
  562. * Return the amount of time, in ticks, that will pass before the kernel will
  563. * next move a task from the Blocked state to the Running state or before the
  564. * tick count overflows (whichever is earlier).
  565. *
  566. * This conditional compilation should use inequality to 0, not equality to 1.
  567. * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
  568. * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
  569. * set to a value other than 1.
  570. */
  571. #if ( configUSE_TICKLESS_IDLE != 0 )
  572. static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
  573. #endif
  574. /*
  575. * Set xNextTaskUnblockTime to the time at which the next Blocked state task
  576. * will exit the Blocked state.
  577. */
  578. static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
  579. #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
  580. /*
  581. * Helper function used to pad task names with spaces when printing out
  582. * human readable tables of task information.
  583. */
  584. static char * prvWriteNameToBuffer( char * pcBuffer,
  585. const char * pcTaskName ) PRIVILEGED_FUNCTION;
  586. #endif
  587. /*
  588. * Called after a Task_t structure has been allocated either statically or
  589. * dynamically to fill in the structure's members.
  590. */
  591. static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
  592. const char * const pcName,
  593. const configSTACK_DEPTH_TYPE uxStackDepth,
  594. void * const pvParameters,
  595. UBaseType_t uxPriority,
  596. TaskHandle_t * const pxCreatedTask,
  597. TCB_t * pxNewTCB,
  598. const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
  599. /*
  600. * Called after a new task has been created and initialised to place the task
  601. * under the control of the scheduler.
  602. */
  603. static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
  604. /*
  605. * Create a task with static buffer for both TCB and stack. Returns a handle to
  606. * the task if it is created successfully. Otherwise, returns NULL.
  607. */
  608. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  609. static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
  610. const char * const pcName,
  611. const configSTACK_DEPTH_TYPE uxStackDepth,
  612. void * const pvParameters,
  613. UBaseType_t uxPriority,
  614. StackType_t * const puxStackBuffer,
  615. StaticTask_t * const pxTaskBuffer,
  616. TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
  617. #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  618. /*
  619. * Create a restricted task with static buffer for both TCB and stack. Returns
  620. * a handle to the task if it is created successfully. Otherwise, returns NULL.
  621. */
  622. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  623. static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
  624. TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
  625. #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) ) */
  626. /*
  627. * Create a restricted task with static buffer for task stack and allocated buffer
  628. * for TCB. Returns a handle to the task if it is created successfully. Otherwise,
  629. * returns NULL.
  630. */
  631. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  632. static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
  633. TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
  634. #endif /* #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) */
  635. /*
  636. * Create a task with allocated buffer for both TCB and stack. Returns a handle to
  637. * the task if it is created successfully. Otherwise, returns NULL.
  638. */
  639. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  640. static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
  641. const char * const pcName,
  642. const configSTACK_DEPTH_TYPE uxStackDepth,
  643. void * const pvParameters,
  644. UBaseType_t uxPriority,
  645. TaskHandle_t * const pxCreatedTask ) PRIVILEGED_FUNCTION;
  646. #endif /* #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) */
  647. /*
  648. * freertos_tasks_c_additions_init() should only be called if the user definable
  649. * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
  650. * called by the function.
  651. */
  652. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  653. static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
  654. #endif
  655. #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
  656. extern void vApplicationPassiveIdleHook( void );
  657. #endif /* #if ( configUSE_PASSIVE_IDLE_HOOK == 1 ) */
  658. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  659. /*
  660. * Convert the snprintf return value to the number of characters
  661. * written. The following are the possible cases:
  662. *
  663. * 1. The buffer supplied to snprintf is large enough to hold the
  664. * generated string. The return value in this case is the number
  665. * of characters actually written, not counting the terminating
  666. * null character.
  667. * 2. The buffer supplied to snprintf is NOT large enough to hold
  668. * the generated string. The return value in this case is the
  669. * number of characters that would have been written if the
  670. * buffer had been sufficiently large, not counting the
  671. * terminating null character.
  672. * 3. Encoding error. The return value in this case is a negative
  673. * number.
  674. *
  675. * From 1 and 2 above ==> Only when the return value is non-negative
  676. * and less than the supplied buffer length, the string has been
  677. * completely written.
  678. */
  679. static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
  680. size_t n );
  681. #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  682. /*-----------------------------------------------------------*/
  683. #if ( configNUMBER_OF_CORES > 1 )
  684. static void prvCheckForRunStateChange( void )
  685. {
  686. UBaseType_t uxPrevCriticalNesting;
  687. const TCB_t * pxThisTCB;
  688. BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
  689. /* This must only be called from within a task. */
  690. portASSERT_IF_IN_ISR();
  691. /* This function is always called with interrupts disabled
  692. * so this is safe. */
  693. pxThisTCB = pxCurrentTCBs[ xCoreID ];
  694. while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
  695. {
  696. /* We are only here if we just entered a critical section
  697. * or if we just suspended the scheduler, and another task
  698. * has requested that we yield.
  699. *
  700. * This is slightly complicated since we need to save and restore
  701. * the suspension and critical nesting counts, as well as release
  702. * and reacquire the correct locks. And then, do it all over again
  703. * if our state changed again during the reacquisition. */
  704. uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT( xCoreID );
  705. if( uxPrevCriticalNesting > 0U )
  706. {
  707. portSET_CRITICAL_NESTING_COUNT( xCoreID, 0U );
  708. portRELEASE_ISR_LOCK( xCoreID );
  709. }
  710. else
  711. {
  712. /* The scheduler is suspended. uxSchedulerSuspended is updated
  713. * only when the task is not requested to yield. */
  714. mtCOVERAGE_TEST_MARKER();
  715. }
  716. portRELEASE_TASK_LOCK( xCoreID );
  717. portMEMORY_BARRIER();
  718. configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
  719. portENABLE_INTERRUPTS();
  720. /* Enabling interrupts should cause this core to immediately service
  721. * the pending interrupt and yield. After servicing the pending interrupt,
  722. * the task needs to re-evaluate its run state within this loop, as
  723. * other cores may have requested this task to yield, potentially altering
  724. * its run state. */
  725. portDISABLE_INTERRUPTS();
  726. xCoreID = ( BaseType_t ) portGET_CORE_ID();
  727. portGET_TASK_LOCK( xCoreID );
  728. portGET_ISR_LOCK( xCoreID );
  729. portSET_CRITICAL_NESTING_COUNT( xCoreID, uxPrevCriticalNesting );
  730. if( uxPrevCriticalNesting == 0U )
  731. {
  732. portRELEASE_ISR_LOCK( xCoreID );
  733. }
  734. }
  735. }
  736. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  737. /*-----------------------------------------------------------*/
  738. #if ( configNUMBER_OF_CORES > 1 )
  739. static void prvYieldForTask( const TCB_t * pxTCB )
  740. {
  741. BaseType_t xLowestPriorityToPreempt;
  742. BaseType_t xCurrentCoreTaskPriority;
  743. BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
  744. BaseType_t xCoreID;
  745. const BaseType_t xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
  746. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  747. BaseType_t xYieldCount = 0;
  748. #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
  749. /* This must be called from a critical section. */
  750. configASSERT( portGET_CRITICAL_NESTING_COUNT( xCurrentCoreID ) > 0U );
  751. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  752. /* No task should yield for this one if it is a lower priority
  753. * than priority level of currently ready tasks. */
  754. if( pxTCB->uxPriority >= uxTopReadyPriority )
  755. #else
  756. /* Yield is not required for a task which is already running. */
  757. if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
  758. #endif
  759. {
  760. xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
  761. /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
  762. * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
  763. --xLowestPriorityToPreempt;
  764. for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
  765. {
  766. xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
  767. /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
  768. if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
  769. {
  770. xCurrentCoreTaskPriority = ( BaseType_t ) ( xCurrentCoreTaskPriority - 1 );
  771. }
  772. if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
  773. {
  774. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  775. if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
  776. #endif
  777. {
  778. if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
  779. {
  780. #if ( configUSE_CORE_AFFINITY == 1 )
  781. if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
  782. #endif
  783. {
  784. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  785. if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
  786. #endif
  787. {
  788. xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
  789. xLowestPriorityCore = xCoreID;
  790. }
  791. }
  792. }
  793. else
  794. {
  795. mtCOVERAGE_TEST_MARKER();
  796. }
  797. }
  798. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  799. {
  800. /* Yield all currently running non-idle tasks with a priority lower than
  801. * the task that needs to run. */
  802. if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
  803. ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
  804. {
  805. prvYieldCore( xCoreID );
  806. xYieldCount++;
  807. }
  808. else
  809. {
  810. mtCOVERAGE_TEST_MARKER();
  811. }
  812. }
  813. #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
  814. }
  815. else
  816. {
  817. mtCOVERAGE_TEST_MARKER();
  818. }
  819. }
  820. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  821. if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
  822. #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
  823. if( xLowestPriorityCore >= 0 )
  824. #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
  825. {
  826. prvYieldCore( xLowestPriorityCore );
  827. }
  828. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  829. /* Verify that the calling core always yields to higher priority tasks. */
  830. if( ( ( pxCurrentTCBs[ xCurrentCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U ) &&
  831. ( pxTCB->uxPriority > pxCurrentTCBs[ xCurrentCoreID ]->uxPriority ) )
  832. {
  833. configASSERT( ( xYieldPendings[ xCurrentCoreID ] == pdTRUE ) ||
  834. ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCurrentCoreID ] ) == pdFALSE ) );
  835. }
  836. #endif
  837. }
  838. }
  839. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  840. /*-----------------------------------------------------------*/
  841. #if ( configNUMBER_OF_CORES > 1 )
  842. static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
  843. {
  844. UBaseType_t uxCurrentPriority = uxTopReadyPriority;
  845. BaseType_t xTaskScheduled = pdFALSE;
  846. BaseType_t xDecrementTopPriority = pdTRUE;
  847. TCB_t * pxTCB = NULL;
  848. #if ( configUSE_CORE_AFFINITY == 1 )
  849. const TCB_t * pxPreviousTCB = NULL;
  850. #endif
  851. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  852. BaseType_t xPriorityDropped = pdFALSE;
  853. #endif
  854. /* This function should be called when scheduler is running. */
  855. configASSERT( xSchedulerRunning == pdTRUE );
  856. /* A new task is created and a running task with the same priority yields
  857. * itself to run the new task. When a running task yields itself, it is still
  858. * in the ready list. This running task will be selected before the new task
  859. * since the new task is always added to the end of the ready list.
  860. * The other problem is that the running task still in the same position of
  861. * the ready list when it yields itself. It is possible that it will be selected
  862. * earlier then other tasks which waits longer than this task.
  863. *
  864. * To fix these problems, the running task should be put to the end of the
  865. * ready list before searching for the ready task in the ready list. */
  866. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
  867. &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
  868. {
  869. ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
  870. vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
  871. &pxCurrentTCBs[ xCoreID ]->xStateListItem );
  872. }
  873. while( xTaskScheduled == pdFALSE )
  874. {
  875. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  876. {
  877. if( uxCurrentPriority < uxTopReadyPriority )
  878. {
  879. /* We can't schedule any tasks, other than idle, that have a
  880. * priority lower than the priority of a task currently running
  881. * on another core. */
  882. uxCurrentPriority = tskIDLE_PRIORITY;
  883. }
  884. }
  885. #endif
  886. if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
  887. {
  888. const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
  889. const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
  890. ListItem_t * pxIterator;
  891. /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
  892. * must not be decremented any further. */
  893. xDecrementTopPriority = pdFALSE;
  894. for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
  895. {
  896. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  897. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  898. /* coverity[misra_c_2012_rule_11_5_violation] */
  899. pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
  900. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  901. {
  902. /* When falling back to the idle priority because only one priority
  903. * level is allowed to run at a time, we should ONLY schedule the true
  904. * idle tasks, not user tasks at the idle priority. */
  905. if( uxCurrentPriority < uxTopReadyPriority )
  906. {
  907. if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0U )
  908. {
  909. continue;
  910. }
  911. }
  912. }
  913. #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
  914. if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
  915. {
  916. #if ( configUSE_CORE_AFFINITY == 1 )
  917. if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
  918. #endif
  919. {
  920. /* If the task is not being executed by any core swap it in. */
  921. pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
  922. #if ( configUSE_CORE_AFFINITY == 1 )
  923. pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
  924. #endif
  925. pxTCB->xTaskRunState = xCoreID;
  926. pxCurrentTCBs[ xCoreID ] = pxTCB;
  927. xTaskScheduled = pdTRUE;
  928. }
  929. }
  930. else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
  931. {
  932. configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
  933. #if ( configUSE_CORE_AFFINITY == 1 )
  934. if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
  935. #endif
  936. {
  937. /* The task is already running on this core, mark it as scheduled. */
  938. pxTCB->xTaskRunState = xCoreID;
  939. xTaskScheduled = pdTRUE;
  940. }
  941. }
  942. else
  943. {
  944. /* This task is running on the core other than xCoreID. */
  945. mtCOVERAGE_TEST_MARKER();
  946. }
  947. if( xTaskScheduled != pdFALSE )
  948. {
  949. /* A task has been selected to run on this core. */
  950. break;
  951. }
  952. }
  953. }
  954. else
  955. {
  956. if( xDecrementTopPriority != pdFALSE )
  957. {
  958. uxTopReadyPriority--;
  959. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  960. {
  961. xPriorityDropped = pdTRUE;
  962. }
  963. #endif
  964. }
  965. }
  966. /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
  967. * The scheduler should be able to select a task to run when uxCurrentPriority
  968. * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
  969. * tskIDLE_PRIORITY. */
  970. if( uxCurrentPriority > tskIDLE_PRIORITY )
  971. {
  972. uxCurrentPriority--;
  973. }
  974. else
  975. {
  976. /* This function is called when idle task is not created. Break the
  977. * loop to prevent uxCurrentPriority overrun. */
  978. break;
  979. }
  980. }
  981. #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
  982. {
  983. if( xTaskScheduled == pdTRUE )
  984. {
  985. if( xPriorityDropped != pdFALSE )
  986. {
  987. /* There may be several ready tasks that were being prevented from running because there was
  988. * a higher priority task running. Now that the last of the higher priority tasks is no longer
  989. * running, make sure all the other idle tasks yield. */
  990. BaseType_t x;
  991. for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
  992. {
  993. if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
  994. {
  995. prvYieldCore( x );
  996. }
  997. }
  998. }
  999. }
  1000. }
  1001. #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
  1002. #if ( configUSE_CORE_AFFINITY == 1 )
  1003. {
  1004. if( xTaskScheduled == pdTRUE )
  1005. {
  1006. if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
  1007. {
  1008. /* A ready task was just evicted from this core. See if it can be
  1009. * scheduled on any other core. */
  1010. UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
  1011. BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
  1012. BaseType_t xLowestPriorityCore = -1;
  1013. BaseType_t x;
  1014. if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
  1015. {
  1016. xLowestPriority = xLowestPriority - 1;
  1017. }
  1018. if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
  1019. {
  1020. /* pxPreviousTCB was removed from this core and this core is not excluded
  1021. * from it's core affinity mask.
  1022. *
  1023. * pxPreviousTCB is preempted by the new higher priority task
  1024. * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
  1025. * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
  1026. * is allowed to run. The reason is - when more than one cores are
  1027. * eligible for an incoming task, we preempt the core with the minimum
  1028. * priority task. Because this core (i.e. xCoreID) was preempted for
  1029. * pxCurrentTCBs[ xCoreID ], this means that all the others cores
  1030. * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
  1031. * no lower than pxPreviousTCB's priority. Therefore, the only cores where
  1032. * which can be preempted for pxPreviousTCB are the ones where
  1033. * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
  1034. * pxPreviousTCB is allowed to run).
  1035. *
  1036. * This is an optimization which reduces the number of cores needed to be
  1037. * searched for pxPreviousTCB to run. */
  1038. uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
  1039. }
  1040. else
  1041. {
  1042. /* pxPreviousTCB's core affinity mask is changed and it is no longer
  1043. * allowed to run on this core. Searching all the cores in pxPreviousTCB's
  1044. * new core affinity mask to find a core on which it can run. */
  1045. }
  1046. uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
  1047. for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
  1048. {
  1049. UBaseType_t uxCore = ( UBaseType_t ) x;
  1050. BaseType_t xTaskPriority;
  1051. if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
  1052. {
  1053. xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
  1054. if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
  1055. {
  1056. xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
  1057. }
  1058. uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
  1059. if( ( xTaskPriority < xLowestPriority ) &&
  1060. ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
  1061. ( xYieldPendings[ uxCore ] == pdFALSE ) )
  1062. {
  1063. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  1064. if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
  1065. #endif
  1066. {
  1067. xLowestPriority = xTaskPriority;
  1068. xLowestPriorityCore = ( BaseType_t ) uxCore;
  1069. }
  1070. }
  1071. }
  1072. }
  1073. if( xLowestPriorityCore >= 0 )
  1074. {
  1075. prvYieldCore( xLowestPriorityCore );
  1076. }
  1077. }
  1078. }
  1079. }
  1080. #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
  1081. }
  1082. #endif /* ( configNUMBER_OF_CORES > 1 ) */
  1083. /*-----------------------------------------------------------*/
  1084. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1085. static TCB_t * prvCreateStaticTask( TaskFunction_t pxTaskCode,
  1086. const char * const pcName,
  1087. const configSTACK_DEPTH_TYPE uxStackDepth,
  1088. void * const pvParameters,
  1089. UBaseType_t uxPriority,
  1090. StackType_t * const puxStackBuffer,
  1091. StaticTask_t * const pxTaskBuffer,
  1092. TaskHandle_t * const pxCreatedTask )
  1093. {
  1094. TCB_t * pxNewTCB;
  1095. configASSERT( puxStackBuffer != NULL );
  1096. configASSERT( pxTaskBuffer != NULL );
  1097. #if ( configASSERT_DEFINED == 1 )
  1098. {
  1099. /* Sanity check that the size of the structure used to declare a
  1100. * variable of type StaticTask_t equals the size of the real task
  1101. * structure. */
  1102. volatile size_t xSize = sizeof( StaticTask_t );
  1103. configASSERT( xSize == sizeof( TCB_t ) );
  1104. ( void ) xSize; /* Prevent unused variable warning when configASSERT() is not used. */
  1105. }
  1106. #endif /* configASSERT_DEFINED */
  1107. if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
  1108. {
  1109. /* The memory used for the task's TCB and stack are passed into this
  1110. * function - use them. */
  1111. /* MISRA Ref 11.3.1 [Misaligned access] */
  1112. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
  1113. /* coverity[misra_c_2012_rule_11_3_violation] */
  1114. pxNewTCB = ( TCB_t * ) pxTaskBuffer;
  1115. ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  1116. pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
  1117. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  1118. {
  1119. /* Tasks can be created statically or dynamically, so note this
  1120. * task was created statically in case the task is later deleted. */
  1121. pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
  1122. }
  1123. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
  1124. prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
  1125. }
  1126. else
  1127. {
  1128. pxNewTCB = NULL;
  1129. }
  1130. return pxNewTCB;
  1131. }
  1132. /*-----------------------------------------------------------*/
  1133. TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
  1134. const char * const pcName,
  1135. const configSTACK_DEPTH_TYPE uxStackDepth,
  1136. void * const pvParameters,
  1137. UBaseType_t uxPriority,
  1138. StackType_t * const puxStackBuffer,
  1139. StaticTask_t * const pxTaskBuffer )
  1140. {
  1141. TaskHandle_t xReturn = NULL;
  1142. TCB_t * pxNewTCB;
  1143. traceENTER_xTaskCreateStatic( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
  1144. pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
  1145. if( pxNewTCB != NULL )
  1146. {
  1147. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1148. {
  1149. /* Set the task's affinity before scheduling it. */
  1150. pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
  1151. }
  1152. #endif
  1153. prvAddNewTaskToReadyList( pxNewTCB );
  1154. }
  1155. else
  1156. {
  1157. mtCOVERAGE_TEST_MARKER();
  1158. }
  1159. traceRETURN_xTaskCreateStatic( xReturn );
  1160. return xReturn;
  1161. }
  1162. /*-----------------------------------------------------------*/
  1163. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1164. TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
  1165. const char * const pcName,
  1166. const configSTACK_DEPTH_TYPE uxStackDepth,
  1167. void * const pvParameters,
  1168. UBaseType_t uxPriority,
  1169. StackType_t * const puxStackBuffer,
  1170. StaticTask_t * const pxTaskBuffer,
  1171. UBaseType_t uxCoreAffinityMask )
  1172. {
  1173. TaskHandle_t xReturn = NULL;
  1174. TCB_t * pxNewTCB;
  1175. traceENTER_xTaskCreateStaticAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, uxCoreAffinityMask );
  1176. pxNewTCB = prvCreateStaticTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, &xReturn );
  1177. if( pxNewTCB != NULL )
  1178. {
  1179. /* Set the task's affinity before scheduling it. */
  1180. pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
  1181. prvAddNewTaskToReadyList( pxNewTCB );
  1182. }
  1183. else
  1184. {
  1185. mtCOVERAGE_TEST_MARKER();
  1186. }
  1187. traceRETURN_xTaskCreateStaticAffinitySet( xReturn );
  1188. return xReturn;
  1189. }
  1190. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  1191. #endif /* SUPPORT_STATIC_ALLOCATION */
  1192. /*-----------------------------------------------------------*/
  1193. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  1194. static TCB_t * prvCreateRestrictedStaticTask( const TaskParameters_t * const pxTaskDefinition,
  1195. TaskHandle_t * const pxCreatedTask )
  1196. {
  1197. TCB_t * pxNewTCB;
  1198. configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
  1199. configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
  1200. if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
  1201. {
  1202. /* Allocate space for the TCB. Where the memory comes from depends
  1203. * on the implementation of the port malloc function and whether or
  1204. * not static allocation is being used. */
  1205. pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
  1206. ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  1207. /* Store the stack location in the TCB. */
  1208. pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
  1209. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  1210. {
  1211. /* Tasks can be created statically or dynamically, so note this
  1212. * task was created statically in case the task is later deleted. */
  1213. pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
  1214. }
  1215. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
  1216. prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
  1217. pxTaskDefinition->pcName,
  1218. pxTaskDefinition->usStackDepth,
  1219. pxTaskDefinition->pvParameters,
  1220. pxTaskDefinition->uxPriority,
  1221. pxCreatedTask, pxNewTCB,
  1222. pxTaskDefinition->xRegions );
  1223. }
  1224. else
  1225. {
  1226. pxNewTCB = NULL;
  1227. }
  1228. return pxNewTCB;
  1229. }
  1230. /*-----------------------------------------------------------*/
  1231. BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
  1232. TaskHandle_t * pxCreatedTask )
  1233. {
  1234. TCB_t * pxNewTCB;
  1235. BaseType_t xReturn;
  1236. traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
  1237. configASSERT( pxTaskDefinition != NULL );
  1238. pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
  1239. if( pxNewTCB != NULL )
  1240. {
  1241. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1242. {
  1243. /* Set the task's affinity before scheduling it. */
  1244. pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
  1245. }
  1246. #endif
  1247. prvAddNewTaskToReadyList( pxNewTCB );
  1248. xReturn = pdPASS;
  1249. }
  1250. else
  1251. {
  1252. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  1253. }
  1254. traceRETURN_xTaskCreateRestrictedStatic( xReturn );
  1255. return xReturn;
  1256. }
  1257. /*-----------------------------------------------------------*/
  1258. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1259. BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
  1260. UBaseType_t uxCoreAffinityMask,
  1261. TaskHandle_t * pxCreatedTask )
  1262. {
  1263. TCB_t * pxNewTCB;
  1264. BaseType_t xReturn;
  1265. traceENTER_xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
  1266. configASSERT( pxTaskDefinition != NULL );
  1267. pxNewTCB = prvCreateRestrictedStaticTask( pxTaskDefinition, pxCreatedTask );
  1268. if( pxNewTCB != NULL )
  1269. {
  1270. /* Set the task's affinity before scheduling it. */
  1271. pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
  1272. prvAddNewTaskToReadyList( pxNewTCB );
  1273. xReturn = pdPASS;
  1274. }
  1275. else
  1276. {
  1277. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  1278. }
  1279. traceRETURN_xTaskCreateRestrictedStaticAffinitySet( xReturn );
  1280. return xReturn;
  1281. }
  1282. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  1283. #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  1284. /*-----------------------------------------------------------*/
  1285. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  1286. static TCB_t * prvCreateRestrictedTask( const TaskParameters_t * const pxTaskDefinition,
  1287. TaskHandle_t * const pxCreatedTask )
  1288. {
  1289. TCB_t * pxNewTCB;
  1290. configASSERT( pxTaskDefinition->puxStackBuffer );
  1291. if( pxTaskDefinition->puxStackBuffer != NULL )
  1292. {
  1293. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  1294. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  1295. /* coverity[misra_c_2012_rule_11_5_violation] */
  1296. pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
  1297. if( pxNewTCB != NULL )
  1298. {
  1299. ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  1300. /* Store the stack location in the TCB. */
  1301. pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
  1302. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  1303. {
  1304. /* Tasks can be created statically or dynamically, so note
  1305. * this task had a statically allocated stack in case it is
  1306. * later deleted. The TCB was allocated dynamically. */
  1307. pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
  1308. }
  1309. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
  1310. prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
  1311. pxTaskDefinition->pcName,
  1312. pxTaskDefinition->usStackDepth,
  1313. pxTaskDefinition->pvParameters,
  1314. pxTaskDefinition->uxPriority,
  1315. pxCreatedTask, pxNewTCB,
  1316. pxTaskDefinition->xRegions );
  1317. }
  1318. }
  1319. else
  1320. {
  1321. pxNewTCB = NULL;
  1322. }
  1323. return pxNewTCB;
  1324. }
  1325. /*-----------------------------------------------------------*/
  1326. BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
  1327. TaskHandle_t * pxCreatedTask )
  1328. {
  1329. TCB_t * pxNewTCB;
  1330. BaseType_t xReturn;
  1331. traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
  1332. pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
  1333. if( pxNewTCB != NULL )
  1334. {
  1335. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1336. {
  1337. /* Set the task's affinity before scheduling it. */
  1338. pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
  1339. }
  1340. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  1341. prvAddNewTaskToReadyList( pxNewTCB );
  1342. xReturn = pdPASS;
  1343. }
  1344. else
  1345. {
  1346. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  1347. }
  1348. traceRETURN_xTaskCreateRestricted( xReturn );
  1349. return xReturn;
  1350. }
  1351. /*-----------------------------------------------------------*/
  1352. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1353. BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
  1354. UBaseType_t uxCoreAffinityMask,
  1355. TaskHandle_t * pxCreatedTask )
  1356. {
  1357. TCB_t * pxNewTCB;
  1358. BaseType_t xReturn;
  1359. traceENTER_xTaskCreateRestrictedAffinitySet( pxTaskDefinition, uxCoreAffinityMask, pxCreatedTask );
  1360. pxNewTCB = prvCreateRestrictedTask( pxTaskDefinition, pxCreatedTask );
  1361. if( pxNewTCB != NULL )
  1362. {
  1363. /* Set the task's affinity before scheduling it. */
  1364. pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
  1365. prvAddNewTaskToReadyList( pxNewTCB );
  1366. xReturn = pdPASS;
  1367. }
  1368. else
  1369. {
  1370. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  1371. }
  1372. traceRETURN_xTaskCreateRestrictedAffinitySet( xReturn );
  1373. return xReturn;
  1374. }
  1375. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  1376. #endif /* portUSING_MPU_WRAPPERS */
  1377. /*-----------------------------------------------------------*/
  1378. #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  1379. static TCB_t * prvCreateTask( TaskFunction_t pxTaskCode,
  1380. const char * const pcName,
  1381. const configSTACK_DEPTH_TYPE uxStackDepth,
  1382. void * const pvParameters,
  1383. UBaseType_t uxPriority,
  1384. TaskHandle_t * const pxCreatedTask )
  1385. {
  1386. TCB_t * pxNewTCB;
  1387. /* If the stack grows down then allocate the stack then the TCB so the stack
  1388. * does not grow into the TCB. Likewise if the stack grows up then allocate
  1389. * the TCB then the stack. */
  1390. #if ( portSTACK_GROWTH > 0 )
  1391. {
  1392. /* Allocate space for the TCB. Where the memory comes from depends on
  1393. * the implementation of the port malloc function and whether or not static
  1394. * allocation is being used. */
  1395. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  1396. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  1397. /* coverity[misra_c_2012_rule_11_5_violation] */
  1398. pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
  1399. if( pxNewTCB != NULL )
  1400. {
  1401. ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  1402. /* Allocate space for the stack used by the task being created.
  1403. * The base of the stack memory stored in the TCB so the task can
  1404. * be deleted later if required. */
  1405. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  1406. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  1407. /* coverity[misra_c_2012_rule_11_5_violation] */
  1408. pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
  1409. if( pxNewTCB->pxStack == NULL )
  1410. {
  1411. /* Could not allocate the stack. Delete the allocated TCB. */
  1412. vPortFree( pxNewTCB );
  1413. pxNewTCB = NULL;
  1414. }
  1415. }
  1416. }
  1417. #else /* portSTACK_GROWTH */
  1418. {
  1419. StackType_t * pxStack;
  1420. /* Allocate space for the stack used by the task being created. */
  1421. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  1422. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  1423. /* coverity[misra_c_2012_rule_11_5_violation] */
  1424. pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) uxStackDepth ) * sizeof( StackType_t ) ) );
  1425. if( pxStack != NULL )
  1426. {
  1427. /* Allocate space for the TCB. */
  1428. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  1429. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  1430. /* coverity[misra_c_2012_rule_11_5_violation] */
  1431. pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
  1432. if( pxNewTCB != NULL )
  1433. {
  1434. ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  1435. /* Store the stack location in the TCB. */
  1436. pxNewTCB->pxStack = pxStack;
  1437. }
  1438. else
  1439. {
  1440. /* The stack cannot be used as the TCB was not created. Free
  1441. * it again. */
  1442. vPortFreeStack( pxStack );
  1443. }
  1444. }
  1445. else
  1446. {
  1447. pxNewTCB = NULL;
  1448. }
  1449. }
  1450. #endif /* portSTACK_GROWTH */
  1451. if( pxNewTCB != NULL )
  1452. {
  1453. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  1454. {
  1455. /* Tasks can be created statically or dynamically, so note this
  1456. * task was created dynamically in case it is later deleted. */
  1457. pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
  1458. }
  1459. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
  1460. prvInitialiseNewTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
  1461. }
  1462. return pxNewTCB;
  1463. }
  1464. /*-----------------------------------------------------------*/
  1465. BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
  1466. const char * const pcName,
  1467. const configSTACK_DEPTH_TYPE uxStackDepth,
  1468. void * const pvParameters,
  1469. UBaseType_t uxPriority,
  1470. TaskHandle_t * const pxCreatedTask )
  1471. {
  1472. TCB_t * pxNewTCB;
  1473. BaseType_t xReturn;
  1474. traceENTER_xTaskCreate( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
  1475. pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
  1476. if( pxNewTCB != NULL )
  1477. {
  1478. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1479. {
  1480. /* Set the task's affinity before scheduling it. */
  1481. pxNewTCB->uxCoreAffinityMask = configTASK_DEFAULT_CORE_AFFINITY;
  1482. }
  1483. #endif
  1484. prvAddNewTaskToReadyList( pxNewTCB );
  1485. xReturn = pdPASS;
  1486. }
  1487. else
  1488. {
  1489. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  1490. }
  1491. traceRETURN_xTaskCreate( xReturn );
  1492. return xReturn;
  1493. }
  1494. /*-----------------------------------------------------------*/
  1495. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  1496. BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
  1497. const char * const pcName,
  1498. const configSTACK_DEPTH_TYPE uxStackDepth,
  1499. void * const pvParameters,
  1500. UBaseType_t uxPriority,
  1501. UBaseType_t uxCoreAffinityMask,
  1502. TaskHandle_t * const pxCreatedTask )
  1503. {
  1504. TCB_t * pxNewTCB;
  1505. BaseType_t xReturn;
  1506. traceENTER_xTaskCreateAffinitySet( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, uxCoreAffinityMask, pxCreatedTask );
  1507. pxNewTCB = prvCreateTask( pxTaskCode, pcName, uxStackDepth, pvParameters, uxPriority, pxCreatedTask );
  1508. if( pxNewTCB != NULL )
  1509. {
  1510. /* Set the task's affinity before scheduling it. */
  1511. pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
  1512. prvAddNewTaskToReadyList( pxNewTCB );
  1513. xReturn = pdPASS;
  1514. }
  1515. else
  1516. {
  1517. xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  1518. }
  1519. traceRETURN_xTaskCreateAffinitySet( xReturn );
  1520. return xReturn;
  1521. }
  1522. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  1523. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  1524. /*-----------------------------------------------------------*/
  1525. static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
  1526. const char * const pcName,
  1527. const configSTACK_DEPTH_TYPE uxStackDepth,
  1528. void * const pvParameters,
  1529. UBaseType_t uxPriority,
  1530. TaskHandle_t * const pxCreatedTask,
  1531. TCB_t * pxNewTCB,
  1532. const MemoryRegion_t * const xRegions )
  1533. {
  1534. StackType_t * pxTopOfStack;
  1535. UBaseType_t x;
  1536. #if ( portUSING_MPU_WRAPPERS == 1 )
  1537. /* Should the task be created in privileged mode? */
  1538. BaseType_t xRunPrivileged;
  1539. if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
  1540. {
  1541. xRunPrivileged = pdTRUE;
  1542. }
  1543. else
  1544. {
  1545. xRunPrivileged = pdFALSE;
  1546. }
  1547. uxPriority &= ~portPRIVILEGE_BIT;
  1548. #endif /* portUSING_MPU_WRAPPERS == 1 */
  1549. /* Avoid dependency on memset() if it is not required. */
  1550. #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
  1551. {
  1552. /* Fill the stack with a known value to assist debugging. */
  1553. ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) uxStackDepth * sizeof( StackType_t ) );
  1554. }
  1555. #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
  1556. /* Calculate the top of stack address. This depends on whether the stack
  1557. * grows from high memory to low (as per the 80x86) or vice versa.
  1558. * portSTACK_GROWTH is used to make the result positive or negative as required
  1559. * by the port. */
  1560. #if ( portSTACK_GROWTH < 0 )
  1561. {
  1562. pxTopOfStack = &( pxNewTCB->pxStack[ uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
  1563. pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  1564. /* Check the alignment of the calculated top of stack is correct. */
  1565. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
  1566. #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
  1567. {
  1568. /* Also record the stack's high address, which may assist
  1569. * debugging. */
  1570. pxNewTCB->pxEndOfStack = pxTopOfStack;
  1571. }
  1572. #endif /* configRECORD_STACK_HIGH_ADDRESS */
  1573. }
  1574. #else /* portSTACK_GROWTH */
  1575. {
  1576. pxTopOfStack = pxNewTCB->pxStack;
  1577. pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
  1578. /* Check the alignment of the calculated top of stack is correct. */
  1579. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0U ) );
  1580. /* The other extreme of the stack space is required if stack checking is
  1581. * performed. */
  1582. pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( uxStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
  1583. }
  1584. #endif /* portSTACK_GROWTH */
  1585. /* Store the task name in the TCB. */
  1586. if( pcName != NULL )
  1587. {
  1588. for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  1589. {
  1590. pxNewTCB->pcTaskName[ x ] = pcName[ x ];
  1591. /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
  1592. * configMAX_TASK_NAME_LEN characters just in case the memory after the
  1593. * string is not accessible (extremely unlikely). */
  1594. if( pcName[ x ] == ( char ) 0x00 )
  1595. {
  1596. break;
  1597. }
  1598. else
  1599. {
  1600. mtCOVERAGE_TEST_MARKER();
  1601. }
  1602. }
  1603. /* Ensure the name string is terminated in the case that the string length
  1604. * was greater or equal to configMAX_TASK_NAME_LEN. */
  1605. pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1U ] = '\0';
  1606. }
  1607. else
  1608. {
  1609. mtCOVERAGE_TEST_MARKER();
  1610. }
  1611. /* This is used as an array index so must ensure it's not too large. */
  1612. configASSERT( uxPriority < configMAX_PRIORITIES );
  1613. if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  1614. {
  1615. uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  1616. }
  1617. else
  1618. {
  1619. mtCOVERAGE_TEST_MARKER();
  1620. }
  1621. pxNewTCB->uxPriority = uxPriority;
  1622. #if ( configUSE_MUTEXES == 1 )
  1623. {
  1624. pxNewTCB->uxBasePriority = uxPriority;
  1625. }
  1626. #endif /* configUSE_MUTEXES */
  1627. vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
  1628. vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
  1629. /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
  1630. * back to the containing TCB from a generic item in a list. */
  1631. listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
  1632. /* Event lists are always in priority order. */
  1633. listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority );
  1634. listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
  1635. #if ( portUSING_MPU_WRAPPERS == 1 )
  1636. {
  1637. vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, uxStackDepth );
  1638. }
  1639. #else
  1640. {
  1641. /* Avoid compiler warning about unreferenced parameter. */
  1642. ( void ) xRegions;
  1643. }
  1644. #endif
  1645. #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
  1646. {
  1647. /* Allocate and initialize memory for the task's TLS Block. */
  1648. configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
  1649. }
  1650. #endif
  1651. /* Initialize the TCB stack to look as if the task was already running,
  1652. * but had been interrupted by the scheduler. The return address is set
  1653. * to the start of the task function. Once the stack has been initialised
  1654. * the top of stack variable is updated. */
  1655. #if ( portUSING_MPU_WRAPPERS == 1 )
  1656. {
  1657. /* If the port has capability to detect stack overflow,
  1658. * pass the stack end address to the stack initialization
  1659. * function as well. */
  1660. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  1661. {
  1662. #if ( portSTACK_GROWTH < 0 )
  1663. {
  1664. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
  1665. }
  1666. #else /* portSTACK_GROWTH */
  1667. {
  1668. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
  1669. }
  1670. #endif /* portSTACK_GROWTH */
  1671. }
  1672. #else /* portHAS_STACK_OVERFLOW_CHECKING */
  1673. {
  1674. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
  1675. }
  1676. #endif /* portHAS_STACK_OVERFLOW_CHECKING */
  1677. }
  1678. #else /* portUSING_MPU_WRAPPERS */
  1679. {
  1680. /* If the port has capability to detect stack overflow,
  1681. * pass the stack end address to the stack initialization
  1682. * function as well. */
  1683. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  1684. {
  1685. #if ( portSTACK_GROWTH < 0 )
  1686. {
  1687. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
  1688. }
  1689. #else /* portSTACK_GROWTH */
  1690. {
  1691. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
  1692. }
  1693. #endif /* portSTACK_GROWTH */
  1694. }
  1695. #else /* portHAS_STACK_OVERFLOW_CHECKING */
  1696. {
  1697. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
  1698. }
  1699. #endif /* portHAS_STACK_OVERFLOW_CHECKING */
  1700. #if ( portSTACK_GROWTH < 0 )
  1701. {
  1702. configASSERT( ( ( portPOINTER_SIZE_TYPE ) ( pxTopOfStack - pxNewTCB->pxTopOfStack ) ) < ( ( portPOINTER_SIZE_TYPE ) uxStackDepth ) );
  1703. }
  1704. #else /* portSTACK_GROWTH */
  1705. {
  1706. configASSERT( ( ( portPOINTER_SIZE_TYPE ) ( pxNewTCB->pxTopOfStack - pxTopOfStack ) ) < ( ( portPOINTER_SIZE_TYPE ) uxStackDepth ) );
  1707. }
  1708. #endif /* portSTACK_GROWTH */
  1709. }
  1710. #endif /* portUSING_MPU_WRAPPERS */
  1711. /* Initialize task state and task attributes. */
  1712. #if ( configNUMBER_OF_CORES > 1 )
  1713. {
  1714. pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
  1715. /* Is this an idle task? */
  1716. if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) ( &prvIdleTask ) ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) ( &prvPassiveIdleTask ) ) )
  1717. {
  1718. pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
  1719. }
  1720. }
  1721. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  1722. if( pxCreatedTask != NULL )
  1723. {
  1724. /* Pass the handle out in an anonymous way. The handle can be used to
  1725. * change the created task's priority, delete the created task, etc.*/
  1726. *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
  1727. }
  1728. else
  1729. {
  1730. mtCOVERAGE_TEST_MARKER();
  1731. }
  1732. }
  1733. /*-----------------------------------------------------------*/
  1734. #if ( configNUMBER_OF_CORES == 1 )
  1735. static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
  1736. {
  1737. /* Ensure interrupts don't access the task lists while the lists are being
  1738. * updated. */
  1739. taskENTER_CRITICAL();
  1740. {
  1741. uxCurrentNumberOfTasks = ( UBaseType_t ) ( uxCurrentNumberOfTasks + 1U );
  1742. if( pxCurrentTCB == NULL )
  1743. {
  1744. /* There are no other tasks, or all the other tasks are in
  1745. * the suspended state - make this the current task. */
  1746. pxCurrentTCB = pxNewTCB;
  1747. if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
  1748. {
  1749. /* This is the first task to be created so do the preliminary
  1750. * initialisation required. We will not recover if this call
  1751. * fails, but we will report the failure. */
  1752. prvInitialiseTaskLists();
  1753. }
  1754. else
  1755. {
  1756. mtCOVERAGE_TEST_MARKER();
  1757. }
  1758. }
  1759. else
  1760. {
  1761. /* If the scheduler is not already running, make this task the
  1762. * current task if it is the highest priority task to be created
  1763. * so far. */
  1764. if( xSchedulerRunning == pdFALSE )
  1765. {
  1766. if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
  1767. {
  1768. pxCurrentTCB = pxNewTCB;
  1769. }
  1770. else
  1771. {
  1772. mtCOVERAGE_TEST_MARKER();
  1773. }
  1774. }
  1775. else
  1776. {
  1777. mtCOVERAGE_TEST_MARKER();
  1778. }
  1779. }
  1780. uxTaskNumber++;
  1781. #if ( configUSE_TRACE_FACILITY == 1 )
  1782. {
  1783. /* Add a counter into the TCB for tracing only. */
  1784. pxNewTCB->uxTCBNumber = uxTaskNumber;
  1785. }
  1786. #endif /* configUSE_TRACE_FACILITY */
  1787. traceTASK_CREATE( pxNewTCB );
  1788. prvAddTaskToReadyList( pxNewTCB );
  1789. portSETUP_TCB( pxNewTCB );
  1790. }
  1791. taskEXIT_CRITICAL();
  1792. if( xSchedulerRunning != pdFALSE )
  1793. {
  1794. /* If the created task is of a higher priority than the current task
  1795. * then it should run now. */
  1796. taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
  1797. }
  1798. else
  1799. {
  1800. mtCOVERAGE_TEST_MARKER();
  1801. }
  1802. }
  1803. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  1804. static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
  1805. {
  1806. /* Ensure interrupts don't access the task lists while the lists are being
  1807. * updated. */
  1808. taskENTER_CRITICAL();
  1809. {
  1810. uxCurrentNumberOfTasks++;
  1811. if( xSchedulerRunning == pdFALSE )
  1812. {
  1813. if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
  1814. {
  1815. /* This is the first task to be created so do the preliminary
  1816. * initialisation required. We will not recover if this call
  1817. * fails, but we will report the failure. */
  1818. prvInitialiseTaskLists();
  1819. }
  1820. else
  1821. {
  1822. mtCOVERAGE_TEST_MARKER();
  1823. }
  1824. /* All the cores start with idle tasks before the SMP scheduler
  1825. * is running. Idle tasks are assigned to cores when they are
  1826. * created in prvCreateIdleTasks(). */
  1827. }
  1828. uxTaskNumber++;
  1829. #if ( configUSE_TRACE_FACILITY == 1 )
  1830. {
  1831. /* Add a counter into the TCB for tracing only. */
  1832. pxNewTCB->uxTCBNumber = uxTaskNumber;
  1833. }
  1834. #endif /* configUSE_TRACE_FACILITY */
  1835. traceTASK_CREATE( pxNewTCB );
  1836. prvAddTaskToReadyList( pxNewTCB );
  1837. portSETUP_TCB( pxNewTCB );
  1838. if( xSchedulerRunning != pdFALSE )
  1839. {
  1840. /* If the created task is of a higher priority than another
  1841. * currently running task and preemption is on then it should
  1842. * run now. */
  1843. taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
  1844. }
  1845. else
  1846. {
  1847. mtCOVERAGE_TEST_MARKER();
  1848. }
  1849. }
  1850. taskEXIT_CRITICAL();
  1851. }
  1852. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  1853. /*-----------------------------------------------------------*/
  1854. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  1855. static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
  1856. size_t n )
  1857. {
  1858. size_t uxCharsWritten;
  1859. if( iSnprintfReturnValue < 0 )
  1860. {
  1861. /* Encoding error - Return 0 to indicate that nothing
  1862. * was written to the buffer. */
  1863. uxCharsWritten = 0;
  1864. }
  1865. else if( iSnprintfReturnValue >= ( int ) n )
  1866. {
  1867. /* This is the case when the supplied buffer is not
  1868. * large to hold the generated string. Return the
  1869. * number of characters actually written without
  1870. * counting the terminating NULL character. */
  1871. uxCharsWritten = n - 1U;
  1872. }
  1873. else
  1874. {
  1875. /* Complete string was written to the buffer. */
  1876. uxCharsWritten = ( size_t ) iSnprintfReturnValue;
  1877. }
  1878. return uxCharsWritten;
  1879. }
  1880. #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  1881. /*-----------------------------------------------------------*/
  1882. #if ( INCLUDE_vTaskDelete == 1 )
  1883. void vTaskDelete( TaskHandle_t xTaskToDelete )
  1884. {
  1885. TCB_t * pxTCB;
  1886. BaseType_t xDeleteTCBInIdleTask = pdFALSE;
  1887. BaseType_t xTaskIsRunningOrYielding;
  1888. traceENTER_vTaskDelete( xTaskToDelete );
  1889. taskENTER_CRITICAL();
  1890. {
  1891. /* If null is passed in here then it is the calling task that is
  1892. * being deleted. */
  1893. pxTCB = prvGetTCBFromHandle( xTaskToDelete );
  1894. configASSERT( pxTCB != NULL );
  1895. /* Remove task from the ready/delayed list. */
  1896. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  1897. {
  1898. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  1899. }
  1900. else
  1901. {
  1902. mtCOVERAGE_TEST_MARKER();
  1903. }
  1904. /* Is the task waiting on an event also? */
  1905. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  1906. {
  1907. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1908. }
  1909. else
  1910. {
  1911. mtCOVERAGE_TEST_MARKER();
  1912. }
  1913. /* Increment the uxTaskNumber also so kernel aware debuggers can
  1914. * detect that the task lists need re-generating. This is done before
  1915. * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
  1916. * not return. */
  1917. uxTaskNumber++;
  1918. /* Use temp variable as distinct sequence points for reading volatile
  1919. * variables prior to a logical operator to ensure compliance with
  1920. * MISRA C 2012 Rule 13.5. */
  1921. xTaskIsRunningOrYielding = taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB );
  1922. /* If the task is running (or yielding), we must add it to the
  1923. * termination list so that an idle task can delete it when it is
  1924. * no longer running. */
  1925. if( ( xSchedulerRunning != pdFALSE ) && ( xTaskIsRunningOrYielding != pdFALSE ) )
  1926. {
  1927. /* A running task or a task which is scheduled to yield is being
  1928. * deleted. This cannot complete when the task is still running
  1929. * on a core, as a context switch to another task is required.
  1930. * Place the task in the termination list. The idle task will check
  1931. * the termination list and free up any memory allocated by the
  1932. * scheduler for the TCB and stack of the deleted task. */
  1933. vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
  1934. /* Increment the ucTasksDeleted variable so the idle task knows
  1935. * there is a task that has been deleted and that it should therefore
  1936. * check the xTasksWaitingTermination list. */
  1937. ++uxDeletedTasksWaitingCleanUp;
  1938. /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
  1939. * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
  1940. traceTASK_DELETE( pxTCB );
  1941. /* Delete the task TCB in idle task. */
  1942. xDeleteTCBInIdleTask = pdTRUE;
  1943. /* The pre-delete hook is primarily for the Windows simulator,
  1944. * in which Windows specific clean up operations are performed,
  1945. * after which it is not possible to yield away from this task -
  1946. * hence xYieldPending is used to latch that a context switch is
  1947. * required. */
  1948. #if ( configNUMBER_OF_CORES == 1 )
  1949. portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
  1950. #else
  1951. portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
  1952. #endif
  1953. /* In the case of SMP, it is possible that the task being deleted
  1954. * is running on another core. We must evict the task before
  1955. * exiting the critical section to ensure that the task cannot
  1956. * take an action which puts it back on ready/state/event list,
  1957. * thereby nullifying the delete operation. Once evicted, the
  1958. * task won't be scheduled ever as it will no longer be on the
  1959. * ready list. */
  1960. #if ( configNUMBER_OF_CORES > 1 )
  1961. {
  1962. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  1963. {
  1964. if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
  1965. {
  1966. configASSERT( uxSchedulerSuspended == 0 );
  1967. taskYIELD_WITHIN_API();
  1968. }
  1969. else
  1970. {
  1971. prvYieldCore( pxTCB->xTaskRunState );
  1972. }
  1973. }
  1974. }
  1975. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  1976. }
  1977. else
  1978. {
  1979. --uxCurrentNumberOfTasks;
  1980. traceTASK_DELETE( pxTCB );
  1981. /* Reset the next expected unblock time in case it referred to
  1982. * the task that has just been deleted. */
  1983. prvResetNextTaskUnblockTime();
  1984. }
  1985. }
  1986. taskEXIT_CRITICAL();
  1987. /* If the task is not deleting itself, call prvDeleteTCB from outside of
  1988. * critical section. If a task deletes itself, prvDeleteTCB is called
  1989. * from prvCheckTasksWaitingTermination which is called from Idle task. */
  1990. if( xDeleteTCBInIdleTask != pdTRUE )
  1991. {
  1992. prvDeleteTCB( pxTCB );
  1993. }
  1994. /* Force a reschedule if it is the currently running task that has just
  1995. * been deleted. */
  1996. #if ( configNUMBER_OF_CORES == 1 )
  1997. {
  1998. if( xSchedulerRunning != pdFALSE )
  1999. {
  2000. if( pxTCB == pxCurrentTCB )
  2001. {
  2002. configASSERT( uxSchedulerSuspended == 0 );
  2003. taskYIELD_WITHIN_API();
  2004. }
  2005. else
  2006. {
  2007. mtCOVERAGE_TEST_MARKER();
  2008. }
  2009. }
  2010. }
  2011. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  2012. traceRETURN_vTaskDelete();
  2013. }
  2014. #endif /* INCLUDE_vTaskDelete */
  2015. /*-----------------------------------------------------------*/
  2016. #if ( INCLUDE_xTaskDelayUntil == 1 )
  2017. BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
  2018. const TickType_t xTimeIncrement )
  2019. {
  2020. TickType_t xTimeToWake;
  2021. BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
  2022. traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
  2023. configASSERT( pxPreviousWakeTime );
  2024. configASSERT( ( xTimeIncrement > 0U ) );
  2025. vTaskSuspendAll();
  2026. {
  2027. /* Minor optimisation. The tick count cannot change in this
  2028. * block. */
  2029. const TickType_t xConstTickCount = xTickCount;
  2030. configASSERT( uxSchedulerSuspended == 1U );
  2031. /* Generate the tick time at which the task wants to wake. */
  2032. xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
  2033. if( xConstTickCount < *pxPreviousWakeTime )
  2034. {
  2035. /* The tick count has overflowed since this function was
  2036. * lasted called. In this case the only time we should ever
  2037. * actually delay is if the wake time has also overflowed,
  2038. * and the wake time is greater than the tick time. When this
  2039. * is the case it is as if neither time had overflowed. */
  2040. if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
  2041. {
  2042. xShouldDelay = pdTRUE;
  2043. }
  2044. else
  2045. {
  2046. mtCOVERAGE_TEST_MARKER();
  2047. }
  2048. }
  2049. else
  2050. {
  2051. /* The tick time has not overflowed. In this case we will
  2052. * delay if either the wake time has overflowed, and/or the
  2053. * tick time is less than the wake time. */
  2054. if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
  2055. {
  2056. xShouldDelay = pdTRUE;
  2057. }
  2058. else
  2059. {
  2060. mtCOVERAGE_TEST_MARKER();
  2061. }
  2062. }
  2063. /* Update the wake time ready for the next call. */
  2064. *pxPreviousWakeTime = xTimeToWake;
  2065. if( xShouldDelay != pdFALSE )
  2066. {
  2067. traceTASK_DELAY_UNTIL( xTimeToWake );
  2068. /* prvAddCurrentTaskToDelayedList() needs the block time, not
  2069. * the time to wake, so subtract the current tick count. */
  2070. prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
  2071. }
  2072. else
  2073. {
  2074. mtCOVERAGE_TEST_MARKER();
  2075. }
  2076. }
  2077. xAlreadyYielded = xTaskResumeAll();
  2078. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  2079. * have put ourselves to sleep. */
  2080. if( xAlreadyYielded == pdFALSE )
  2081. {
  2082. taskYIELD_WITHIN_API();
  2083. }
  2084. else
  2085. {
  2086. mtCOVERAGE_TEST_MARKER();
  2087. }
  2088. traceRETURN_xTaskDelayUntil( xShouldDelay );
  2089. return xShouldDelay;
  2090. }
  2091. #endif /* INCLUDE_xTaskDelayUntil */
  2092. /*-----------------------------------------------------------*/
  2093. #if ( INCLUDE_vTaskDelay == 1 )
  2094. void vTaskDelay( const TickType_t xTicksToDelay )
  2095. {
  2096. BaseType_t xAlreadyYielded = pdFALSE;
  2097. traceENTER_vTaskDelay( xTicksToDelay );
  2098. /* A delay time of zero just forces a reschedule. */
  2099. if( xTicksToDelay > ( TickType_t ) 0U )
  2100. {
  2101. vTaskSuspendAll();
  2102. {
  2103. configASSERT( uxSchedulerSuspended == 1U );
  2104. traceTASK_DELAY();
  2105. /* A task that is removed from the event list while the
  2106. * scheduler is suspended will not get placed in the ready
  2107. * list or removed from the blocked list until the scheduler
  2108. * is resumed.
  2109. *
  2110. * This task cannot be in an event list as it is the currently
  2111. * executing task. */
  2112. prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
  2113. }
  2114. xAlreadyYielded = xTaskResumeAll();
  2115. }
  2116. else
  2117. {
  2118. mtCOVERAGE_TEST_MARKER();
  2119. }
  2120. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  2121. * have put ourselves to sleep. */
  2122. if( xAlreadyYielded == pdFALSE )
  2123. {
  2124. taskYIELD_WITHIN_API();
  2125. }
  2126. else
  2127. {
  2128. mtCOVERAGE_TEST_MARKER();
  2129. }
  2130. traceRETURN_vTaskDelay();
  2131. }
  2132. #endif /* INCLUDE_vTaskDelay */
  2133. /*-----------------------------------------------------------*/
  2134. #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
  2135. eTaskState eTaskGetState( TaskHandle_t xTask )
  2136. {
  2137. eTaskState eReturn;
  2138. List_t const * pxStateList;
  2139. List_t const * pxEventList;
  2140. List_t const * pxDelayedList;
  2141. List_t const * pxOverflowedDelayedList;
  2142. const TCB_t * const pxTCB = xTask;
  2143. traceENTER_eTaskGetState( xTask );
  2144. configASSERT( pxTCB != NULL );
  2145. #if ( configNUMBER_OF_CORES == 1 )
  2146. if( pxTCB == pxCurrentTCB )
  2147. {
  2148. /* The task calling this function is querying its own state. */
  2149. eReturn = eRunning;
  2150. }
  2151. else
  2152. #endif
  2153. {
  2154. taskENTER_CRITICAL();
  2155. {
  2156. pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
  2157. pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
  2158. pxDelayedList = pxDelayedTaskList;
  2159. pxOverflowedDelayedList = pxOverflowDelayedTaskList;
  2160. }
  2161. taskEXIT_CRITICAL();
  2162. if( pxEventList == &xPendingReadyList )
  2163. {
  2164. /* The task has been placed on the pending ready list, so its
  2165. * state is eReady regardless of what list the task's state list
  2166. * item is currently placed on. */
  2167. eReturn = eReady;
  2168. }
  2169. else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
  2170. {
  2171. /* The task being queried is referenced from one of the Blocked
  2172. * lists. */
  2173. eReturn = eBlocked;
  2174. }
  2175. #if ( INCLUDE_vTaskSuspend == 1 )
  2176. else if( pxStateList == &xSuspendedTaskList )
  2177. {
  2178. /* The task being queried is referenced from the suspended
  2179. * list. Is it genuinely suspended or is it blocked
  2180. * indefinitely? */
  2181. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
  2182. {
  2183. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  2184. {
  2185. BaseType_t x;
  2186. /* The task does not appear on the event list item of
  2187. * and of the RTOS objects, but could still be in the
  2188. * blocked state if it is waiting on its notification
  2189. * rather than waiting on an object. If not, is
  2190. * suspended. */
  2191. eReturn = eSuspended;
  2192. for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  2193. {
  2194. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  2195. {
  2196. eReturn = eBlocked;
  2197. break;
  2198. }
  2199. }
  2200. }
  2201. #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  2202. {
  2203. eReturn = eSuspended;
  2204. }
  2205. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  2206. }
  2207. else
  2208. {
  2209. eReturn = eBlocked;
  2210. }
  2211. }
  2212. #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
  2213. #if ( INCLUDE_vTaskDelete == 1 )
  2214. else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
  2215. {
  2216. /* The task being queried is referenced from the deleted
  2217. * tasks list, or it is not referenced from any lists at
  2218. * all. */
  2219. eReturn = eDeleted;
  2220. }
  2221. #endif
  2222. else
  2223. {
  2224. #if ( configNUMBER_OF_CORES == 1 )
  2225. {
  2226. /* If the task is not in any other state, it must be in the
  2227. * Ready (including pending ready) state. */
  2228. eReturn = eReady;
  2229. }
  2230. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  2231. {
  2232. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  2233. {
  2234. /* Is it actively running on a core? */
  2235. eReturn = eRunning;
  2236. }
  2237. else
  2238. {
  2239. /* If the task is not in any other state, it must be in the
  2240. * Ready (including pending ready) state. */
  2241. eReturn = eReady;
  2242. }
  2243. }
  2244. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  2245. }
  2246. }
  2247. traceRETURN_eTaskGetState( eReturn );
  2248. return eReturn;
  2249. }
  2250. #endif /* INCLUDE_eTaskGetState */
  2251. /*-----------------------------------------------------------*/
  2252. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  2253. UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
  2254. {
  2255. TCB_t const * pxTCB;
  2256. UBaseType_t uxReturn;
  2257. traceENTER_uxTaskPriorityGet( xTask );
  2258. portBASE_TYPE_ENTER_CRITICAL();
  2259. {
  2260. /* If null is passed in here then it is the priority of the task
  2261. * that called uxTaskPriorityGet() that is being queried. */
  2262. pxTCB = prvGetTCBFromHandle( xTask );
  2263. configASSERT( pxTCB != NULL );
  2264. uxReturn = pxTCB->uxPriority;
  2265. }
  2266. portBASE_TYPE_EXIT_CRITICAL();
  2267. traceRETURN_uxTaskPriorityGet( uxReturn );
  2268. return uxReturn;
  2269. }
  2270. #endif /* INCLUDE_uxTaskPriorityGet */
  2271. /*-----------------------------------------------------------*/
  2272. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  2273. UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
  2274. {
  2275. TCB_t const * pxTCB;
  2276. UBaseType_t uxReturn;
  2277. UBaseType_t uxSavedInterruptStatus;
  2278. traceENTER_uxTaskPriorityGetFromISR( xTask );
  2279. /* RTOS ports that support interrupt nesting have the concept of a
  2280. * maximum system call (or maximum API call) interrupt priority.
  2281. * Interrupts that are above the maximum system call priority are keep
  2282. * permanently enabled, even when the RTOS kernel is in a critical section,
  2283. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  2284. * is defined in FreeRTOSConfig.h then
  2285. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  2286. * failure if a FreeRTOS API function is called from an interrupt that has
  2287. * been assigned a priority above the configured maximum system call
  2288. * priority. Only FreeRTOS functions that end in FromISR can be called
  2289. * from interrupts that have been assigned a priority at or (logically)
  2290. * below the maximum system call interrupt priority. FreeRTOS maintains a
  2291. * separate interrupt safe API to ensure interrupt entry is as fast and as
  2292. * simple as possible. More information (albeit Cortex-M specific) is
  2293. * provided on the following link:
  2294. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  2295. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  2296. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  2297. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  2298. /* coverity[misra_c_2012_directive_4_7_violation] */
  2299. uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
  2300. {
  2301. /* If null is passed in here then it is the priority of the calling
  2302. * task that is being queried. */
  2303. pxTCB = prvGetTCBFromHandle( xTask );
  2304. configASSERT( pxTCB != NULL );
  2305. uxReturn = pxTCB->uxPriority;
  2306. }
  2307. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  2308. traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
  2309. return uxReturn;
  2310. }
  2311. #endif /* INCLUDE_uxTaskPriorityGet */
  2312. /*-----------------------------------------------------------*/
  2313. #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
  2314. UBaseType_t uxTaskBasePriorityGet( const TaskHandle_t xTask )
  2315. {
  2316. TCB_t const * pxTCB;
  2317. UBaseType_t uxReturn;
  2318. traceENTER_uxTaskBasePriorityGet( xTask );
  2319. portBASE_TYPE_ENTER_CRITICAL();
  2320. {
  2321. /* If null is passed in here then it is the base priority of the task
  2322. * that called uxTaskBasePriorityGet() that is being queried. */
  2323. pxTCB = prvGetTCBFromHandle( xTask );
  2324. configASSERT( pxTCB != NULL );
  2325. uxReturn = pxTCB->uxBasePriority;
  2326. }
  2327. portBASE_TYPE_EXIT_CRITICAL();
  2328. traceRETURN_uxTaskBasePriorityGet( uxReturn );
  2329. return uxReturn;
  2330. }
  2331. #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
  2332. /*-----------------------------------------------------------*/
  2333. #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) )
  2334. UBaseType_t uxTaskBasePriorityGetFromISR( const TaskHandle_t xTask )
  2335. {
  2336. TCB_t const * pxTCB;
  2337. UBaseType_t uxReturn;
  2338. UBaseType_t uxSavedInterruptStatus;
  2339. traceENTER_uxTaskBasePriorityGetFromISR( xTask );
  2340. /* RTOS ports that support interrupt nesting have the concept of a
  2341. * maximum system call (or maximum API call) interrupt priority.
  2342. * Interrupts that are above the maximum system call priority are keep
  2343. * permanently enabled, even when the RTOS kernel is in a critical section,
  2344. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  2345. * is defined in FreeRTOSConfig.h then
  2346. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  2347. * failure if a FreeRTOS API function is called from an interrupt that has
  2348. * been assigned a priority above the configured maximum system call
  2349. * priority. Only FreeRTOS functions that end in FromISR can be called
  2350. * from interrupts that have been assigned a priority at or (logically)
  2351. * below the maximum system call interrupt priority. FreeRTOS maintains a
  2352. * separate interrupt safe API to ensure interrupt entry is as fast and as
  2353. * simple as possible. More information (albeit Cortex-M specific) is
  2354. * provided on the following link:
  2355. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  2356. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  2357. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  2358. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  2359. /* coverity[misra_c_2012_directive_4_7_violation] */
  2360. uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
  2361. {
  2362. /* If null is passed in here then it is the base priority of the calling
  2363. * task that is being queried. */
  2364. pxTCB = prvGetTCBFromHandle( xTask );
  2365. configASSERT( pxTCB != NULL );
  2366. uxReturn = pxTCB->uxBasePriority;
  2367. }
  2368. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  2369. traceRETURN_uxTaskBasePriorityGetFromISR( uxReturn );
  2370. return uxReturn;
  2371. }
  2372. #endif /* #if ( ( INCLUDE_uxTaskPriorityGet == 1 ) && ( configUSE_MUTEXES == 1 ) ) */
  2373. /*-----------------------------------------------------------*/
  2374. #if ( INCLUDE_vTaskPrioritySet == 1 )
  2375. void vTaskPrioritySet( TaskHandle_t xTask,
  2376. UBaseType_t uxNewPriority )
  2377. {
  2378. TCB_t * pxTCB;
  2379. UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
  2380. BaseType_t xYieldRequired = pdFALSE;
  2381. #if ( configNUMBER_OF_CORES > 1 )
  2382. BaseType_t xYieldForTask = pdFALSE;
  2383. #endif
  2384. traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
  2385. configASSERT( uxNewPriority < configMAX_PRIORITIES );
  2386. /* Ensure the new priority is valid. */
  2387. if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  2388. {
  2389. uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  2390. }
  2391. else
  2392. {
  2393. mtCOVERAGE_TEST_MARKER();
  2394. }
  2395. taskENTER_CRITICAL();
  2396. {
  2397. /* If null is passed in here then it is the priority of the calling
  2398. * task that is being changed. */
  2399. pxTCB = prvGetTCBFromHandle( xTask );
  2400. configASSERT( pxTCB != NULL );
  2401. traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
  2402. #if ( configUSE_MUTEXES == 1 )
  2403. {
  2404. uxCurrentBasePriority = pxTCB->uxBasePriority;
  2405. }
  2406. #else
  2407. {
  2408. uxCurrentBasePriority = pxTCB->uxPriority;
  2409. }
  2410. #endif
  2411. if( uxCurrentBasePriority != uxNewPriority )
  2412. {
  2413. /* The priority change may have readied a task of higher
  2414. * priority than a running task. */
  2415. if( uxNewPriority > uxCurrentBasePriority )
  2416. {
  2417. #if ( configNUMBER_OF_CORES == 1 )
  2418. {
  2419. if( pxTCB != pxCurrentTCB )
  2420. {
  2421. /* The priority of a task other than the currently
  2422. * running task is being raised. Is the priority being
  2423. * raised above that of the running task? */
  2424. if( uxNewPriority > pxCurrentTCB->uxPriority )
  2425. {
  2426. xYieldRequired = pdTRUE;
  2427. }
  2428. else
  2429. {
  2430. mtCOVERAGE_TEST_MARKER();
  2431. }
  2432. }
  2433. else
  2434. {
  2435. /* The priority of the running task is being raised,
  2436. * but the running task must already be the highest
  2437. * priority task able to run so no yield is required. */
  2438. }
  2439. }
  2440. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  2441. {
  2442. /* The priority of a task is being raised so
  2443. * perform a yield for this task later. */
  2444. xYieldForTask = pdTRUE;
  2445. }
  2446. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  2447. }
  2448. else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  2449. {
  2450. /* Setting the priority of a running task down means
  2451. * there may now be another task of higher priority that
  2452. * is ready to execute. */
  2453. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  2454. if( pxTCB->xPreemptionDisable == pdFALSE )
  2455. #endif
  2456. {
  2457. xYieldRequired = pdTRUE;
  2458. }
  2459. }
  2460. else
  2461. {
  2462. /* Setting the priority of any other task down does not
  2463. * require a yield as the running task must be above the
  2464. * new priority of the task being modified. */
  2465. }
  2466. /* Remember the ready list the task might be referenced from
  2467. * before its uxPriority member is changed so the
  2468. * taskRESET_READY_PRIORITY() macro can function correctly. */
  2469. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  2470. #if ( configUSE_MUTEXES == 1 )
  2471. {
  2472. /* Only change the priority being used if the task is not
  2473. * currently using an inherited priority or the new priority
  2474. * is bigger than the inherited priority. */
  2475. if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
  2476. {
  2477. pxTCB->uxPriority = uxNewPriority;
  2478. }
  2479. else
  2480. {
  2481. mtCOVERAGE_TEST_MARKER();
  2482. }
  2483. /* The base priority gets set whatever. */
  2484. pxTCB->uxBasePriority = uxNewPriority;
  2485. }
  2486. #else /* if ( configUSE_MUTEXES == 1 ) */
  2487. {
  2488. pxTCB->uxPriority = uxNewPriority;
  2489. }
  2490. #endif /* if ( configUSE_MUTEXES == 1 ) */
  2491. /* Only reset the event list item value if the value is not
  2492. * being used for anything else. */
  2493. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
  2494. {
  2495. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) );
  2496. }
  2497. else
  2498. {
  2499. mtCOVERAGE_TEST_MARKER();
  2500. }
  2501. /* If the task is in the blocked or suspended list we need do
  2502. * nothing more than change its priority variable. However, if
  2503. * the task is in a ready list it needs to be removed and placed
  2504. * in the list appropriate to its new priority. */
  2505. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
  2506. {
  2507. /* The task is currently in its ready list - remove before
  2508. * adding it to its new ready list. As we are in a critical
  2509. * section we can do this even if the scheduler is suspended. */
  2510. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  2511. {
  2512. /* It is known that the task is in its ready list so
  2513. * there is no need to check again and the port level
  2514. * reset macro can be called directly. */
  2515. portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
  2516. }
  2517. else
  2518. {
  2519. mtCOVERAGE_TEST_MARKER();
  2520. }
  2521. prvAddTaskToReadyList( pxTCB );
  2522. }
  2523. else
  2524. {
  2525. #if ( configNUMBER_OF_CORES == 1 )
  2526. {
  2527. mtCOVERAGE_TEST_MARKER();
  2528. }
  2529. #else
  2530. {
  2531. /* It's possible that xYieldForTask was already set to pdTRUE because
  2532. * its priority is being raised. However, since it is not in a ready list
  2533. * we don't actually need to yield for it. */
  2534. xYieldForTask = pdFALSE;
  2535. }
  2536. #endif
  2537. }
  2538. if( xYieldRequired != pdFALSE )
  2539. {
  2540. /* The running task priority is set down. Request the task to yield. */
  2541. taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
  2542. }
  2543. else
  2544. {
  2545. #if ( configNUMBER_OF_CORES > 1 )
  2546. if( xYieldForTask != pdFALSE )
  2547. {
  2548. /* The priority of the task is being raised. If a running
  2549. * task has priority lower than this task, it should yield
  2550. * for this task. */
  2551. taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
  2552. }
  2553. else
  2554. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  2555. {
  2556. mtCOVERAGE_TEST_MARKER();
  2557. }
  2558. }
  2559. /* Remove compiler warning about unused variables when the port
  2560. * optimised task selection is not being used. */
  2561. ( void ) uxPriorityUsedOnEntry;
  2562. }
  2563. }
  2564. taskEXIT_CRITICAL();
  2565. traceRETURN_vTaskPrioritySet();
  2566. }
  2567. #endif /* INCLUDE_vTaskPrioritySet */
  2568. /*-----------------------------------------------------------*/
  2569. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  2570. void vTaskCoreAffinitySet( const TaskHandle_t xTask,
  2571. UBaseType_t uxCoreAffinityMask )
  2572. {
  2573. TCB_t * pxTCB;
  2574. BaseType_t xCoreID;
  2575. traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
  2576. taskENTER_CRITICAL();
  2577. {
  2578. pxTCB = prvGetTCBFromHandle( xTask );
  2579. configASSERT( pxTCB != NULL );
  2580. pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
  2581. if( xSchedulerRunning != pdFALSE )
  2582. {
  2583. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  2584. {
  2585. xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
  2586. /* If the task can no longer run on the core it was running,
  2587. * request the core to yield. */
  2588. if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
  2589. {
  2590. prvYieldCore( xCoreID );
  2591. }
  2592. }
  2593. else
  2594. {
  2595. #if ( configUSE_PREEMPTION == 1 )
  2596. {
  2597. /* The SMP scheduler requests a core to yield when a ready
  2598. * task is able to run. It is possible that the core affinity
  2599. * of the ready task is changed before the requested core
  2600. * can select it to run. In that case, the task may not be
  2601. * selected by the previously requested core due to core affinity
  2602. * constraint and the SMP scheduler must select a new core to
  2603. * yield for the task. */
  2604. prvYieldForTask( xTask );
  2605. }
  2606. #else /* #if( configUSE_PREEMPTION == 1 ) */
  2607. {
  2608. mtCOVERAGE_TEST_MARKER();
  2609. }
  2610. #endif /* #if( configUSE_PREEMPTION == 1 ) */
  2611. }
  2612. }
  2613. }
  2614. taskEXIT_CRITICAL();
  2615. traceRETURN_vTaskCoreAffinitySet();
  2616. }
  2617. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  2618. /*-----------------------------------------------------------*/
  2619. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
  2620. UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
  2621. {
  2622. const TCB_t * pxTCB;
  2623. UBaseType_t uxCoreAffinityMask;
  2624. traceENTER_vTaskCoreAffinityGet( xTask );
  2625. portBASE_TYPE_ENTER_CRITICAL();
  2626. {
  2627. pxTCB = prvGetTCBFromHandle( xTask );
  2628. configASSERT( pxTCB != NULL );
  2629. uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
  2630. }
  2631. portBASE_TYPE_EXIT_CRITICAL();
  2632. traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
  2633. return uxCoreAffinityMask;
  2634. }
  2635. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
  2636. /*-----------------------------------------------------------*/
  2637. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  2638. void vTaskPreemptionDisable( const TaskHandle_t xTask )
  2639. {
  2640. TCB_t * pxTCB;
  2641. traceENTER_vTaskPreemptionDisable( xTask );
  2642. taskENTER_CRITICAL();
  2643. {
  2644. pxTCB = prvGetTCBFromHandle( xTask );
  2645. configASSERT( pxTCB != NULL );
  2646. pxTCB->xPreemptionDisable = pdTRUE;
  2647. }
  2648. taskEXIT_CRITICAL();
  2649. traceRETURN_vTaskPreemptionDisable();
  2650. }
  2651. #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
  2652. /*-----------------------------------------------------------*/
  2653. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  2654. void vTaskPreemptionEnable( const TaskHandle_t xTask )
  2655. {
  2656. TCB_t * pxTCB;
  2657. BaseType_t xCoreID;
  2658. traceENTER_vTaskPreemptionEnable( xTask );
  2659. taskENTER_CRITICAL();
  2660. {
  2661. pxTCB = prvGetTCBFromHandle( xTask );
  2662. configASSERT( pxTCB != NULL );
  2663. pxTCB->xPreemptionDisable = pdFALSE;
  2664. if( xSchedulerRunning != pdFALSE )
  2665. {
  2666. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  2667. {
  2668. xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
  2669. prvYieldCore( xCoreID );
  2670. }
  2671. }
  2672. }
  2673. taskEXIT_CRITICAL();
  2674. traceRETURN_vTaskPreemptionEnable();
  2675. }
  2676. #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
  2677. /*-----------------------------------------------------------*/
  2678. #if ( INCLUDE_vTaskSuspend == 1 )
  2679. void vTaskSuspend( TaskHandle_t xTaskToSuspend )
  2680. {
  2681. TCB_t * pxTCB;
  2682. traceENTER_vTaskSuspend( xTaskToSuspend );
  2683. taskENTER_CRITICAL();
  2684. {
  2685. /* If null is passed in here then it is the running task that is
  2686. * being suspended. */
  2687. pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
  2688. configASSERT( pxTCB != NULL );
  2689. traceTASK_SUSPEND( pxTCB );
  2690. /* Remove task from the ready/delayed list and place in the
  2691. * suspended list. */
  2692. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  2693. {
  2694. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  2695. }
  2696. else
  2697. {
  2698. mtCOVERAGE_TEST_MARKER();
  2699. }
  2700. /* Is the task waiting on an event also? */
  2701. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  2702. {
  2703. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  2704. }
  2705. else
  2706. {
  2707. mtCOVERAGE_TEST_MARKER();
  2708. }
  2709. vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
  2710. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  2711. {
  2712. BaseType_t x;
  2713. for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  2714. {
  2715. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  2716. {
  2717. /* The task was blocked to wait for a notification, but is
  2718. * now suspended, so no notification was received. */
  2719. pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
  2720. }
  2721. }
  2722. }
  2723. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  2724. /* In the case of SMP, it is possible that the task being suspended
  2725. * is running on another core. We must evict the task before
  2726. * exiting the critical section to ensure that the task cannot
  2727. * take an action which puts it back on ready/state/event list,
  2728. * thereby nullifying the suspend operation. Once evicted, the
  2729. * task won't be scheduled before it is resumed as it will no longer
  2730. * be on the ready list. */
  2731. #if ( configNUMBER_OF_CORES > 1 )
  2732. {
  2733. if( xSchedulerRunning != pdFALSE )
  2734. {
  2735. /* Reset the next expected unblock time in case it referred to the
  2736. * task that is now in the Suspended state. */
  2737. prvResetNextTaskUnblockTime();
  2738. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  2739. {
  2740. if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
  2741. {
  2742. /* The current task has just been suspended. */
  2743. configASSERT( uxSchedulerSuspended == 0 );
  2744. vTaskYieldWithinAPI();
  2745. }
  2746. else
  2747. {
  2748. prvYieldCore( pxTCB->xTaskRunState );
  2749. }
  2750. }
  2751. else
  2752. {
  2753. mtCOVERAGE_TEST_MARKER();
  2754. }
  2755. }
  2756. else
  2757. {
  2758. mtCOVERAGE_TEST_MARKER();
  2759. }
  2760. }
  2761. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  2762. }
  2763. taskEXIT_CRITICAL();
  2764. #if ( configNUMBER_OF_CORES == 1 )
  2765. {
  2766. UBaseType_t uxCurrentListLength;
  2767. if( xSchedulerRunning != pdFALSE )
  2768. {
  2769. /* Reset the next expected unblock time in case it referred to the
  2770. * task that is now in the Suspended state. */
  2771. taskENTER_CRITICAL();
  2772. {
  2773. prvResetNextTaskUnblockTime();
  2774. }
  2775. taskEXIT_CRITICAL();
  2776. }
  2777. else
  2778. {
  2779. mtCOVERAGE_TEST_MARKER();
  2780. }
  2781. if( pxTCB == pxCurrentTCB )
  2782. {
  2783. if( xSchedulerRunning != pdFALSE )
  2784. {
  2785. /* The current task has just been suspended. */
  2786. configASSERT( uxSchedulerSuspended == 0 );
  2787. portYIELD_WITHIN_API();
  2788. }
  2789. else
  2790. {
  2791. /* The scheduler is not running, but the task that was pointed
  2792. * to by pxCurrentTCB has just been suspended and pxCurrentTCB
  2793. * must be adjusted to point to a different task. */
  2794. /* Use a temp variable as a distinct sequence point for reading
  2795. * volatile variables prior to a comparison to ensure compliance
  2796. * with MISRA C 2012 Rule 13.2. */
  2797. uxCurrentListLength = listCURRENT_LIST_LENGTH( &xSuspendedTaskList );
  2798. if( uxCurrentListLength == uxCurrentNumberOfTasks )
  2799. {
  2800. /* No other tasks are ready, so set pxCurrentTCB back to
  2801. * NULL so when the next task is created pxCurrentTCB will
  2802. * be set to point to it no matter what its relative priority
  2803. * is. */
  2804. pxCurrentTCB = NULL;
  2805. }
  2806. else
  2807. {
  2808. vTaskSwitchContext();
  2809. }
  2810. }
  2811. }
  2812. else
  2813. {
  2814. mtCOVERAGE_TEST_MARKER();
  2815. }
  2816. }
  2817. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  2818. traceRETURN_vTaskSuspend();
  2819. }
  2820. #endif /* INCLUDE_vTaskSuspend */
  2821. /*-----------------------------------------------------------*/
  2822. #if ( INCLUDE_vTaskSuspend == 1 )
  2823. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
  2824. {
  2825. BaseType_t xReturn = pdFALSE;
  2826. const TCB_t * const pxTCB = xTask;
  2827. /* Accesses xPendingReadyList so must be called from a critical
  2828. * section. */
  2829. /* It does not make sense to check if the calling task is suspended. */
  2830. configASSERT( xTask );
  2831. /* Is the task being resumed actually in the suspended list? */
  2832. if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
  2833. {
  2834. /* Has the task already been resumed from within an ISR? */
  2835. if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
  2836. {
  2837. /* Is it in the suspended list because it is in the Suspended
  2838. * state, or because it is blocked with no timeout? */
  2839. if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
  2840. {
  2841. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  2842. {
  2843. BaseType_t x;
  2844. /* The task does not appear on the event list item of
  2845. * and of the RTOS objects, but could still be in the
  2846. * blocked state if it is waiting on its notification
  2847. * rather than waiting on an object. If not, is
  2848. * suspended. */
  2849. xReturn = pdTRUE;
  2850. for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  2851. {
  2852. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  2853. {
  2854. xReturn = pdFALSE;
  2855. break;
  2856. }
  2857. }
  2858. }
  2859. #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  2860. {
  2861. xReturn = pdTRUE;
  2862. }
  2863. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  2864. }
  2865. else
  2866. {
  2867. mtCOVERAGE_TEST_MARKER();
  2868. }
  2869. }
  2870. else
  2871. {
  2872. mtCOVERAGE_TEST_MARKER();
  2873. }
  2874. }
  2875. else
  2876. {
  2877. mtCOVERAGE_TEST_MARKER();
  2878. }
  2879. return xReturn;
  2880. }
  2881. #endif /* INCLUDE_vTaskSuspend */
  2882. /*-----------------------------------------------------------*/
  2883. #if ( INCLUDE_vTaskSuspend == 1 )
  2884. void vTaskResume( TaskHandle_t xTaskToResume )
  2885. {
  2886. TCB_t * const pxTCB = xTaskToResume;
  2887. traceENTER_vTaskResume( xTaskToResume );
  2888. /* It does not make sense to resume the calling task. */
  2889. configASSERT( xTaskToResume );
  2890. #if ( configNUMBER_OF_CORES == 1 )
  2891. /* The parameter cannot be NULL as it is impossible to resume the
  2892. * currently executing task. */
  2893. if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
  2894. #else
  2895. /* The parameter cannot be NULL as it is impossible to resume the
  2896. * currently executing task. It is also impossible to resume a task
  2897. * that is actively running on another core but it is not safe
  2898. * to check their run state here. Therefore, we get into a critical
  2899. * section and check if the task is actually suspended or not. */
  2900. if( pxTCB != NULL )
  2901. #endif
  2902. {
  2903. taskENTER_CRITICAL();
  2904. {
  2905. if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
  2906. {
  2907. traceTASK_RESUME( pxTCB );
  2908. /* The ready list can be accessed even if the scheduler is
  2909. * suspended because this is inside a critical section. */
  2910. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  2911. prvAddTaskToReadyList( pxTCB );
  2912. /* This yield may not cause the task just resumed to run,
  2913. * but will leave the lists in the correct state for the
  2914. * next yield. */
  2915. taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
  2916. }
  2917. else
  2918. {
  2919. mtCOVERAGE_TEST_MARKER();
  2920. }
  2921. }
  2922. taskEXIT_CRITICAL();
  2923. }
  2924. else
  2925. {
  2926. mtCOVERAGE_TEST_MARKER();
  2927. }
  2928. traceRETURN_vTaskResume();
  2929. }
  2930. #endif /* INCLUDE_vTaskSuspend */
  2931. /*-----------------------------------------------------------*/
  2932. #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
  2933. BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
  2934. {
  2935. BaseType_t xYieldRequired = pdFALSE;
  2936. TCB_t * const pxTCB = xTaskToResume;
  2937. UBaseType_t uxSavedInterruptStatus;
  2938. traceENTER_xTaskResumeFromISR( xTaskToResume );
  2939. configASSERT( xTaskToResume );
  2940. /* RTOS ports that support interrupt nesting have the concept of a
  2941. * maximum system call (or maximum API call) interrupt priority.
  2942. * Interrupts that are above the maximum system call priority are keep
  2943. * permanently enabled, even when the RTOS kernel is in a critical section,
  2944. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  2945. * is defined in FreeRTOSConfig.h then
  2946. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  2947. * failure if a FreeRTOS API function is called from an interrupt that has
  2948. * been assigned a priority above the configured maximum system call
  2949. * priority. Only FreeRTOS functions that end in FromISR can be called
  2950. * from interrupts that have been assigned a priority at or (logically)
  2951. * below the maximum system call interrupt priority. FreeRTOS maintains a
  2952. * separate interrupt safe API to ensure interrupt entry is as fast and as
  2953. * simple as possible. More information (albeit Cortex-M specific) is
  2954. * provided on the following link:
  2955. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  2956. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  2957. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  2958. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  2959. /* coverity[misra_c_2012_directive_4_7_violation] */
  2960. uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
  2961. {
  2962. if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
  2963. {
  2964. traceTASK_RESUME_FROM_ISR( pxTCB );
  2965. /* Check the ready lists can be accessed. */
  2966. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  2967. {
  2968. #if ( configNUMBER_OF_CORES == 1 )
  2969. {
  2970. /* Ready lists can be accessed so move the task from the
  2971. * suspended list to the ready list directly. */
  2972. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  2973. {
  2974. xYieldRequired = pdTRUE;
  2975. /* Mark that a yield is pending in case the user is not
  2976. * using the return value to initiate a context switch
  2977. * from the ISR using the port specific portYIELD_FROM_ISR(). */
  2978. xYieldPendings[ 0 ] = pdTRUE;
  2979. }
  2980. else
  2981. {
  2982. mtCOVERAGE_TEST_MARKER();
  2983. }
  2984. }
  2985. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  2986. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  2987. prvAddTaskToReadyList( pxTCB );
  2988. }
  2989. else
  2990. {
  2991. /* The delayed or ready lists cannot be accessed so the task
  2992. * is held in the pending ready list until the scheduler is
  2993. * unsuspended. */
  2994. vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  2995. }
  2996. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
  2997. {
  2998. prvYieldForTask( pxTCB );
  2999. if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
  3000. {
  3001. xYieldRequired = pdTRUE;
  3002. }
  3003. }
  3004. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
  3005. }
  3006. else
  3007. {
  3008. mtCOVERAGE_TEST_MARKER();
  3009. }
  3010. }
  3011. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  3012. traceRETURN_xTaskResumeFromISR( xYieldRequired );
  3013. return xYieldRequired;
  3014. }
  3015. #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
  3016. /*-----------------------------------------------------------*/
  3017. static BaseType_t prvCreateIdleTasks( void )
  3018. {
  3019. BaseType_t xReturn = pdPASS;
  3020. BaseType_t xCoreID;
  3021. char cIdleName[ configMAX_TASK_NAME_LEN ] = { 0 };
  3022. TaskFunction_t pxIdleTaskFunction = NULL;
  3023. UBaseType_t xIdleTaskNameIndex;
  3024. /* MISRA Ref 14.3.1 [Configuration dependent invariant] */
  3025. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-143. */
  3026. /* coverity[misra_c_2012_rule_14_3_violation] */
  3027. for( xIdleTaskNameIndex = 0U; xIdleTaskNameIndex < ( configMAX_TASK_NAME_LEN - taskRESERVED_TASK_NAME_LENGTH ); xIdleTaskNameIndex++ )
  3028. {
  3029. /* MISRA Ref 18.1.1 [Configuration dependent bounds checking] */
  3030. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-181. */
  3031. /* coverity[misra_c_2012_rule_18_1_violation] */
  3032. cIdleName[ xIdleTaskNameIndex ] = configIDLE_TASK_NAME[ xIdleTaskNameIndex ];
  3033. if( cIdleName[ xIdleTaskNameIndex ] == ( char ) 0x00 )
  3034. {
  3035. break;
  3036. }
  3037. else
  3038. {
  3039. mtCOVERAGE_TEST_MARKER();
  3040. }
  3041. }
  3042. /* Ensure null termination. */
  3043. cIdleName[ xIdleTaskNameIndex ] = '\0';
  3044. /* Add each idle task at the lowest priority. */
  3045. for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
  3046. {
  3047. #if ( configNUMBER_OF_CORES == 1 )
  3048. {
  3049. pxIdleTaskFunction = &prvIdleTask;
  3050. }
  3051. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  3052. {
  3053. /* In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 passive idle tasks
  3054. * are also created to ensure that each core has an idle task to
  3055. * run when no other task is available to run. */
  3056. if( xCoreID == 0 )
  3057. {
  3058. pxIdleTaskFunction = &prvIdleTask;
  3059. }
  3060. else
  3061. {
  3062. pxIdleTaskFunction = &prvPassiveIdleTask;
  3063. }
  3064. }
  3065. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  3066. /* Update the idle task name with suffix to differentiate the idle tasks.
  3067. * This function is not required in single core FreeRTOS since there is
  3068. * only one idle task. */
  3069. #if ( configNUMBER_OF_CORES > 1 )
  3070. {
  3071. /* Append the idle task number to the end of the name.
  3072. *
  3073. * Note: Idle task name index only supports single-character
  3074. * core IDs (0-9). If the core ID exceeds 9, the idle task
  3075. * name will contain an incorrect ASCII character. This is
  3076. * acceptable as the task name is used mainly for debugging. */
  3077. cIdleName[ xIdleTaskNameIndex ] = ( char ) ( xCoreID + '0' );
  3078. cIdleName[ xIdleTaskNameIndex + 1U ] = '\0';
  3079. }
  3080. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  3081. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  3082. {
  3083. StaticTask_t * pxIdleTaskTCBBuffer = NULL;
  3084. StackType_t * pxIdleTaskStackBuffer = NULL;
  3085. configSTACK_DEPTH_TYPE uxIdleTaskStackSize;
  3086. /* The Idle task is created using user provided RAM - obtain the
  3087. * address of the RAM then create the idle task. */
  3088. #if ( configNUMBER_OF_CORES == 1 )
  3089. {
  3090. vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
  3091. }
  3092. #else
  3093. {
  3094. if( xCoreID == 0 )
  3095. {
  3096. vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize );
  3097. }
  3098. else
  3099. {
  3100. vApplicationGetPassiveIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &uxIdleTaskStackSize, ( BaseType_t ) ( xCoreID - 1 ) );
  3101. }
  3102. }
  3103. #endif /* if ( configNUMBER_OF_CORES == 1 ) */
  3104. xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( pxIdleTaskFunction,
  3105. cIdleName,
  3106. uxIdleTaskStackSize,
  3107. ( void * ) NULL,
  3108. portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
  3109. pxIdleTaskStackBuffer,
  3110. pxIdleTaskTCBBuffer );
  3111. if( xIdleTaskHandles[ xCoreID ] != NULL )
  3112. {
  3113. xReturn = pdPASS;
  3114. }
  3115. else
  3116. {
  3117. xReturn = pdFAIL;
  3118. }
  3119. }
  3120. #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  3121. {
  3122. /* The Idle task is being created using dynamically allocated RAM. */
  3123. xReturn = xTaskCreate( pxIdleTaskFunction,
  3124. cIdleName,
  3125. configMINIMAL_STACK_SIZE,
  3126. ( void * ) NULL,
  3127. portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
  3128. &xIdleTaskHandles[ xCoreID ] );
  3129. }
  3130. #endif /* configSUPPORT_STATIC_ALLOCATION */
  3131. /* Break the loop if any of the idle task is failed to be created. */
  3132. if( xReturn != pdPASS )
  3133. {
  3134. break;
  3135. }
  3136. else
  3137. {
  3138. #if ( configNUMBER_OF_CORES == 1 )
  3139. {
  3140. mtCOVERAGE_TEST_MARKER();
  3141. }
  3142. #else
  3143. {
  3144. /* Assign idle task to each core before SMP scheduler is running. */
  3145. xIdleTaskHandles[ xCoreID ]->xTaskRunState = xCoreID;
  3146. pxCurrentTCBs[ xCoreID ] = xIdleTaskHandles[ xCoreID ];
  3147. }
  3148. #endif
  3149. }
  3150. }
  3151. return xReturn;
  3152. }
  3153. /*-----------------------------------------------------------*/
  3154. void vTaskStartScheduler( void )
  3155. {
  3156. BaseType_t xReturn;
  3157. traceENTER_vTaskStartScheduler();
  3158. #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
  3159. {
  3160. /* Sanity check that the UBaseType_t must have greater than or equal to
  3161. * the number of bits as confNUMBER_OF_CORES. */
  3162. configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
  3163. }
  3164. #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
  3165. xReturn = prvCreateIdleTasks();
  3166. #if ( configUSE_TIMERS == 1 )
  3167. {
  3168. if( xReturn == pdPASS )
  3169. {
  3170. xReturn = xTimerCreateTimerTask();
  3171. }
  3172. else
  3173. {
  3174. mtCOVERAGE_TEST_MARKER();
  3175. }
  3176. }
  3177. #endif /* configUSE_TIMERS */
  3178. if( xReturn == pdPASS )
  3179. {
  3180. /* freertos_tasks_c_additions_init() should only be called if the user
  3181. * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
  3182. * the only macro called by the function. */
  3183. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  3184. {
  3185. freertos_tasks_c_additions_init();
  3186. }
  3187. #endif
  3188. /* Interrupts are turned off here, to ensure a tick does not occur
  3189. * before or during the call to xPortStartScheduler(). The stacks of
  3190. * the created tasks contain a status word with interrupts switched on
  3191. * so interrupts will automatically get re-enabled when the first task
  3192. * starts to run. */
  3193. portDISABLE_INTERRUPTS();
  3194. #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
  3195. {
  3196. /* Switch C-Runtime's TLS Block to point to the TLS
  3197. * block specific to the task that will run first. */
  3198. configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
  3199. }
  3200. #endif
  3201. xNextTaskUnblockTime = portMAX_DELAY;
  3202. xSchedulerRunning = pdTRUE;
  3203. xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
  3204. /* If configGENERATE_RUN_TIME_STATS is defined then the following
  3205. * macro must be defined to configure the timer/counter used to generate
  3206. * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
  3207. * is set to 0 and the following line fails to build then ensure you do not
  3208. * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
  3209. * FreeRTOSConfig.h file. */
  3210. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
  3211. traceTASK_SWITCHED_IN();
  3212. traceSTARTING_SCHEDULER( xIdleTaskHandles );
  3213. /* Setting up the timer tick is hardware specific and thus in the
  3214. * portable interface. */
  3215. /* The return value for xPortStartScheduler is not required
  3216. * hence using a void datatype. */
  3217. ( void ) xPortStartScheduler();
  3218. /* In most cases, xPortStartScheduler() will not return. If it
  3219. * returns pdTRUE then there was not enough heap memory available
  3220. * to create either the Idle or the Timer task. If it returned
  3221. * pdFALSE, then the application called xTaskEndScheduler().
  3222. * Most ports don't implement xTaskEndScheduler() as there is
  3223. * nothing to return to. */
  3224. }
  3225. else
  3226. {
  3227. /* This line will only be reached if the kernel could not be started,
  3228. * because there was not enough FreeRTOS heap to create the idle task
  3229. * or the timer task. */
  3230. configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
  3231. }
  3232. /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
  3233. * meaning xIdleTaskHandles are not used anywhere else. */
  3234. ( void ) xIdleTaskHandles;
  3235. /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
  3236. * from getting optimized out as it is no longer used by the kernel. */
  3237. ( void ) uxTopUsedPriority;
  3238. traceRETURN_vTaskStartScheduler();
  3239. }
  3240. /*-----------------------------------------------------------*/
  3241. void vTaskEndScheduler( void )
  3242. {
  3243. traceENTER_vTaskEndScheduler();
  3244. #if ( INCLUDE_vTaskDelete == 1 )
  3245. {
  3246. BaseType_t xCoreID;
  3247. #if ( configUSE_TIMERS == 1 )
  3248. {
  3249. /* Delete the timer task created by the kernel. */
  3250. vTaskDelete( xTimerGetTimerDaemonTaskHandle() );
  3251. }
  3252. #endif /* #if ( configUSE_TIMERS == 1 ) */
  3253. /* Delete Idle tasks created by the kernel.*/
  3254. for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
  3255. {
  3256. vTaskDelete( xIdleTaskHandles[ xCoreID ] );
  3257. }
  3258. /* Idle task is responsible for reclaiming the resources of the tasks in
  3259. * xTasksWaitingTermination list. Since the idle task is now deleted and
  3260. * no longer going to run, we need to reclaim resources of all the tasks
  3261. * in the xTasksWaitingTermination list. */
  3262. prvCheckTasksWaitingTermination();
  3263. }
  3264. #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
  3265. /* Stop the scheduler interrupts and call the portable scheduler end
  3266. * routine so the original ISRs can be restored if necessary. The port
  3267. * layer must ensure interrupts enable bit is left in the correct state. */
  3268. portDISABLE_INTERRUPTS();
  3269. xSchedulerRunning = pdFALSE;
  3270. /* This function must be called from a task and the application is
  3271. * responsible for deleting that task after the scheduler is stopped. */
  3272. vPortEndScheduler();
  3273. traceRETURN_vTaskEndScheduler();
  3274. }
  3275. /*----------------------------------------------------------*/
  3276. void vTaskSuspendAll( void )
  3277. {
  3278. traceENTER_vTaskSuspendAll();
  3279. #if ( configNUMBER_OF_CORES == 1 )
  3280. {
  3281. /* A critical section is not required as the variable is of type
  3282. * BaseType_t. Each task maintains its own context, and a context switch
  3283. * cannot occur if the variable is non zero. So, as long as the writing
  3284. * from the register back into the memory is atomic, it is not a
  3285. * problem.
  3286. *
  3287. * Consider the following scenario, which starts with
  3288. * uxSchedulerSuspended at zero.
  3289. *
  3290. * 1. load uxSchedulerSuspended into register.
  3291. * 2. Now a context switch causes another task to run, and the other
  3292. * task uses the same variable. The other task will see the variable
  3293. * as zero because the variable has not yet been updated by the
  3294. * original task. Eventually the original task runs again. **That can
  3295. * only happen when uxSchedulerSuspended is once again zero**. When
  3296. * the original task runs again, the contents of the CPU registers
  3297. * are restored to exactly how they were when it was switched out -
  3298. * therefore the value it read into the register still matches the
  3299. * value of the uxSchedulerSuspended variable.
  3300. *
  3301. * 3. increment register.
  3302. * 4. store register into uxSchedulerSuspended. The value restored to
  3303. * uxSchedulerSuspended will be the correct value of 1, even though
  3304. * the variable was used by other tasks in the mean time.
  3305. */
  3306. /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
  3307. * do not otherwise exhibit real time behaviour. */
  3308. portSOFTWARE_BARRIER();
  3309. /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
  3310. * is used to allow calls to vTaskSuspendAll() to nest. */
  3311. uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended + 1U );
  3312. /* Enforces ordering for ports and optimised compilers that may otherwise place
  3313. * the above increment elsewhere. */
  3314. portMEMORY_BARRIER();
  3315. }
  3316. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  3317. {
  3318. UBaseType_t ulState;
  3319. BaseType_t xCoreID;
  3320. /* This must only be called from within a task. */
  3321. portASSERT_IF_IN_ISR();
  3322. if( xSchedulerRunning != pdFALSE )
  3323. {
  3324. /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
  3325. * We must disable interrupts before we grab the locks in the event that this task is
  3326. * interrupted and switches context before incrementing uxSchedulerSuspended.
  3327. * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
  3328. * uxSchedulerSuspended since that will prevent context switches. */
  3329. ulState = portSET_INTERRUPT_MASK();
  3330. xCoreID = ( BaseType_t ) portGET_CORE_ID();
  3331. /* This must never be called from inside a critical section. */
  3332. configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 );
  3333. /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
  3334. * do not otherwise exhibit real time behaviour. */
  3335. portSOFTWARE_BARRIER();
  3336. portGET_TASK_LOCK( xCoreID );
  3337. /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
  3338. * purpose is to prevent altering the variable when fromISR APIs are readying
  3339. * it. */
  3340. if( uxSchedulerSuspended == 0U )
  3341. {
  3342. prvCheckForRunStateChange();
  3343. }
  3344. else
  3345. {
  3346. mtCOVERAGE_TEST_MARKER();
  3347. }
  3348. /* Query the coreID again as prvCheckForRunStateChange may have
  3349. * caused the task to get scheduled on a different core. The correct
  3350. * task lock for the core is acquired in prvCheckForRunStateChange. */
  3351. xCoreID = ( BaseType_t ) portGET_CORE_ID();
  3352. portGET_ISR_LOCK( xCoreID );
  3353. /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
  3354. * is used to allow calls to vTaskSuspendAll() to nest. */
  3355. ++uxSchedulerSuspended;
  3356. portRELEASE_ISR_LOCK( xCoreID );
  3357. portCLEAR_INTERRUPT_MASK( ulState );
  3358. }
  3359. else
  3360. {
  3361. mtCOVERAGE_TEST_MARKER();
  3362. }
  3363. }
  3364. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  3365. traceRETURN_vTaskSuspendAll();
  3366. }
  3367. /*----------------------------------------------------------*/
  3368. #if ( configUSE_TICKLESS_IDLE != 0 )
  3369. static TickType_t prvGetExpectedIdleTime( void )
  3370. {
  3371. TickType_t xReturn;
  3372. BaseType_t xHigherPriorityReadyTasks = pdFALSE;
  3373. /* xHigherPriorityReadyTasks takes care of the case where
  3374. * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
  3375. * task that are in the Ready state, even though the idle task is
  3376. * running. */
  3377. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  3378. {
  3379. if( uxTopReadyPriority > tskIDLE_PRIORITY )
  3380. {
  3381. xHigherPriorityReadyTasks = pdTRUE;
  3382. }
  3383. }
  3384. #else
  3385. {
  3386. const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
  3387. /* When port optimised task selection is used the uxTopReadyPriority
  3388. * variable is used as a bit map. If bits other than the least
  3389. * significant bit are set then there are tasks that have a priority
  3390. * above the idle priority that are in the Ready state. This takes
  3391. * care of the case where the co-operative scheduler is in use. */
  3392. if( uxTopReadyPriority > uxLeastSignificantBit )
  3393. {
  3394. xHigherPriorityReadyTasks = pdTRUE;
  3395. }
  3396. }
  3397. #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
  3398. if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
  3399. {
  3400. xReturn = 0;
  3401. }
  3402. else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1U )
  3403. {
  3404. /* There are other idle priority tasks in the ready state. If
  3405. * time slicing is used then the very next tick interrupt must be
  3406. * processed. */
  3407. xReturn = 0;
  3408. }
  3409. else if( xHigherPriorityReadyTasks != pdFALSE )
  3410. {
  3411. /* There are tasks in the Ready state that have a priority above the
  3412. * idle priority. This path can only be reached if
  3413. * configUSE_PREEMPTION is 0. */
  3414. xReturn = 0;
  3415. }
  3416. else
  3417. {
  3418. xReturn = xNextTaskUnblockTime;
  3419. xReturn -= xTickCount;
  3420. }
  3421. return xReturn;
  3422. }
  3423. #endif /* configUSE_TICKLESS_IDLE */
  3424. /*----------------------------------------------------------*/
  3425. BaseType_t xTaskResumeAll( void )
  3426. {
  3427. TCB_t * pxTCB = NULL;
  3428. BaseType_t xAlreadyYielded = pdFALSE;
  3429. traceENTER_xTaskResumeAll();
  3430. #if ( configNUMBER_OF_CORES > 1 )
  3431. if( xSchedulerRunning != pdFALSE )
  3432. #endif
  3433. {
  3434. /* It is possible that an ISR caused a task to be removed from an event
  3435. * list while the scheduler was suspended. If this was the case then the
  3436. * removed task will have been added to the xPendingReadyList. Once the
  3437. * scheduler has been resumed it is safe to move all the pending ready
  3438. * tasks from this list into their appropriate ready list. */
  3439. taskENTER_CRITICAL();
  3440. {
  3441. const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
  3442. /* If uxSchedulerSuspended is zero then this function does not match a
  3443. * previous call to vTaskSuspendAll(). */
  3444. configASSERT( uxSchedulerSuspended != 0U );
  3445. uxSchedulerSuspended = ( UBaseType_t ) ( uxSchedulerSuspended - 1U );
  3446. portRELEASE_TASK_LOCK( xCoreID );
  3447. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  3448. {
  3449. if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
  3450. {
  3451. /* Move any readied tasks from the pending list into the
  3452. * appropriate ready list. */
  3453. while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
  3454. {
  3455. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  3456. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  3457. /* coverity[misra_c_2012_rule_11_5_violation] */
  3458. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) );
  3459. listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
  3460. portMEMORY_BARRIER();
  3461. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  3462. prvAddTaskToReadyList( pxTCB );
  3463. #if ( configNUMBER_OF_CORES == 1 )
  3464. {
  3465. /* If the moved task has a priority higher than the current
  3466. * task then a yield must be performed. */
  3467. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  3468. {
  3469. xYieldPendings[ xCoreID ] = pdTRUE;
  3470. }
  3471. else
  3472. {
  3473. mtCOVERAGE_TEST_MARKER();
  3474. }
  3475. }
  3476. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  3477. {
  3478. /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
  3479. * If the current core yielded then vTaskSwitchContext() has already been called
  3480. * which sets xYieldPendings for the current core to pdTRUE. */
  3481. }
  3482. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  3483. }
  3484. if( pxTCB != NULL )
  3485. {
  3486. /* A task was unblocked while the scheduler was suspended,
  3487. * which may have prevented the next unblock time from being
  3488. * re-calculated, in which case re-calculate it now. Mainly
  3489. * important for low power tickless implementations, where
  3490. * this can prevent an unnecessary exit from low power
  3491. * state. */
  3492. prvResetNextTaskUnblockTime();
  3493. }
  3494. /* If any ticks occurred while the scheduler was suspended then
  3495. * they should be processed now. This ensures the tick count does
  3496. * not slip, and that any delayed tasks are resumed at the correct
  3497. * time.
  3498. *
  3499. * It should be safe to call xTaskIncrementTick here from any core
  3500. * since we are in a critical section and xTaskIncrementTick itself
  3501. * protects itself within a critical section. Suspending the scheduler
  3502. * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
  3503. {
  3504. TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
  3505. if( xPendedCounts > ( TickType_t ) 0U )
  3506. {
  3507. do
  3508. {
  3509. if( xTaskIncrementTick() != pdFALSE )
  3510. {
  3511. /* Other cores are interrupted from
  3512. * within xTaskIncrementTick(). */
  3513. xYieldPendings[ xCoreID ] = pdTRUE;
  3514. }
  3515. else
  3516. {
  3517. mtCOVERAGE_TEST_MARKER();
  3518. }
  3519. --xPendedCounts;
  3520. } while( xPendedCounts > ( TickType_t ) 0U );
  3521. xPendedTicks = 0;
  3522. }
  3523. else
  3524. {
  3525. mtCOVERAGE_TEST_MARKER();
  3526. }
  3527. }
  3528. if( xYieldPendings[ xCoreID ] != pdFALSE )
  3529. {
  3530. #if ( configUSE_PREEMPTION != 0 )
  3531. {
  3532. xAlreadyYielded = pdTRUE;
  3533. }
  3534. #endif /* #if ( configUSE_PREEMPTION != 0 ) */
  3535. #if ( configNUMBER_OF_CORES == 1 )
  3536. {
  3537. taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
  3538. }
  3539. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  3540. }
  3541. else
  3542. {
  3543. mtCOVERAGE_TEST_MARKER();
  3544. }
  3545. }
  3546. }
  3547. else
  3548. {
  3549. mtCOVERAGE_TEST_MARKER();
  3550. }
  3551. }
  3552. taskEXIT_CRITICAL();
  3553. }
  3554. traceRETURN_xTaskResumeAll( xAlreadyYielded );
  3555. return xAlreadyYielded;
  3556. }
  3557. /*-----------------------------------------------------------*/
  3558. TickType_t xTaskGetTickCount( void )
  3559. {
  3560. TickType_t xTicks;
  3561. traceENTER_xTaskGetTickCount();
  3562. /* Critical section required if running on a 16 bit processor. */
  3563. portTICK_TYPE_ENTER_CRITICAL();
  3564. {
  3565. xTicks = xTickCount;
  3566. }
  3567. portTICK_TYPE_EXIT_CRITICAL();
  3568. traceRETURN_xTaskGetTickCount( xTicks );
  3569. return xTicks;
  3570. }
  3571. /*-----------------------------------------------------------*/
  3572. TickType_t xTaskGetTickCountFromISR( void )
  3573. {
  3574. TickType_t xReturn;
  3575. UBaseType_t uxSavedInterruptStatus;
  3576. traceENTER_xTaskGetTickCountFromISR();
  3577. /* RTOS ports that support interrupt nesting have the concept of a maximum
  3578. * system call (or maximum API call) interrupt priority. Interrupts that are
  3579. * above the maximum system call priority are kept permanently enabled, even
  3580. * when the RTOS kernel is in a critical section, but cannot make any calls to
  3581. * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  3582. * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  3583. * failure if a FreeRTOS API function is called from an interrupt that has been
  3584. * assigned a priority above the configured maximum system call priority.
  3585. * Only FreeRTOS functions that end in FromISR can be called from interrupts
  3586. * that have been assigned a priority at or (logically) below the maximum
  3587. * system call interrupt priority. FreeRTOS maintains a separate interrupt
  3588. * safe API to ensure interrupt entry is as fast and as simple as possible.
  3589. * More information (albeit Cortex-M specific) is provided on the following
  3590. * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  3591. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  3592. uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
  3593. {
  3594. xReturn = xTickCount;
  3595. }
  3596. portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  3597. traceRETURN_xTaskGetTickCountFromISR( xReturn );
  3598. return xReturn;
  3599. }
  3600. /*-----------------------------------------------------------*/
  3601. UBaseType_t uxTaskGetNumberOfTasks( void )
  3602. {
  3603. traceENTER_uxTaskGetNumberOfTasks();
  3604. /* A critical section is not required because the variables are of type
  3605. * BaseType_t. */
  3606. traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
  3607. return uxCurrentNumberOfTasks;
  3608. }
  3609. /*-----------------------------------------------------------*/
  3610. char * pcTaskGetName( TaskHandle_t xTaskToQuery )
  3611. {
  3612. TCB_t * pxTCB;
  3613. traceENTER_pcTaskGetName( xTaskToQuery );
  3614. /* If null is passed in here then the name of the calling task is being
  3615. * queried. */
  3616. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  3617. configASSERT( pxTCB != NULL );
  3618. traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
  3619. return &( pxTCB->pcTaskName[ 0 ] );
  3620. }
  3621. /*-----------------------------------------------------------*/
  3622. #if ( INCLUDE_xTaskGetHandle == 1 )
  3623. static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
  3624. const char pcNameToQuery[] )
  3625. {
  3626. TCB_t * pxReturn = NULL;
  3627. TCB_t * pxTCB = NULL;
  3628. UBaseType_t x;
  3629. char cNextChar;
  3630. BaseType_t xBreakLoop;
  3631. const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
  3632. ListItem_t * pxIterator;
  3633. /* This function is called with the scheduler suspended. */
  3634. if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
  3635. {
  3636. for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
  3637. {
  3638. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  3639. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  3640. /* coverity[misra_c_2012_rule_11_5_violation] */
  3641. pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
  3642. /* Check each character in the name looking for a match or
  3643. * mismatch. */
  3644. xBreakLoop = pdFALSE;
  3645. for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  3646. {
  3647. cNextChar = pxTCB->pcTaskName[ x ];
  3648. if( cNextChar != pcNameToQuery[ x ] )
  3649. {
  3650. /* Characters didn't match. */
  3651. xBreakLoop = pdTRUE;
  3652. }
  3653. else if( cNextChar == ( char ) 0x00 )
  3654. {
  3655. /* Both strings terminated, a match must have been
  3656. * found. */
  3657. pxReturn = pxTCB;
  3658. xBreakLoop = pdTRUE;
  3659. }
  3660. else
  3661. {
  3662. mtCOVERAGE_TEST_MARKER();
  3663. }
  3664. if( xBreakLoop != pdFALSE )
  3665. {
  3666. break;
  3667. }
  3668. }
  3669. if( pxReturn != NULL )
  3670. {
  3671. /* The handle has been found. */
  3672. break;
  3673. }
  3674. }
  3675. }
  3676. else
  3677. {
  3678. mtCOVERAGE_TEST_MARKER();
  3679. }
  3680. return pxReturn;
  3681. }
  3682. #endif /* INCLUDE_xTaskGetHandle */
  3683. /*-----------------------------------------------------------*/
  3684. #if ( INCLUDE_xTaskGetHandle == 1 )
  3685. TaskHandle_t xTaskGetHandle( const char * pcNameToQuery )
  3686. {
  3687. UBaseType_t uxQueue = configMAX_PRIORITIES;
  3688. TCB_t * pxTCB;
  3689. traceENTER_xTaskGetHandle( pcNameToQuery );
  3690. /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
  3691. configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
  3692. vTaskSuspendAll();
  3693. {
  3694. /* Search the ready lists. */
  3695. do
  3696. {
  3697. uxQueue--;
  3698. pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
  3699. if( pxTCB != NULL )
  3700. {
  3701. /* Found the handle. */
  3702. break;
  3703. }
  3704. } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
  3705. /* Search the delayed lists. */
  3706. if( pxTCB == NULL )
  3707. {
  3708. pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
  3709. }
  3710. if( pxTCB == NULL )
  3711. {
  3712. pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
  3713. }
  3714. #if ( INCLUDE_vTaskSuspend == 1 )
  3715. {
  3716. if( pxTCB == NULL )
  3717. {
  3718. /* Search the suspended list. */
  3719. pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
  3720. }
  3721. }
  3722. #endif
  3723. #if ( INCLUDE_vTaskDelete == 1 )
  3724. {
  3725. if( pxTCB == NULL )
  3726. {
  3727. /* Search the deleted list. */
  3728. pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
  3729. }
  3730. }
  3731. #endif
  3732. }
  3733. ( void ) xTaskResumeAll();
  3734. traceRETURN_xTaskGetHandle( pxTCB );
  3735. return pxTCB;
  3736. }
  3737. #endif /* INCLUDE_xTaskGetHandle */
  3738. /*-----------------------------------------------------------*/
  3739. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  3740. BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
  3741. StackType_t ** ppuxStackBuffer,
  3742. StaticTask_t ** ppxTaskBuffer )
  3743. {
  3744. BaseType_t xReturn;
  3745. TCB_t * pxTCB;
  3746. traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
  3747. configASSERT( ppuxStackBuffer != NULL );
  3748. configASSERT( ppxTaskBuffer != NULL );
  3749. pxTCB = prvGetTCBFromHandle( xTask );
  3750. configASSERT( pxTCB != NULL );
  3751. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
  3752. {
  3753. if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
  3754. {
  3755. *ppuxStackBuffer = pxTCB->pxStack;
  3756. /* MISRA Ref 11.3.1 [Misaligned access] */
  3757. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-113 */
  3758. /* coverity[misra_c_2012_rule_11_3_violation] */
  3759. *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
  3760. xReturn = pdTRUE;
  3761. }
  3762. else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
  3763. {
  3764. *ppuxStackBuffer = pxTCB->pxStack;
  3765. *ppxTaskBuffer = NULL;
  3766. xReturn = pdTRUE;
  3767. }
  3768. else
  3769. {
  3770. xReturn = pdFALSE;
  3771. }
  3772. }
  3773. #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
  3774. {
  3775. *ppuxStackBuffer = pxTCB->pxStack;
  3776. *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
  3777. xReturn = pdTRUE;
  3778. }
  3779. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
  3780. traceRETURN_xTaskGetStaticBuffers( xReturn );
  3781. return xReturn;
  3782. }
  3783. #endif /* configSUPPORT_STATIC_ALLOCATION */
  3784. /*-----------------------------------------------------------*/
  3785. #if ( configUSE_TRACE_FACILITY == 1 )
  3786. UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
  3787. const UBaseType_t uxArraySize,
  3788. configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
  3789. {
  3790. UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
  3791. traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
  3792. vTaskSuspendAll();
  3793. {
  3794. /* Is there a space in the array for each task in the system? */
  3795. if( uxArraySize >= uxCurrentNumberOfTasks )
  3796. {
  3797. /* Fill in an TaskStatus_t structure with information on each
  3798. * task in the Ready state. */
  3799. do
  3800. {
  3801. uxQueue--;
  3802. uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
  3803. } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY );
  3804. /* Fill in an TaskStatus_t structure with information on each
  3805. * task in the Blocked state. */
  3806. uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
  3807. uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
  3808. #if ( INCLUDE_vTaskDelete == 1 )
  3809. {
  3810. /* Fill in an TaskStatus_t structure with information on
  3811. * each task that has been deleted but not yet cleaned up. */
  3812. uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
  3813. }
  3814. #endif
  3815. #if ( INCLUDE_vTaskSuspend == 1 )
  3816. {
  3817. /* Fill in an TaskStatus_t structure with information on
  3818. * each task in the Suspended state. */
  3819. uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
  3820. }
  3821. #endif
  3822. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  3823. {
  3824. if( pulTotalRunTime != NULL )
  3825. {
  3826. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  3827. portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
  3828. #else
  3829. *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
  3830. #endif
  3831. }
  3832. }
  3833. #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  3834. {
  3835. if( pulTotalRunTime != NULL )
  3836. {
  3837. *pulTotalRunTime = 0;
  3838. }
  3839. }
  3840. #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  3841. }
  3842. else
  3843. {
  3844. mtCOVERAGE_TEST_MARKER();
  3845. }
  3846. }
  3847. ( void ) xTaskResumeAll();
  3848. traceRETURN_uxTaskGetSystemState( uxTask );
  3849. return uxTask;
  3850. }
  3851. #endif /* configUSE_TRACE_FACILITY */
  3852. /*----------------------------------------------------------*/
  3853. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  3854. #if ( configNUMBER_OF_CORES == 1 )
  3855. TaskHandle_t xTaskGetIdleTaskHandle( void )
  3856. {
  3857. traceENTER_xTaskGetIdleTaskHandle();
  3858. /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
  3859. * started, then xIdleTaskHandles will be NULL. */
  3860. configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
  3861. traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
  3862. return xIdleTaskHandles[ 0 ];
  3863. }
  3864. #endif /* if ( configNUMBER_OF_CORES == 1 ) */
  3865. TaskHandle_t xTaskGetIdleTaskHandleForCore( BaseType_t xCoreID )
  3866. {
  3867. traceENTER_xTaskGetIdleTaskHandleForCore( xCoreID );
  3868. /* Ensure the core ID is valid. */
  3869. configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
  3870. /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
  3871. * started, then xIdleTaskHandles will be NULL. */
  3872. configASSERT( ( xIdleTaskHandles[ xCoreID ] != NULL ) );
  3873. traceRETURN_xTaskGetIdleTaskHandleForCore( xIdleTaskHandles[ xCoreID ] );
  3874. return xIdleTaskHandles[ xCoreID ];
  3875. }
  3876. #endif /* INCLUDE_xTaskGetIdleTaskHandle */
  3877. /*----------------------------------------------------------*/
  3878. /* This conditional compilation should use inequality to 0, not equality to 1.
  3879. * This is to ensure vTaskStepTick() is available when user defined low power mode
  3880. * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
  3881. * 1. */
  3882. #if ( configUSE_TICKLESS_IDLE != 0 )
  3883. void vTaskStepTick( TickType_t xTicksToJump )
  3884. {
  3885. TickType_t xUpdatedTickCount;
  3886. traceENTER_vTaskStepTick( xTicksToJump );
  3887. /* Correct the tick count value after a period during which the tick
  3888. * was suppressed. Note this does *not* call the tick hook function for
  3889. * each stepped tick. */
  3890. xUpdatedTickCount = xTickCount + xTicksToJump;
  3891. configASSERT( xUpdatedTickCount <= xNextTaskUnblockTime );
  3892. if( xUpdatedTickCount == xNextTaskUnblockTime )
  3893. {
  3894. /* Arrange for xTickCount to reach xNextTaskUnblockTime in
  3895. * xTaskIncrementTick() when the scheduler resumes. This ensures
  3896. * that any delayed tasks are resumed at the correct time. */
  3897. configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
  3898. configASSERT( xTicksToJump != ( TickType_t ) 0 );
  3899. /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
  3900. taskENTER_CRITICAL();
  3901. {
  3902. xPendedTicks++;
  3903. }
  3904. taskEXIT_CRITICAL();
  3905. xTicksToJump--;
  3906. }
  3907. else
  3908. {
  3909. mtCOVERAGE_TEST_MARKER();
  3910. }
  3911. xTickCount += xTicksToJump;
  3912. traceINCREASE_TICK_COUNT( xTicksToJump );
  3913. traceRETURN_vTaskStepTick();
  3914. }
  3915. #endif /* configUSE_TICKLESS_IDLE */
  3916. /*----------------------------------------------------------*/
  3917. BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
  3918. {
  3919. BaseType_t xYieldOccurred;
  3920. traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
  3921. /* Must not be called with the scheduler suspended as the implementation
  3922. * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
  3923. configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
  3924. /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
  3925. * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
  3926. vTaskSuspendAll();
  3927. /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
  3928. taskENTER_CRITICAL();
  3929. {
  3930. xPendedTicks += xTicksToCatchUp;
  3931. }
  3932. taskEXIT_CRITICAL();
  3933. xYieldOccurred = xTaskResumeAll();
  3934. traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
  3935. return xYieldOccurred;
  3936. }
  3937. /*----------------------------------------------------------*/
  3938. #if ( INCLUDE_xTaskAbortDelay == 1 )
  3939. BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
  3940. {
  3941. TCB_t * pxTCB = xTask;
  3942. BaseType_t xReturn;
  3943. traceENTER_xTaskAbortDelay( xTask );
  3944. configASSERT( pxTCB != NULL );
  3945. vTaskSuspendAll();
  3946. {
  3947. /* A task can only be prematurely removed from the Blocked state if
  3948. * it is actually in the Blocked state. */
  3949. if( eTaskGetState( xTask ) == eBlocked )
  3950. {
  3951. xReturn = pdPASS;
  3952. /* Remove the reference to the task from the blocked list. An
  3953. * interrupt won't touch the xStateListItem because the
  3954. * scheduler is suspended. */
  3955. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  3956. /* Is the task waiting on an event also? If so remove it from
  3957. * the event list too. Interrupts can touch the event list item,
  3958. * even though the scheduler is suspended, so a critical section
  3959. * is used. */
  3960. taskENTER_CRITICAL();
  3961. {
  3962. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  3963. {
  3964. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  3965. /* This lets the task know it was forcibly removed from the
  3966. * blocked state so it should not re-evaluate its block time and
  3967. * then block again. */
  3968. pxTCB->ucDelayAborted = ( uint8_t ) pdTRUE;
  3969. }
  3970. else
  3971. {
  3972. mtCOVERAGE_TEST_MARKER();
  3973. }
  3974. }
  3975. taskEXIT_CRITICAL();
  3976. /* Place the unblocked task into the appropriate ready list. */
  3977. prvAddTaskToReadyList( pxTCB );
  3978. /* A task being unblocked cannot cause an immediate context
  3979. * switch if preemption is turned off. */
  3980. #if ( configUSE_PREEMPTION == 1 )
  3981. {
  3982. #if ( configNUMBER_OF_CORES == 1 )
  3983. {
  3984. /* Preemption is on, but a context switch should only be
  3985. * performed if the unblocked task has a priority that is
  3986. * higher than the currently executing task. */
  3987. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  3988. {
  3989. /* Pend the yield to be performed when the scheduler
  3990. * is unsuspended. */
  3991. xYieldPendings[ 0 ] = pdTRUE;
  3992. }
  3993. else
  3994. {
  3995. mtCOVERAGE_TEST_MARKER();
  3996. }
  3997. }
  3998. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  3999. {
  4000. taskENTER_CRITICAL();
  4001. {
  4002. prvYieldForTask( pxTCB );
  4003. }
  4004. taskEXIT_CRITICAL();
  4005. }
  4006. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  4007. }
  4008. #endif /* #if ( configUSE_PREEMPTION == 1 ) */
  4009. }
  4010. else
  4011. {
  4012. xReturn = pdFAIL;
  4013. }
  4014. }
  4015. ( void ) xTaskResumeAll();
  4016. traceRETURN_xTaskAbortDelay( xReturn );
  4017. return xReturn;
  4018. }
  4019. #endif /* INCLUDE_xTaskAbortDelay */
  4020. /*----------------------------------------------------------*/
  4021. BaseType_t xTaskIncrementTick( void )
  4022. {
  4023. TCB_t * pxTCB;
  4024. TickType_t xItemValue;
  4025. BaseType_t xSwitchRequired = pdFALSE;
  4026. traceENTER_xTaskIncrementTick();
  4027. /* Called by the portable layer each time a tick interrupt occurs.
  4028. * Increments the tick then checks to see if the new tick value will cause any
  4029. * tasks to be unblocked. */
  4030. traceTASK_INCREMENT_TICK( xTickCount );
  4031. /* Tick increment should occur on every kernel timer event. Core 0 has the
  4032. * responsibility to increment the tick, or increment the pended ticks if the
  4033. * scheduler is suspended. If pended ticks is greater than zero, the core that
  4034. * calls xTaskResumeAll has the responsibility to increment the tick. */
  4035. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  4036. {
  4037. /* Minor optimisation. The tick count cannot change in this
  4038. * block. */
  4039. const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
  4040. /* Increment the RTOS tick, switching the delayed and overflowed
  4041. * delayed lists if it wraps to 0. */
  4042. xTickCount = xConstTickCount;
  4043. if( xConstTickCount == ( TickType_t ) 0U )
  4044. {
  4045. taskSWITCH_DELAYED_LISTS();
  4046. }
  4047. else
  4048. {
  4049. mtCOVERAGE_TEST_MARKER();
  4050. }
  4051. /* See if this tick has made a timeout expire. Tasks are stored in
  4052. * the queue in the order of their wake time - meaning once one task
  4053. * has been found whose block time has not expired there is no need to
  4054. * look any further down the list. */
  4055. if( xConstTickCount >= xNextTaskUnblockTime )
  4056. {
  4057. for( ; ; )
  4058. {
  4059. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  4060. {
  4061. /* The delayed list is empty. Set xNextTaskUnblockTime
  4062. * to the maximum possible value so it is extremely
  4063. * unlikely that the
  4064. * if( xTickCount >= xNextTaskUnblockTime ) test will pass
  4065. * next time through. */
  4066. xNextTaskUnblockTime = portMAX_DELAY;
  4067. break;
  4068. }
  4069. else
  4070. {
  4071. /* The delayed list is not empty, get the value of the
  4072. * item at the head of the delayed list. This is the time
  4073. * at which the task at the head of the delayed list must
  4074. * be removed from the Blocked state. */
  4075. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  4076. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  4077. /* coverity[misra_c_2012_rule_11_5_violation] */
  4078. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
  4079. xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
  4080. if( xConstTickCount < xItemValue )
  4081. {
  4082. /* It is not time to unblock this item yet, but the
  4083. * item value is the time at which the task at the head
  4084. * of the blocked list must be removed from the Blocked
  4085. * state - so record the item value in
  4086. * xNextTaskUnblockTime. */
  4087. xNextTaskUnblockTime = xItemValue;
  4088. break;
  4089. }
  4090. else
  4091. {
  4092. mtCOVERAGE_TEST_MARKER();
  4093. }
  4094. /* It is time to remove the item from the Blocked state. */
  4095. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  4096. /* Is the task waiting on an event also? If so remove
  4097. * it from the event list. */
  4098. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  4099. {
  4100. listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
  4101. }
  4102. else
  4103. {
  4104. mtCOVERAGE_TEST_MARKER();
  4105. }
  4106. /* Place the unblocked task into the appropriate ready
  4107. * list. */
  4108. prvAddTaskToReadyList( pxTCB );
  4109. /* A task being unblocked cannot cause an immediate
  4110. * context switch if preemption is turned off. */
  4111. #if ( configUSE_PREEMPTION == 1 )
  4112. {
  4113. #if ( configNUMBER_OF_CORES == 1 )
  4114. {
  4115. /* Preemption is on, but a context switch should
  4116. * only be performed if the unblocked task's
  4117. * priority is higher than the currently executing
  4118. * task.
  4119. * The case of equal priority tasks sharing
  4120. * processing time (which happens when both
  4121. * preemption and time slicing are on) is
  4122. * handled below.*/
  4123. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  4124. {
  4125. xSwitchRequired = pdTRUE;
  4126. }
  4127. else
  4128. {
  4129. mtCOVERAGE_TEST_MARKER();
  4130. }
  4131. }
  4132. #else /* #if( configNUMBER_OF_CORES == 1 ) */
  4133. {
  4134. prvYieldForTask( pxTCB );
  4135. }
  4136. #endif /* #if( configNUMBER_OF_CORES == 1 ) */
  4137. }
  4138. #endif /* #if ( configUSE_PREEMPTION == 1 ) */
  4139. }
  4140. }
  4141. }
  4142. /* Tasks of equal priority to the currently running task will share
  4143. * processing time (time slice) if preemption is on, and the application
  4144. * writer has not explicitly turned time slicing off. */
  4145. #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
  4146. {
  4147. #if ( configNUMBER_OF_CORES == 1 )
  4148. {
  4149. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > 1U )
  4150. {
  4151. xSwitchRequired = pdTRUE;
  4152. }
  4153. else
  4154. {
  4155. mtCOVERAGE_TEST_MARKER();
  4156. }
  4157. }
  4158. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  4159. {
  4160. BaseType_t xCoreID;
  4161. for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
  4162. {
  4163. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1U )
  4164. {
  4165. xYieldPendings[ xCoreID ] = pdTRUE;
  4166. }
  4167. else
  4168. {
  4169. mtCOVERAGE_TEST_MARKER();
  4170. }
  4171. }
  4172. }
  4173. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  4174. }
  4175. #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
  4176. #if ( configUSE_TICK_HOOK == 1 )
  4177. {
  4178. /* Guard against the tick hook being called when the pended tick
  4179. * count is being unwound (when the scheduler is being unlocked). */
  4180. if( xPendedTicks == ( TickType_t ) 0 )
  4181. {
  4182. vApplicationTickHook();
  4183. }
  4184. else
  4185. {
  4186. mtCOVERAGE_TEST_MARKER();
  4187. }
  4188. }
  4189. #endif /* configUSE_TICK_HOOK */
  4190. #if ( configUSE_PREEMPTION == 1 )
  4191. {
  4192. #if ( configNUMBER_OF_CORES == 1 )
  4193. {
  4194. /* For single core the core ID is always 0. */
  4195. if( xYieldPendings[ 0 ] != pdFALSE )
  4196. {
  4197. xSwitchRequired = pdTRUE;
  4198. }
  4199. else
  4200. {
  4201. mtCOVERAGE_TEST_MARKER();
  4202. }
  4203. }
  4204. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  4205. {
  4206. BaseType_t xCoreID, xCurrentCoreID;
  4207. xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
  4208. for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
  4209. {
  4210. #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
  4211. if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
  4212. #endif
  4213. {
  4214. if( xYieldPendings[ xCoreID ] != pdFALSE )
  4215. {
  4216. if( xCoreID == xCurrentCoreID )
  4217. {
  4218. xSwitchRequired = pdTRUE;
  4219. }
  4220. else
  4221. {
  4222. prvYieldCore( xCoreID );
  4223. }
  4224. }
  4225. else
  4226. {
  4227. mtCOVERAGE_TEST_MARKER();
  4228. }
  4229. }
  4230. }
  4231. }
  4232. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  4233. }
  4234. #endif /* #if ( configUSE_PREEMPTION == 1 ) */
  4235. }
  4236. else
  4237. {
  4238. xPendedTicks += 1U;
  4239. /* The tick hook gets called at regular intervals, even if the
  4240. * scheduler is locked. */
  4241. #if ( configUSE_TICK_HOOK == 1 )
  4242. {
  4243. vApplicationTickHook();
  4244. }
  4245. #endif
  4246. }
  4247. traceRETURN_xTaskIncrementTick( xSwitchRequired );
  4248. return xSwitchRequired;
  4249. }
  4250. /*-----------------------------------------------------------*/
  4251. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  4252. void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
  4253. TaskHookFunction_t pxHookFunction )
  4254. {
  4255. TCB_t * xTCB;
  4256. traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
  4257. /* If xTask is NULL then it is the task hook of the calling task that is
  4258. * getting set. */
  4259. if( xTask == NULL )
  4260. {
  4261. xTCB = ( TCB_t * ) pxCurrentTCB;
  4262. }
  4263. else
  4264. {
  4265. xTCB = xTask;
  4266. }
  4267. /* Save the hook function in the TCB. A critical section is required as
  4268. * the value can be accessed from an interrupt. */
  4269. taskENTER_CRITICAL();
  4270. {
  4271. xTCB->pxTaskTag = pxHookFunction;
  4272. }
  4273. taskEXIT_CRITICAL();
  4274. traceRETURN_vTaskSetApplicationTaskTag();
  4275. }
  4276. #endif /* configUSE_APPLICATION_TASK_TAG */
  4277. /*-----------------------------------------------------------*/
  4278. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  4279. TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
  4280. {
  4281. TCB_t * pxTCB;
  4282. TaskHookFunction_t xReturn;
  4283. traceENTER_xTaskGetApplicationTaskTag( xTask );
  4284. /* If xTask is NULL then set the calling task's hook. */
  4285. pxTCB = prvGetTCBFromHandle( xTask );
  4286. configASSERT( pxTCB != NULL );
  4287. /* Save the hook function in the TCB. A critical section is required as
  4288. * the value can be accessed from an interrupt. */
  4289. taskENTER_CRITICAL();
  4290. {
  4291. xReturn = pxTCB->pxTaskTag;
  4292. }
  4293. taskEXIT_CRITICAL();
  4294. traceRETURN_xTaskGetApplicationTaskTag( xReturn );
  4295. return xReturn;
  4296. }
  4297. #endif /* configUSE_APPLICATION_TASK_TAG */
  4298. /*-----------------------------------------------------------*/
  4299. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  4300. TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
  4301. {
  4302. TCB_t * pxTCB;
  4303. TaskHookFunction_t xReturn;
  4304. UBaseType_t uxSavedInterruptStatus;
  4305. traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
  4306. /* If xTask is NULL then set the calling task's hook. */
  4307. pxTCB = prvGetTCBFromHandle( xTask );
  4308. configASSERT( pxTCB != NULL );
  4309. /* Save the hook function in the TCB. A critical section is required as
  4310. * the value can be accessed from an interrupt. */
  4311. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  4312. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  4313. /* coverity[misra_c_2012_directive_4_7_violation] */
  4314. uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
  4315. {
  4316. xReturn = pxTCB->pxTaskTag;
  4317. }
  4318. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  4319. traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
  4320. return xReturn;
  4321. }
  4322. #endif /* configUSE_APPLICATION_TASK_TAG */
  4323. /*-----------------------------------------------------------*/
  4324. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  4325. BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
  4326. void * pvParameter )
  4327. {
  4328. TCB_t * xTCB;
  4329. BaseType_t xReturn;
  4330. traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
  4331. /* If xTask is NULL then we are calling our own task hook. */
  4332. if( xTask == NULL )
  4333. {
  4334. xTCB = pxCurrentTCB;
  4335. }
  4336. else
  4337. {
  4338. xTCB = xTask;
  4339. }
  4340. if( xTCB->pxTaskTag != NULL )
  4341. {
  4342. xReturn = xTCB->pxTaskTag( pvParameter );
  4343. }
  4344. else
  4345. {
  4346. xReturn = pdFAIL;
  4347. }
  4348. traceRETURN_xTaskCallApplicationTaskHook( xReturn );
  4349. return xReturn;
  4350. }
  4351. #endif /* configUSE_APPLICATION_TASK_TAG */
  4352. /*-----------------------------------------------------------*/
  4353. #if ( configNUMBER_OF_CORES == 1 )
  4354. void vTaskSwitchContext( void )
  4355. {
  4356. traceENTER_vTaskSwitchContext();
  4357. if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
  4358. {
  4359. /* The scheduler is currently suspended - do not allow a context
  4360. * switch. */
  4361. xYieldPendings[ 0 ] = pdTRUE;
  4362. }
  4363. else
  4364. {
  4365. xYieldPendings[ 0 ] = pdFALSE;
  4366. traceTASK_SWITCHED_OUT();
  4367. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  4368. {
  4369. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  4370. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
  4371. #else
  4372. ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
  4373. #endif
  4374. /* Add the amount of time the task has been running to the
  4375. * accumulated time so far. The time the task started running was
  4376. * stored in ulTaskSwitchedInTime. Note that there is no overflow
  4377. * protection here so count values are only valid until the timer
  4378. * overflows. The guard against negative values is to protect
  4379. * against suspect run time stat counter implementations - which
  4380. * are provided by the application, not the kernel. */
  4381. if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
  4382. {
  4383. pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
  4384. }
  4385. else
  4386. {
  4387. mtCOVERAGE_TEST_MARKER();
  4388. }
  4389. ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
  4390. }
  4391. #endif /* configGENERATE_RUN_TIME_STATS */
  4392. /* Check for stack overflow, if configured. */
  4393. taskCHECK_FOR_STACK_OVERFLOW();
  4394. /* Before the currently running task is switched out, save its errno. */
  4395. #if ( configUSE_POSIX_ERRNO == 1 )
  4396. {
  4397. pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
  4398. }
  4399. #endif
  4400. /* Select a new task to run using either the generic C or port
  4401. * optimised asm code. */
  4402. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  4403. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  4404. /* coverity[misra_c_2012_rule_11_5_violation] */
  4405. taskSELECT_HIGHEST_PRIORITY_TASK();
  4406. traceTASK_SWITCHED_IN();
  4407. /* Macro to inject port specific behaviour immediately after
  4408. * switching tasks, such as setting an end of stack watchpoint
  4409. * or reconfiguring the MPU. */
  4410. portTASK_SWITCH_HOOK( pxCurrentTCB );
  4411. /* After the new task is switched in, update the global errno. */
  4412. #if ( configUSE_POSIX_ERRNO == 1 )
  4413. {
  4414. FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
  4415. }
  4416. #endif
  4417. #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
  4418. {
  4419. /* Switch C-Runtime's TLS Block to point to the TLS
  4420. * Block specific to this task. */
  4421. configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
  4422. }
  4423. #endif
  4424. }
  4425. traceRETURN_vTaskSwitchContext();
  4426. }
  4427. #else /* if ( configNUMBER_OF_CORES == 1 ) */
  4428. void vTaskSwitchContext( BaseType_t xCoreID )
  4429. {
  4430. traceENTER_vTaskSwitchContext();
  4431. /* Acquire both locks:
  4432. * - The ISR lock protects the ready list from simultaneous access by
  4433. * both other ISRs and tasks.
  4434. * - We also take the task lock to pause here in case another core has
  4435. * suspended the scheduler. We don't want to simply set xYieldPending
  4436. * and move on if another core suspended the scheduler. We should only
  4437. * do that if the current core has suspended the scheduler. */
  4438. portGET_TASK_LOCK( xCoreID ); /* Must always acquire the task lock first. */
  4439. portGET_ISR_LOCK( xCoreID );
  4440. {
  4441. /* vTaskSwitchContext() must never be called from within a critical section.
  4442. * This is not necessarily true for single core FreeRTOS, but it is for this
  4443. * SMP port. */
  4444. configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0 );
  4445. if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
  4446. {
  4447. /* The scheduler is currently suspended - do not allow a context
  4448. * switch. */
  4449. xYieldPendings[ xCoreID ] = pdTRUE;
  4450. }
  4451. else
  4452. {
  4453. xYieldPendings[ xCoreID ] = pdFALSE;
  4454. traceTASK_SWITCHED_OUT();
  4455. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  4456. {
  4457. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  4458. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
  4459. #else
  4460. ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
  4461. #endif
  4462. /* Add the amount of time the task has been running to the
  4463. * accumulated time so far. The time the task started running was
  4464. * stored in ulTaskSwitchedInTime. Note that there is no overflow
  4465. * protection here so count values are only valid until the timer
  4466. * overflows. The guard against negative values is to protect
  4467. * against suspect run time stat counter implementations - which
  4468. * are provided by the application, not the kernel. */
  4469. if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
  4470. {
  4471. pxCurrentTCBs[ xCoreID ]->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
  4472. }
  4473. else
  4474. {
  4475. mtCOVERAGE_TEST_MARKER();
  4476. }
  4477. ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
  4478. }
  4479. #endif /* configGENERATE_RUN_TIME_STATS */
  4480. /* Check for stack overflow, if configured. */
  4481. taskCHECK_FOR_STACK_OVERFLOW();
  4482. /* Before the currently running task is switched out, save its errno. */
  4483. #if ( configUSE_POSIX_ERRNO == 1 )
  4484. {
  4485. pxCurrentTCBs[ xCoreID ]->iTaskErrno = FreeRTOS_errno;
  4486. }
  4487. #endif
  4488. /* Select a new task to run. */
  4489. taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
  4490. traceTASK_SWITCHED_IN();
  4491. /* Macro to inject port specific behaviour immediately after
  4492. * switching tasks, such as setting an end of stack watchpoint
  4493. * or reconfiguring the MPU. */
  4494. portTASK_SWITCH_HOOK( pxCurrentTCBs[ portGET_CORE_ID() ] );
  4495. /* After the new task is switched in, update the global errno. */
  4496. #if ( configUSE_POSIX_ERRNO == 1 )
  4497. {
  4498. FreeRTOS_errno = pxCurrentTCBs[ xCoreID ]->iTaskErrno;
  4499. }
  4500. #endif
  4501. #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
  4502. {
  4503. /* Switch C-Runtime's TLS Block to point to the TLS
  4504. * Block specific to this task. */
  4505. configSET_TLS_BLOCK( pxCurrentTCBs[ xCoreID ]->xTLSBlock );
  4506. }
  4507. #endif
  4508. }
  4509. }
  4510. portRELEASE_ISR_LOCK( xCoreID );
  4511. portRELEASE_TASK_LOCK( xCoreID );
  4512. traceRETURN_vTaskSwitchContext();
  4513. }
  4514. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  4515. /*-----------------------------------------------------------*/
  4516. void vTaskPlaceOnEventList( List_t * const pxEventList,
  4517. const TickType_t xTicksToWait )
  4518. {
  4519. traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
  4520. configASSERT( pxEventList );
  4521. /* THIS FUNCTION MUST BE CALLED WITH THE
  4522. * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
  4523. /* Place the event list item of the TCB in the appropriate event list.
  4524. * This is placed in the list in priority order so the highest priority task
  4525. * is the first to be woken by the event.
  4526. *
  4527. * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
  4528. * Normally, the xItemValue of a TCB's ListItem_t members is:
  4529. * xItemValue = ( configMAX_PRIORITIES - uxPriority )
  4530. * Therefore, the event list is sorted in descending priority order.
  4531. *
  4532. * The queue that contains the event list is locked, preventing
  4533. * simultaneous access from interrupts. */
  4534. vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  4535. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  4536. traceRETURN_vTaskPlaceOnEventList();
  4537. }
  4538. /*-----------------------------------------------------------*/
  4539. void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
  4540. const TickType_t xItemValue,
  4541. const TickType_t xTicksToWait )
  4542. {
  4543. traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
  4544. configASSERT( pxEventList );
  4545. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  4546. * the event groups implementation. */
  4547. configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
  4548. /* Store the item value in the event list item. It is safe to access the
  4549. * event list item here as interrupts won't access the event list item of a
  4550. * task that is not in the Blocked state. */
  4551. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  4552. /* Place the event list item of the TCB at the end of the appropriate event
  4553. * list. It is safe to access the event list here because it is part of an
  4554. * event group implementation - and interrupts don't access event groups
  4555. * directly (instead they access them indirectly by pending function calls to
  4556. * the task level). */
  4557. listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  4558. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  4559. traceRETURN_vTaskPlaceOnUnorderedEventList();
  4560. }
  4561. /*-----------------------------------------------------------*/
  4562. #if ( configUSE_TIMERS == 1 )
  4563. void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
  4564. TickType_t xTicksToWait,
  4565. const BaseType_t xWaitIndefinitely )
  4566. {
  4567. traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
  4568. configASSERT( pxEventList );
  4569. /* This function should not be called by application code hence the
  4570. * 'Restricted' in its name. It is not part of the public API. It is
  4571. * designed for use by kernel code, and has special calling requirements -
  4572. * it should be called with the scheduler suspended. */
  4573. /* Place the event list item of the TCB in the appropriate event list.
  4574. * In this case it is assume that this is the only task that is going to
  4575. * be waiting on this event list, so the faster vListInsertEnd() function
  4576. * can be used in place of vListInsert. */
  4577. listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
  4578. /* If the task should block indefinitely then set the block time to a
  4579. * value that will be recognised as an indefinite delay inside the
  4580. * prvAddCurrentTaskToDelayedList() function. */
  4581. if( xWaitIndefinitely != pdFALSE )
  4582. {
  4583. xTicksToWait = portMAX_DELAY;
  4584. }
  4585. traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
  4586. prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
  4587. traceRETURN_vTaskPlaceOnEventListRestricted();
  4588. }
  4589. #endif /* configUSE_TIMERS */
  4590. /*-----------------------------------------------------------*/
  4591. BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
  4592. {
  4593. TCB_t * pxUnblockedTCB;
  4594. BaseType_t xReturn;
  4595. traceENTER_xTaskRemoveFromEventList( pxEventList );
  4596. /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
  4597. * called from a critical section within an ISR. */
  4598. /* The event list is sorted in priority order, so the first in the list can
  4599. * be removed as it is known to be the highest priority. Remove the TCB from
  4600. * the delayed list, and add it to the ready list.
  4601. *
  4602. * If an event is for a queue that is locked then this function will never
  4603. * get called - the lock count on the queue will get modified instead. This
  4604. * means exclusive access to the event list is guaranteed here.
  4605. *
  4606. * This function assumes that a check has already been made to ensure that
  4607. * pxEventList is not empty. */
  4608. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  4609. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  4610. /* coverity[misra_c_2012_rule_11_5_violation] */
  4611. pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
  4612. configASSERT( pxUnblockedTCB );
  4613. listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
  4614. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  4615. {
  4616. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  4617. prvAddTaskToReadyList( pxUnblockedTCB );
  4618. #if ( configUSE_TICKLESS_IDLE != 0 )
  4619. {
  4620. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  4621. * might be set to the blocked task's time out time. If the task is
  4622. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  4623. * normally left unchanged, because it is automatically reset to a new
  4624. * value when the tick count equals xNextTaskUnblockTime. However if
  4625. * tickless idling is used it might be more important to enter sleep mode
  4626. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  4627. * ensure it is updated at the earliest possible time. */
  4628. prvResetNextTaskUnblockTime();
  4629. }
  4630. #endif
  4631. }
  4632. else
  4633. {
  4634. /* The delayed and ready lists cannot be accessed, so hold this task
  4635. * pending until the scheduler is resumed. */
  4636. listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
  4637. }
  4638. #if ( configNUMBER_OF_CORES == 1 )
  4639. {
  4640. if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
  4641. {
  4642. /* Return true if the task removed from the event list has a higher
  4643. * priority than the calling task. This allows the calling task to know if
  4644. * it should force a context switch now. */
  4645. xReturn = pdTRUE;
  4646. /* Mark that a yield is pending in case the user is not using the
  4647. * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
  4648. xYieldPendings[ 0 ] = pdTRUE;
  4649. }
  4650. else
  4651. {
  4652. xReturn = pdFALSE;
  4653. }
  4654. }
  4655. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  4656. {
  4657. xReturn = pdFALSE;
  4658. #if ( configUSE_PREEMPTION == 1 )
  4659. {
  4660. prvYieldForTask( pxUnblockedTCB );
  4661. if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
  4662. {
  4663. xReturn = pdTRUE;
  4664. }
  4665. }
  4666. #endif /* #if ( configUSE_PREEMPTION == 1 ) */
  4667. }
  4668. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  4669. traceRETURN_xTaskRemoveFromEventList( xReturn );
  4670. return xReturn;
  4671. }
  4672. /*-----------------------------------------------------------*/
  4673. void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
  4674. const TickType_t xItemValue )
  4675. {
  4676. TCB_t * pxUnblockedTCB;
  4677. traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
  4678. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  4679. * the event flags implementation. */
  4680. configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
  4681. /* Store the new item value in the event list. */
  4682. listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  4683. /* Remove the event list form the event flag. Interrupts do not access
  4684. * event flags. */
  4685. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  4686. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  4687. /* coverity[misra_c_2012_rule_11_5_violation] */
  4688. pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem );
  4689. configASSERT( pxUnblockedTCB );
  4690. listREMOVE_ITEM( pxEventListItem );
  4691. #if ( configUSE_TICKLESS_IDLE != 0 )
  4692. {
  4693. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  4694. * might be set to the blocked task's time out time. If the task is
  4695. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  4696. * normally left unchanged, because it is automatically reset to a new
  4697. * value when the tick count equals xNextTaskUnblockTime. However if
  4698. * tickless idling is used it might be more important to enter sleep mode
  4699. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  4700. * ensure it is updated at the earliest possible time. */
  4701. prvResetNextTaskUnblockTime();
  4702. }
  4703. #endif
  4704. /* Remove the task from the delayed list and add it to the ready list. The
  4705. * scheduler is suspended so interrupts will not be accessing the ready
  4706. * lists. */
  4707. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  4708. prvAddTaskToReadyList( pxUnblockedTCB );
  4709. #if ( configNUMBER_OF_CORES == 1 )
  4710. {
  4711. if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
  4712. {
  4713. /* The unblocked task has a priority above that of the calling task, so
  4714. * a context switch is required. This function is called with the
  4715. * scheduler suspended so xYieldPending is set so the context switch
  4716. * occurs immediately that the scheduler is resumed (unsuspended). */
  4717. xYieldPendings[ 0 ] = pdTRUE;
  4718. }
  4719. }
  4720. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  4721. {
  4722. #if ( configUSE_PREEMPTION == 1 )
  4723. {
  4724. taskENTER_CRITICAL();
  4725. {
  4726. prvYieldForTask( pxUnblockedTCB );
  4727. }
  4728. taskEXIT_CRITICAL();
  4729. }
  4730. #endif
  4731. }
  4732. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  4733. traceRETURN_vTaskRemoveFromUnorderedEventList();
  4734. }
  4735. /*-----------------------------------------------------------*/
  4736. void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
  4737. {
  4738. traceENTER_vTaskSetTimeOutState( pxTimeOut );
  4739. configASSERT( pxTimeOut );
  4740. taskENTER_CRITICAL();
  4741. {
  4742. pxTimeOut->xOverflowCount = xNumOfOverflows;
  4743. pxTimeOut->xTimeOnEntering = xTickCount;
  4744. }
  4745. taskEXIT_CRITICAL();
  4746. traceRETURN_vTaskSetTimeOutState();
  4747. }
  4748. /*-----------------------------------------------------------*/
  4749. void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
  4750. {
  4751. traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
  4752. /* For internal use only as it does not use a critical section. */
  4753. pxTimeOut->xOverflowCount = xNumOfOverflows;
  4754. pxTimeOut->xTimeOnEntering = xTickCount;
  4755. traceRETURN_vTaskInternalSetTimeOutState();
  4756. }
  4757. /*-----------------------------------------------------------*/
  4758. BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
  4759. TickType_t * const pxTicksToWait )
  4760. {
  4761. BaseType_t xReturn;
  4762. traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
  4763. configASSERT( pxTimeOut );
  4764. configASSERT( pxTicksToWait );
  4765. taskENTER_CRITICAL();
  4766. {
  4767. /* Minor optimisation. The tick count cannot change in this block. */
  4768. const TickType_t xConstTickCount = xTickCount;
  4769. const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
  4770. #if ( INCLUDE_xTaskAbortDelay == 1 )
  4771. if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
  4772. {
  4773. /* The delay was aborted, which is not the same as a time out,
  4774. * but has the same result. */
  4775. pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
  4776. xReturn = pdTRUE;
  4777. }
  4778. else
  4779. #endif
  4780. #if ( INCLUDE_vTaskSuspend == 1 )
  4781. if( *pxTicksToWait == portMAX_DELAY )
  4782. {
  4783. /* If INCLUDE_vTaskSuspend is set to 1 and the block time
  4784. * specified is the maximum block time then the task should block
  4785. * indefinitely, and therefore never time out. */
  4786. xReturn = pdFALSE;
  4787. }
  4788. else
  4789. #endif
  4790. if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) )
  4791. {
  4792. /* The tick count is greater than the time at which
  4793. * vTaskSetTimeout() was called, but has also overflowed since
  4794. * vTaskSetTimeOut() was called. It must have wrapped all the way
  4795. * around and gone past again. This passed since vTaskSetTimeout()
  4796. * was called. */
  4797. xReturn = pdTRUE;
  4798. *pxTicksToWait = ( TickType_t ) 0;
  4799. }
  4800. else if( xElapsedTime < *pxTicksToWait )
  4801. {
  4802. /* Not a genuine timeout. Adjust parameters for time remaining. */
  4803. *pxTicksToWait -= xElapsedTime;
  4804. vTaskInternalSetTimeOutState( pxTimeOut );
  4805. xReturn = pdFALSE;
  4806. }
  4807. else
  4808. {
  4809. *pxTicksToWait = ( TickType_t ) 0;
  4810. xReturn = pdTRUE;
  4811. }
  4812. }
  4813. taskEXIT_CRITICAL();
  4814. traceRETURN_xTaskCheckForTimeOut( xReturn );
  4815. return xReturn;
  4816. }
  4817. /*-----------------------------------------------------------*/
  4818. void vTaskMissedYield( void )
  4819. {
  4820. traceENTER_vTaskMissedYield();
  4821. /* Must be called from within a critical section. */
  4822. xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
  4823. traceRETURN_vTaskMissedYield();
  4824. }
  4825. /*-----------------------------------------------------------*/
  4826. #if ( configUSE_TRACE_FACILITY == 1 )
  4827. UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
  4828. {
  4829. UBaseType_t uxReturn;
  4830. TCB_t const * pxTCB;
  4831. traceENTER_uxTaskGetTaskNumber( xTask );
  4832. if( xTask != NULL )
  4833. {
  4834. pxTCB = xTask;
  4835. uxReturn = pxTCB->uxTaskNumber;
  4836. }
  4837. else
  4838. {
  4839. uxReturn = 0U;
  4840. }
  4841. traceRETURN_uxTaskGetTaskNumber( uxReturn );
  4842. return uxReturn;
  4843. }
  4844. #endif /* configUSE_TRACE_FACILITY */
  4845. /*-----------------------------------------------------------*/
  4846. #if ( configUSE_TRACE_FACILITY == 1 )
  4847. void vTaskSetTaskNumber( TaskHandle_t xTask,
  4848. const UBaseType_t uxHandle )
  4849. {
  4850. TCB_t * pxTCB;
  4851. traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
  4852. if( xTask != NULL )
  4853. {
  4854. pxTCB = xTask;
  4855. pxTCB->uxTaskNumber = uxHandle;
  4856. }
  4857. traceRETURN_vTaskSetTaskNumber();
  4858. }
  4859. #endif /* configUSE_TRACE_FACILITY */
  4860. /*-----------------------------------------------------------*/
  4861. /*
  4862. * -----------------------------------------------------------
  4863. * The passive idle task.
  4864. * ----------------------------------------------------------
  4865. *
  4866. * The passive idle task is used for all the additional cores in a SMP
  4867. * system. There must be only 1 active idle task and the rest are passive
  4868. * idle tasks.
  4869. *
  4870. * The portTASK_FUNCTION() macro is used to allow port/compiler specific
  4871. * language extensions. The equivalent prototype for this function is:
  4872. *
  4873. * void prvPassiveIdleTask( void *pvParameters );
  4874. */
  4875. #if ( configNUMBER_OF_CORES > 1 )
  4876. static portTASK_FUNCTION( prvPassiveIdleTask, pvParameters )
  4877. {
  4878. ( void ) pvParameters;
  4879. taskYIELD();
  4880. for( ; configCONTROL_INFINITE_LOOP(); )
  4881. {
  4882. #if ( configUSE_PREEMPTION == 0 )
  4883. {
  4884. /* If we are not using preemption we keep forcing a task switch to
  4885. * see if any other task has become available. If we are using
  4886. * preemption we don't need to do this as any task becoming available
  4887. * will automatically get the processor anyway. */
  4888. taskYIELD();
  4889. }
  4890. #endif /* configUSE_PREEMPTION */
  4891. #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
  4892. {
  4893. /* When using preemption tasks of equal priority will be
  4894. * timesliced. If a task that is sharing the idle priority is ready
  4895. * to run then the idle task should yield before the end of the
  4896. * timeslice.
  4897. *
  4898. * A critical region is not required here as we are just reading from
  4899. * the list, and an occasional incorrect value will not matter. If
  4900. * the ready list at the idle priority contains one more task than the
  4901. * number of idle tasks, which is equal to the configured numbers of cores
  4902. * then a task other than the idle task is ready to execute. */
  4903. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
  4904. {
  4905. taskYIELD();
  4906. }
  4907. else
  4908. {
  4909. mtCOVERAGE_TEST_MARKER();
  4910. }
  4911. }
  4912. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
  4913. #if ( configUSE_PASSIVE_IDLE_HOOK == 1 )
  4914. {
  4915. /* Call the user defined function from within the idle task. This
  4916. * allows the application designer to add background functionality
  4917. * without the overhead of a separate task.
  4918. *
  4919. * This hook is intended to manage core activity such as disabling cores that go idle.
  4920. *
  4921. * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
  4922. * CALL A FUNCTION THAT MIGHT BLOCK. */
  4923. vApplicationPassiveIdleHook();
  4924. }
  4925. #endif /* configUSE_PASSIVE_IDLE_HOOK */
  4926. }
  4927. }
  4928. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  4929. /*
  4930. * -----------------------------------------------------------
  4931. * The idle task.
  4932. * ----------------------------------------------------------
  4933. *
  4934. * The portTASK_FUNCTION() macro is used to allow port/compiler specific
  4935. * language extensions. The equivalent prototype for this function is:
  4936. *
  4937. * void prvIdleTask( void *pvParameters );
  4938. *
  4939. */
  4940. static portTASK_FUNCTION( prvIdleTask, pvParameters )
  4941. {
  4942. /* Stop warnings. */
  4943. ( void ) pvParameters;
  4944. /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
  4945. * SCHEDULER IS STARTED. **/
  4946. /* In case a task that has a secure context deletes itself, in which case
  4947. * the idle task is responsible for deleting the task's secure context, if
  4948. * any. */
  4949. portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
  4950. #if ( configNUMBER_OF_CORES > 1 )
  4951. {
  4952. /* SMP all cores start up in the idle task. This initial yield gets the application
  4953. * tasks started. */
  4954. taskYIELD();
  4955. }
  4956. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  4957. for( ; configCONTROL_INFINITE_LOOP(); )
  4958. {
  4959. /* See if any tasks have deleted themselves - if so then the idle task
  4960. * is responsible for freeing the deleted task's TCB and stack. */
  4961. prvCheckTasksWaitingTermination();
  4962. #if ( configUSE_PREEMPTION == 0 )
  4963. {
  4964. /* If we are not using preemption we keep forcing a task switch to
  4965. * see if any other task has become available. If we are using
  4966. * preemption we don't need to do this as any task becoming available
  4967. * will automatically get the processor anyway. */
  4968. taskYIELD();
  4969. }
  4970. #endif /* configUSE_PREEMPTION */
  4971. #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
  4972. {
  4973. /* When using preemption tasks of equal priority will be
  4974. * timesliced. If a task that is sharing the idle priority is ready
  4975. * to run then the idle task should yield before the end of the
  4976. * timeslice.
  4977. *
  4978. * A critical region is not required here as we are just reading from
  4979. * the list, and an occasional incorrect value will not matter. If
  4980. * the ready list at the idle priority contains one more task than the
  4981. * number of idle tasks, which is equal to the configured numbers of cores
  4982. * then a task other than the idle task is ready to execute. */
  4983. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
  4984. {
  4985. taskYIELD();
  4986. }
  4987. else
  4988. {
  4989. mtCOVERAGE_TEST_MARKER();
  4990. }
  4991. }
  4992. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
  4993. #if ( configUSE_IDLE_HOOK == 1 )
  4994. {
  4995. /* Call the user defined function from within the idle task. */
  4996. vApplicationIdleHook();
  4997. }
  4998. #endif /* configUSE_IDLE_HOOK */
  4999. /* This conditional compilation should use inequality to 0, not equality
  5000. * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
  5001. * user defined low power mode implementations require
  5002. * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
  5003. #if ( configUSE_TICKLESS_IDLE != 0 )
  5004. {
  5005. TickType_t xExpectedIdleTime;
  5006. /* It is not desirable to suspend then resume the scheduler on
  5007. * each iteration of the idle task. Therefore, a preliminary
  5008. * test of the expected idle time is performed without the
  5009. * scheduler suspended. The result here is not necessarily
  5010. * valid. */
  5011. xExpectedIdleTime = prvGetExpectedIdleTime();
  5012. if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  5013. {
  5014. vTaskSuspendAll();
  5015. {
  5016. /* Now the scheduler is suspended, the expected idle
  5017. * time can be sampled again, and this time its value can
  5018. * be used. */
  5019. configASSERT( xNextTaskUnblockTime >= xTickCount );
  5020. xExpectedIdleTime = prvGetExpectedIdleTime();
  5021. /* Define the following macro to set xExpectedIdleTime to 0
  5022. * if the application does not want
  5023. * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
  5024. configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
  5025. if( xExpectedIdleTime >= ( TickType_t ) configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  5026. {
  5027. traceLOW_POWER_IDLE_BEGIN();
  5028. portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
  5029. traceLOW_POWER_IDLE_END();
  5030. }
  5031. else
  5032. {
  5033. mtCOVERAGE_TEST_MARKER();
  5034. }
  5035. }
  5036. ( void ) xTaskResumeAll();
  5037. }
  5038. else
  5039. {
  5040. mtCOVERAGE_TEST_MARKER();
  5041. }
  5042. }
  5043. #endif /* configUSE_TICKLESS_IDLE */
  5044. #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) )
  5045. {
  5046. /* Call the user defined function from within the idle task. This
  5047. * allows the application designer to add background functionality
  5048. * without the overhead of a separate task.
  5049. *
  5050. * This hook is intended to manage core activity such as disabling cores that go idle.
  5051. *
  5052. * NOTE: vApplicationPassiveIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
  5053. * CALL A FUNCTION THAT MIGHT BLOCK. */
  5054. vApplicationPassiveIdleHook();
  5055. }
  5056. #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PASSIVE_IDLE_HOOK == 1 ) ) */
  5057. }
  5058. }
  5059. /*-----------------------------------------------------------*/
  5060. #if ( configUSE_TICKLESS_IDLE != 0 )
  5061. eSleepModeStatus eTaskConfirmSleepModeStatus( void )
  5062. {
  5063. #if ( INCLUDE_vTaskSuspend == 1 )
  5064. /* The idle task exists in addition to the application tasks. */
  5065. const UBaseType_t uxNonApplicationTasks = configNUMBER_OF_CORES;
  5066. #endif /* INCLUDE_vTaskSuspend */
  5067. eSleepModeStatus eReturn = eStandardSleep;
  5068. traceENTER_eTaskConfirmSleepModeStatus();
  5069. /* This function must be called from a critical section. */
  5070. if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0U )
  5071. {
  5072. /* A task was made ready while the scheduler was suspended. */
  5073. eReturn = eAbortSleep;
  5074. }
  5075. else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
  5076. {
  5077. /* A yield was pended while the scheduler was suspended. */
  5078. eReturn = eAbortSleep;
  5079. }
  5080. else if( xPendedTicks != 0U )
  5081. {
  5082. /* A tick interrupt has already occurred but was held pending
  5083. * because the scheduler is suspended. */
  5084. eReturn = eAbortSleep;
  5085. }
  5086. #if ( INCLUDE_vTaskSuspend == 1 )
  5087. else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
  5088. {
  5089. /* If all the tasks are in the suspended list (which might mean they
  5090. * have an infinite block time rather than actually being suspended)
  5091. * then it is safe to turn all clocks off and just wait for external
  5092. * interrupts. */
  5093. eReturn = eNoTasksWaitingTimeout;
  5094. }
  5095. #endif /* INCLUDE_vTaskSuspend */
  5096. else
  5097. {
  5098. mtCOVERAGE_TEST_MARKER();
  5099. }
  5100. traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
  5101. return eReturn;
  5102. }
  5103. #endif /* configUSE_TICKLESS_IDLE */
  5104. /*-----------------------------------------------------------*/
  5105. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  5106. void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
  5107. BaseType_t xIndex,
  5108. void * pvValue )
  5109. {
  5110. TCB_t * pxTCB;
  5111. traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
  5112. if( ( xIndex >= 0 ) &&
  5113. ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
  5114. {
  5115. pxTCB = prvGetTCBFromHandle( xTaskToSet );
  5116. configASSERT( pxTCB != NULL );
  5117. pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
  5118. }
  5119. traceRETURN_vTaskSetThreadLocalStoragePointer();
  5120. }
  5121. #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
  5122. /*-----------------------------------------------------------*/
  5123. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  5124. void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
  5125. BaseType_t xIndex )
  5126. {
  5127. void * pvReturn = NULL;
  5128. TCB_t * pxTCB;
  5129. traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
  5130. if( ( xIndex >= 0 ) &&
  5131. ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
  5132. {
  5133. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  5134. configASSERT( pxTCB != NULL );
  5135. pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
  5136. }
  5137. else
  5138. {
  5139. pvReturn = NULL;
  5140. }
  5141. traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
  5142. return pvReturn;
  5143. }
  5144. #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
  5145. /*-----------------------------------------------------------*/
  5146. #if ( portUSING_MPU_WRAPPERS == 1 )
  5147. void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
  5148. const MemoryRegion_t * const pxRegions )
  5149. {
  5150. TCB_t * pxTCB;
  5151. traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
  5152. /* If null is passed in here then we are modifying the MPU settings of
  5153. * the calling task. */
  5154. pxTCB = prvGetTCBFromHandle( xTaskToModify );
  5155. configASSERT( pxTCB != NULL );
  5156. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
  5157. traceRETURN_vTaskAllocateMPURegions();
  5158. }
  5159. #endif /* portUSING_MPU_WRAPPERS */
  5160. /*-----------------------------------------------------------*/
  5161. static void prvInitialiseTaskLists( void )
  5162. {
  5163. UBaseType_t uxPriority;
  5164. for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
  5165. {
  5166. vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
  5167. }
  5168. vListInitialise( &xDelayedTaskList1 );
  5169. vListInitialise( &xDelayedTaskList2 );
  5170. vListInitialise( &xPendingReadyList );
  5171. #if ( INCLUDE_vTaskDelete == 1 )
  5172. {
  5173. vListInitialise( &xTasksWaitingTermination );
  5174. }
  5175. #endif /* INCLUDE_vTaskDelete */
  5176. #if ( INCLUDE_vTaskSuspend == 1 )
  5177. {
  5178. vListInitialise( &xSuspendedTaskList );
  5179. }
  5180. #endif /* INCLUDE_vTaskSuspend */
  5181. /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
  5182. * using list2. */
  5183. pxDelayedTaskList = &xDelayedTaskList1;
  5184. pxOverflowDelayedTaskList = &xDelayedTaskList2;
  5185. }
  5186. /*-----------------------------------------------------------*/
  5187. static void prvCheckTasksWaitingTermination( void )
  5188. {
  5189. /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
  5190. #if ( INCLUDE_vTaskDelete == 1 )
  5191. {
  5192. TCB_t * pxTCB;
  5193. /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
  5194. * being called too often in the idle task. */
  5195. while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
  5196. {
  5197. #if ( configNUMBER_OF_CORES == 1 )
  5198. {
  5199. taskENTER_CRITICAL();
  5200. {
  5201. {
  5202. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  5203. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  5204. /* coverity[misra_c_2012_rule_11_5_violation] */
  5205. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
  5206. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  5207. --uxCurrentNumberOfTasks;
  5208. --uxDeletedTasksWaitingCleanUp;
  5209. }
  5210. }
  5211. taskEXIT_CRITICAL();
  5212. prvDeleteTCB( pxTCB );
  5213. }
  5214. #else /* #if( configNUMBER_OF_CORES == 1 ) */
  5215. {
  5216. pxTCB = NULL;
  5217. taskENTER_CRITICAL();
  5218. {
  5219. /* For SMP, multiple idles can be running simultaneously
  5220. * and we need to check that other idles did not cleanup while we were
  5221. * waiting to enter the critical section. */
  5222. if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
  5223. {
  5224. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  5225. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  5226. /* coverity[misra_c_2012_rule_11_5_violation] */
  5227. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) );
  5228. if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
  5229. {
  5230. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  5231. --uxCurrentNumberOfTasks;
  5232. --uxDeletedTasksWaitingCleanUp;
  5233. }
  5234. else
  5235. {
  5236. /* The TCB to be deleted still has not yet been switched out
  5237. * by the scheduler, so we will just exit this loop early and
  5238. * try again next time. */
  5239. taskEXIT_CRITICAL();
  5240. break;
  5241. }
  5242. }
  5243. }
  5244. taskEXIT_CRITICAL();
  5245. if( pxTCB != NULL )
  5246. {
  5247. prvDeleteTCB( pxTCB );
  5248. }
  5249. }
  5250. #endif /* #if( configNUMBER_OF_CORES == 1 ) */
  5251. }
  5252. }
  5253. #endif /* INCLUDE_vTaskDelete */
  5254. }
  5255. /*-----------------------------------------------------------*/
  5256. #if ( configUSE_TRACE_FACILITY == 1 )
  5257. void vTaskGetInfo( TaskHandle_t xTask,
  5258. TaskStatus_t * pxTaskStatus,
  5259. BaseType_t xGetFreeStackSpace,
  5260. eTaskState eState )
  5261. {
  5262. TCB_t * pxTCB;
  5263. traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
  5264. /* xTask is NULL then get the state of the calling task. */
  5265. pxTCB = prvGetTCBFromHandle( xTask );
  5266. configASSERT( pxTCB != NULL );
  5267. pxTaskStatus->xHandle = pxTCB;
  5268. pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
  5269. pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
  5270. pxTaskStatus->pxStackBase = pxTCB->pxStack;
  5271. #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
  5272. pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
  5273. pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
  5274. #endif
  5275. pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
  5276. #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
  5277. {
  5278. pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
  5279. }
  5280. #endif
  5281. #if ( configUSE_MUTEXES == 1 )
  5282. {
  5283. pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
  5284. }
  5285. #else
  5286. {
  5287. pxTaskStatus->uxBasePriority = 0;
  5288. }
  5289. #endif
  5290. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  5291. {
  5292. pxTaskStatus->ulRunTimeCounter = ulTaskGetRunTimeCounter( xTask );
  5293. }
  5294. #else
  5295. {
  5296. pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
  5297. }
  5298. #endif
  5299. /* Obtaining the task state is a little fiddly, so is only done if the
  5300. * value of eState passed into this function is eInvalid - otherwise the
  5301. * state is just set to whatever is passed in. */
  5302. if( eState != eInvalid )
  5303. {
  5304. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  5305. {
  5306. pxTaskStatus->eCurrentState = eRunning;
  5307. }
  5308. else
  5309. {
  5310. pxTaskStatus->eCurrentState = eState;
  5311. #if ( INCLUDE_vTaskSuspend == 1 )
  5312. {
  5313. /* If the task is in the suspended list then there is a
  5314. * chance it is actually just blocked indefinitely - so really
  5315. * it should be reported as being in the Blocked state. */
  5316. if( eState == eSuspended )
  5317. {
  5318. vTaskSuspendAll();
  5319. {
  5320. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  5321. {
  5322. pxTaskStatus->eCurrentState = eBlocked;
  5323. }
  5324. else
  5325. {
  5326. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  5327. {
  5328. BaseType_t x;
  5329. /* The task does not appear on the event list item of
  5330. * and of the RTOS objects, but could still be in the
  5331. * blocked state if it is waiting on its notification
  5332. * rather than waiting on an object. If not, is
  5333. * suspended. */
  5334. for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  5335. {
  5336. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  5337. {
  5338. pxTaskStatus->eCurrentState = eBlocked;
  5339. break;
  5340. }
  5341. }
  5342. }
  5343. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  5344. }
  5345. }
  5346. ( void ) xTaskResumeAll();
  5347. }
  5348. }
  5349. #endif /* INCLUDE_vTaskSuspend */
  5350. /* Tasks can be in pending ready list and other state list at the
  5351. * same time. These tasks are in ready state no matter what state
  5352. * list the task is in. */
  5353. taskENTER_CRITICAL();
  5354. {
  5355. if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
  5356. {
  5357. pxTaskStatus->eCurrentState = eReady;
  5358. }
  5359. }
  5360. taskEXIT_CRITICAL();
  5361. }
  5362. }
  5363. else
  5364. {
  5365. pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
  5366. }
  5367. /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
  5368. * parameter is provided to allow it to be skipped. */
  5369. if( xGetFreeStackSpace != pdFALSE )
  5370. {
  5371. #if ( portSTACK_GROWTH > 0 )
  5372. {
  5373. pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
  5374. }
  5375. #else
  5376. {
  5377. pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
  5378. }
  5379. #endif
  5380. }
  5381. else
  5382. {
  5383. pxTaskStatus->usStackHighWaterMark = 0;
  5384. }
  5385. traceRETURN_vTaskGetInfo();
  5386. }
  5387. #endif /* configUSE_TRACE_FACILITY */
  5388. /*-----------------------------------------------------------*/
  5389. #if ( configUSE_TRACE_FACILITY == 1 )
  5390. static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
  5391. List_t * pxList,
  5392. eTaskState eState )
  5393. {
  5394. UBaseType_t uxTask = 0;
  5395. const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
  5396. ListItem_t * pxIterator;
  5397. TCB_t * pxTCB = NULL;
  5398. if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
  5399. {
  5400. /* Populate an TaskStatus_t structure within the
  5401. * pxTaskStatusArray array for each task that is referenced from
  5402. * pxList. See the definition of TaskStatus_t in task.h for the
  5403. * meaning of each TaskStatus_t structure member. */
  5404. for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
  5405. {
  5406. /* MISRA Ref 11.5.3 [Void pointer assignment] */
  5407. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  5408. /* coverity[misra_c_2012_rule_11_5_violation] */
  5409. pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
  5410. vTaskGetInfo( ( TaskHandle_t ) pxTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
  5411. uxTask++;
  5412. }
  5413. }
  5414. else
  5415. {
  5416. mtCOVERAGE_TEST_MARKER();
  5417. }
  5418. return uxTask;
  5419. }
  5420. #endif /* configUSE_TRACE_FACILITY */
  5421. /*-----------------------------------------------------------*/
  5422. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  5423. static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
  5424. {
  5425. configSTACK_DEPTH_TYPE uxCount = 0U;
  5426. while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
  5427. {
  5428. pucStackByte -= portSTACK_GROWTH;
  5429. uxCount++;
  5430. }
  5431. uxCount /= ( configSTACK_DEPTH_TYPE ) sizeof( StackType_t );
  5432. return uxCount;
  5433. }
  5434. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
  5435. /*-----------------------------------------------------------*/
  5436. #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
  5437. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
  5438. * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
  5439. * user to determine the return type. It gets around the problem of the value
  5440. * overflowing on 8-bit types without breaking backward compatibility for
  5441. * applications that expect an 8-bit return type. */
  5442. configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
  5443. {
  5444. TCB_t * pxTCB;
  5445. uint8_t * pucEndOfStack;
  5446. configSTACK_DEPTH_TYPE uxReturn;
  5447. traceENTER_uxTaskGetStackHighWaterMark2( xTask );
  5448. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
  5449. * the same except for their return type. Using configSTACK_DEPTH_TYPE
  5450. * allows the user to determine the return type. It gets around the
  5451. * problem of the value overflowing on 8-bit types without breaking
  5452. * backward compatibility for applications that expect an 8-bit return
  5453. * type. */
  5454. pxTCB = prvGetTCBFromHandle( xTask );
  5455. configASSERT( pxTCB != NULL );
  5456. #if portSTACK_GROWTH < 0
  5457. {
  5458. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  5459. }
  5460. #else
  5461. {
  5462. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  5463. }
  5464. #endif
  5465. uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
  5466. traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
  5467. return uxReturn;
  5468. }
  5469. #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
  5470. /*-----------------------------------------------------------*/
  5471. #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
  5472. UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
  5473. {
  5474. TCB_t * pxTCB;
  5475. uint8_t * pucEndOfStack;
  5476. UBaseType_t uxReturn;
  5477. traceENTER_uxTaskGetStackHighWaterMark( xTask );
  5478. pxTCB = prvGetTCBFromHandle( xTask );
  5479. configASSERT( pxTCB != NULL );
  5480. #if portSTACK_GROWTH < 0
  5481. {
  5482. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  5483. }
  5484. #else
  5485. {
  5486. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  5487. }
  5488. #endif
  5489. uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
  5490. traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
  5491. return uxReturn;
  5492. }
  5493. #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
  5494. /*-----------------------------------------------------------*/
  5495. #if ( INCLUDE_vTaskDelete == 1 )
  5496. static void prvDeleteTCB( TCB_t * pxTCB )
  5497. {
  5498. /* This call is required specifically for the TriCore port. It must be
  5499. * above the vPortFree() calls. The call is also used by ports/demos that
  5500. * want to allocate and clean RAM statically. */
  5501. portCLEAN_UP_TCB( pxTCB );
  5502. #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
  5503. {
  5504. /* Free up the memory allocated for the task's TLS Block. */
  5505. configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
  5506. }
  5507. #endif
  5508. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
  5509. {
  5510. /* The task can only have been allocated dynamically - free both
  5511. * the stack and TCB. */
  5512. vPortFreeStack( pxTCB->pxStack );
  5513. vPortFree( pxTCB );
  5514. }
  5515. #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  5516. {
  5517. /* The task could have been allocated statically or dynamically, so
  5518. * check what was statically allocated before trying to free the
  5519. * memory. */
  5520. if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
  5521. {
  5522. /* Both the stack and TCB were allocated dynamically, so both
  5523. * must be freed. */
  5524. vPortFreeStack( pxTCB->pxStack );
  5525. vPortFree( pxTCB );
  5526. }
  5527. else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
  5528. {
  5529. /* Only the stack was statically allocated, so the TCB is the
  5530. * only memory that must be freed. */
  5531. vPortFree( pxTCB );
  5532. }
  5533. else
  5534. {
  5535. /* Neither the stack nor the TCB were allocated dynamically, so
  5536. * nothing needs to be freed. */
  5537. configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
  5538. mtCOVERAGE_TEST_MARKER();
  5539. }
  5540. }
  5541. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  5542. }
  5543. #endif /* INCLUDE_vTaskDelete */
  5544. /*-----------------------------------------------------------*/
  5545. static void prvResetNextTaskUnblockTime( void )
  5546. {
  5547. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  5548. {
  5549. /* The new current delayed list is empty. Set xNextTaskUnblockTime to
  5550. * the maximum possible value so it is extremely unlikely that the
  5551. * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
  5552. * there is an item in the delayed list. */
  5553. xNextTaskUnblockTime = portMAX_DELAY;
  5554. }
  5555. else
  5556. {
  5557. /* The new current delayed list is not empty, get the value of
  5558. * the item at the head of the delayed list. This is the time at
  5559. * which the task at the head of the delayed list should be removed
  5560. * from the Blocked state. */
  5561. xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
  5562. }
  5563. }
  5564. /*-----------------------------------------------------------*/
  5565. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_RECURSIVE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
  5566. #if ( configNUMBER_OF_CORES == 1 )
  5567. TaskHandle_t xTaskGetCurrentTaskHandle( void )
  5568. {
  5569. TaskHandle_t xReturn;
  5570. traceENTER_xTaskGetCurrentTaskHandle();
  5571. /* A critical section is not required as this is not called from
  5572. * an interrupt and the current TCB will always be the same for any
  5573. * individual execution thread. */
  5574. xReturn = pxCurrentTCB;
  5575. traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
  5576. return xReturn;
  5577. }
  5578. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  5579. TaskHandle_t xTaskGetCurrentTaskHandle( void )
  5580. {
  5581. TaskHandle_t xReturn;
  5582. UBaseType_t uxSavedInterruptStatus;
  5583. traceENTER_xTaskGetCurrentTaskHandle();
  5584. uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
  5585. {
  5586. xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
  5587. }
  5588. portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
  5589. traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
  5590. return xReturn;
  5591. }
  5592. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  5593. TaskHandle_t xTaskGetCurrentTaskHandleForCore( BaseType_t xCoreID )
  5594. {
  5595. TaskHandle_t xReturn = NULL;
  5596. traceENTER_xTaskGetCurrentTaskHandleForCore( xCoreID );
  5597. if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
  5598. {
  5599. #if ( configNUMBER_OF_CORES == 1 )
  5600. xReturn = pxCurrentTCB;
  5601. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  5602. xReturn = pxCurrentTCBs[ xCoreID ];
  5603. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  5604. }
  5605. traceRETURN_xTaskGetCurrentTaskHandleForCore( xReturn );
  5606. return xReturn;
  5607. }
  5608. #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_RECURSIVE_MUTEXES == 1 ) ) */
  5609. /*-----------------------------------------------------------*/
  5610. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  5611. BaseType_t xTaskGetSchedulerState( void )
  5612. {
  5613. BaseType_t xReturn;
  5614. traceENTER_xTaskGetSchedulerState();
  5615. if( xSchedulerRunning == pdFALSE )
  5616. {
  5617. xReturn = taskSCHEDULER_NOT_STARTED;
  5618. }
  5619. else
  5620. {
  5621. #if ( configNUMBER_OF_CORES > 1 )
  5622. taskENTER_CRITICAL();
  5623. #endif
  5624. {
  5625. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  5626. {
  5627. xReturn = taskSCHEDULER_RUNNING;
  5628. }
  5629. else
  5630. {
  5631. xReturn = taskSCHEDULER_SUSPENDED;
  5632. }
  5633. }
  5634. #if ( configNUMBER_OF_CORES > 1 )
  5635. taskEXIT_CRITICAL();
  5636. #endif
  5637. }
  5638. traceRETURN_xTaskGetSchedulerState( xReturn );
  5639. return xReturn;
  5640. }
  5641. #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
  5642. /*-----------------------------------------------------------*/
  5643. #if ( configUSE_MUTEXES == 1 )
  5644. BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
  5645. {
  5646. TCB_t * const pxMutexHolderTCB = pxMutexHolder;
  5647. BaseType_t xReturn = pdFALSE;
  5648. traceENTER_xTaskPriorityInherit( pxMutexHolder );
  5649. /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
  5650. * inheritance is not applied in this scenario. */
  5651. if( pxMutexHolder != NULL )
  5652. {
  5653. /* If the holder of the mutex has a priority below the priority of
  5654. * the task attempting to obtain the mutex then it will temporarily
  5655. * inherit the priority of the task attempting to obtain the mutex. */
  5656. if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
  5657. {
  5658. /* Adjust the mutex holder state to account for its new
  5659. * priority. Only reset the event list item value if the value is
  5660. * not being used for anything else. */
  5661. if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
  5662. {
  5663. listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority );
  5664. }
  5665. else
  5666. {
  5667. mtCOVERAGE_TEST_MARKER();
  5668. }
  5669. /* If the task being modified is in the ready state it will need
  5670. * to be moved into a new list. */
  5671. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
  5672. {
  5673. if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  5674. {
  5675. /* It is known that the task is in its ready list so
  5676. * there is no need to check again and the port level
  5677. * reset macro can be called directly. */
  5678. portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
  5679. }
  5680. else
  5681. {
  5682. mtCOVERAGE_TEST_MARKER();
  5683. }
  5684. /* Inherit the priority before being moved into the new list. */
  5685. pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
  5686. prvAddTaskToReadyList( pxMutexHolderTCB );
  5687. #if ( configNUMBER_OF_CORES > 1 )
  5688. {
  5689. /* The priority of the task is raised. Yield for this task
  5690. * if it is not running. */
  5691. if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
  5692. {
  5693. prvYieldForTask( pxMutexHolderTCB );
  5694. }
  5695. }
  5696. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  5697. }
  5698. else
  5699. {
  5700. /* Just inherit the priority. */
  5701. pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
  5702. }
  5703. traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
  5704. /* Inheritance occurred. */
  5705. xReturn = pdTRUE;
  5706. }
  5707. else
  5708. {
  5709. if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
  5710. {
  5711. /* The base priority of the mutex holder is lower than the
  5712. * priority of the task attempting to take the mutex, but the
  5713. * current priority of the mutex holder is not lower than the
  5714. * priority of the task attempting to take the mutex.
  5715. * Therefore the mutex holder must have already inherited a
  5716. * priority, but inheritance would have occurred if that had
  5717. * not been the case. */
  5718. xReturn = pdTRUE;
  5719. }
  5720. else
  5721. {
  5722. mtCOVERAGE_TEST_MARKER();
  5723. }
  5724. }
  5725. }
  5726. else
  5727. {
  5728. mtCOVERAGE_TEST_MARKER();
  5729. }
  5730. traceRETURN_xTaskPriorityInherit( xReturn );
  5731. return xReturn;
  5732. }
  5733. #endif /* configUSE_MUTEXES */
  5734. /*-----------------------------------------------------------*/
  5735. #if ( configUSE_MUTEXES == 1 )
  5736. BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
  5737. {
  5738. TCB_t * const pxTCB = pxMutexHolder;
  5739. BaseType_t xReturn = pdFALSE;
  5740. traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
  5741. if( pxMutexHolder != NULL )
  5742. {
  5743. /* A task can only have an inherited priority if it holds the mutex.
  5744. * If the mutex is held by a task then it cannot be given from an
  5745. * interrupt, and if a mutex is given by the holding task then it must
  5746. * be the running state task. */
  5747. configASSERT( pxTCB == pxCurrentTCB );
  5748. configASSERT( pxTCB->uxMutexesHeld );
  5749. ( pxTCB->uxMutexesHeld )--;
  5750. /* Has the holder of the mutex inherited the priority of another
  5751. * task? */
  5752. if( pxTCB->uxPriority != pxTCB->uxBasePriority )
  5753. {
  5754. /* Only disinherit if no other mutexes are held. */
  5755. if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
  5756. {
  5757. /* A task can only have an inherited priority if it holds
  5758. * the mutex. If the mutex is held by a task then it cannot be
  5759. * given from an interrupt, and if a mutex is given by the
  5760. * holding task then it must be the running state task. Remove
  5761. * the holding task from the ready list. */
  5762. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  5763. {
  5764. portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
  5765. }
  5766. else
  5767. {
  5768. mtCOVERAGE_TEST_MARKER();
  5769. }
  5770. /* Disinherit the priority before adding the task into the
  5771. * new ready list. */
  5772. traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
  5773. pxTCB->uxPriority = pxTCB->uxBasePriority;
  5774. /* Reset the event list item value. It cannot be in use for
  5775. * any other purpose if this task is running, and it must be
  5776. * running to give back the mutex. */
  5777. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority );
  5778. prvAddTaskToReadyList( pxTCB );
  5779. #if ( configNUMBER_OF_CORES > 1 )
  5780. {
  5781. /* The priority of the task is dropped. Yield the core on
  5782. * which the task is running. */
  5783. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  5784. {
  5785. prvYieldCore( pxTCB->xTaskRunState );
  5786. }
  5787. }
  5788. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  5789. /* Return true to indicate that a context switch is required.
  5790. * This is only actually required in the corner case whereby
  5791. * multiple mutexes were held and the mutexes were given back
  5792. * in an order different to that in which they were taken.
  5793. * If a context switch did not occur when the first mutex was
  5794. * returned, even if a task was waiting on it, then a context
  5795. * switch should occur when the last mutex is returned whether
  5796. * a task is waiting on it or not. */
  5797. xReturn = pdTRUE;
  5798. }
  5799. else
  5800. {
  5801. mtCOVERAGE_TEST_MARKER();
  5802. }
  5803. }
  5804. else
  5805. {
  5806. mtCOVERAGE_TEST_MARKER();
  5807. }
  5808. }
  5809. else
  5810. {
  5811. mtCOVERAGE_TEST_MARKER();
  5812. }
  5813. traceRETURN_xTaskPriorityDisinherit( xReturn );
  5814. return xReturn;
  5815. }
  5816. #endif /* configUSE_MUTEXES */
  5817. /*-----------------------------------------------------------*/
  5818. #if ( configUSE_MUTEXES == 1 )
  5819. void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
  5820. UBaseType_t uxHighestPriorityWaitingTask )
  5821. {
  5822. TCB_t * const pxTCB = pxMutexHolder;
  5823. UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
  5824. const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
  5825. traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
  5826. if( pxMutexHolder != NULL )
  5827. {
  5828. /* If pxMutexHolder is not NULL then the holder must hold at least
  5829. * one mutex. */
  5830. configASSERT( pxTCB->uxMutexesHeld );
  5831. /* Determine the priority to which the priority of the task that
  5832. * holds the mutex should be set. This will be the greater of the
  5833. * holding task's base priority and the priority of the highest
  5834. * priority task that is waiting to obtain the mutex. */
  5835. if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
  5836. {
  5837. uxPriorityToUse = uxHighestPriorityWaitingTask;
  5838. }
  5839. else
  5840. {
  5841. uxPriorityToUse = pxTCB->uxBasePriority;
  5842. }
  5843. /* Does the priority need to change? */
  5844. if( pxTCB->uxPriority != uxPriorityToUse )
  5845. {
  5846. /* Only disinherit if no other mutexes are held. This is a
  5847. * simplification in the priority inheritance implementation. If
  5848. * the task that holds the mutex is also holding other mutexes then
  5849. * the other mutexes may have caused the priority inheritance. */
  5850. if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
  5851. {
  5852. /* If a task has timed out because it already holds the
  5853. * mutex it was trying to obtain then it cannot of inherited
  5854. * its own priority. */
  5855. configASSERT( pxTCB != pxCurrentTCB );
  5856. /* Disinherit the priority, remembering the previous
  5857. * priority to facilitate determining the subject task's
  5858. * state. */
  5859. traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
  5860. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  5861. pxTCB->uxPriority = uxPriorityToUse;
  5862. /* Only reset the event list item value if the value is not
  5863. * being used for anything else. */
  5864. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == ( ( TickType_t ) 0U ) )
  5865. {
  5866. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse );
  5867. }
  5868. else
  5869. {
  5870. mtCOVERAGE_TEST_MARKER();
  5871. }
  5872. /* If the running task is not the task that holds the mutex
  5873. * then the task that holds the mutex could be in either the
  5874. * Ready, Blocked or Suspended states. Only remove the task
  5875. * from its current state list if it is in the Ready state as
  5876. * the task's priority is going to change and there is one
  5877. * Ready list per priority. */
  5878. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
  5879. {
  5880. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  5881. {
  5882. /* It is known that the task is in its ready list so
  5883. * there is no need to check again and the port level
  5884. * reset macro can be called directly. */
  5885. portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
  5886. }
  5887. else
  5888. {
  5889. mtCOVERAGE_TEST_MARKER();
  5890. }
  5891. prvAddTaskToReadyList( pxTCB );
  5892. #if ( configNUMBER_OF_CORES > 1 )
  5893. {
  5894. /* The priority of the task is dropped. Yield the core on
  5895. * which the task is running. */
  5896. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  5897. {
  5898. prvYieldCore( pxTCB->xTaskRunState );
  5899. }
  5900. }
  5901. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  5902. }
  5903. else
  5904. {
  5905. mtCOVERAGE_TEST_MARKER();
  5906. }
  5907. }
  5908. else
  5909. {
  5910. mtCOVERAGE_TEST_MARKER();
  5911. }
  5912. }
  5913. else
  5914. {
  5915. mtCOVERAGE_TEST_MARKER();
  5916. }
  5917. }
  5918. else
  5919. {
  5920. mtCOVERAGE_TEST_MARKER();
  5921. }
  5922. traceRETURN_vTaskPriorityDisinheritAfterTimeout();
  5923. }
  5924. #endif /* configUSE_MUTEXES */
  5925. /*-----------------------------------------------------------*/
  5926. #if ( configNUMBER_OF_CORES > 1 )
  5927. /* If not in a critical section then yield immediately.
  5928. * Otherwise set xYieldPendings to true to wait to
  5929. * yield until exiting the critical section.
  5930. */
  5931. void vTaskYieldWithinAPI( void )
  5932. {
  5933. UBaseType_t ulState;
  5934. traceENTER_vTaskYieldWithinAPI();
  5935. ulState = portSET_INTERRUPT_MASK();
  5936. {
  5937. const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
  5938. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
  5939. {
  5940. portYIELD();
  5941. }
  5942. else
  5943. {
  5944. xYieldPendings[ xCoreID ] = pdTRUE;
  5945. }
  5946. }
  5947. portCLEAR_INTERRUPT_MASK( ulState );
  5948. traceRETURN_vTaskYieldWithinAPI();
  5949. }
  5950. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  5951. /*-----------------------------------------------------------*/
  5952. #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
  5953. void vTaskEnterCritical( void )
  5954. {
  5955. traceENTER_vTaskEnterCritical();
  5956. portDISABLE_INTERRUPTS();
  5957. if( xSchedulerRunning != pdFALSE )
  5958. {
  5959. ( pxCurrentTCB->uxCriticalNesting )++;
  5960. /* This is not the interrupt safe version of the enter critical
  5961. * function so assert() if it is being called from an interrupt
  5962. * context. Only API functions that end in "FromISR" can be used in an
  5963. * interrupt. Only assert if the critical nesting count is 1 to
  5964. * protect against recursive calls if the assert function also uses a
  5965. * critical section. */
  5966. if( pxCurrentTCB->uxCriticalNesting == 1U )
  5967. {
  5968. portASSERT_IF_IN_ISR();
  5969. }
  5970. }
  5971. else
  5972. {
  5973. mtCOVERAGE_TEST_MARKER();
  5974. }
  5975. traceRETURN_vTaskEnterCritical();
  5976. }
  5977. #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
  5978. /*-----------------------------------------------------------*/
  5979. #if ( configNUMBER_OF_CORES > 1 )
  5980. void vTaskEnterCritical( void )
  5981. {
  5982. traceENTER_vTaskEnterCritical();
  5983. portDISABLE_INTERRUPTS();
  5984. {
  5985. const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
  5986. if( xSchedulerRunning != pdFALSE )
  5987. {
  5988. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
  5989. {
  5990. portGET_TASK_LOCK( xCoreID );
  5991. portGET_ISR_LOCK( xCoreID );
  5992. }
  5993. portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
  5994. /* This is not the interrupt safe version of the enter critical
  5995. * function so assert() if it is being called from an interrupt
  5996. * context. Only API functions that end in "FromISR" can be used in an
  5997. * interrupt. Only assert if the critical nesting count is 1 to
  5998. * protect against recursive calls if the assert function also uses a
  5999. * critical section. */
  6000. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 1U )
  6001. {
  6002. portASSERT_IF_IN_ISR();
  6003. if( uxSchedulerSuspended == 0U )
  6004. {
  6005. /* The only time there would be a problem is if this is called
  6006. * before a context switch and vTaskExitCritical() is called
  6007. * after pxCurrentTCB changes. Therefore this should not be
  6008. * used within vTaskSwitchContext(). */
  6009. prvCheckForRunStateChange();
  6010. }
  6011. }
  6012. }
  6013. else
  6014. {
  6015. mtCOVERAGE_TEST_MARKER();
  6016. }
  6017. }
  6018. traceRETURN_vTaskEnterCritical();
  6019. }
  6020. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  6021. /*-----------------------------------------------------------*/
  6022. #if ( configNUMBER_OF_CORES > 1 )
  6023. UBaseType_t vTaskEnterCriticalFromISR( void )
  6024. {
  6025. UBaseType_t uxSavedInterruptStatus = 0;
  6026. const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
  6027. traceENTER_vTaskEnterCriticalFromISR();
  6028. if( xSchedulerRunning != pdFALSE )
  6029. {
  6030. uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
  6031. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
  6032. {
  6033. portGET_ISR_LOCK( xCoreID );
  6034. }
  6035. portINCREMENT_CRITICAL_NESTING_COUNT( xCoreID );
  6036. }
  6037. else
  6038. {
  6039. mtCOVERAGE_TEST_MARKER();
  6040. }
  6041. traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
  6042. return uxSavedInterruptStatus;
  6043. }
  6044. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  6045. /*-----------------------------------------------------------*/
  6046. #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
  6047. void vTaskExitCritical( void )
  6048. {
  6049. traceENTER_vTaskExitCritical();
  6050. if( xSchedulerRunning != pdFALSE )
  6051. {
  6052. /* If pxCurrentTCB->uxCriticalNesting is zero then this function
  6053. * does not match a previous call to vTaskEnterCritical(). */
  6054. configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
  6055. /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
  6056. * to exit critical section from ISR. */
  6057. portASSERT_IF_IN_ISR();
  6058. if( pxCurrentTCB->uxCriticalNesting > 0U )
  6059. {
  6060. ( pxCurrentTCB->uxCriticalNesting )--;
  6061. if( pxCurrentTCB->uxCriticalNesting == 0U )
  6062. {
  6063. portENABLE_INTERRUPTS();
  6064. }
  6065. else
  6066. {
  6067. mtCOVERAGE_TEST_MARKER();
  6068. }
  6069. }
  6070. else
  6071. {
  6072. mtCOVERAGE_TEST_MARKER();
  6073. }
  6074. }
  6075. else
  6076. {
  6077. mtCOVERAGE_TEST_MARKER();
  6078. }
  6079. traceRETURN_vTaskExitCritical();
  6080. }
  6081. #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
  6082. /*-----------------------------------------------------------*/
  6083. #if ( configNUMBER_OF_CORES > 1 )
  6084. void vTaskExitCritical( void )
  6085. {
  6086. const BaseType_t xCoreID = ( BaseType_t ) portGET_CORE_ID();
  6087. traceENTER_vTaskExitCritical();
  6088. if( xSchedulerRunning != pdFALSE )
  6089. {
  6090. /* If critical nesting count is zero then this function
  6091. * does not match a previous call to vTaskEnterCritical(). */
  6092. configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U );
  6093. /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
  6094. * to exit critical section from ISR. */
  6095. portASSERT_IF_IN_ISR();
  6096. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U )
  6097. {
  6098. portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID );
  6099. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
  6100. {
  6101. BaseType_t xYieldCurrentTask;
  6102. /* Get the xYieldPending stats inside the critical section. */
  6103. xYieldCurrentTask = xYieldPendings[ xCoreID ];
  6104. portRELEASE_ISR_LOCK( xCoreID );
  6105. portRELEASE_TASK_LOCK( xCoreID );
  6106. portENABLE_INTERRUPTS();
  6107. /* When a task yields in a critical section it just sets
  6108. * xYieldPending to true. So now that we have exited the
  6109. * critical section check if xYieldPending is true, and
  6110. * if so yield. */
  6111. if( xYieldCurrentTask != pdFALSE )
  6112. {
  6113. portYIELD();
  6114. }
  6115. }
  6116. else
  6117. {
  6118. mtCOVERAGE_TEST_MARKER();
  6119. }
  6120. }
  6121. else
  6122. {
  6123. mtCOVERAGE_TEST_MARKER();
  6124. }
  6125. }
  6126. else
  6127. {
  6128. mtCOVERAGE_TEST_MARKER();
  6129. }
  6130. traceRETURN_vTaskExitCritical();
  6131. }
  6132. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  6133. /*-----------------------------------------------------------*/
  6134. #if ( configNUMBER_OF_CORES > 1 )
  6135. void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
  6136. {
  6137. BaseType_t xCoreID;
  6138. traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
  6139. if( xSchedulerRunning != pdFALSE )
  6140. {
  6141. xCoreID = ( BaseType_t ) portGET_CORE_ID();
  6142. /* If critical nesting count is zero then this function
  6143. * does not match a previous call to vTaskEnterCritical(). */
  6144. configASSERT( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U );
  6145. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) > 0U )
  6146. {
  6147. portDECREMENT_CRITICAL_NESTING_COUNT( xCoreID );
  6148. if( portGET_CRITICAL_NESTING_COUNT( xCoreID ) == 0U )
  6149. {
  6150. portRELEASE_ISR_LOCK( xCoreID );
  6151. portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  6152. }
  6153. else
  6154. {
  6155. mtCOVERAGE_TEST_MARKER();
  6156. }
  6157. }
  6158. else
  6159. {
  6160. mtCOVERAGE_TEST_MARKER();
  6161. }
  6162. }
  6163. else
  6164. {
  6165. mtCOVERAGE_TEST_MARKER();
  6166. }
  6167. traceRETURN_vTaskExitCriticalFromISR();
  6168. }
  6169. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  6170. /*-----------------------------------------------------------*/
  6171. #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
  6172. static char * prvWriteNameToBuffer( char * pcBuffer,
  6173. const char * pcTaskName )
  6174. {
  6175. size_t x;
  6176. /* Start by copying the entire string. */
  6177. ( void ) strcpy( pcBuffer, pcTaskName );
  6178. /* Pad the end of the string with spaces to ensure columns line up when
  6179. * printed out. */
  6180. for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
  6181. {
  6182. pcBuffer[ x ] = ' ';
  6183. }
  6184. /* Terminate. */
  6185. pcBuffer[ x ] = ( char ) 0x00;
  6186. /* Return the new end of string. */
  6187. return &( pcBuffer[ x ] );
  6188. }
  6189. #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
  6190. /*-----------------------------------------------------------*/
  6191. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  6192. void vTaskListTasks( char * pcWriteBuffer,
  6193. size_t uxBufferLength )
  6194. {
  6195. TaskStatus_t * pxTaskStatusArray;
  6196. size_t uxConsumedBufferLength = 0;
  6197. size_t uxCharsWrittenBySnprintf;
  6198. int iSnprintfReturnValue;
  6199. BaseType_t xOutputBufferFull = pdFALSE;
  6200. UBaseType_t uxArraySize, x;
  6201. char cStatus;
  6202. traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
  6203. /*
  6204. * PLEASE NOTE:
  6205. *
  6206. * This function is provided for convenience only, and is used by many
  6207. * of the demo applications. Do not consider it to be part of the
  6208. * scheduler.
  6209. *
  6210. * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
  6211. * uxTaskGetSystemState() output into a human readable table that
  6212. * displays task: names, states, priority, stack usage and task number.
  6213. * Stack usage specified as the number of unused StackType_t words stack can hold
  6214. * on top of stack - not the number of bytes.
  6215. *
  6216. * vTaskListTasks() has a dependency on the snprintf() C library function that
  6217. * might bloat the code size, use a lot of stack, and provide different
  6218. * results on different platforms. An alternative, tiny, third party,
  6219. * and limited functionality implementation of snprintf() is provided in
  6220. * many of the FreeRTOS/Demo sub-directories in a file called
  6221. * printf-stdarg.c (note printf-stdarg.c does not provide a full
  6222. * snprintf() implementation!).
  6223. *
  6224. * It is recommended that production systems call uxTaskGetSystemState()
  6225. * directly to get access to raw stats data, rather than indirectly
  6226. * through a call to vTaskListTasks().
  6227. */
  6228. /* Make sure the write buffer does not contain a string. */
  6229. *pcWriteBuffer = ( char ) 0x00;
  6230. /* Take a snapshot of the number of tasks in case it changes while this
  6231. * function is executing. */
  6232. uxArraySize = uxCurrentNumberOfTasks;
  6233. /* Allocate an array index for each task. NOTE! if
  6234. * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
  6235. * equate to NULL. */
  6236. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  6237. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  6238. /* coverity[misra_c_2012_rule_11_5_violation] */
  6239. pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
  6240. if( pxTaskStatusArray != NULL )
  6241. {
  6242. /* Generate the (binary) data. */
  6243. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
  6244. /* Create a human readable table from the binary data. */
  6245. for( x = 0; x < uxArraySize; x++ )
  6246. {
  6247. switch( pxTaskStatusArray[ x ].eCurrentState )
  6248. {
  6249. case eRunning:
  6250. cStatus = tskRUNNING_CHAR;
  6251. break;
  6252. case eReady:
  6253. cStatus = tskREADY_CHAR;
  6254. break;
  6255. case eBlocked:
  6256. cStatus = tskBLOCKED_CHAR;
  6257. break;
  6258. case eSuspended:
  6259. cStatus = tskSUSPENDED_CHAR;
  6260. break;
  6261. case eDeleted:
  6262. cStatus = tskDELETED_CHAR;
  6263. break;
  6264. case eInvalid: /* Fall through. */
  6265. default: /* Should not get here, but it is included
  6266. * to prevent static checking errors. */
  6267. cStatus = ( char ) 0x00;
  6268. break;
  6269. }
  6270. /* Is there enough space in the buffer to hold task name? */
  6271. if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
  6272. {
  6273. /* Write the task name to the string, padding with spaces so it
  6274. * can be printed in tabular form more easily. */
  6275. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  6276. /* Do not count the terminating null character. */
  6277. uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
  6278. /* Is there space left in the buffer? -1 is done because snprintf
  6279. * writes a terminating null character. So we are essentially
  6280. * checking if the buffer has space to write at least one non-null
  6281. * character. */
  6282. if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
  6283. {
  6284. /* Write the rest of the string. */
  6285. #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
  6286. /* MISRA Ref 21.6.1 [snprintf for utility] */
  6287. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
  6288. /* coverity[misra_c_2012_rule_21_6_violation] */
  6289. iSnprintfReturnValue = snprintf( pcWriteBuffer,
  6290. uxBufferLength - uxConsumedBufferLength,
  6291. "\t%c\t%u\t%u\t%u\t0x%x\r\n",
  6292. cStatus,
  6293. ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
  6294. ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
  6295. ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber,
  6296. ( unsigned int ) pxTaskStatusArray[ x ].uxCoreAffinityMask );
  6297. #else /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
  6298. /* MISRA Ref 21.6.1 [snprintf for utility] */
  6299. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
  6300. /* coverity[misra_c_2012_rule_21_6_violation] */
  6301. iSnprintfReturnValue = snprintf( pcWriteBuffer,
  6302. uxBufferLength - uxConsumedBufferLength,
  6303. "\t%c\t%u\t%u\t%u\r\n",
  6304. cStatus,
  6305. ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
  6306. ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
  6307. ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
  6308. #endif /* ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) ) */
  6309. uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
  6310. uxConsumedBufferLength += uxCharsWrittenBySnprintf;
  6311. pcWriteBuffer += uxCharsWrittenBySnprintf;
  6312. }
  6313. else
  6314. {
  6315. xOutputBufferFull = pdTRUE;
  6316. }
  6317. }
  6318. else
  6319. {
  6320. xOutputBufferFull = pdTRUE;
  6321. }
  6322. if( xOutputBufferFull == pdTRUE )
  6323. {
  6324. break;
  6325. }
  6326. }
  6327. /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
  6328. * is 0 then vPortFree() will be #defined to nothing. */
  6329. vPortFree( pxTaskStatusArray );
  6330. }
  6331. else
  6332. {
  6333. mtCOVERAGE_TEST_MARKER();
  6334. }
  6335. traceRETURN_vTaskListTasks();
  6336. }
  6337. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  6338. /*----------------------------------------------------------*/
  6339. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
  6340. void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
  6341. size_t uxBufferLength )
  6342. {
  6343. TaskStatus_t * pxTaskStatusArray;
  6344. size_t uxConsumedBufferLength = 0;
  6345. size_t uxCharsWrittenBySnprintf;
  6346. int iSnprintfReturnValue;
  6347. BaseType_t xOutputBufferFull = pdFALSE;
  6348. UBaseType_t uxArraySize, x;
  6349. configRUN_TIME_COUNTER_TYPE ulTotalTime = 0;
  6350. configRUN_TIME_COUNTER_TYPE ulStatsAsPercentage;
  6351. traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
  6352. /*
  6353. * PLEASE NOTE:
  6354. *
  6355. * This function is provided for convenience only, and is used by many
  6356. * of the demo applications. Do not consider it to be part of the
  6357. * scheduler.
  6358. *
  6359. * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
  6360. * of the uxTaskGetSystemState() output into a human readable table that
  6361. * displays the amount of time each task has spent in the Running state
  6362. * in both absolute and percentage terms.
  6363. *
  6364. * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
  6365. * function that might bloat the code size, use a lot of stack, and
  6366. * provide different results on different platforms. An alternative,
  6367. * tiny, third party, and limited functionality implementation of
  6368. * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
  6369. * a file called printf-stdarg.c (note printf-stdarg.c does not provide
  6370. * a full snprintf() implementation!).
  6371. *
  6372. * It is recommended that production systems call uxTaskGetSystemState()
  6373. * directly to get access to raw stats data, rather than indirectly
  6374. * through a call to vTaskGetRunTimeStatistics().
  6375. */
  6376. /* Make sure the write buffer does not contain a string. */
  6377. *pcWriteBuffer = ( char ) 0x00;
  6378. /* Take a snapshot of the number of tasks in case it changes while this
  6379. * function is executing. */
  6380. uxArraySize = uxCurrentNumberOfTasks;
  6381. /* Allocate an array index for each task. NOTE! If
  6382. * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
  6383. * equate to NULL. */
  6384. /* MISRA Ref 11.5.1 [Malloc memory assignment] */
  6385. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-115 */
  6386. /* coverity[misra_c_2012_rule_11_5_violation] */
  6387. pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
  6388. if( pxTaskStatusArray != NULL )
  6389. {
  6390. /* Generate the (binary) data. */
  6391. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
  6392. /* For percentage calculations. */
  6393. ulTotalTime /= ( ( configRUN_TIME_COUNTER_TYPE ) 100U );
  6394. /* Avoid divide by zero errors. */
  6395. if( ulTotalTime > 0U )
  6396. {
  6397. /* Create a human readable table from the binary data. */
  6398. for( x = 0; x < uxArraySize; x++ )
  6399. {
  6400. /* What percentage of the total run time has the task used?
  6401. * This will always be rounded down to the nearest integer.
  6402. * ulTotalRunTime has already been divided by 100. */
  6403. ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
  6404. /* Is there enough space in the buffer to hold task name? */
  6405. if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
  6406. {
  6407. /* Write the task name to the string, padding with
  6408. * spaces so it can be printed in tabular form more
  6409. * easily. */
  6410. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  6411. /* Do not count the terminating null character. */
  6412. uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1U );
  6413. /* Is there space left in the buffer? -1 is done because snprintf
  6414. * writes a terminating null character. So we are essentially
  6415. * checking if the buffer has space to write at least one non-null
  6416. * character. */
  6417. if( uxConsumedBufferLength < ( uxBufferLength - 1U ) )
  6418. {
  6419. if( ulStatsAsPercentage > 0U )
  6420. {
  6421. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  6422. {
  6423. /* MISRA Ref 21.6.1 [snprintf for utility] */
  6424. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
  6425. /* coverity[misra_c_2012_rule_21_6_violation] */
  6426. iSnprintfReturnValue = snprintf( pcWriteBuffer,
  6427. uxBufferLength - uxConsumedBufferLength,
  6428. "\t%lu\t\t%lu%%\r\n",
  6429. pxTaskStatusArray[ x ].ulRunTimeCounter,
  6430. ulStatsAsPercentage );
  6431. }
  6432. #else /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
  6433. {
  6434. /* sizeof( int ) == sizeof( long ) so a smaller
  6435. * printf() library can be used. */
  6436. /* MISRA Ref 21.6.1 [snprintf for utility] */
  6437. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
  6438. /* coverity[misra_c_2012_rule_21_6_violation] */
  6439. iSnprintfReturnValue = snprintf( pcWriteBuffer,
  6440. uxBufferLength - uxConsumedBufferLength,
  6441. "\t%u\t\t%u%%\r\n",
  6442. ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
  6443. ( unsigned int ) ulStatsAsPercentage );
  6444. }
  6445. #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
  6446. }
  6447. else
  6448. {
  6449. /* If the percentage is zero here then the task has
  6450. * consumed less than 1% of the total run time. */
  6451. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  6452. {
  6453. /* MISRA Ref 21.6.1 [snprintf for utility] */
  6454. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
  6455. /* coverity[misra_c_2012_rule_21_6_violation] */
  6456. iSnprintfReturnValue = snprintf( pcWriteBuffer,
  6457. uxBufferLength - uxConsumedBufferLength,
  6458. "\t%lu\t\t<1%%\r\n",
  6459. pxTaskStatusArray[ x ].ulRunTimeCounter );
  6460. }
  6461. #else
  6462. {
  6463. /* sizeof( int ) == sizeof( long ) so a smaller
  6464. * printf() library can be used. */
  6465. /* MISRA Ref 21.6.1 [snprintf for utility] */
  6466. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-216 */
  6467. /* coverity[misra_c_2012_rule_21_6_violation] */
  6468. iSnprintfReturnValue = snprintf( pcWriteBuffer,
  6469. uxBufferLength - uxConsumedBufferLength,
  6470. "\t%u\t\t<1%%\r\n",
  6471. ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
  6472. }
  6473. #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
  6474. }
  6475. uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
  6476. uxConsumedBufferLength += uxCharsWrittenBySnprintf;
  6477. pcWriteBuffer += uxCharsWrittenBySnprintf;
  6478. }
  6479. else
  6480. {
  6481. xOutputBufferFull = pdTRUE;
  6482. }
  6483. }
  6484. else
  6485. {
  6486. xOutputBufferFull = pdTRUE;
  6487. }
  6488. if( xOutputBufferFull == pdTRUE )
  6489. {
  6490. break;
  6491. }
  6492. }
  6493. }
  6494. else
  6495. {
  6496. mtCOVERAGE_TEST_MARKER();
  6497. }
  6498. /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
  6499. * is 0 then vPortFree() will be #defined to nothing. */
  6500. vPortFree( pxTaskStatusArray );
  6501. }
  6502. else
  6503. {
  6504. mtCOVERAGE_TEST_MARKER();
  6505. }
  6506. traceRETURN_vTaskGetRunTimeStatistics();
  6507. }
  6508. #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  6509. /*-----------------------------------------------------------*/
  6510. TickType_t uxTaskResetEventItemValue( void )
  6511. {
  6512. TickType_t uxReturn;
  6513. traceENTER_uxTaskResetEventItemValue();
  6514. uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
  6515. /* Reset the event list item to its normal value - so it can be used with
  6516. * queues and semaphores. */
  6517. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) );
  6518. traceRETURN_uxTaskResetEventItemValue( uxReturn );
  6519. return uxReturn;
  6520. }
  6521. /*-----------------------------------------------------------*/
  6522. #if ( configUSE_MUTEXES == 1 )
  6523. TaskHandle_t pvTaskIncrementMutexHeldCount( void )
  6524. {
  6525. TCB_t * pxTCB;
  6526. traceENTER_pvTaskIncrementMutexHeldCount();
  6527. pxTCB = pxCurrentTCB;
  6528. /* If xSemaphoreCreateMutex() is called before any tasks have been created
  6529. * then pxCurrentTCB will be NULL. */
  6530. if( pxTCB != NULL )
  6531. {
  6532. ( pxTCB->uxMutexesHeld )++;
  6533. }
  6534. traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
  6535. return pxTCB;
  6536. }
  6537. #endif /* configUSE_MUTEXES */
  6538. /*-----------------------------------------------------------*/
  6539. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  6540. uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
  6541. BaseType_t xClearCountOnExit,
  6542. TickType_t xTicksToWait )
  6543. {
  6544. uint32_t ulReturn;
  6545. BaseType_t xAlreadyYielded, xShouldBlock = pdFALSE;
  6546. traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
  6547. configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  6548. /* If the notification count is zero, and if we are willing to wait for a
  6549. * notification, then block the task and wait. */
  6550. if( ( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U ) && ( xTicksToWait > ( TickType_t ) 0 ) )
  6551. {
  6552. /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
  6553. * non-deterministic operation. */
  6554. vTaskSuspendAll();
  6555. {
  6556. /* We MUST enter a critical section to atomically check if a notification
  6557. * has occurred and set the flag to indicate that we are waiting for
  6558. * a notification. If we do not do so, a notification sent from an ISR
  6559. * will get lost. */
  6560. taskENTER_CRITICAL();
  6561. {
  6562. /* Only block if the notification count is not already non-zero. */
  6563. if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0U )
  6564. {
  6565. /* Mark this task as waiting for a notification. */
  6566. pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
  6567. /* Arrange to wait for a notification. */
  6568. xShouldBlock = pdTRUE;
  6569. }
  6570. else
  6571. {
  6572. mtCOVERAGE_TEST_MARKER();
  6573. }
  6574. }
  6575. taskEXIT_CRITICAL();
  6576. /* We are now out of the critical section but the scheduler is still
  6577. * suspended, so we are safe to do non-deterministic operations such
  6578. * as prvAddCurrentTaskToDelayedList. */
  6579. if( xShouldBlock == pdTRUE )
  6580. {
  6581. traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
  6582. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  6583. }
  6584. else
  6585. {
  6586. mtCOVERAGE_TEST_MARKER();
  6587. }
  6588. }
  6589. xAlreadyYielded = xTaskResumeAll();
  6590. /* Force a reschedule if xTaskResumeAll has not already done so. */
  6591. if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
  6592. {
  6593. taskYIELD_WITHIN_API();
  6594. }
  6595. else
  6596. {
  6597. mtCOVERAGE_TEST_MARKER();
  6598. }
  6599. }
  6600. taskENTER_CRITICAL();
  6601. {
  6602. traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
  6603. ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
  6604. if( ulReturn != 0U )
  6605. {
  6606. if( xClearCountOnExit != pdFALSE )
  6607. {
  6608. pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ( uint32_t ) 0U;
  6609. }
  6610. else
  6611. {
  6612. pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
  6613. }
  6614. }
  6615. else
  6616. {
  6617. mtCOVERAGE_TEST_MARKER();
  6618. }
  6619. pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
  6620. }
  6621. taskEXIT_CRITICAL();
  6622. traceRETURN_ulTaskGenericNotifyTake( ulReturn );
  6623. return ulReturn;
  6624. }
  6625. #endif /* configUSE_TASK_NOTIFICATIONS */
  6626. /*-----------------------------------------------------------*/
  6627. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  6628. BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
  6629. uint32_t ulBitsToClearOnEntry,
  6630. uint32_t ulBitsToClearOnExit,
  6631. uint32_t * pulNotificationValue,
  6632. TickType_t xTicksToWait )
  6633. {
  6634. BaseType_t xReturn, xAlreadyYielded, xShouldBlock = pdFALSE;
  6635. traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
  6636. configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  6637. /* If the task hasn't received a notification, and if we are willing to wait
  6638. * for it, then block the task and wait. */
  6639. if( ( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED ) && ( xTicksToWait > ( TickType_t ) 0 ) )
  6640. {
  6641. /* We suspend the scheduler here as prvAddCurrentTaskToDelayedList is a
  6642. * non-deterministic operation. */
  6643. vTaskSuspendAll();
  6644. {
  6645. /* We MUST enter a critical section to atomically check and update the
  6646. * task notification value. If we do not do so, a notification from
  6647. * an ISR will get lost. */
  6648. taskENTER_CRITICAL();
  6649. {
  6650. /* Only block if a notification is not already pending. */
  6651. if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
  6652. {
  6653. /* Clear bits in the task's notification value as bits may get
  6654. * set by the notifying task or interrupt. This can be used
  6655. * to clear the value to zero. */
  6656. pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
  6657. /* Mark this task as waiting for a notification. */
  6658. pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
  6659. /* Arrange to wait for a notification. */
  6660. xShouldBlock = pdTRUE;
  6661. }
  6662. else
  6663. {
  6664. mtCOVERAGE_TEST_MARKER();
  6665. }
  6666. }
  6667. taskEXIT_CRITICAL();
  6668. /* We are now out of the critical section but the scheduler is still
  6669. * suspended, so we are safe to do non-deterministic operations such
  6670. * as prvAddCurrentTaskToDelayedList. */
  6671. if( xShouldBlock == pdTRUE )
  6672. {
  6673. traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
  6674. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  6675. }
  6676. else
  6677. {
  6678. mtCOVERAGE_TEST_MARKER();
  6679. }
  6680. }
  6681. xAlreadyYielded = xTaskResumeAll();
  6682. /* Force a reschedule if xTaskResumeAll has not already done so. */
  6683. if( ( xShouldBlock == pdTRUE ) && ( xAlreadyYielded == pdFALSE ) )
  6684. {
  6685. taskYIELD_WITHIN_API();
  6686. }
  6687. else
  6688. {
  6689. mtCOVERAGE_TEST_MARKER();
  6690. }
  6691. }
  6692. taskENTER_CRITICAL();
  6693. {
  6694. traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
  6695. if( pulNotificationValue != NULL )
  6696. {
  6697. /* Output the current notification value, which may or may not
  6698. * have changed. */
  6699. *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
  6700. }
  6701. /* If ucNotifyValue is set then either the task never entered the
  6702. * blocked state (because a notification was already pending) or the
  6703. * task unblocked because of a notification. Otherwise the task
  6704. * unblocked because of a timeout. */
  6705. if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
  6706. {
  6707. /* A notification was not received. */
  6708. xReturn = pdFALSE;
  6709. }
  6710. else
  6711. {
  6712. /* A notification was already pending or a notification was
  6713. * received while the task was waiting. */
  6714. pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
  6715. xReturn = pdTRUE;
  6716. }
  6717. pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
  6718. }
  6719. taskEXIT_CRITICAL();
  6720. traceRETURN_xTaskGenericNotifyWait( xReturn );
  6721. return xReturn;
  6722. }
  6723. #endif /* configUSE_TASK_NOTIFICATIONS */
  6724. /*-----------------------------------------------------------*/
  6725. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  6726. BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
  6727. UBaseType_t uxIndexToNotify,
  6728. uint32_t ulValue,
  6729. eNotifyAction eAction,
  6730. uint32_t * pulPreviousNotificationValue )
  6731. {
  6732. TCB_t * pxTCB;
  6733. BaseType_t xReturn = pdPASS;
  6734. uint8_t ucOriginalNotifyState;
  6735. traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
  6736. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  6737. configASSERT( xTaskToNotify );
  6738. pxTCB = xTaskToNotify;
  6739. taskENTER_CRITICAL();
  6740. {
  6741. if( pulPreviousNotificationValue != NULL )
  6742. {
  6743. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  6744. }
  6745. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  6746. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  6747. switch( eAction )
  6748. {
  6749. case eSetBits:
  6750. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  6751. break;
  6752. case eIncrement:
  6753. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  6754. break;
  6755. case eSetValueWithOverwrite:
  6756. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  6757. break;
  6758. case eSetValueWithoutOverwrite:
  6759. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  6760. {
  6761. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  6762. }
  6763. else
  6764. {
  6765. /* The value could not be written to the task. */
  6766. xReturn = pdFAIL;
  6767. }
  6768. break;
  6769. case eNoAction:
  6770. /* The task is being notified without its notify value being
  6771. * updated. */
  6772. break;
  6773. default:
  6774. /* Should not get here if all enums are handled.
  6775. * Artificially force an assert by testing a value the
  6776. * compiler can't assume is const. */
  6777. configASSERT( xTickCount == ( TickType_t ) 0 );
  6778. break;
  6779. }
  6780. traceTASK_NOTIFY( uxIndexToNotify );
  6781. /* If the task is in the blocked state specifically to wait for a
  6782. * notification then unblock it now. */
  6783. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  6784. {
  6785. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  6786. prvAddTaskToReadyList( pxTCB );
  6787. /* The task should not have been on an event list. */
  6788. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  6789. #if ( configUSE_TICKLESS_IDLE != 0 )
  6790. {
  6791. /* If a task is blocked waiting for a notification then
  6792. * xNextTaskUnblockTime might be set to the blocked task's time
  6793. * out time. If the task is unblocked for a reason other than
  6794. * a timeout xNextTaskUnblockTime is normally left unchanged,
  6795. * because it will automatically get reset to a new value when
  6796. * the tick count equals xNextTaskUnblockTime. However if
  6797. * tickless idling is used it might be more important to enter
  6798. * sleep mode at the earliest possible time - so reset
  6799. * xNextTaskUnblockTime here to ensure it is updated at the
  6800. * earliest possible time. */
  6801. prvResetNextTaskUnblockTime();
  6802. }
  6803. #endif
  6804. /* Check if the notified task has a priority above the currently
  6805. * executing task. */
  6806. taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
  6807. }
  6808. else
  6809. {
  6810. mtCOVERAGE_TEST_MARKER();
  6811. }
  6812. }
  6813. taskEXIT_CRITICAL();
  6814. traceRETURN_xTaskGenericNotify( xReturn );
  6815. return xReturn;
  6816. }
  6817. #endif /* configUSE_TASK_NOTIFICATIONS */
  6818. /*-----------------------------------------------------------*/
  6819. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  6820. BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
  6821. UBaseType_t uxIndexToNotify,
  6822. uint32_t ulValue,
  6823. eNotifyAction eAction,
  6824. uint32_t * pulPreviousNotificationValue,
  6825. BaseType_t * pxHigherPriorityTaskWoken )
  6826. {
  6827. TCB_t * pxTCB;
  6828. uint8_t ucOriginalNotifyState;
  6829. BaseType_t xReturn = pdPASS;
  6830. UBaseType_t uxSavedInterruptStatus;
  6831. traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
  6832. configASSERT( xTaskToNotify );
  6833. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  6834. /* RTOS ports that support interrupt nesting have the concept of a
  6835. * maximum system call (or maximum API call) interrupt priority.
  6836. * Interrupts that are above the maximum system call priority are keep
  6837. * permanently enabled, even when the RTOS kernel is in a critical section,
  6838. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  6839. * is defined in FreeRTOSConfig.h then
  6840. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  6841. * failure if a FreeRTOS API function is called from an interrupt that has
  6842. * been assigned a priority above the configured maximum system call
  6843. * priority. Only FreeRTOS functions that end in FromISR can be called
  6844. * from interrupts that have been assigned a priority at or (logically)
  6845. * below the maximum system call interrupt priority. FreeRTOS maintains a
  6846. * separate interrupt safe API to ensure interrupt entry is as fast and as
  6847. * simple as possible. More information (albeit Cortex-M specific) is
  6848. * provided on the following link:
  6849. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  6850. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  6851. pxTCB = xTaskToNotify;
  6852. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  6853. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  6854. /* coverity[misra_c_2012_directive_4_7_violation] */
  6855. uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
  6856. {
  6857. if( pulPreviousNotificationValue != NULL )
  6858. {
  6859. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  6860. }
  6861. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  6862. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  6863. switch( eAction )
  6864. {
  6865. case eSetBits:
  6866. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  6867. break;
  6868. case eIncrement:
  6869. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  6870. break;
  6871. case eSetValueWithOverwrite:
  6872. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  6873. break;
  6874. case eSetValueWithoutOverwrite:
  6875. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  6876. {
  6877. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  6878. }
  6879. else
  6880. {
  6881. /* The value could not be written to the task. */
  6882. xReturn = pdFAIL;
  6883. }
  6884. break;
  6885. case eNoAction:
  6886. /* The task is being notified without its notify value being
  6887. * updated. */
  6888. break;
  6889. default:
  6890. /* Should not get here if all enums are handled.
  6891. * Artificially force an assert by testing a value the
  6892. * compiler can't assume is const. */
  6893. configASSERT( xTickCount == ( TickType_t ) 0 );
  6894. break;
  6895. }
  6896. traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
  6897. /* If the task is in the blocked state specifically to wait for a
  6898. * notification then unblock it now. */
  6899. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  6900. {
  6901. /* The task should not have been on an event list. */
  6902. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  6903. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  6904. {
  6905. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  6906. prvAddTaskToReadyList( pxTCB );
  6907. #if ( configUSE_TICKLESS_IDLE != 0 )
  6908. {
  6909. /* If a task is blocked waiting for a notification then
  6910. * xNextTaskUnblockTime might be set to the blocked task's time
  6911. * out time. If the task is unblocked for a reason other than
  6912. * a timeout xNextTaskUnblockTime is normally left unchanged,
  6913. * because it will automatically get reset to a new value when
  6914. * the tick count equals xNextTaskUnblockTime. However if
  6915. * tickless idling is used it might be more important to enter
  6916. * sleep mode at the earliest possible time - so reset
  6917. * xNextTaskUnblockTime here to ensure it is updated at the
  6918. * earliest possible time. */
  6919. prvResetNextTaskUnblockTime();
  6920. }
  6921. #endif
  6922. }
  6923. else
  6924. {
  6925. /* The delayed and ready lists cannot be accessed, so hold
  6926. * this task pending until the scheduler is resumed. */
  6927. listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  6928. }
  6929. #if ( configNUMBER_OF_CORES == 1 )
  6930. {
  6931. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  6932. {
  6933. /* The notified task has a priority above the currently
  6934. * executing task so a yield is required. */
  6935. if( pxHigherPriorityTaskWoken != NULL )
  6936. {
  6937. *pxHigherPriorityTaskWoken = pdTRUE;
  6938. }
  6939. /* Mark that a yield is pending in case the user is not
  6940. * using the "xHigherPriorityTaskWoken" parameter to an ISR
  6941. * safe FreeRTOS function. */
  6942. xYieldPendings[ 0 ] = pdTRUE;
  6943. }
  6944. else
  6945. {
  6946. mtCOVERAGE_TEST_MARKER();
  6947. }
  6948. }
  6949. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  6950. {
  6951. #if ( configUSE_PREEMPTION == 1 )
  6952. {
  6953. prvYieldForTask( pxTCB );
  6954. if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
  6955. {
  6956. if( pxHigherPriorityTaskWoken != NULL )
  6957. {
  6958. *pxHigherPriorityTaskWoken = pdTRUE;
  6959. }
  6960. }
  6961. }
  6962. #endif /* if ( configUSE_PREEMPTION == 1 ) */
  6963. }
  6964. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  6965. }
  6966. }
  6967. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  6968. traceRETURN_xTaskGenericNotifyFromISR( xReturn );
  6969. return xReturn;
  6970. }
  6971. #endif /* configUSE_TASK_NOTIFICATIONS */
  6972. /*-----------------------------------------------------------*/
  6973. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  6974. void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
  6975. UBaseType_t uxIndexToNotify,
  6976. BaseType_t * pxHigherPriorityTaskWoken )
  6977. {
  6978. TCB_t * pxTCB;
  6979. uint8_t ucOriginalNotifyState;
  6980. UBaseType_t uxSavedInterruptStatus;
  6981. traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
  6982. configASSERT( xTaskToNotify );
  6983. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  6984. /* RTOS ports that support interrupt nesting have the concept of a
  6985. * maximum system call (or maximum API call) interrupt priority.
  6986. * Interrupts that are above the maximum system call priority are keep
  6987. * permanently enabled, even when the RTOS kernel is in a critical section,
  6988. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  6989. * is defined in FreeRTOSConfig.h then
  6990. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  6991. * failure if a FreeRTOS API function is called from an interrupt that has
  6992. * been assigned a priority above the configured maximum system call
  6993. * priority. Only FreeRTOS functions that end in FromISR can be called
  6994. * from interrupts that have been assigned a priority at or (logically)
  6995. * below the maximum system call interrupt priority. FreeRTOS maintains a
  6996. * separate interrupt safe API to ensure interrupt entry is as fast and as
  6997. * simple as possible. More information (albeit Cortex-M specific) is
  6998. * provided on the following link:
  6999. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  7000. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  7001. pxTCB = xTaskToNotify;
  7002. /* MISRA Ref 4.7.1 [Return value shall be checked] */
  7003. /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#dir-47 */
  7004. /* coverity[misra_c_2012_directive_4_7_violation] */
  7005. uxSavedInterruptStatus = ( UBaseType_t ) taskENTER_CRITICAL_FROM_ISR();
  7006. {
  7007. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  7008. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  7009. /* 'Giving' is equivalent to incrementing a count in a counting
  7010. * semaphore. */
  7011. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  7012. traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
  7013. /* If the task is in the blocked state specifically to wait for a
  7014. * notification then unblock it now. */
  7015. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  7016. {
  7017. /* The task should not have been on an event list. */
  7018. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  7019. if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
  7020. {
  7021. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  7022. prvAddTaskToReadyList( pxTCB );
  7023. #if ( configUSE_TICKLESS_IDLE != 0 )
  7024. {
  7025. /* If a task is blocked waiting for a notification then
  7026. * xNextTaskUnblockTime might be set to the blocked task's time
  7027. * out time. If the task is unblocked for a reason other than
  7028. * a timeout xNextTaskUnblockTime is normally left unchanged,
  7029. * because it will automatically get reset to a new value when
  7030. * the tick count equals xNextTaskUnblockTime. However if
  7031. * tickless idling is used it might be more important to enter
  7032. * sleep mode at the earliest possible time - so reset
  7033. * xNextTaskUnblockTime here to ensure it is updated at the
  7034. * earliest possible time. */
  7035. prvResetNextTaskUnblockTime();
  7036. }
  7037. #endif
  7038. }
  7039. else
  7040. {
  7041. /* The delayed and ready lists cannot be accessed, so hold
  7042. * this task pending until the scheduler is resumed. */
  7043. listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
  7044. }
  7045. #if ( configNUMBER_OF_CORES == 1 )
  7046. {
  7047. if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
  7048. {
  7049. /* The notified task has a priority above the currently
  7050. * executing task so a yield is required. */
  7051. if( pxHigherPriorityTaskWoken != NULL )
  7052. {
  7053. *pxHigherPriorityTaskWoken = pdTRUE;
  7054. }
  7055. /* Mark that a yield is pending in case the user is not
  7056. * using the "xHigherPriorityTaskWoken" parameter in an ISR
  7057. * safe FreeRTOS function. */
  7058. xYieldPendings[ 0 ] = pdTRUE;
  7059. }
  7060. else
  7061. {
  7062. mtCOVERAGE_TEST_MARKER();
  7063. }
  7064. }
  7065. #else /* #if ( configNUMBER_OF_CORES == 1 ) */
  7066. {
  7067. #if ( configUSE_PREEMPTION == 1 )
  7068. {
  7069. prvYieldForTask( pxTCB );
  7070. if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
  7071. {
  7072. if( pxHigherPriorityTaskWoken != NULL )
  7073. {
  7074. *pxHigherPriorityTaskWoken = pdTRUE;
  7075. }
  7076. }
  7077. }
  7078. #endif /* #if ( configUSE_PREEMPTION == 1 ) */
  7079. }
  7080. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  7081. }
  7082. }
  7083. taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
  7084. traceRETURN_vTaskGenericNotifyGiveFromISR();
  7085. }
  7086. #endif /* configUSE_TASK_NOTIFICATIONS */
  7087. /*-----------------------------------------------------------*/
  7088. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  7089. BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
  7090. UBaseType_t uxIndexToClear )
  7091. {
  7092. TCB_t * pxTCB;
  7093. BaseType_t xReturn;
  7094. traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
  7095. configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  7096. /* If null is passed in here then it is the calling task that is having
  7097. * its notification state cleared. */
  7098. pxTCB = prvGetTCBFromHandle( xTask );
  7099. configASSERT( pxTCB != NULL );
  7100. taskENTER_CRITICAL();
  7101. {
  7102. if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
  7103. {
  7104. pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
  7105. xReturn = pdPASS;
  7106. }
  7107. else
  7108. {
  7109. xReturn = pdFAIL;
  7110. }
  7111. }
  7112. taskEXIT_CRITICAL();
  7113. traceRETURN_xTaskGenericNotifyStateClear( xReturn );
  7114. return xReturn;
  7115. }
  7116. #endif /* configUSE_TASK_NOTIFICATIONS */
  7117. /*-----------------------------------------------------------*/
  7118. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  7119. uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
  7120. UBaseType_t uxIndexToClear,
  7121. uint32_t ulBitsToClear )
  7122. {
  7123. TCB_t * pxTCB;
  7124. uint32_t ulReturn;
  7125. traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
  7126. configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  7127. /* If null is passed in here then it is the calling task that is having
  7128. * its notification state cleared. */
  7129. pxTCB = prvGetTCBFromHandle( xTask );
  7130. configASSERT( pxTCB != NULL );
  7131. taskENTER_CRITICAL();
  7132. {
  7133. /* Return the notification as it was before the bits were cleared,
  7134. * then clear the bit mask. */
  7135. ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
  7136. pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
  7137. }
  7138. taskEXIT_CRITICAL();
  7139. traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
  7140. return ulReturn;
  7141. }
  7142. #endif /* configUSE_TASK_NOTIFICATIONS */
  7143. /*-----------------------------------------------------------*/
  7144. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  7145. configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
  7146. {
  7147. TCB_t * pxTCB;
  7148. configRUN_TIME_COUNTER_TYPE ulTotalTime = 0, ulTimeSinceLastSwitchedIn = 0, ulTaskRunTime = 0;
  7149. traceENTER_ulTaskGetRunTimeCounter( xTask );
  7150. pxTCB = prvGetTCBFromHandle( xTask );
  7151. configASSERT( pxTCB != NULL );
  7152. taskENTER_CRITICAL();
  7153. {
  7154. if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
  7155. {
  7156. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  7157. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime );
  7158. #else
  7159. ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
  7160. #endif
  7161. #if ( configNUMBER_OF_CORES == 1 )
  7162. ulTimeSinceLastSwitchedIn = ulTotalTime - ulTaskSwitchedInTime[ 0 ];
  7163. #else
  7164. ulTimeSinceLastSwitchedIn = ulTotalTime - ulTaskSwitchedInTime[ pxTCB->xTaskRunState ];
  7165. #endif
  7166. }
  7167. ulTaskRunTime = pxTCB->ulRunTimeCounter + ulTimeSinceLastSwitchedIn;
  7168. }
  7169. taskEXIT_CRITICAL();
  7170. traceRETURN_ulTaskGetRunTimeCounter( ulTaskRunTime );
  7171. return ulTaskRunTime;
  7172. }
  7173. #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  7174. /*-----------------------------------------------------------*/
  7175. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  7176. configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
  7177. {
  7178. TCB_t * pxTCB;
  7179. configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn, ulTaskRunTime;
  7180. traceENTER_ulTaskGetRunTimePercent( xTask );
  7181. ulTaskRunTime = ulTaskGetRunTimeCounter( xTask );
  7182. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  7183. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime );
  7184. #else
  7185. ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
  7186. #endif
  7187. /* For percentage calculations. */
  7188. ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
  7189. /* Avoid divide by zero errors. */
  7190. if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
  7191. {
  7192. pxTCB = prvGetTCBFromHandle( xTask );
  7193. configASSERT( pxTCB != NULL );
  7194. ulReturn = ulTaskRunTime / ulTotalTime;
  7195. }
  7196. else
  7197. {
  7198. ulReturn = 0;
  7199. }
  7200. traceRETURN_ulTaskGetRunTimePercent( ulReturn );
  7201. return ulReturn;
  7202. }
  7203. #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  7204. /*-----------------------------------------------------------*/
  7205. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
  7206. configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
  7207. {
  7208. configRUN_TIME_COUNTER_TYPE ulTotalTime = 0, ulTimeSinceLastSwitchedIn = 0, ulIdleTaskRunTime = 0;
  7209. BaseType_t i;
  7210. traceENTER_ulTaskGetIdleRunTimeCounter();
  7211. taskENTER_CRITICAL();
  7212. {
  7213. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  7214. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime );
  7215. #else
  7216. ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
  7217. #endif
  7218. for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
  7219. {
  7220. if( taskTASK_IS_RUNNING( xIdleTaskHandles[ i ] ) == pdTRUE )
  7221. {
  7222. #if ( configNUMBER_OF_CORES == 1 )
  7223. ulTimeSinceLastSwitchedIn = ulTotalTime - ulTaskSwitchedInTime[ 0 ];
  7224. #else
  7225. ulTimeSinceLastSwitchedIn = ulTotalTime - ulTaskSwitchedInTime[ xIdleTaskHandles[ i ]->xTaskRunState ];
  7226. #endif
  7227. }
  7228. else
  7229. {
  7230. ulTimeSinceLastSwitchedIn = 0;
  7231. }
  7232. ulIdleTaskRunTime += ( xIdleTaskHandles[ i ]->ulRunTimeCounter + ulTimeSinceLastSwitchedIn );
  7233. }
  7234. }
  7235. taskEXIT_CRITICAL();
  7236. traceRETURN_ulTaskGetIdleRunTimeCounter( ulIdleTaskRunTime );
  7237. return ulIdleTaskRunTime;
  7238. }
  7239. #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
  7240. /*-----------------------------------------------------------*/
  7241. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
  7242. configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
  7243. {
  7244. configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
  7245. configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
  7246. traceENTER_ulTaskGetIdleRunTimePercent();
  7247. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  7248. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalTime );
  7249. #else
  7250. ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
  7251. #endif
  7252. ulTotalTime *= configNUMBER_OF_CORES;
  7253. /* For percentage calculations. */
  7254. ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
  7255. /* Avoid divide by zero errors. */
  7256. if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
  7257. {
  7258. ulRunTimeCounter = ulTaskGetIdleRunTimeCounter();
  7259. ulReturn = ulRunTimeCounter / ulTotalTime;
  7260. }
  7261. else
  7262. {
  7263. ulReturn = 0;
  7264. }
  7265. traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
  7266. return ulReturn;
  7267. }
  7268. #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
  7269. /*-----------------------------------------------------------*/
  7270. static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
  7271. const BaseType_t xCanBlockIndefinitely )
  7272. {
  7273. TickType_t xTimeToWake;
  7274. const TickType_t xConstTickCount = xTickCount;
  7275. List_t * const pxDelayedList = pxDelayedTaskList;
  7276. List_t * const pxOverflowDelayedList = pxOverflowDelayedTaskList;
  7277. #if ( INCLUDE_xTaskAbortDelay == 1 )
  7278. {
  7279. /* About to enter a delayed list, so ensure the ucDelayAborted flag is
  7280. * reset to pdFALSE so it can be detected as having been set to pdTRUE
  7281. * when the task leaves the Blocked state. */
  7282. pxCurrentTCB->ucDelayAborted = ( uint8_t ) pdFALSE;
  7283. }
  7284. #endif
  7285. /* Remove the task from the ready list before adding it to the blocked list
  7286. * as the same list item is used for both lists. */
  7287. if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  7288. {
  7289. /* The current task must be in a ready list, so there is no need to
  7290. * check, and the port reset macro can be called directly. */
  7291. portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority );
  7292. }
  7293. else
  7294. {
  7295. mtCOVERAGE_TEST_MARKER();
  7296. }
  7297. #if ( INCLUDE_vTaskSuspend == 1 )
  7298. {
  7299. if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
  7300. {
  7301. /* Add the task to the suspended task list instead of a delayed task
  7302. * list to ensure it is not woken by a timing event. It will block
  7303. * indefinitely. */
  7304. listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
  7305. }
  7306. else
  7307. {
  7308. /* Calculate the time at which the task should be woken if the event
  7309. * does not occur. This may overflow but this doesn't matter, the
  7310. * kernel will manage it correctly. */
  7311. xTimeToWake = xConstTickCount + xTicksToWait;
  7312. /* The list item will be inserted in wake time order. */
  7313. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
  7314. if( xTimeToWake < xConstTickCount )
  7315. {
  7316. /* Wake time has overflowed. Place this item in the overflow
  7317. * list. */
  7318. traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
  7319. vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
  7320. }
  7321. else
  7322. {
  7323. /* The wake time has not overflowed, so the current block list
  7324. * is used. */
  7325. traceMOVED_TASK_TO_DELAYED_LIST();
  7326. vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
  7327. /* If the task entering the blocked state was placed at the
  7328. * head of the list of blocked tasks then xNextTaskUnblockTime
  7329. * needs to be updated too. */
  7330. if( xTimeToWake < xNextTaskUnblockTime )
  7331. {
  7332. xNextTaskUnblockTime = xTimeToWake;
  7333. }
  7334. else
  7335. {
  7336. mtCOVERAGE_TEST_MARKER();
  7337. }
  7338. }
  7339. }
  7340. }
  7341. #else /* INCLUDE_vTaskSuspend */
  7342. {
  7343. /* Calculate the time at which the task should be woken if the event
  7344. * does not occur. This may overflow but this doesn't matter, the kernel
  7345. * will manage it correctly. */
  7346. xTimeToWake = xConstTickCount + xTicksToWait;
  7347. /* The list item will be inserted in wake time order. */
  7348. listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
  7349. if( xTimeToWake < xConstTickCount )
  7350. {
  7351. traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
  7352. /* Wake time has overflowed. Place this item in the overflow list. */
  7353. vListInsert( pxOverflowDelayedList, &( pxCurrentTCB->xStateListItem ) );
  7354. }
  7355. else
  7356. {
  7357. traceMOVED_TASK_TO_DELAYED_LIST();
  7358. /* The wake time has not overflowed, so the current block list is used. */
  7359. vListInsert( pxDelayedList, &( pxCurrentTCB->xStateListItem ) );
  7360. /* If the task entering the blocked state was placed at the head of the
  7361. * list of blocked tasks then xNextTaskUnblockTime needs to be updated
  7362. * too. */
  7363. if( xTimeToWake < xNextTaskUnblockTime )
  7364. {
  7365. xNextTaskUnblockTime = xTimeToWake;
  7366. }
  7367. else
  7368. {
  7369. mtCOVERAGE_TEST_MARKER();
  7370. }
  7371. }
  7372. /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
  7373. ( void ) xCanBlockIndefinitely;
  7374. }
  7375. #endif /* INCLUDE_vTaskSuspend */
  7376. }
  7377. /*-----------------------------------------------------------*/
  7378. #if ( portUSING_MPU_WRAPPERS == 1 )
  7379. xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
  7380. {
  7381. TCB_t * pxTCB;
  7382. traceENTER_xTaskGetMPUSettings( xTask );
  7383. pxTCB = prvGetTCBFromHandle( xTask );
  7384. configASSERT( pxTCB != NULL );
  7385. traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
  7386. return &( pxTCB->xMPUSettings );
  7387. }
  7388. #endif /* portUSING_MPU_WRAPPERS */
  7389. /*-----------------------------------------------------------*/
  7390. /* Code below here allows additional code to be inserted into this source file,
  7391. * especially where access to file scope functions and data is needed (for example
  7392. * when performing module tests). */
  7393. #ifdef FREERTOS_MODULE_TEST
  7394. #include "tasks_test_access_functions.h"
  7395. #endif
  7396. #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
  7397. #include "freertos_tasks_c_additions.h"
  7398. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  7399. static void freertos_tasks_c_additions_init( void )
  7400. {
  7401. FREERTOS_TASKS_C_ADDITIONS_INIT();
  7402. }
  7403. #endif
  7404. #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
  7405. /*-----------------------------------------------------------*/
  7406. #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
  7407. /*
  7408. * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
  7409. * to provide the memory that is used by the Idle task. It is used when
  7410. * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
  7411. * it's own implementation of vApplicationGetIdleTaskMemory by setting
  7412. * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
  7413. */
  7414. void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
  7415. StackType_t ** ppxIdleTaskStackBuffer,
  7416. configSTACK_DEPTH_TYPE * puxIdleTaskStackSize )
  7417. {
  7418. static StaticTask_t xIdleTaskTCB;
  7419. static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
  7420. *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
  7421. *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
  7422. *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  7423. }
  7424. #if ( configNUMBER_OF_CORES > 1 )
  7425. void vApplicationGetPassiveIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
  7426. StackType_t ** ppxIdleTaskStackBuffer,
  7427. configSTACK_DEPTH_TYPE * puxIdleTaskStackSize,
  7428. BaseType_t xPassiveIdleTaskIndex )
  7429. {
  7430. static StaticTask_t xIdleTaskTCBs[ configNUMBER_OF_CORES - 1 ];
  7431. static StackType_t uxIdleTaskStacks[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
  7432. *ppxIdleTaskTCBBuffer = &( xIdleTaskTCBs[ xPassiveIdleTaskIndex ] );
  7433. *ppxIdleTaskStackBuffer = &( uxIdleTaskStacks[ xPassiveIdleTaskIndex ][ 0 ] );
  7434. *puxIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  7435. }
  7436. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  7437. #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
  7438. /*-----------------------------------------------------------*/
  7439. #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) && ( configUSE_TIMERS == 1 ) )
  7440. /*
  7441. * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
  7442. * to provide the memory that is used by the Timer service task. It is used when
  7443. * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
  7444. * it's own implementation of vApplicationGetTimerTaskMemory by setting
  7445. * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
  7446. */
  7447. void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
  7448. StackType_t ** ppxTimerTaskStackBuffer,
  7449. configSTACK_DEPTH_TYPE * puxTimerTaskStackSize )
  7450. {
  7451. static StaticTask_t xTimerTaskTCB;
  7452. static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
  7453. *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
  7454. *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
  7455. *puxTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
  7456. }
  7457. #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) && ( configUSE_TIMERS == 1 ) ) */
  7458. /*-----------------------------------------------------------*/
  7459. /*
  7460. * Reset the state in this file. This state is normally initialized at start up.
  7461. * This function must be called by the application before restarting the
  7462. * scheduler.
  7463. */
  7464. void vTaskResetState( void )
  7465. {
  7466. BaseType_t xCoreID;
  7467. /* Task control block. */
  7468. #if ( configNUMBER_OF_CORES == 1 )
  7469. {
  7470. pxCurrentTCB = NULL;
  7471. }
  7472. #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
  7473. #if ( INCLUDE_vTaskDelete == 1 )
  7474. {
  7475. uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
  7476. }
  7477. #endif /* #if ( INCLUDE_vTaskDelete == 1 ) */
  7478. #if ( configUSE_POSIX_ERRNO == 1 )
  7479. {
  7480. FreeRTOS_errno = 0;
  7481. }
  7482. #endif /* #if ( configUSE_POSIX_ERRNO == 1 ) */
  7483. /* Other file private variables. */
  7484. uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
  7485. xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
  7486. uxTopReadyPriority = tskIDLE_PRIORITY;
  7487. xSchedulerRunning = pdFALSE;
  7488. xPendedTicks = ( TickType_t ) 0U;
  7489. for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
  7490. {
  7491. xYieldPendings[ xCoreID ] = pdFALSE;
  7492. }
  7493. xNumOfOverflows = ( BaseType_t ) 0;
  7494. uxTaskNumber = ( UBaseType_t ) 0U;
  7495. xNextTaskUnblockTime = ( TickType_t ) 0U;
  7496. uxSchedulerSuspended = ( UBaseType_t ) 0U;
  7497. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  7498. {
  7499. for( xCoreID = 0; xCoreID < configNUMBER_OF_CORES; xCoreID++ )
  7500. {
  7501. ulTaskSwitchedInTime[ xCoreID ] = 0U;
  7502. ulTotalRunTime[ xCoreID ] = 0U;
  7503. }
  7504. }
  7505. #endif /* #if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  7506. }
  7507. /*-----------------------------------------------------------*/