tasks.c 262 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374
  1. /*
  2. * FreeRTOS Kernel V10.5.1 (ESP-IDF SMP modified)
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-FileCopyrightText: 2021 Amazon.com, Inc. or its affiliates
  6. *
  7. * SPDX-License-Identifier: MIT
  8. *
  9. * SPDX-FileContributor: 2023 Espressif Systems (Shanghai) CO LTD
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  12. * this software and associated documentation files (the "Software"), to deal in
  13. * the Software without restriction, including without limitation the rights to
  14. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  15. * the Software, and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in all
  19. * copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  23. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  24. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  25. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  26. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * https://www.FreeRTOS.org
  29. * https://github.com/FreeRTOS
  30. *
  31. */
  32. /* Standard includes. */
  33. #include <stdlib.h>
  34. #include <string.h>
  35. /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
  36. * all the API functions to use the MPU wrappers. That should only be done when
  37. * task.h is included from an application file. */
  38. #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
  39. /* FreeRTOS includes. */
  40. #include "FreeRTOS.h"
  41. #include "task.h"
  42. #include "timers.h"
  43. #include "stack_macros.h"
  44. /* Include private IDF API additions for critical thread safety macros */
  45. #include "esp_private/freertos_idf_additions_priv.h"
  46. #include "freertos/idf_additions.h"
  47. /* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
  48. * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
  49. * for the header files above, but not in this file, in order to generate the
  50. * correct privileged Vs unprivileged linkage and placement. */
  51. #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
  52. /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
  53. * functions but without including stdio.h here. */
  54. #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
  55. /* At the bottom of this file are two optional functions that can be used
  56. * to generate human readable text from the raw data generated by the
  57. * uxTaskGetSystemState() function. Note the formatting functions are provided
  58. * for convenience only, and are NOT considered part of the kernel. */
  59. #include <stdio.h>
  60. #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
  61. #if ( configUSE_PREEMPTION == 0 )
  62. /* If the cooperative scheduler is being used then a yield should not be
  63. * performed just because a higher priority task has been woken. */
  64. #define taskYIELD_IF_USING_PREEMPTION()
  65. #else
  66. #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
  67. #endif
  68. #if ( configNUMBER_OF_CORES > 1 )
  69. #define taskYIELD_CORE( xCoreID ) portYIELD_CORE( xCoreID )
  70. #endif /* configNUMBER_OF_CORES > 1 */
  71. /* Values that can be assigned to the ucNotifyState member of the TCB. */
  72. #define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
  73. #define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
  74. #define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
  75. /*
  76. * The value used to fill the stack of a task when the task is created. This
  77. * is used purely for checking the high water mark for tasks.
  78. */
  79. #define tskSTACK_FILL_BYTE ( 0xa5U )
  80. /* Bits used to record how a task's stack and TCB were allocated. */
  81. #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
  82. #define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
  83. #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
  84. /* If any of the following are set then task stacks are filled with a known
  85. * value so the high water mark can be determined. If none of the following are
  86. * set then don't fill the stack so there is no unnecessary dependency on memset. */
  87. #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  88. #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
  89. #else
  90. #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
  91. #endif
  92. /*
  93. * Macros used by vListTask to indicate which state a task is in.
  94. */
  95. #define tskRUNNING_CHAR ( 'X' )
  96. #define tskBLOCKED_CHAR ( 'B' )
  97. #define tskREADY_CHAR ( 'R' )
  98. #define tskDELETED_CHAR ( 'D' )
  99. #define tskSUSPENDED_CHAR ( 'S' )
  100. /*
  101. * Some kernel aware debuggers require the data the debugger needs access to to
  102. * be global, rather than file scope.
  103. */
  104. #ifdef portREMOVE_STATIC_QUALIFIER
  105. #define static
  106. #endif
  107. /* The name allocated to the Idle task. This can be overridden by defining
  108. * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
  109. #ifndef configIDLE_TASK_NAME
  110. #define configIDLE_TASK_NAME "IDLE"
  111. #endif
  112. /*-----------------------------------------------------------*/
  113. /* Macros to check if an unblocked task causes a yield on the current core.
  114. * - pxTCB is the TCB of the task to check
  115. * - xCurCoreID is the current core's ID
  116. * - xYieldEqualPriority indicates whether a yield should occur if the unblocked
  117. * task's priority is equal to the priority of the task currently running on the
  118. * current core.
  119. * - uxTaskPriority is the task's priority
  120. * - xTaskCoreID is the task's core affinity */
  121. #if ( configNUMBER_OF_CORES > 1 )
  122. #define taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, xYieldEqualPriority ) prvIsYieldUsingPrioritySMP( ( pxTCB )->uxPriority, ( pxTCB )->xCoreID, xCurCoreID, xYieldEqualPriority )
  123. #define taskIS_YIELD_REQUIRED_USING_PRIORITY( uxTaskPriority, xTaskCoreID, xCurCoreID, xYieldEqualPriority ) prvIsYieldUsingPrioritySMP( uxTaskPriority, xTaskCoreID, xCurCoreID, xYieldEqualPriority )
  124. #else
  125. #define taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, xYieldEqualPriority ) \
  126. ( { \
  127. /* xCurCoreID is unused */ \
  128. ( void ) xCurCoreID; \
  129. ( ( ( pxTCB )->uxPriority + ( ( xYieldEqualPriority == pdTRUE ) ? 1 : 0 ) ) > pxCurrentTCBs[ 0 ]->uxPriority ) ? pdTRUE : pdFALSE; \
  130. } )
  131. #define taskIS_YIELD_REQUIRED_USING_PRIORITY( uxTaskPriority, xTaskCoreID, xCurCoreID, xYieldEqualPriority ) \
  132. ( { \
  133. /* xTaskCoreID and xCurCoreID are unused */ \
  134. ( void ) xTaskCoreID; \
  135. ( void ) xCurCoreID; \
  136. ( ( uxTaskPriority + ( ( xYieldEqualPriority == pdTRUE ) ? 1 : 0 ) ) >= pxCurrentTCBs[ 0 ]->uxPriority ) ? pdTRUE : pdFALSE; \
  137. } )
  138. #endif /* configNUMBER_OF_CORES > 1 */
  139. /*-----------------------------------------------------------*/
  140. /* Macros to check if a task has a compatible affinity with a particular core.
  141. * - xCore is the target core
  142. * - xCoreID is the affinity of the task to check
  143. *
  144. * This macro will always return true on single core as the concept of core
  145. * affinity doesn't exist. */
  146. #if ( configNUMBER_OF_CORES > 1 )
  147. #define taskIS_AFFINITY_COMPATIBLE( xCore, xCoreID ) ( ( ( ( xCoreID ) == xCore ) || ( ( xCoreID ) == tskNO_AFFINITY ) ) ? pdTRUE : pdFALSE )
  148. #else
  149. #define taskIS_AFFINITY_COMPATIBLE( xCore, xCoreID ) \
  150. ( { \
  151. /* xCoreID is unused */ \
  152. ( void ) xCoreID; \
  153. pdTRUE; \
  154. } )
  155. #endif /* configNUMBER_OF_CORES > 1 */
  156. /*-----------------------------------------------------------*/
  157. /* Macros to check if a particular task is a currently running. */
  158. #if ( configNUMBER_OF_CORES > 1 )
  159. #define taskIS_CURRENTLY_RUNNING( pxTCB ) ( ( ( ( pxTCB ) == pxCurrentTCBs[ 0 ] ) || ( ( pxTCB ) == pxCurrentTCBs[ 1 ] ) ) ? pdTRUE : pdFALSE )
  160. #define taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, xCoreID ) ( ( ( pxTCB ) == pxCurrentTCBs[ ( xCoreID ) ] ) ? pdTRUE : pdFALSE )
  161. #else
  162. #define taskIS_CURRENTLY_RUNNING( pxTCB ) ( ( ( pxTCB ) == pxCurrentTCBs[ 0 ] ) ? pdTRUE : pdFALSE )
  163. #define taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, xCoreID ) \
  164. ( { \
  165. /* xCoreID is unused */ \
  166. ( void ) xCoreID; \
  167. taskIS_CURRENTLY_RUNNING( pxTCB ); \
  168. } )
  169. #endif /* configNUMBER_OF_CORES > 1 */
  170. /*-----------------------------------------------------------*/
  171. /* Macro to check if a particular task can currently be scheduled (i.e., is
  172. * the scheduler suspended). */
  173. #if ( configNUMBER_OF_CORES > 1 )
  174. #define taskCAN_BE_SCHEDULED( pxTCB ) prvCheckTaskCanBeScheduledSMP( pxTCB )
  175. #else
  176. #define taskCAN_BE_SCHEDULED( pxTCB ) \
  177. ( { \
  178. /* pxTCB is unused */ \
  179. ( void ) pxTCB; \
  180. ( ( uxSchedulerSuspended[ 0 ] == ( UBaseType_t ) 0U ) ) ? pdTRUE : pdFALSE; \
  181. } )
  182. #endif /* configNUMBER_OF_CORES > 1 */
  183. /*-----------------------------------------------------------*/
  184. /* Macro to check if the scheduler is suspended (on the current core)
  185. *
  186. * There are various blocking tasks.c APIs that call configASSERT() to check if
  187. * the API is being called while the scheduler is suspended. However, these
  188. * asserts are done outside a critical section or interrupt disabled block.
  189. * Directly checking uxSchedulerSuspended[ portGET_CORE_ID() ] outside a
  190. * critical section can lead to false positives in SMP. Thus for SMP, we call
  191. * xTaskGetSchedulerState() instead.
  192. *
  193. * Take the following example of an unpinned Task A in SMP calling
  194. * uxSchedulerSuspended[ portGET_CORE_ID() ]:
  195. * - Task A calls portGET_CORE_ID() which is 0
  196. * - Task A gets preempted by Task B, Task A switches to core 1
  197. * - Task B on core 0 calls vTaskSuspendAll()
  198. * - Task A checks uxSchedulerSuspended[ 0 ] leading to a false positive
  199. */
  200. #if ( configNUMBER_OF_CORES > 1 )
  201. #define taskIS_SCHEDULER_SUSPENDED() ( ( xTaskGetSchedulerState() == taskSCHEDULER_SUSPENDED ) ? pdTRUE : pdFALSE )
  202. #else
  203. #define taskIS_SCHEDULER_SUSPENDED() ( ( ( uxSchedulerSuspended[ 0 ] != ( UBaseType_t ) 0U ) ) ? pdTRUE : pdFALSE )
  204. #endif /* configNUMBER_OF_CORES > 1 */
  205. /*-----------------------------------------------------------*/
  206. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  207. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
  208. * performed in a generic way that is not optimised to any particular
  209. * microcontroller architecture. */
  210. /* uxTopReadyPriority holds the priority of the highest priority ready
  211. * state task. */
  212. #define taskRECORD_READY_PRIORITY( uxPriority ) \
  213. { \
  214. if( ( uxPriority ) > uxTopReadyPriority ) \
  215. { \
  216. uxTopReadyPriority = ( uxPriority ); \
  217. } \
  218. } /* taskRECORD_READY_PRIORITY */
  219. /*-----------------------------------------------------------*/
  220. #if ( configNUMBER_OF_CORES > 1 )
  221. #define taskSELECT_HIGHEST_PRIORITY_TASK() prvSelectHighestPriorityTaskSMP()
  222. #else /* if ( configNUMBER_OF_CORES > 1 ) */
  223. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  224. { \
  225. UBaseType_t uxTopPriority = uxTopReadyPriority; \
  226. \
  227. /* Find the highest priority queue that contains ready tasks. */ \
  228. while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
  229. { \
  230. configASSERT( uxTopPriority ); \
  231. --uxTopPriority; \
  232. } \
  233. \
  234. /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
  235. * the same priority get an equal share of the processor time. */ \
  236. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCBs[ 0 ], &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  237. uxTopReadyPriority = uxTopPriority; \
  238. } /* taskSELECT_HIGHEST_PRIORITY_TASK */
  239. #endif /* if ( configNUMBER_OF_CORES > 1 ) */
  240. /*-----------------------------------------------------------*/
  241. /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
  242. * they are only required when a port optimised method of task selection is
  243. * being used. */
  244. #define taskRESET_READY_PRIORITY( uxPriority )
  245. #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
  246. #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  247. /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
  248. * performed in a way that is tailored to the particular microcontroller
  249. * architecture being used. */
  250. /* A port optimised version is provided. Call the port defined macros. */
  251. #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
  252. /*-----------------------------------------------------------*/
  253. #define taskSELECT_HIGHEST_PRIORITY_TASK() \
  254. { \
  255. UBaseType_t uxTopPriority; \
  256. \
  257. /* Find the highest priority list that contains ready tasks. */ \
  258. portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
  259. configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
  260. listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCBs[ 0 ], &( pxReadyTasksLists[ uxTopPriority ] ) ); \
  261. } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
  262. /*-----------------------------------------------------------*/
  263. /* A port optimised version is provided, call it only if the TCB being reset
  264. * is being referenced from a ready list. If it is referenced from a delayed
  265. * or suspended list then it won't be in a ready list. */
  266. #define taskRESET_READY_PRIORITY( uxPriority ) \
  267. { \
  268. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
  269. { \
  270. portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
  271. } \
  272. }
  273. #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
  274. /*-----------------------------------------------------------*/
  275. /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
  276. * count overflows. */
  277. #define taskSWITCH_DELAYED_LISTS() \
  278. { \
  279. List_t * pxTemp; \
  280. \
  281. /* The delayed tasks list should be empty when the lists are switched. */ \
  282. configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
  283. \
  284. pxTemp = pxDelayedTaskList; \
  285. pxDelayedTaskList = pxOverflowDelayedTaskList; \
  286. pxOverflowDelayedTaskList = pxTemp; \
  287. xNumOfOverflows++; \
  288. prvResetNextTaskUnblockTime(); \
  289. }
  290. /*-----------------------------------------------------------*/
  291. /*
  292. * Place the task represented by pxTCB into the appropriate ready list for
  293. * the task. It is inserted at the end of the list.
  294. */
  295. #define prvAddTaskToReadyList( pxTCB ) \
  296. traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
  297. taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
  298. listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
  299. tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
  300. /*-----------------------------------------------------------*/
  301. /*
  302. * Several functions take a TaskHandle_t parameter that can optionally be NULL,
  303. * where NULL is used to indicate that the handle of the currently executing
  304. * task should be used in place of the parameter. This macro simply checks to
  305. * see if the parameter is NULL and returns a pointer to the appropriate TCB.
  306. *
  307. * In SMP, calling xTaskGetCurrentTaskHandle() ensures atomic access to pxCurrentTCBs
  308. */
  309. #if ( configNUMBER_OF_CORES > 1 )
  310. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? xTaskGetCurrentTaskHandle() : ( pxHandle ) )
  311. #else
  312. #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCBs[ 0 ] : ( pxHandle ) )
  313. #endif
  314. /* The item value of the event list item is normally used to hold the priority
  315. * of the task to which it belongs (coded to allow it to be held in reverse
  316. * priority order). However, it is occasionally borrowed for other purposes. It
  317. * is important its value is not updated due to a task priority change while it is
  318. * being used for another purpose. The following bit definition is used to inform
  319. * the scheduler that the value should not be changed - in which case it is the
  320. * responsibility of whichever module is using the value to ensure it gets set back
  321. * to its original value when it is released. */
  322. #if ( configUSE_16_BIT_TICKS == 1 )
  323. #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
  324. #else
  325. #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
  326. #endif
  327. /*
  328. * Task control block. A task control block (TCB) is allocated for each task,
  329. * and stores task state information, including a pointer to the task's context
  330. * (the task's run time environment, including register values)
  331. */
  332. typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
  333. {
  334. 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. */
  335. #if ( portUSING_MPU_WRAPPERS == 1 )
  336. xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
  337. #endif
  338. 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 ). */
  339. ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
  340. UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
  341. StackType_t * pxStack; /*< Points to the start of the stack. */
  342. char pcTaskName[ configMAX_TASK_NAME_LEN ]; /*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  343. /* Todo: Remove xCoreID for single core builds (IDF-7894) */
  344. BaseType_t xCoreID; /*< The core that this task is pinned to */
  345. #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
  346. StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
  347. #endif
  348. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  349. UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
  350. #endif
  351. #if ( configUSE_TRACE_FACILITY == 1 )
  352. 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. */
  353. UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
  354. #endif
  355. #if ( configUSE_MUTEXES == 1 )
  356. UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
  357. UBaseType_t uxMutexesHeld;
  358. #endif
  359. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  360. TaskHookFunction_t pxTaskTag;
  361. #endif
  362. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
  363. void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
  364. #endif
  365. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  366. configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
  367. #endif
  368. #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
  369. configTLS_BLOCK_TYPE xTLSBlock; /*< Memory block used as Thread Local Storage (TLS) Block for the task. */
  370. #endif
  371. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  372. volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
  373. volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
  374. #endif
  375. /* See the comments in FreeRTOS.h with the definition of
  376. * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
  377. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
  378. uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
  379. #endif
  380. #if ( INCLUDE_xTaskAbortDelay == 1 )
  381. uint8_t ucDelayAborted;
  382. #endif
  383. #if ( configUSE_POSIX_ERRNO == 1 )
  384. int iTaskErrno;
  385. #endif
  386. } tskTCB;
  387. /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
  388. * below to enable the use of older kernel aware debuggers. */
  389. typedef tskTCB TCB_t;
  390. /*lint -save -e956 A manual analysis and inspection has been used to determine
  391. * which static variables must be declared volatile. */
  392. portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ] = { NULL };
  393. /* Lists for ready and blocked tasks. --------------------
  394. * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
  395. * doing so breaks some kernel aware debuggers and debuggers that rely on removing
  396. * the static qualifier. */
  397. PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
  398. PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
  399. PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
  400. PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
  401. 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. */
  402. PRIVILEGED_DATA static List_t xPendingReadyList[ configNUMBER_OF_CORES ]; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
  403. #if ( INCLUDE_vTaskDelete == 1 )
  404. PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
  405. PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
  406. #endif
  407. #if ( INCLUDE_vTaskSuspend == 1 )
  408. PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
  409. #endif
  410. /* Global POSIX errno. Its value is changed upon context switching to match
  411. * the errno of the currently running task. */
  412. #if ( configUSE_POSIX_ERRNO == 1 )
  413. int FreeRTOS_errno = 0;
  414. #endif
  415. /* Other file private variables. --------------------------------*/
  416. PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
  417. PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
  418. PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
  419. PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
  420. PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
  421. PRIVILEGED_DATA static volatile BaseType_t xYieldPending[ configNUMBER_OF_CORES ] = { pdFALSE };
  422. PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
  423. PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
  424. PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
  425. PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[ configNUMBER_OF_CORES ] = { NULL }; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
  426. /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
  427. * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
  428. * to determine the number of priority lists to read back from the remote target. */
  429. const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
  430. /* Context switches are held pending while the scheduler is suspended. Also,
  431. * interrupts must not manipulate the xStateListItem of a TCB, or any of the
  432. * lists the xStateListItem can be referenced from, if the scheduler is suspended.
  433. * If an interrupt needs to unblock a task while the scheduler is suspended then it
  434. * moves the task's event list item into the xPendingReadyList, ready for the
  435. * kernel to move the task from the pending ready list into the real ready list
  436. * when the scheduler is unsuspended. The pending ready list itself can only be
  437. * accessed from a critical section. */
  438. PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended[ configNUMBER_OF_CORES ] = { ( UBaseType_t ) pdFALSE };
  439. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  440. /* Do not move these variables to function scope as doing so prevents the
  441. * code working with debuggers that need to remove the static qualifier. */
  442. PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0UL }; /*< Holds the value of a timer/counter the last time a task was switched in. */
  443. PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
  444. #endif
  445. /* Spinlock required for SMP critical sections. This lock protects all of the
  446. * kernel's data structures such as various tasks lists, flags, and tick counts. */
  447. PRIVILEGED_DATA static portMUX_TYPE xKernelLock = portMUX_INITIALIZER_UNLOCKED;
  448. /*lint -restore */
  449. /*-----------------------------------------------------------*/
  450. /* File private functions. --------------------------------*/
  451. /*
  452. * Creates the idle tasks during scheduler start.
  453. */
  454. static BaseType_t prvCreateIdleTasks( void );
  455. /**
  456. * Utility function to check whether a yield (on either core) is required after
  457. * unblocking (or changing the priority of) a particular task.
  458. *
  459. * - This function is the SMP replacement for checking if an unblocked task has
  460. * a higher (or equal) priority than the current task.
  461. * - It should be called before calling taskYIELD_IF_USING_PREEMPTION() or
  462. * before setting xYieldRequired
  463. * - If it is the other core that requires a yield, this function will
  464. * internally trigger the other core to yield
  465. *
  466. * Note: In some special instances, a yield is triggered if the unblocked task
  467. * has an equal priority (such as in xTaskResumeAll). Thus the
  468. * xYieldEqualPriority parameter specifies whether to yield if the current
  469. * task has equal priority.
  470. *
  471. * Scheduling Algorithm:
  472. * This function will bias towards yielding the current core.
  473. * - If the unblocked task has a higher (or equal) priority than the current
  474. * core, the current core is yielded regardless of the current priority of the
  475. * other core.
  476. * - A core (current or other) will only yield if their schedulers are not
  477. * suspended.
  478. *
  479. * Todo: This can be optimized (IDF-5772)
  480. *
  481. * Entry:
  482. * - This function must be called in a critical section
  483. * - A task must just have been unblocked, or its priority raised
  484. * Exit:
  485. * - Returns pdTRUE if the current core requires yielding
  486. * - The other core will be triggered to yield if required
  487. */
  488. #if ( configNUMBER_OF_CORES > 1 )
  489. static BaseType_t prvIsYieldUsingPrioritySMP( UBaseType_t uxTaskPriority,
  490. BaseType_t xTaskCoreID,
  491. BaseType_t xCurCoreID,
  492. BaseType_t xYieldEqualPriority ) PRIVILEGED_FUNCTION;
  493. #endif /* configNUMBER_OF_CORES > 1 */
  494. /**
  495. * Utility function to check whether a task can currently be scheduled on one
  496. * or more cores. This function is the SMP replacement for checking if
  497. * `uxSchedulerSuspended == 0`.
  498. *
  499. * - If a task is pinned, check the scheduler suspension state on the task's
  500. * pinned core. The task can be scheduled if the scheduler is not suspended on
  501. * the pinned core.
  502. * - If a task is unpinned, check the scheduler suspension state on both cores.
  503. * The task can be scheduled if the scheduler is not suspended on either of
  504. * the cores.
  505. */
  506. #if ( configNUMBER_OF_CORES > 1 )
  507. static BaseType_t prvCheckTaskCanBeScheduledSMP( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
  508. #endif /* configNUMBER_OF_CORES > 1 */
  509. /**
  510. * Utility function to select the highest priority and runnable task for the
  511. * current core.
  512. */
  513. #if ( configNUMBER_OF_CORES > 1 )
  514. static void prvSelectHighestPriorityTaskSMP( void ) PRIVILEGED_FUNCTION;
  515. #endif /* configNUMBER_OF_CORES > 1 */
  516. /**
  517. * Utility task that simply returns pdTRUE if the task referenced by xTask is
  518. * currently in the Suspended state, or pdFALSE if the task referenced by xTask
  519. * is in any other state.
  520. */
  521. #if ( INCLUDE_vTaskSuspend == 1 )
  522. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
  523. #endif /* INCLUDE_vTaskSuspend */
  524. /*
  525. * Utility to ready all the lists used by the scheduler. This is called
  526. * automatically upon the creation of the first task.
  527. */
  528. static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
  529. /*
  530. * The idle task, which as all tasks is implemented as a never ending loop.
  531. * The idle task is automatically created and added to the ready lists upon
  532. * creation of the first user task.
  533. *
  534. * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
  535. * language extensions. The equivalent prototype for this function is:
  536. *
  537. * void prvIdleTask( void *pvParameters );
  538. *
  539. */
  540. static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
  541. /*
  542. * Utility to free all memory allocated by the scheduler to hold a TCB,
  543. * including the stack pointed to by the TCB.
  544. *
  545. * This does not free memory allocated by the task itself (i.e. memory
  546. * allocated by calls to pvPortMalloc from within the tasks application code).
  547. */
  548. #if ( INCLUDE_vTaskDelete == 1 )
  549. static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
  550. #endif
  551. /*
  552. * Used only by the idle task. This checks to see if anything has been placed
  553. * in the list of tasks waiting to be deleted. If so the task is cleaned up
  554. * and its TCB deleted.
  555. */
  556. static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
  557. /*
  558. * The currently executing task is entering the Blocked state. Add the task to
  559. * either the current or the overflow delayed task list.
  560. */
  561. static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
  562. const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
  563. /*
  564. * Fills an TaskStatus_t structure with information on each task that is
  565. * referenced from the pxList list (which may be a ready list, a delayed list,
  566. * a suspended list, etc.).
  567. *
  568. * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
  569. * NORMAL APPLICATION CODE.
  570. */
  571. #if ( configUSE_TRACE_FACILITY == 1 )
  572. static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
  573. List_t * pxList,
  574. eTaskState eState ) PRIVILEGED_FUNCTION;
  575. #endif
  576. /*
  577. * Searches pxList for a task with name pcNameToQuery - returning a handle to
  578. * the task if it is found, or NULL if the task is not found.
  579. */
  580. #if ( INCLUDE_xTaskGetHandle == 1 )
  581. static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
  582. const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
  583. #endif
  584. /*
  585. * When a task is created, the stack of the task is filled with a known value.
  586. * This function determines the 'high water mark' of the task stack by
  587. * determining how much of the stack remains at the original preset value.
  588. */
  589. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  590. static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
  591. #endif
  592. /*
  593. * Return the amount of time, in ticks, that will pass before the kernel will
  594. * next move a task from the Blocked state to the Running state.
  595. *
  596. * This conditional compilation should use inequality to 0, not equality to 1.
  597. * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
  598. * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
  599. * set to a value other than 1.
  600. */
  601. #if ( configUSE_TICKLESS_IDLE != 0 )
  602. static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
  603. #endif
  604. /*
  605. * Set xNextTaskUnblockTime to the time at which the next Blocked state task
  606. * will exit the Blocked state.
  607. */
  608. static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
  609. #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
  610. /*
  611. * Helper function used to pad task names with spaces when printing out
  612. * human readable tables of task information.
  613. */
  614. static char * prvWriteNameToBuffer( char * pcBuffer,
  615. const char * pcTaskName ) PRIVILEGED_FUNCTION;
  616. #endif
  617. /*
  618. * Called after a Task_t structure has been allocated either statically or
  619. * dynamically to fill in the structure's members.
  620. */
  621. static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
  622. const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  623. const uint32_t ulStackDepth,
  624. void * const pvParameters,
  625. UBaseType_t uxPriority,
  626. TaskHandle_t * const pxCreatedTask,
  627. TCB_t * pxNewTCB,
  628. const MemoryRegion_t * const xRegions,
  629. BaseType_t xCoreID ) PRIVILEGED_FUNCTION;
  630. /*
  631. * Called after a new task has been created and initialised to place the task
  632. * under the control of the scheduler.
  633. */
  634. static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
  635. /*
  636. * freertos_tasks_c_additions_init() should only be called if the user definable
  637. * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
  638. * called by the function.
  639. */
  640. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  641. static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
  642. #endif
  643. /*-----------------------------------------------------------*/
  644. #if ( configNUMBER_OF_CORES > 1 )
  645. static BaseType_t prvIsYieldUsingPrioritySMP( UBaseType_t uxTaskPriority,
  646. BaseType_t xTaskCoreID,
  647. BaseType_t xCurCoreID,
  648. BaseType_t xYieldEqualPriority )
  649. {
  650. configASSERT( uxTaskPriority < configMAX_PRIORITIES );
  651. if( xYieldEqualPriority == pdTRUE )
  652. {
  653. /* Increment the task priority to achieve the same affect as
  654. * if( uxTaskPriority >= pxCurrentTCBs->uxPriority ). */
  655. uxTaskPriority++;
  656. }
  657. /* Indicate whether the current core needs to yield */
  658. BaseType_t xYieldRequiredCurrentCore;
  659. /* If the target task can run on the current core, and has a higher
  660. * priority than the current core, and the core has not suspended
  661. * scheduling, then yield the current core.
  662. * Todo: Make fair scheduling a configurable option (IDF-5772). */
  663. if( ( taskIS_AFFINITY_COMPATIBLE( xCurCoreID, xTaskCoreID ) == pdTRUE ) &&
  664. ( uxTaskPriority > pxCurrentTCBs[ xCurCoreID ]->uxPriority ) &&
  665. ( uxSchedulerSuspended[ xCurCoreID ] == ( UBaseType_t ) 0U ) )
  666. {
  667. /* Return true for the caller to yield the current core */
  668. xYieldRequiredCurrentCore = pdTRUE;
  669. }
  670. /* If the target task can run on the other core, and has a higher
  671. * priority then the other core, and the other core has not suspended
  672. * scheduling, then yield the other core */
  673. else if( ( taskIS_AFFINITY_COMPATIBLE( !xCurCoreID, xTaskCoreID ) == pdTRUE ) &&
  674. ( uxTaskPriority > pxCurrentTCBs[ !xCurCoreID ]->uxPriority ) &&
  675. ( uxSchedulerSuspended[ !xCurCoreID ] == ( UBaseType_t ) 0U ) )
  676. {
  677. /* Signal the other core to yield */
  678. taskYIELD_CORE( !xCurCoreID );
  679. xYieldRequiredCurrentCore = pdFALSE;
  680. }
  681. else
  682. {
  683. xYieldRequiredCurrentCore = pdFALSE;
  684. }
  685. return xYieldRequiredCurrentCore;
  686. }
  687. #endif /* configNUMBER_OF_CORES > 1 */
  688. /*-----------------------------------------------------------*/
  689. #if ( configNUMBER_OF_CORES > 1 )
  690. static BaseType_t prvCheckTaskCanBeScheduledSMP( TCB_t * pxTCB )
  691. {
  692. BaseType_t xReturn;
  693. if( pxTCB->xCoreID == tskNO_AFFINITY )
  694. {
  695. /* Task is unpinned. As long as one core has not suspended
  696. * scheduling, the task can be scheduled. */
  697. if( ( uxSchedulerSuspended[ 0 ] == ( UBaseType_t ) 0U ) || ( uxSchedulerSuspended[ 1 ] == ( UBaseType_t ) 0U ) )
  698. {
  699. xReturn = pdTRUE;
  700. }
  701. else
  702. {
  703. xReturn = pdFALSE;
  704. }
  705. }
  706. else if( uxSchedulerSuspended[ pxTCB->xCoreID ] == ( UBaseType_t ) 0U )
  707. {
  708. /* The task is pinned to a core. If it's pinned core has not
  709. * suspended scheduling, the task can be scheduled. */
  710. xReturn = pdTRUE;
  711. }
  712. else
  713. {
  714. xReturn = pdFALSE;
  715. }
  716. return xReturn;
  717. }
  718. #endif /* configNUMBER_OF_CORES > 1 */
  719. /*-----------------------------------------------------------*/
  720. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
  721. /* Todo: Add support for task restricted API (IDF-7895) */
  722. BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
  723. TaskHandle_t * pxCreatedTask )
  724. {
  725. TCB_t * pxNewTCB;
  726. BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  727. configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
  728. configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
  729. if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
  730. {
  731. /* Allocate space for the TCB. Where the memory comes from depends
  732. * on the implementation of the port malloc function and whether or
  733. * not static allocation is being used. */
  734. pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
  735. memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  736. /* Store the stack location in the TCB. */
  737. pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
  738. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  739. {
  740. /* Tasks can be created statically or dynamically, so note this
  741. * task was created statically in case the task is later deleted. */
  742. pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
  743. }
  744. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
  745. prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
  746. pxTaskDefinition->pcName,
  747. ( uint32_t ) pxTaskDefinition->usStackDepth,
  748. pxTaskDefinition->pvParameters,
  749. pxTaskDefinition->uxPriority,
  750. pxCreatedTask, pxNewTCB,
  751. pxTaskDefinition->xRegions );
  752. prvAddNewTaskToReadyList( pxNewTCB );
  753. xReturn = pdPASS;
  754. }
  755. return xReturn;
  756. }
  757. #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  758. /*-----------------------------------------------------------*/
  759. #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
  760. /* Todo: Add support for task restricted API (IDF-7895) */
  761. BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
  762. TaskHandle_t * pxCreatedTask )
  763. {
  764. TCB_t * pxNewTCB;
  765. BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
  766. configASSERT( pxTaskDefinition->puxStackBuffer );
  767. if( pxTaskDefinition->puxStackBuffer != NULL )
  768. {
  769. /* Allocate space for the TCB. Where the memory comes from depends
  770. * on the implementation of the port malloc function and whether or
  771. * not static allocation is being used. */
  772. pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
  773. if( pxNewTCB != NULL )
  774. {
  775. memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
  776. /* Store the stack location in the TCB. */
  777. pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
  778. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
  779. {
  780. /* Tasks can be created statically or dynamically, so note
  781. * this task had a statically allocated stack in case it is
  782. * later deleted. The TCB was allocated dynamically. */
  783. pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
  784. }
  785. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
  786. prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
  787. pxTaskDefinition->pcName,
  788. ( uint32_t ) pxTaskDefinition->usStackDepth,
  789. pxTaskDefinition->pvParameters,
  790. pxTaskDefinition->uxPriority,
  791. pxCreatedTask, pxNewTCB,
  792. pxTaskDefinition->xRegions );
  793. prvAddNewTaskToReadyList( pxNewTCB );
  794. xReturn = pdPASS;
  795. }
  796. }
  797. return xReturn;
  798. }
  799. #endif /* portUSING_MPU_WRAPPERS */
  800. /*-----------------------------------------------------------*/
  801. static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
  802. const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  803. const uint32_t ulStackDepth,
  804. void * const pvParameters,
  805. UBaseType_t uxPriority,
  806. TaskHandle_t * const pxCreatedTask,
  807. TCB_t * pxNewTCB,
  808. const MemoryRegion_t * const xRegions,
  809. BaseType_t xCoreID )
  810. {
  811. StackType_t * pxTopOfStack;
  812. UBaseType_t x;
  813. #if ( configNUMBER_OF_CORES > 1 )
  814. /* Check that xCoreID is valid */
  815. configASSERT( taskVALID_CORE_ID( xCoreID ) == pdTRUE );
  816. #else
  817. /* Hard code xCoreID to 0 */
  818. xCoreID = 0;
  819. #endif
  820. #if ( portUSING_MPU_WRAPPERS == 1 )
  821. /* Should the task be created in privileged mode? */
  822. BaseType_t xRunPrivileged;
  823. if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
  824. {
  825. xRunPrivileged = pdTRUE;
  826. }
  827. else
  828. {
  829. xRunPrivileged = pdFALSE;
  830. }
  831. uxPriority &= ~portPRIVILEGE_BIT;
  832. #endif /* portUSING_MPU_WRAPPERS == 1 */
  833. /* Avoid dependency on memset() if it is not required. */
  834. #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
  835. {
  836. /* Fill the stack with a known value to assist debugging. */
  837. ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
  838. }
  839. #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
  840. /* Calculate the top of stack address. This depends on whether the stack
  841. * grows from high memory to low (as per the 80x86) or vice versa.
  842. * portSTACK_GROWTH is used to make the result positive or negative as required
  843. * by the port. */
  844. #if ( portSTACK_GROWTH < 0 )
  845. {
  846. pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
  847. pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. Checked by assert(). */
  848. /* Check the alignment of the calculated top of stack is correct. */
  849. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  850. #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
  851. {
  852. /* Also record the stack's high address, which may assist
  853. * debugging. */
  854. pxNewTCB->pxEndOfStack = pxTopOfStack;
  855. }
  856. #endif /* configRECORD_STACK_HIGH_ADDRESS */
  857. }
  858. #else /* portSTACK_GROWTH */
  859. {
  860. pxTopOfStack = pxNewTCB->pxStack;
  861. /* Check the alignment of the stack buffer is correct. */
  862. configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
  863. /* The other extreme of the stack space is required if stack checking is
  864. * performed. */
  865. pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
  866. }
  867. #endif /* portSTACK_GROWTH */
  868. /* Store the task name in the TCB. */
  869. if( pcName != NULL )
  870. {
  871. for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  872. {
  873. pxNewTCB->pcTaskName[ x ] = pcName[ x ];
  874. /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
  875. * configMAX_TASK_NAME_LEN characters just in case the memory after the
  876. * string is not accessible (extremely unlikely). */
  877. if( pcName[ x ] == ( char ) 0x00 )
  878. {
  879. break;
  880. }
  881. else
  882. {
  883. mtCOVERAGE_TEST_MARKER();
  884. }
  885. }
  886. /* Ensure the name string is terminated in the case that the string length
  887. * was greater or equal to configMAX_TASK_NAME_LEN. */
  888. pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
  889. }
  890. else
  891. {
  892. mtCOVERAGE_TEST_MARKER();
  893. }
  894. /* This is used as an array index so must ensure it's not too large. */
  895. configASSERT( uxPriority < configMAX_PRIORITIES );
  896. if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  897. {
  898. uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  899. }
  900. else
  901. {
  902. mtCOVERAGE_TEST_MARKER();
  903. }
  904. pxNewTCB->uxPriority = uxPriority;
  905. pxNewTCB->xCoreID = xCoreID; /* Todo: Remove xCoreID for single core builds (IDF-7894) */
  906. #if ( configUSE_MUTEXES == 1 )
  907. {
  908. pxNewTCB->uxBasePriority = uxPriority;
  909. }
  910. #endif /* configUSE_MUTEXES */
  911. vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
  912. vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
  913. /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
  914. * back to the containing TCB from a generic item in a list. */
  915. listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
  916. /* Event lists are always in priority order. */
  917. listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  918. listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
  919. #if ( portUSING_MPU_WRAPPERS == 1 )
  920. {
  921. vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
  922. }
  923. #else
  924. {
  925. /* Avoid compiler warning about unreferenced parameter. */
  926. ( void ) xRegions;
  927. }
  928. #endif
  929. #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
  930. {
  931. /* Allocate and initialize memory for the task's TLS Block. */
  932. configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock );
  933. }
  934. #endif
  935. /* Initialize the TCB stack to look as if the task was already running,
  936. * but had been interrupted by the scheduler. The return address is set
  937. * to the start of the task function. Once the stack has been initialised
  938. * the top of stack variable is updated. */
  939. #if ( portUSING_MPU_WRAPPERS == 1 )
  940. {
  941. /* If the port has capability to detect stack overflow,
  942. * pass the stack end address to the stack initialization
  943. * function as well. */
  944. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  945. {
  946. #if ( portSTACK_GROWTH < 0 )
  947. {
  948. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
  949. }
  950. #else /* portSTACK_GROWTH */
  951. {
  952. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
  953. }
  954. #endif /* portSTACK_GROWTH */
  955. }
  956. #else /* portHAS_STACK_OVERFLOW_CHECKING */
  957. {
  958. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
  959. }
  960. #endif /* portHAS_STACK_OVERFLOW_CHECKING */
  961. }
  962. #else /* portUSING_MPU_WRAPPERS */
  963. {
  964. /* If the port has capability to detect stack overflow,
  965. * pass the stack end address to the stack initialization
  966. * function as well. */
  967. #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
  968. {
  969. #if ( portSTACK_GROWTH < 0 )
  970. {
  971. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
  972. }
  973. #else /* portSTACK_GROWTH */
  974. {
  975. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
  976. }
  977. #endif /* portSTACK_GROWTH */
  978. }
  979. #else /* portHAS_STACK_OVERFLOW_CHECKING */
  980. {
  981. pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
  982. }
  983. #endif /* portHAS_STACK_OVERFLOW_CHECKING */
  984. }
  985. #endif /* portUSING_MPU_WRAPPERS */
  986. if( pxCreatedTask != NULL )
  987. {
  988. /* Pass the handle out in an anonymous way. The handle can be used to
  989. * change the created task's priority, delete the created task, etc.*/
  990. *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
  991. }
  992. else
  993. {
  994. mtCOVERAGE_TEST_MARKER();
  995. }
  996. }
  997. /*-----------------------------------------------------------*/
  998. static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
  999. {
  1000. /* Ensure interrupts don't access the task lists while the lists are being
  1001. * updated. */
  1002. taskENTER_CRITICAL( &xKernelLock );
  1003. {
  1004. uxCurrentNumberOfTasks++;
  1005. if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
  1006. {
  1007. /* This is the first task to be created so do the preliminary
  1008. * initialisation required. We will not recover if this call
  1009. * fails, but we will report the failure. */
  1010. prvInitialiseTaskLists();
  1011. }
  1012. else
  1013. {
  1014. mtCOVERAGE_TEST_MARKER();
  1015. }
  1016. if( ( pxCurrentTCBs[ 0 ] == NULL ) && ( taskIS_AFFINITY_COMPATIBLE( 0, pxNewTCB->xCoreID ) == pdTRUE ) )
  1017. {
  1018. /* On core 0, there are no other tasks, or all the other tasks
  1019. * are in the suspended state - make this the current task. */
  1020. pxCurrentTCBs[ 0 ] = pxNewTCB;
  1021. }
  1022. #if ( configNUMBER_OF_CORES > 1 )
  1023. else if( ( pxCurrentTCBs[ 1 ] == NULL ) && ( taskIS_AFFINITY_COMPATIBLE( 1, pxNewTCB->xCoreID ) == pdTRUE ) )
  1024. {
  1025. /* On core 1, there are no other tasks, or all the other tasks
  1026. * are in the suspended state - make this the current task. */
  1027. pxCurrentTCBs[ 1 ] = pxNewTCB;
  1028. }
  1029. #endif /* configNUMBER_OF_CORES > 1 */
  1030. else
  1031. {
  1032. /* If the scheduler is not already running, make this task the
  1033. * current task if it is the highest priority task to be created
  1034. * so far. */
  1035. if( xSchedulerRunning == pdFALSE )
  1036. {
  1037. if( ( pxCurrentTCBs[ 0 ] != NULL ) &&
  1038. ( taskIS_AFFINITY_COMPATIBLE( 0, pxNewTCB->xCoreID ) == pdTRUE ) &&
  1039. ( pxCurrentTCBs[ 0 ]->uxPriority <= pxNewTCB->uxPriority ) )
  1040. {
  1041. pxCurrentTCBs[ 0 ] = pxNewTCB;
  1042. }
  1043. #if ( configNUMBER_OF_CORES > 1 )
  1044. else if( ( pxCurrentTCBs[ 1 ] != NULL ) &&
  1045. ( taskIS_AFFINITY_COMPATIBLE( 1, pxNewTCB->xCoreID ) == pdTRUE ) &&
  1046. ( pxCurrentTCBs[ 1 ]->uxPriority <= pxNewTCB->uxPriority ) )
  1047. {
  1048. pxCurrentTCBs[ 1 ] = pxNewTCB;
  1049. }
  1050. #endif /* configNUMBER_OF_CORES > 1 */
  1051. else
  1052. {
  1053. mtCOVERAGE_TEST_MARKER();
  1054. }
  1055. }
  1056. else
  1057. {
  1058. mtCOVERAGE_TEST_MARKER();
  1059. }
  1060. }
  1061. uxTaskNumber++;
  1062. #if ( configUSE_TRACE_FACILITY == 1 )
  1063. {
  1064. /* Add a counter into the TCB for tracing only. */
  1065. pxNewTCB->uxTCBNumber = uxTaskNumber;
  1066. }
  1067. #endif /* configUSE_TRACE_FACILITY */
  1068. traceTASK_CREATE( pxNewTCB );
  1069. prvAddTaskToReadyList( pxNewTCB );
  1070. portSETUP_TCB( pxNewTCB );
  1071. if( xSchedulerRunning != pdFALSE )
  1072. {
  1073. /* If the created task is of a higher priority than the current task
  1074. * then it should run now. */
  1075. if( taskIS_YIELD_REQUIRED( pxNewTCB, portGET_CORE_ID(), pdTRUE ) == pdTRUE )
  1076. {
  1077. taskYIELD_IF_USING_PREEMPTION();
  1078. }
  1079. else
  1080. {
  1081. mtCOVERAGE_TEST_MARKER();
  1082. }
  1083. }
  1084. else
  1085. {
  1086. mtCOVERAGE_TEST_MARKER();
  1087. }
  1088. }
  1089. taskEXIT_CRITICAL( &xKernelLock );
  1090. /* SINGLE-CORE MODIFICATION: Extended critical section so that SMP can check
  1091. * for yield inside critical section. */
  1092. }
  1093. /*-----------------------------------------------------------*/
  1094. #if ( INCLUDE_vTaskDelete == 1 )
  1095. void vTaskDelete( TaskHandle_t xTaskToDelete )
  1096. {
  1097. TCB_t * pxTCB;
  1098. BaseType_t xSelfDelete;
  1099. BaseType_t xIsCurRunning;
  1100. taskENTER_CRITICAL( &xKernelLock );
  1101. {
  1102. /* Get current core ID as we can no longer be preempted. */
  1103. const BaseType_t xCurCoreID = portGET_CORE_ID();
  1104. /* If null is passed in here then it is the calling task that is
  1105. * being deleted. */
  1106. pxTCB = prvGetTCBFromHandle( xTaskToDelete );
  1107. /* Remove task from the ready/delayed list. */
  1108. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  1109. {
  1110. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  1111. }
  1112. else
  1113. {
  1114. mtCOVERAGE_TEST_MARKER();
  1115. }
  1116. /* Is the task waiting on an event also? */
  1117. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  1118. {
  1119. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1120. }
  1121. else
  1122. {
  1123. mtCOVERAGE_TEST_MARKER();
  1124. }
  1125. /* Increment the uxTaskNumber also so kernel aware debuggers can
  1126. * detect that the task lists need re-generating. This is done before
  1127. * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
  1128. * not return. */
  1129. uxTaskNumber++;
  1130. /* Check if the task is deleting itself, or is currently running on
  1131. * the other core. */
  1132. if( taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, xCurCoreID ) == pdTRUE )
  1133. {
  1134. xSelfDelete = pdTRUE;
  1135. xIsCurRunning = pdTRUE;
  1136. }
  1137. #if ( configNUMBER_OF_CORES > 1 )
  1138. else if( taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, !xCurCoreID ) == pdTRUE )
  1139. {
  1140. xSelfDelete = pdFALSE;
  1141. xIsCurRunning = pdTRUE;
  1142. }
  1143. #endif /* configNUMBER_OF_CORES > 1 */
  1144. else
  1145. {
  1146. xSelfDelete = pdFALSE;
  1147. xIsCurRunning = pdFALSE;
  1148. }
  1149. if( xIsCurRunning == pdTRUE )
  1150. {
  1151. /* A task is deleting itself or is currently running. This
  1152. * cannot complete within the task itself, as a context switch
  1153. * to another task is required. Place the task in the
  1154. * termination list. The idle task will check the termination
  1155. * list and free up any memory allocated by the scheduler for
  1156. * the TCB and stack of the deleted task. */
  1157. vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
  1158. /* Increment the ucTasksDeleted variable so the idle task knows
  1159. * there is a task that has been deleted and that it should therefore
  1160. * check the xTasksWaitingTermination list. */
  1161. ++uxDeletedTasksWaitingCleanUp;
  1162. /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
  1163. * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
  1164. traceTASK_DELETE( pxTCB );
  1165. /* The pre-delete hook is primarily for the Windows simulator,
  1166. * in which Windows specific clean up operations are performed,
  1167. * after which it is not possible to yield away from this task -
  1168. * hence xYieldPending is used to latch that a context switch is
  1169. * required. */
  1170. portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending[ xCurCoreID ] );
  1171. #if ( configNUMBER_OF_CORES > 1 )
  1172. if( xSelfDelete == pdFALSE )
  1173. {
  1174. /* The task that is being deleted is currently running
  1175. * on the other core. Send a yield request to the other
  1176. * core so that the task is swapped out. */
  1177. taskYIELD_CORE( !xCurCoreID );
  1178. }
  1179. #else /* configNUMBER_OF_CORES > 1 */
  1180. /* xCurCoreID is unused */
  1181. ( void ) xCurCoreID;
  1182. #endif /* configNUMBER_OF_CORES > 1 */
  1183. }
  1184. else
  1185. {
  1186. --uxCurrentNumberOfTasks;
  1187. traceTASK_DELETE( pxTCB );
  1188. /* Reset the next expected unblock time in case it referred to
  1189. * the task that has just been deleted. */
  1190. prvResetNextTaskUnblockTime();
  1191. }
  1192. }
  1193. taskEXIT_CRITICAL( &xKernelLock );
  1194. /* If the task is currently running, call prvDeleteTCB from outside of
  1195. * critical section. If a task is currently running, prvDeleteTCB is
  1196. * called from prvCheckTasksWaitingTermination which is called from
  1197. * Idle task. */
  1198. if( xIsCurRunning == pdFALSE )
  1199. {
  1200. prvDeleteTCB( pxTCB );
  1201. }
  1202. /* For SMP, we need to take the kernel lock here as we are about to
  1203. * access kernel data structures. */
  1204. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  1205. {
  1206. /* Force a reschedule if it is the currently running task that has just
  1207. * been deleted. */
  1208. if( xSchedulerRunning != pdFALSE )
  1209. {
  1210. if( xSelfDelete == pdTRUE )
  1211. {
  1212. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdFALSE );
  1213. portYIELD_WITHIN_API();
  1214. }
  1215. else
  1216. {
  1217. mtCOVERAGE_TEST_MARKER();
  1218. }
  1219. }
  1220. }
  1221. /* Release the previously taken kernel lock. */
  1222. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  1223. }
  1224. #endif /* INCLUDE_vTaskDelete */
  1225. /*-----------------------------------------------------------*/
  1226. #if ( INCLUDE_xTaskDelayUntil == 1 )
  1227. BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
  1228. const TickType_t xTimeIncrement )
  1229. {
  1230. TickType_t xTimeToWake;
  1231. BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
  1232. configASSERT( pxPreviousWakeTime );
  1233. configASSERT( ( xTimeIncrement > 0U ) );
  1234. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdFALSE );
  1235. prvENTER_CRITICAL_OR_SUSPEND_ALL( &xKernelLock );
  1236. {
  1237. /* Minor optimisation. The tick count cannot change in this
  1238. * block. */
  1239. const TickType_t xConstTickCount = xTickCount;
  1240. /* Generate the tick time at which the task wants to wake. */
  1241. xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
  1242. if( xConstTickCount < *pxPreviousWakeTime )
  1243. {
  1244. /* The tick count has overflowed since this function was
  1245. * lasted called. In this case the only time we should ever
  1246. * actually delay is if the wake time has also overflowed,
  1247. * and the wake time is greater than the tick time. When this
  1248. * is the case it is as if neither time had overflowed. */
  1249. if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
  1250. {
  1251. xShouldDelay = pdTRUE;
  1252. }
  1253. else
  1254. {
  1255. mtCOVERAGE_TEST_MARKER();
  1256. }
  1257. }
  1258. else
  1259. {
  1260. /* The tick time has not overflowed. In this case we will
  1261. * delay if either the wake time has overflowed, and/or the
  1262. * tick time is less than the wake time. */
  1263. if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
  1264. {
  1265. xShouldDelay = pdTRUE;
  1266. }
  1267. else
  1268. {
  1269. mtCOVERAGE_TEST_MARKER();
  1270. }
  1271. }
  1272. /* Update the wake time ready for the next call. */
  1273. *pxPreviousWakeTime = xTimeToWake;
  1274. if( xShouldDelay != pdFALSE )
  1275. {
  1276. traceTASK_DELAY_UNTIL( xTimeToWake );
  1277. /* prvAddCurrentTaskToDelayedList() needs the block time, not
  1278. * the time to wake, so subtract the current tick count. */
  1279. prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
  1280. }
  1281. else
  1282. {
  1283. mtCOVERAGE_TEST_MARKER();
  1284. }
  1285. }
  1286. xAlreadyYielded = prvEXIT_CRITICAL_OR_RESUME_ALL( &xKernelLock );
  1287. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  1288. * have put ourselves to sleep. */
  1289. if( xAlreadyYielded == pdFALSE )
  1290. {
  1291. portYIELD_WITHIN_API();
  1292. }
  1293. else
  1294. {
  1295. mtCOVERAGE_TEST_MARKER();
  1296. }
  1297. return xShouldDelay;
  1298. }
  1299. #endif /* INCLUDE_xTaskDelayUntil */
  1300. /*-----------------------------------------------------------*/
  1301. #if ( INCLUDE_vTaskDelay == 1 )
  1302. void vTaskDelay( const TickType_t xTicksToDelay )
  1303. {
  1304. BaseType_t xAlreadyYielded = pdFALSE;
  1305. /* A delay time of zero just forces a reschedule. */
  1306. if( xTicksToDelay > ( TickType_t ) 0U )
  1307. {
  1308. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdFALSE );
  1309. prvENTER_CRITICAL_OR_SUSPEND_ALL( &xKernelLock );
  1310. {
  1311. traceTASK_DELAY();
  1312. /* A task that is removed from the event list while the
  1313. * scheduler is suspended will not get placed in the ready
  1314. * list or removed from the blocked list until the scheduler
  1315. * is resumed.
  1316. *
  1317. * This task cannot be in an event list as it is the currently
  1318. * executing task. */
  1319. prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
  1320. }
  1321. xAlreadyYielded = prvEXIT_CRITICAL_OR_RESUME_ALL( &xKernelLock );
  1322. }
  1323. else
  1324. {
  1325. mtCOVERAGE_TEST_MARKER();
  1326. }
  1327. /* Force a reschedule if xTaskResumeAll has not already done so, we may
  1328. * have put ourselves to sleep. */
  1329. if( xAlreadyYielded == pdFALSE )
  1330. {
  1331. portYIELD_WITHIN_API();
  1332. }
  1333. else
  1334. {
  1335. mtCOVERAGE_TEST_MARKER();
  1336. }
  1337. }
  1338. #endif /* INCLUDE_vTaskDelay */
  1339. /*-----------------------------------------------------------*/
  1340. #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
  1341. eTaskState eTaskGetState( TaskHandle_t xTask )
  1342. {
  1343. eTaskState eReturn;
  1344. List_t const * pxStateList;
  1345. List_t const * pxDelayedList;
  1346. List_t const * pxOverflowedDelayedList;
  1347. const TCB_t * const pxTCB = xTask;
  1348. configASSERT( pxTCB );
  1349. taskENTER_CRITICAL( &xKernelLock );
  1350. {
  1351. if( taskIS_CURRENTLY_RUNNING( pxTCB ) == pdTRUE )
  1352. {
  1353. /* The task calling this function is querying its own state. */
  1354. eReturn = eRunning;
  1355. }
  1356. else
  1357. {
  1358. pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
  1359. pxDelayedList = pxDelayedTaskList;
  1360. pxOverflowedDelayedList = pxOverflowDelayedTaskList;
  1361. if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
  1362. {
  1363. /* The task being queried is referenced from one of the Blocked
  1364. * lists. */
  1365. eReturn = eBlocked;
  1366. }
  1367. #if ( INCLUDE_vTaskSuspend == 1 )
  1368. else if( pxStateList == &xSuspendedTaskList )
  1369. {
  1370. /* The task being queried is referenced from the suspended
  1371. * list. Is it genuinely suspended or is it blocked
  1372. * indefinitely? */
  1373. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
  1374. {
  1375. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  1376. {
  1377. BaseType_t x;
  1378. /* The task does not appear on the event list item of
  1379. * and of the RTOS objects, but could still be in the
  1380. * blocked state if it is waiting on its notification
  1381. * rather than waiting on an object. If not, is
  1382. * suspended. */
  1383. eReturn = eSuspended;
  1384. for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  1385. {
  1386. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  1387. {
  1388. eReturn = eBlocked;
  1389. break;
  1390. }
  1391. }
  1392. }
  1393. #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  1394. {
  1395. eReturn = eSuspended;
  1396. }
  1397. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  1398. }
  1399. else
  1400. {
  1401. eReturn = eBlocked;
  1402. }
  1403. }
  1404. #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
  1405. #if ( INCLUDE_vTaskDelete == 1 )
  1406. else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
  1407. {
  1408. /* The task being queried is referenced from the deleted
  1409. * tasks list, or it is not referenced from any lists at
  1410. * all. */
  1411. eReturn = eDeleted;
  1412. }
  1413. #endif
  1414. else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
  1415. {
  1416. /* If the task is not in any other state, it must be in the
  1417. * Ready (including pending ready) state. */
  1418. eReturn = eReady;
  1419. }
  1420. }
  1421. }
  1422. taskEXIT_CRITICAL( &xKernelLock );
  1423. return eReturn;
  1424. } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
  1425. #endif /* INCLUDE_eTaskGetState */
  1426. /*-----------------------------------------------------------*/
  1427. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  1428. UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
  1429. {
  1430. TCB_t const * pxTCB;
  1431. UBaseType_t uxReturn;
  1432. taskENTER_CRITICAL( &xKernelLock );
  1433. {
  1434. /* If null is passed in here then it is the priority of the task
  1435. * that called uxTaskPriorityGet() that is being queried. */
  1436. pxTCB = prvGetTCBFromHandle( xTask );
  1437. uxReturn = pxTCB->uxPriority;
  1438. }
  1439. taskEXIT_CRITICAL( &xKernelLock );
  1440. return uxReturn;
  1441. }
  1442. #endif /* INCLUDE_uxTaskPriorityGet */
  1443. /*-----------------------------------------------------------*/
  1444. #if ( INCLUDE_uxTaskPriorityGet == 1 )
  1445. UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
  1446. {
  1447. TCB_t const * pxTCB;
  1448. UBaseType_t uxReturn, uxSavedInterruptState;
  1449. /* RTOS ports that support interrupt nesting have the concept of a
  1450. * maximum system call (or maximum API call) interrupt priority.
  1451. * Interrupts that are above the maximum system call priority are keep
  1452. * permanently enabled, even when the RTOS kernel is in a critical section,
  1453. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  1454. * is defined in FreeRTOSConfig.h then
  1455. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1456. * failure if a FreeRTOS API function is called from an interrupt that has
  1457. * been assigned a priority above the configured maximum system call
  1458. * priority. Only FreeRTOS functions that end in FromISR can be called
  1459. * from interrupts that have been assigned a priority at or (logically)
  1460. * below the maximum system call interrupt priority. FreeRTOS maintains a
  1461. * separate interrupt safe API to ensure interrupt entry is as fast and as
  1462. * simple as possible. More information (albeit Cortex-M specific) is
  1463. * provided on the following link:
  1464. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  1465. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1466. prvENTER_CRITICAL_OR_MASK_ISR( &xKernelLock, uxSavedInterruptState );
  1467. {
  1468. /* If null is passed in here then it is the priority of the calling
  1469. * task that is being queried. */
  1470. pxTCB = prvGetTCBFromHandle( xTask );
  1471. uxReturn = pxTCB->uxPriority;
  1472. }
  1473. prvEXIT_CRITICAL_OR_UNMASK_ISR( &xKernelLock, uxSavedInterruptState );
  1474. return uxReturn;
  1475. }
  1476. #endif /* INCLUDE_uxTaskPriorityGet */
  1477. /*-----------------------------------------------------------*/
  1478. #if ( INCLUDE_vTaskPrioritySet == 1 )
  1479. void vTaskPrioritySet( TaskHandle_t xTask,
  1480. UBaseType_t uxNewPriority )
  1481. {
  1482. TCB_t * pxTCB;
  1483. UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
  1484. BaseType_t xYieldRequired = pdFALSE;
  1485. configASSERT( uxNewPriority < configMAX_PRIORITIES );
  1486. /* Ensure the new priority is valid. */
  1487. if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
  1488. {
  1489. uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
  1490. }
  1491. else
  1492. {
  1493. mtCOVERAGE_TEST_MARKER();
  1494. }
  1495. taskENTER_CRITICAL( &xKernelLock );
  1496. {
  1497. /* Get current core ID as we can no longer be preempted. */
  1498. const BaseType_t xCurCoreID = portGET_CORE_ID();
  1499. /* If null is passed in here then it is the priority of the calling
  1500. * task that is being changed. */
  1501. pxTCB = prvGetTCBFromHandle( xTask );
  1502. traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
  1503. #if ( configUSE_MUTEXES == 1 )
  1504. {
  1505. uxCurrentBasePriority = pxTCB->uxBasePriority;
  1506. }
  1507. #else
  1508. {
  1509. uxCurrentBasePriority = pxTCB->uxPriority;
  1510. }
  1511. #endif
  1512. if( uxCurrentBasePriority != uxNewPriority )
  1513. {
  1514. /* The priority change may have readied a task of higher
  1515. * priority than the calling task. */
  1516. if( uxNewPriority > uxCurrentBasePriority )
  1517. {
  1518. if( taskIS_CURRENTLY_RUNNING( pxTCB ) == pdFALSE )
  1519. {
  1520. /* The priority of a task other than the currently
  1521. * running task is being raised. Is the priority being
  1522. * raised above that of the running task? */
  1523. if( taskIS_YIELD_REQUIRED_USING_PRIORITY( uxNewPriority, pxTCB->xCoreID, portGET_CORE_ID(), pdTRUE ) == pdTRUE )
  1524. {
  1525. xYieldRequired = pdTRUE;
  1526. }
  1527. else
  1528. {
  1529. mtCOVERAGE_TEST_MARKER();
  1530. }
  1531. }
  1532. else
  1533. {
  1534. /* The priority of the running task is being raised,
  1535. * but the running task must already be the highest
  1536. * priority task able to run so no yield is required. */
  1537. }
  1538. }
  1539. else if( taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, xCurCoreID ) == pdTRUE )
  1540. {
  1541. /* Lowering the priority of task currently running on the
  1542. * current core means there may now be another task of
  1543. * higher priority that is ready to execute. */
  1544. xYieldRequired = pdTRUE;
  1545. }
  1546. #if ( configNUMBER_OF_CORES > 1 )
  1547. else if( taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, !xCurCoreID ) == pdTRUE )
  1548. {
  1549. /* Lowering the priority of task currently running on the
  1550. * other core also means there may now be another task of
  1551. * higher priority that is ready to execute. */
  1552. taskYIELD_CORE( !xCurCoreID );
  1553. }
  1554. #endif /* configNUMBER_OF_CORES > 1 */
  1555. else
  1556. {
  1557. /* Setting the priority of any other task down does not
  1558. * require a yield as the running task must be above the
  1559. * new priority of the task being modified. */
  1560. }
  1561. /* Remember the ready list the task might be referenced from
  1562. * before its uxPriority member is changed so the
  1563. * taskRESET_READY_PRIORITY() macro can function correctly. */
  1564. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  1565. #if ( configUSE_MUTEXES == 1 )
  1566. {
  1567. /* Only change the priority being used if the task is not
  1568. * currently using an inherited priority. */
  1569. if( pxTCB->uxBasePriority == pxTCB->uxPriority )
  1570. {
  1571. pxTCB->uxPriority = uxNewPriority;
  1572. }
  1573. else
  1574. {
  1575. mtCOVERAGE_TEST_MARKER();
  1576. }
  1577. /* The base priority gets set whatever. */
  1578. pxTCB->uxBasePriority = uxNewPriority;
  1579. }
  1580. #else /* if ( configUSE_MUTEXES == 1 ) */
  1581. {
  1582. pxTCB->uxPriority = uxNewPriority;
  1583. }
  1584. #endif /* if ( configUSE_MUTEXES == 1 ) */
  1585. /* Only reset the event list item value if the value is not
  1586. * being used for anything else. */
  1587. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  1588. {
  1589. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  1590. }
  1591. else
  1592. {
  1593. mtCOVERAGE_TEST_MARKER();
  1594. }
  1595. /* If the task is in the blocked or suspended list we need do
  1596. * nothing more than change its priority variable. However, if
  1597. * the task is in a ready list it needs to be removed and placed
  1598. * in the list appropriate to its new priority. */
  1599. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
  1600. {
  1601. /* The task is currently in its ready list - remove before
  1602. * adding it to its new ready list. As we are in a critical
  1603. * section we can do this even if the scheduler is suspended. */
  1604. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  1605. {
  1606. /* It is known that the task is in its ready list so
  1607. * there is no need to check again and the port level
  1608. * reset macro can be called directly. */
  1609. portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
  1610. }
  1611. else
  1612. {
  1613. mtCOVERAGE_TEST_MARKER();
  1614. }
  1615. prvAddTaskToReadyList( pxTCB );
  1616. }
  1617. else
  1618. {
  1619. mtCOVERAGE_TEST_MARKER();
  1620. }
  1621. if( xYieldRequired != pdFALSE )
  1622. {
  1623. taskYIELD_IF_USING_PREEMPTION();
  1624. }
  1625. else
  1626. {
  1627. mtCOVERAGE_TEST_MARKER();
  1628. }
  1629. /* Remove compiler warning about unused variables when the port
  1630. * optimised task selection is not being used. */
  1631. ( void ) uxPriorityUsedOnEntry;
  1632. }
  1633. }
  1634. taskEXIT_CRITICAL( &xKernelLock );
  1635. }
  1636. #endif /* INCLUDE_vTaskPrioritySet */
  1637. /*-----------------------------------------------------------*/
  1638. #if ( INCLUDE_vTaskSuspend == 1 )
  1639. void vTaskSuspend( TaskHandle_t xTaskToSuspend )
  1640. {
  1641. TCB_t * pxTCB;
  1642. taskENTER_CRITICAL( &xKernelLock );
  1643. {
  1644. /* Get current core ID as we can no longer be preempted. */
  1645. const BaseType_t xCurCoreID = portGET_CORE_ID();
  1646. /* If null is passed in here then it is the running task that is
  1647. * being suspended. */
  1648. pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
  1649. traceTASK_SUSPEND( pxTCB );
  1650. /* Remove task from the ready/delayed list and place in the
  1651. * suspended list. */
  1652. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  1653. {
  1654. taskRESET_READY_PRIORITY( pxTCB->uxPriority );
  1655. }
  1656. else
  1657. {
  1658. mtCOVERAGE_TEST_MARKER();
  1659. }
  1660. /* Is the task waiting on an event also? */
  1661. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  1662. {
  1663. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  1664. }
  1665. else
  1666. {
  1667. mtCOVERAGE_TEST_MARKER();
  1668. }
  1669. vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
  1670. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  1671. {
  1672. BaseType_t x;
  1673. for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
  1674. {
  1675. if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
  1676. {
  1677. /* The task was blocked to wait for a notification, but is
  1678. * now suspended, so no notification was received. */
  1679. pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
  1680. }
  1681. }
  1682. }
  1683. #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
  1684. if( xSchedulerRunning != pdFALSE )
  1685. {
  1686. /* Reset the next expected unblock time in case it referred to the
  1687. * task that is now in the Suspended state. */
  1688. prvResetNextTaskUnblockTime();
  1689. }
  1690. else
  1691. {
  1692. mtCOVERAGE_TEST_MARKER();
  1693. }
  1694. if( taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, xCurCoreID ) == pdTRUE )
  1695. {
  1696. if( xSchedulerRunning != pdFALSE )
  1697. {
  1698. /* The current task has just been suspended. */
  1699. configASSERT( uxSchedulerSuspended[ xCurCoreID ] == 0 );
  1700. portYIELD_WITHIN_API();
  1701. }
  1702. else
  1703. {
  1704. /* The scheduler is not running, but the task that was pointed
  1705. * to by pxCurrentTCBs has just been suspended and pxCurrentTCBs
  1706. * must be adjusted to point to a different task. */
  1707. if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
  1708. {
  1709. /* No other tasks are ready, so set pxCurrentTCBs back to
  1710. * NULL so when the next task is created pxCurrentTCBs will
  1711. * be set to point to it no matter what its relative priority
  1712. * is. */
  1713. pxCurrentTCBs[ xCurCoreID ] = NULL;
  1714. }
  1715. else
  1716. {
  1717. vTaskSwitchContext();
  1718. }
  1719. }
  1720. }
  1721. #if ( configNUMBER_OF_CORES > 1 )
  1722. else if( taskIS_CURRENTLY_RUNNING_ON_CORE( pxTCB, !xCurCoreID ) == pdTRUE )
  1723. {
  1724. /* The other core's current task has just been suspended */
  1725. if( xSchedulerRunning != pdFALSE )
  1726. {
  1727. taskYIELD_CORE( !xCurCoreID );
  1728. }
  1729. else
  1730. {
  1731. /* The scheduler is not running, but the task that was
  1732. * pointed to by pxCurrentTCBs[ otherCore ] has just been
  1733. * suspended. We simply set the
  1734. * pxCurrentTCBs[ otherCore ] to NULL for now.
  1735. *
  1736. * Todo: Update vTaskSwitchContext() to be able to run
  1737. * on behalf of the other core. */
  1738. pxCurrentTCBs[ !xCurCoreID ] = NULL;
  1739. }
  1740. }
  1741. #endif /* configNUMBER_OF_CORES > 1 */
  1742. else
  1743. {
  1744. mtCOVERAGE_TEST_MARKER();
  1745. }
  1746. }
  1747. taskEXIT_CRITICAL( &xKernelLock );
  1748. }
  1749. #endif /* INCLUDE_vTaskSuspend */
  1750. /*-----------------------------------------------------------*/
  1751. #if ( INCLUDE_vTaskSuspend == 1 )
  1752. static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
  1753. {
  1754. BaseType_t xReturn = pdFALSE;
  1755. const TCB_t * const pxTCB = xTask;
  1756. /* Accesses xPendingReadyList so must be called from a critical
  1757. * section. */
  1758. /* It does not make sense to check if the calling task is suspended. */
  1759. configASSERT( xTask );
  1760. /* Is the task being resumed actually in the suspended list? */
  1761. if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
  1762. {
  1763. /* Has the task already been resumed from within an ISR? */
  1764. #if ( configNUMBER_OF_CORES > 1 )
  1765. if( ( listIS_CONTAINED_WITHIN( &xPendingReadyList[ 0 ], &( pxTCB->xEventListItem ) ) == pdFALSE ) &&
  1766. ( listIS_CONTAINED_WITHIN( &xPendingReadyList[ 1 ], &( pxTCB->xEventListItem ) ) == pdFALSE ) )
  1767. #else
  1768. if( listIS_CONTAINED_WITHIN( &xPendingReadyList[ 0 ], &( pxTCB->xEventListItem ) ) == pdFALSE )
  1769. #endif /* configNUMBER_OF_CORES > 1 */
  1770. {
  1771. /* Is it in the suspended list because it is in the Suspended
  1772. * state, or because is is blocked with no timeout? */
  1773. if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
  1774. {
  1775. xReturn = pdTRUE;
  1776. }
  1777. else
  1778. {
  1779. mtCOVERAGE_TEST_MARKER();
  1780. }
  1781. }
  1782. else
  1783. {
  1784. mtCOVERAGE_TEST_MARKER();
  1785. }
  1786. }
  1787. else
  1788. {
  1789. mtCOVERAGE_TEST_MARKER();
  1790. }
  1791. return xReturn;
  1792. } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
  1793. #endif /* INCLUDE_vTaskSuspend */
  1794. /*-----------------------------------------------------------*/
  1795. #if ( INCLUDE_vTaskSuspend == 1 )
  1796. void vTaskResume( TaskHandle_t xTaskToResume )
  1797. {
  1798. TCB_t * const pxTCB = xTaskToResume;
  1799. /* It does not make sense to resume the calling task. */
  1800. configASSERT( xTaskToResume );
  1801. taskENTER_CRITICAL( &xKernelLock );
  1802. {
  1803. /* The parameter cannot be NULL as it is impossible to resume the
  1804. * currently executing task. */
  1805. if( ( taskIS_CURRENTLY_RUNNING( pxTCB ) == pdFALSE ) && ( pxTCB != NULL ) )
  1806. {
  1807. if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
  1808. {
  1809. traceTASK_RESUME( pxTCB );
  1810. /* The ready list can be accessed even if the scheduler is
  1811. * suspended because this is inside a critical section. */
  1812. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  1813. prvAddTaskToReadyList( pxTCB );
  1814. /* A higher priority task may have just been resumed. */
  1815. if( taskIS_YIELD_REQUIRED( pxTCB, portGET_CORE_ID(), pdTRUE ) == pdTRUE )
  1816. {
  1817. /* This yield may not cause the task just resumed to run,
  1818. * but will leave the lists in the correct state for the
  1819. * next yield. */
  1820. taskYIELD_IF_USING_PREEMPTION();
  1821. }
  1822. else
  1823. {
  1824. mtCOVERAGE_TEST_MARKER();
  1825. }
  1826. }
  1827. else
  1828. {
  1829. mtCOVERAGE_TEST_MARKER();
  1830. }
  1831. }
  1832. else
  1833. {
  1834. mtCOVERAGE_TEST_MARKER();
  1835. }
  1836. }
  1837. taskEXIT_CRITICAL( &xKernelLock );
  1838. }
  1839. #endif /* INCLUDE_vTaskSuspend */
  1840. /*-----------------------------------------------------------*/
  1841. #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
  1842. BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
  1843. {
  1844. BaseType_t xYieldRequired = pdFALSE;
  1845. TCB_t * const pxTCB = xTaskToResume;
  1846. UBaseType_t uxSavedInterruptStatus;
  1847. configASSERT( xTaskToResume );
  1848. /* RTOS ports that support interrupt nesting have the concept of a
  1849. * maximum system call (or maximum API call) interrupt priority.
  1850. * Interrupts that are above the maximum system call priority are keep
  1851. * permanently enabled, even when the RTOS kernel is in a critical section,
  1852. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  1853. * is defined in FreeRTOSConfig.h then
  1854. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  1855. * failure if a FreeRTOS API function is called from an interrupt that has
  1856. * been assigned a priority above the configured maximum system call
  1857. * priority. Only FreeRTOS functions that end in FromISR can be called
  1858. * from interrupts that have been assigned a priority at or (logically)
  1859. * below the maximum system call interrupt priority. FreeRTOS maintains a
  1860. * separate interrupt safe API to ensure interrupt entry is as fast and as
  1861. * simple as possible. More information (albeit Cortex-M specific) is
  1862. * provided on the following link:
  1863. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  1864. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  1865. prvENTER_CRITICAL_OR_MASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  1866. {
  1867. if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
  1868. {
  1869. /* Get current core ID as we can no longer be preempted. */
  1870. const BaseType_t xCurCoreID = portGET_CORE_ID();
  1871. traceTASK_RESUME_FROM_ISR( pxTCB );
  1872. /* Check the ready lists can be accessed. */
  1873. if( taskCAN_BE_SCHEDULED( pxTCB ) == pdTRUE )
  1874. {
  1875. /* Ready lists can be accessed so move the task from the
  1876. * suspended list to the ready list directly. */
  1877. if( taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, pdTRUE ) == pdTRUE )
  1878. {
  1879. xYieldRequired = pdTRUE;
  1880. /* Mark that a yield is pending in case the user is not
  1881. * using the return value to initiate a context switch
  1882. * from the ISR using portYIELD_FROM_ISR. */
  1883. xYieldPending[ xCurCoreID ] = pdTRUE;
  1884. }
  1885. else
  1886. {
  1887. mtCOVERAGE_TEST_MARKER();
  1888. }
  1889. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  1890. prvAddTaskToReadyList( pxTCB );
  1891. }
  1892. else
  1893. {
  1894. /* The delayed or ready lists cannot be accessed so the task
  1895. * is held in the pending ready list until the scheduler is
  1896. * unsuspended. */
  1897. vListInsertEnd( &( xPendingReadyList[ xCurCoreID ] ), &( pxTCB->xEventListItem ) );
  1898. }
  1899. }
  1900. else
  1901. {
  1902. mtCOVERAGE_TEST_MARKER();
  1903. }
  1904. }
  1905. prvEXIT_CRITICAL_OR_UNMASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  1906. return xYieldRequired;
  1907. }
  1908. #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
  1909. /*-----------------------------------------------------------*/
  1910. static BaseType_t prvCreateIdleTasks( void )
  1911. {
  1912. BaseType_t xReturn = pdPASS;
  1913. BaseType_t xCoreID;
  1914. #if ( configNUMBER_OF_CORES > 1 )
  1915. char cIdleName[ configMAX_TASK_NAME_LEN ];
  1916. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  1917. /* Add each idle task at the lowest priority. */
  1918. for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
  1919. {
  1920. #if ( configNUMBER_OF_CORES > 1 )
  1921. {
  1922. BaseType_t x;
  1923. if( xReturn == pdFAIL )
  1924. {
  1925. /* TODO: IDF-8240 - Memory leaks occur if IDLE task creation fails on some core
  1926. * as we do not free memory for the successfully created IDLE tasks. */
  1927. break;
  1928. }
  1929. else
  1930. {
  1931. mtCOVERAGE_TEST_MARKER();
  1932. }
  1933. for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  1934. {
  1935. cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
  1936. /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
  1937. * configMAX_TASK_NAME_LEN characters just in case the memory after the
  1938. * string is not accessible (extremely unlikely). */
  1939. if( cIdleName[ x ] == ( char ) 0x00 )
  1940. {
  1941. break;
  1942. }
  1943. else
  1944. {
  1945. mtCOVERAGE_TEST_MARKER();
  1946. }
  1947. }
  1948. /* Append the idle task number to the end of the name if there is space. */
  1949. if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
  1950. {
  1951. cIdleName[ x ] = ( char ) ( xCoreID + '0' );
  1952. x++;
  1953. /* And append a null character if there is space. */
  1954. if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
  1955. {
  1956. cIdleName[ x ] = '\0';
  1957. }
  1958. else
  1959. {
  1960. mtCOVERAGE_TEST_MARKER();
  1961. }
  1962. }
  1963. else
  1964. {
  1965. mtCOVERAGE_TEST_MARKER();
  1966. }
  1967. }
  1968. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  1969. /* Add the idle task at the lowest priority. */
  1970. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  1971. {
  1972. StaticTask_t * pxIdleTaskTCBBuffer = NULL;
  1973. StackType_t * pxIdleTaskStackBuffer = NULL;
  1974. uint32_t ulIdleTaskStackSize;
  1975. /* The Idle task is created using user provided RAM - obtain the
  1976. * address of the RAM then create the idle task. */
  1977. vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
  1978. xIdleTaskHandle[ xCoreID ] = xTaskCreateStaticPinnedToCore( prvIdleTask,
  1979. #if ( configNUMBER_OF_CORES > 1 )
  1980. cIdleName,
  1981. #else /* #if ( configNUMBER_OF_CORES > 1 ) */
  1982. configIDLE_TASK_NAME,
  1983. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  1984. ulIdleTaskStackSize,
  1985. ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
  1986. portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
  1987. pxIdleTaskStackBuffer,
  1988. pxIdleTaskTCBBuffer, /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
  1989. xCoreID );
  1990. if( xIdleTaskHandle[ xCoreID ] != NULL )
  1991. {
  1992. xReturn = pdPASS;
  1993. }
  1994. else
  1995. {
  1996. xReturn = pdFAIL;
  1997. }
  1998. }
  1999. #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
  2000. {
  2001. /* The Idle task is being created using dynamically allocated RAM. */
  2002. xReturn = xTaskCreatePinnedToCore( prvIdleTask,
  2003. #if ( configNUMBER_OF_CORES > 1 )
  2004. cIdleName,
  2005. #else /* #if ( configNUMBER_OF_CORES > 1 ) */
  2006. configIDLE_TASK_NAME,
  2007. #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
  2008. configMINIMAL_STACK_SIZE,
  2009. ( void * ) NULL,
  2010. portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
  2011. &xIdleTaskHandle[ xCoreID ], /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
  2012. xCoreID );
  2013. }
  2014. #endif /* configSUPPORT_STATIC_ALLOCATION */
  2015. }
  2016. return xReturn;
  2017. }
  2018. /*-----------------------------------------------------------*/
  2019. void vTaskStartScheduler( void )
  2020. {
  2021. BaseType_t xReturn;
  2022. /* The code for prvCreateIdleTasks() has been backported from the upstream
  2023. * FreeRTOS-Kernel source. The reference for the same is on the mainline
  2024. * at the commit id# 2f94b181a2f049ec342deba0927bed51f7174ab0. */
  2025. xReturn = prvCreateIdleTasks();
  2026. #if ( configUSE_TIMERS == 1 )
  2027. {
  2028. if( xReturn == pdPASS )
  2029. {
  2030. xReturn = xTimerCreateTimerTask();
  2031. }
  2032. else
  2033. {
  2034. mtCOVERAGE_TEST_MARKER();
  2035. }
  2036. }
  2037. #endif /* configUSE_TIMERS */
  2038. if( xReturn == pdPASS )
  2039. {
  2040. /* freertos_tasks_c_additions_init() should only be called if the user
  2041. * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
  2042. * the only macro called by the function. */
  2043. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  2044. {
  2045. freertos_tasks_c_additions_init();
  2046. }
  2047. #endif
  2048. /* Interrupts are turned off here, to ensure a tick does not occur
  2049. * before or during the call to xPortStartScheduler(). The stacks of
  2050. * the created tasks contain a status word with interrupts switched on
  2051. * so interrupts will automatically get re-enabled when the first task
  2052. * starts to run. */
  2053. portDISABLE_INTERRUPTS();
  2054. /* For SMP, we need to take the kernel lock here as we are about to
  2055. * access kernel data structures. */
  2056. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  2057. {
  2058. #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
  2059. {
  2060. /* Switch C-Runtime's TLS Block to point to the TLS
  2061. * block specific to the task that will run first. */
  2062. configSET_TLS_BLOCK( pxCurrentTCBs[ portGET_CORE_ID() ]->xTLSBlock );
  2063. }
  2064. #endif
  2065. xNextTaskUnblockTime = portMAX_DELAY;
  2066. xSchedulerRunning = pdTRUE;
  2067. xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
  2068. }
  2069. /* Release the previously taken kernel lock. */
  2070. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  2071. /* If configGENERATE_RUN_TIME_STATS is defined then the following
  2072. * macro must be defined to configure the timer/counter used to generate
  2073. * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
  2074. * is set to 0 and the following line fails to build then ensure you do not
  2075. * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
  2076. * FreeRTOSConfig.h file. */
  2077. portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
  2078. traceTASK_SWITCHED_IN();
  2079. /* Setting up the timer tick is hardware specific and thus in the
  2080. * portable interface. */
  2081. xPortStartScheduler();
  2082. /* In most cases, xPortStartScheduler() will not return. If it
  2083. * returns pdTRUE then there was not enough heap memory available
  2084. * to create either the Idle or the Timer task. If it returned
  2085. * pdFALSE, then the application called xTaskEndScheduler().
  2086. * Most ports don't implement xTaskEndScheduler() as there is
  2087. * nothing to return to. */
  2088. }
  2089. else
  2090. {
  2091. /* This line will only be reached if the kernel could not be started,
  2092. * because there was not enough FreeRTOS heap to create the idle task
  2093. * or the timer task. */
  2094. configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
  2095. }
  2096. /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
  2097. * meaning xIdleTaskHandle is not used anywhere else. */
  2098. ( void ) xIdleTaskHandle[ 0 ];
  2099. /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
  2100. * from getting optimized out as it is no longer used by the kernel. */
  2101. ( void ) uxTopUsedPriority;
  2102. }
  2103. /*-----------------------------------------------------------*/
  2104. void vTaskEndScheduler( void )
  2105. {
  2106. /* Stop the scheduler interrupts and call the portable scheduler end
  2107. * routine so the original ISRs can be restored if necessary. The port
  2108. * layer must ensure interrupts enable bit is left in the correct state. */
  2109. portDISABLE_INTERRUPTS();
  2110. /* For SMP, we need to take the kernel lock here as we are about to access
  2111. * kernel data structures. */
  2112. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  2113. {
  2114. xSchedulerRunning = pdFALSE;
  2115. }
  2116. /* Release the previously taken kernel lock. */
  2117. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  2118. vPortEndScheduler();
  2119. }
  2120. /*----------------------------------------------------------*/
  2121. void vTaskSuspendAll( void )
  2122. {
  2123. /* A critical section is not required as the variable is of type
  2124. * BaseType_t. Please read Richard Barry's reply in the following link to a
  2125. * post in the FreeRTOS support forum before reporting this as a bug! -
  2126. * https://goo.gl/wu4acr */
  2127. /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
  2128. * do not otherwise exhibit real time behaviour. */
  2129. portSOFTWARE_BARRIER();
  2130. /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
  2131. * is used to allow calls to vTaskSuspendAll() to nest. */
  2132. ++uxSchedulerSuspended[ portGET_CORE_ID() ];
  2133. /* Enforces ordering for ports and optimised compilers that may otherwise place
  2134. * the above increment elsewhere. */
  2135. portMEMORY_BARRIER();
  2136. }
  2137. /*----------------------------------------------------------*/
  2138. #if ( configUSE_TICKLESS_IDLE != 0 )
  2139. static TickType_t prvGetExpectedIdleTime( void )
  2140. {
  2141. TickType_t xReturn;
  2142. UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
  2143. /* uxHigherPriorityReadyTasks takes care of the case where
  2144. * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
  2145. * task that are in the Ready state, even though the idle task is
  2146. * running. */
  2147. #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
  2148. {
  2149. if( uxTopReadyPriority > tskIDLE_PRIORITY )
  2150. {
  2151. uxHigherPriorityReadyTasks = pdTRUE;
  2152. }
  2153. }
  2154. #else
  2155. {
  2156. const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
  2157. /* When port optimised task selection is used the uxTopReadyPriority
  2158. * variable is used as a bit map. If bits other than the least
  2159. * significant bit are set then there are tasks that have a priority
  2160. * above the idle priority that are in the Ready state. This takes
  2161. * care of the case where the co-operative scheduler is in use. */
  2162. if( uxTopReadyPriority > uxLeastSignificantBit )
  2163. {
  2164. uxHigherPriorityReadyTasks = pdTRUE;
  2165. }
  2166. }
  2167. #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
  2168. if( pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority > tskIDLE_PRIORITY )
  2169. {
  2170. xReturn = 0;
  2171. }
  2172. else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > configNUMBER_OF_CORES )
  2173. {
  2174. /* There are other idle priority tasks in the ready state. If
  2175. * time slicing is used then the very next tick interrupt must be
  2176. * processed. */
  2177. xReturn = 0;
  2178. }
  2179. else if( uxHigherPriorityReadyTasks != pdFALSE )
  2180. {
  2181. /* There are tasks in the Ready state that have a priority above the
  2182. * idle priority. This path can only be reached if
  2183. * configUSE_PREEMPTION is 0. */
  2184. xReturn = 0;
  2185. }
  2186. else
  2187. {
  2188. xReturn = xNextTaskUnblockTime - xTickCount;
  2189. }
  2190. return xReturn;
  2191. }
  2192. #endif /* configUSE_TICKLESS_IDLE */
  2193. /*----------------------------------------------------------*/
  2194. BaseType_t xTaskResumeAll( void )
  2195. {
  2196. TCB_t * pxTCB = NULL;
  2197. BaseType_t xAlreadyYielded = pdFALSE;
  2198. /* If uxSchedulerSuspended is zero then this function does not match a
  2199. * previous call to vTaskSuspendAll(). */
  2200. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdTRUE );
  2201. /* It is possible that an ISR caused a task to be removed from an event
  2202. * list while the scheduler was suspended. If this was the case then the
  2203. * removed task will have been added to the xPendingReadyList. Once the
  2204. * scheduler has been resumed it is safe to move all the pending ready
  2205. * tasks from this list into their appropriate ready list. */
  2206. taskENTER_CRITICAL( &xKernelLock );
  2207. {
  2208. /* Get current core ID as we can no longer be preempted. */
  2209. const BaseType_t xCurCoreID = portGET_CORE_ID();
  2210. --uxSchedulerSuspended[ xCurCoreID ];
  2211. if( uxSchedulerSuspended[ xCurCoreID ] == ( UBaseType_t ) pdFALSE )
  2212. {
  2213. if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
  2214. {
  2215. /* Move any readied tasks from the pending list into the
  2216. * appropriate ready list. */
  2217. while( listLIST_IS_EMPTY( &xPendingReadyList[ xCurCoreID ] ) == pdFALSE )
  2218. {
  2219. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList[ xCurCoreID ] ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  2220. listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
  2221. portMEMORY_BARRIER();
  2222. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  2223. prvAddTaskToReadyList( pxTCB );
  2224. /* If the moved task has a priority higher than or equal to
  2225. * the current task then a yield must be performed. */
  2226. if( taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, pdTRUE ) == pdTRUE )
  2227. {
  2228. xYieldPending[ xCurCoreID ] = pdTRUE;
  2229. }
  2230. else
  2231. {
  2232. mtCOVERAGE_TEST_MARKER();
  2233. }
  2234. }
  2235. if( pxTCB != NULL )
  2236. {
  2237. /* A task was unblocked while the scheduler was suspended,
  2238. * which may have prevented the next unblock time from being
  2239. * re-calculated, in which case re-calculate it now. Mainly
  2240. * important for low power tickless implementations, where
  2241. * this can prevent an unnecessary exit from low power
  2242. * state. */
  2243. prvResetNextTaskUnblockTime();
  2244. }
  2245. #if ( configNUMBER_OF_CORES > 1 )
  2246. /* Core 0 is solely responsible for managing tick count, thus it
  2247. * must be the only core to unwind the pended ticks */
  2248. if( xCurCoreID == 0 )
  2249. #endif
  2250. /* If any ticks occurred while the scheduler was suspended then
  2251. * they should be processed now. This ensures the tick count does
  2252. * not slip, and that any delayed tasks are resumed at the correct
  2253. * time. */
  2254. {
  2255. TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
  2256. if( xPendedCounts > ( TickType_t ) 0U )
  2257. {
  2258. do
  2259. {
  2260. if( xTaskIncrementTick() != pdFALSE )
  2261. {
  2262. xYieldPending[ xCurCoreID ] = pdTRUE;
  2263. }
  2264. else
  2265. {
  2266. mtCOVERAGE_TEST_MARKER();
  2267. }
  2268. --xPendedCounts;
  2269. } while( xPendedCounts > ( TickType_t ) 0U );
  2270. xPendedTicks = 0;
  2271. }
  2272. else
  2273. {
  2274. mtCOVERAGE_TEST_MARKER();
  2275. }
  2276. }
  2277. if( xYieldPending[ xCurCoreID ] != pdFALSE )
  2278. {
  2279. #if ( configUSE_PREEMPTION != 0 )
  2280. {
  2281. xAlreadyYielded = pdTRUE;
  2282. }
  2283. #endif
  2284. taskYIELD_IF_USING_PREEMPTION();
  2285. }
  2286. else
  2287. {
  2288. mtCOVERAGE_TEST_MARKER();
  2289. }
  2290. }
  2291. }
  2292. else
  2293. {
  2294. mtCOVERAGE_TEST_MARKER();
  2295. }
  2296. }
  2297. taskEXIT_CRITICAL( &xKernelLock );
  2298. return xAlreadyYielded;
  2299. }
  2300. /*-----------------------------------------------------------*/
  2301. TickType_t xTaskGetTickCount( void )
  2302. {
  2303. TickType_t xTicks;
  2304. /* Critical section required if running on a 16 bit processor. */
  2305. portTICK_TYPE_ENTER_CRITICAL();
  2306. {
  2307. xTicks = xTickCount;
  2308. }
  2309. portTICK_TYPE_EXIT_CRITICAL();
  2310. return xTicks;
  2311. }
  2312. /*-----------------------------------------------------------*/
  2313. TickType_t xTaskGetTickCountFromISR( void )
  2314. {
  2315. TickType_t xReturn;
  2316. UBaseType_t uxSavedInterruptStatus;
  2317. /* RTOS ports that support interrupt nesting have the concept of a maximum
  2318. * system call (or maximum API call) interrupt priority. Interrupts that are
  2319. * above the maximum system call priority are kept permanently enabled, even
  2320. * when the RTOS kernel is in a critical section, but cannot make any calls to
  2321. * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
  2322. * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  2323. * failure if a FreeRTOS API function is called from an interrupt that has been
  2324. * assigned a priority above the configured maximum system call priority.
  2325. * Only FreeRTOS functions that end in FromISR can be called from interrupts
  2326. * that have been assigned a priority at or (logically) below the maximum
  2327. * system call interrupt priority. FreeRTOS maintains a separate interrupt
  2328. * safe API to ensure interrupt entry is as fast and as simple as possible.
  2329. * More information (albeit Cortex-M specific) is provided on the following
  2330. * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  2331. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  2332. /* For SMP, we need to take the kernel lock here as we are about to access
  2333. * kernel data structures. */
  2334. prvENTER_CRITICAL_ISR_SMP_ONLY( &xKernelLock );
  2335. {
  2336. uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
  2337. {
  2338. xReturn = xTickCount;
  2339. }
  2340. portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
  2341. }
  2342. /* Release the previously taken kernel lock. */
  2343. prvEXIT_CRITICAL_ISR_SMP_ONLY( &xKernelLock );
  2344. return xReturn;
  2345. }
  2346. /*-----------------------------------------------------------*/
  2347. UBaseType_t uxTaskGetNumberOfTasks( void )
  2348. {
  2349. /* A critical section is not required because the variables are of type
  2350. * BaseType_t. */
  2351. return uxCurrentNumberOfTasks;
  2352. }
  2353. /*-----------------------------------------------------------*/
  2354. char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2355. {
  2356. TCB_t * pxTCB;
  2357. /* If null is passed in here then the name of the calling task is being
  2358. * queried. */
  2359. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  2360. configASSERT( pxTCB );
  2361. return &( pxTCB->pcTaskName[ 0 ] );
  2362. }
  2363. /*-----------------------------------------------------------*/
  2364. #if ( INCLUDE_xTaskGetHandle == 1 )
  2365. static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
  2366. const char pcNameToQuery[] )
  2367. {
  2368. TCB_t * pxNextTCB;
  2369. TCB_t * pxFirstTCB;
  2370. TCB_t * pxReturn = NULL;
  2371. UBaseType_t x;
  2372. char cNextChar;
  2373. BaseType_t xBreakLoop;
  2374. /* This function is called with the scheduler suspended. */
  2375. if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
  2376. {
  2377. listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  2378. do
  2379. {
  2380. listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  2381. /* Check each character in the name looking for a match or
  2382. * mismatch. */
  2383. xBreakLoop = pdFALSE;
  2384. for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
  2385. {
  2386. cNextChar = pxNextTCB->pcTaskName[ x ];
  2387. if( cNextChar != pcNameToQuery[ x ] )
  2388. {
  2389. /* Characters didn't match. */
  2390. xBreakLoop = pdTRUE;
  2391. }
  2392. else if( cNextChar == ( char ) 0x00 )
  2393. {
  2394. /* Both strings terminated, a match must have been
  2395. * found. */
  2396. pxReturn = pxNextTCB;
  2397. xBreakLoop = pdTRUE;
  2398. }
  2399. else
  2400. {
  2401. mtCOVERAGE_TEST_MARKER();
  2402. }
  2403. if( xBreakLoop != pdFALSE )
  2404. {
  2405. break;
  2406. }
  2407. }
  2408. if( pxReturn != NULL )
  2409. {
  2410. /* The handle has been found. */
  2411. break;
  2412. }
  2413. } while( pxNextTCB != pxFirstTCB );
  2414. }
  2415. else
  2416. {
  2417. mtCOVERAGE_TEST_MARKER();
  2418. }
  2419. return pxReturn;
  2420. }
  2421. #endif /* INCLUDE_xTaskGetHandle */
  2422. /*-----------------------------------------------------------*/
  2423. #if ( INCLUDE_xTaskGetHandle == 1 )
  2424. TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
  2425. {
  2426. UBaseType_t uxQueue = configMAX_PRIORITIES;
  2427. TCB_t * pxTCB;
  2428. /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
  2429. configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
  2430. prvENTER_CRITICAL_OR_SUSPEND_ALL( &xKernelLock );
  2431. {
  2432. /* Search the ready lists. */
  2433. do
  2434. {
  2435. uxQueue--;
  2436. pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
  2437. if( pxTCB != NULL )
  2438. {
  2439. /* Found the handle. */
  2440. break;
  2441. }
  2442. } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2443. /* Search the delayed lists. */
  2444. if( pxTCB == NULL )
  2445. {
  2446. pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
  2447. }
  2448. if( pxTCB == NULL )
  2449. {
  2450. pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
  2451. }
  2452. #if ( INCLUDE_vTaskSuspend == 1 )
  2453. {
  2454. if( pxTCB == NULL )
  2455. {
  2456. /* Search the suspended list. */
  2457. pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
  2458. }
  2459. }
  2460. #endif
  2461. #if ( INCLUDE_vTaskDelete == 1 )
  2462. {
  2463. if( pxTCB == NULL )
  2464. {
  2465. /* Search the deleted list. */
  2466. pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
  2467. }
  2468. }
  2469. #endif
  2470. }
  2471. ( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &xKernelLock );
  2472. return pxTCB;
  2473. }
  2474. #endif /* INCLUDE_xTaskGetHandle */
  2475. /*-----------------------------------------------------------*/
  2476. #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
  2477. BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
  2478. StackType_t ** ppuxStackBuffer,
  2479. StaticTask_t ** ppxTaskBuffer )
  2480. {
  2481. BaseType_t xReturn;
  2482. TCB_t * pxTCB;
  2483. configASSERT( ppuxStackBuffer != NULL );
  2484. configASSERT( ppxTaskBuffer != NULL );
  2485. pxTCB = prvGetTCBFromHandle( xTask );
  2486. #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
  2487. {
  2488. if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
  2489. {
  2490. *ppuxStackBuffer = pxTCB->pxStack;
  2491. *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
  2492. xReturn = pdTRUE;
  2493. }
  2494. else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
  2495. {
  2496. *ppuxStackBuffer = pxTCB->pxStack;
  2497. *ppxTaskBuffer = NULL;
  2498. xReturn = pdTRUE;
  2499. }
  2500. else
  2501. {
  2502. xReturn = pdFALSE;
  2503. }
  2504. }
  2505. #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
  2506. {
  2507. *ppuxStackBuffer = pxTCB->pxStack;
  2508. *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
  2509. xReturn = pdTRUE;
  2510. }
  2511. #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
  2512. return xReturn;
  2513. }
  2514. #endif /* configSUPPORT_STATIC_ALLOCATION */
  2515. /*-----------------------------------------------------------*/
  2516. #if ( configUSE_TRACE_FACILITY == 1 )
  2517. UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
  2518. const UBaseType_t uxArraySize,
  2519. configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
  2520. {
  2521. UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
  2522. prvENTER_CRITICAL_OR_SUSPEND_ALL( &xKernelLock );
  2523. {
  2524. /* Is there a space in the array for each task in the system? */
  2525. if( uxArraySize >= uxCurrentNumberOfTasks )
  2526. {
  2527. /* Fill in an TaskStatus_t structure with information on each
  2528. * task in the Ready state. */
  2529. do
  2530. {
  2531. uxQueue--;
  2532. uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
  2533. } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2534. /* Fill in an TaskStatus_t structure with information on each
  2535. * task in the Blocked state. */
  2536. uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
  2537. uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
  2538. #if ( INCLUDE_vTaskDelete == 1 )
  2539. {
  2540. /* Fill in an TaskStatus_t structure with information on
  2541. * each task that has been deleted but not yet cleaned up. */
  2542. uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
  2543. }
  2544. #endif
  2545. #if ( INCLUDE_vTaskSuspend == 1 )
  2546. {
  2547. /* Fill in an TaskStatus_t structure with information on
  2548. * each task in the Suspended state. */
  2549. uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
  2550. }
  2551. #endif
  2552. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  2553. {
  2554. if( pulTotalRunTime != NULL )
  2555. {
  2556. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  2557. portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
  2558. #else
  2559. *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
  2560. #endif
  2561. }
  2562. }
  2563. #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  2564. {
  2565. if( pulTotalRunTime != NULL )
  2566. {
  2567. *pulTotalRunTime = 0;
  2568. }
  2569. }
  2570. #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
  2571. }
  2572. else
  2573. {
  2574. mtCOVERAGE_TEST_MARKER();
  2575. }
  2576. }
  2577. ( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &xKernelLock );
  2578. return uxTask;
  2579. }
  2580. #endif /* configUSE_TRACE_FACILITY */
  2581. /*----------------------------------------------------------*/
  2582. #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
  2583. TaskHandle_t xTaskGetIdleTaskHandle( void )
  2584. {
  2585. return xTaskGetIdleTaskHandleForCore( portGET_CORE_ID() );
  2586. }
  2587. #endif /* INCLUDE_xTaskGetIdleTaskHandle */
  2588. /*----------------------------------------------------------*/
  2589. /* This conditional compilation should use inequality to 0, not equality to 1.
  2590. * This is to ensure vTaskStepTick() is available when user defined low power mode
  2591. * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
  2592. * 1. */
  2593. #if ( configUSE_TICKLESS_IDLE != 0 )
  2594. void vTaskStepTick( TickType_t xTicksToJump )
  2595. {
  2596. /* SINGLE-CORE MODIFICATION: Expanded critical section so that SMP
  2597. * accesses xTickCount inside a critical section. */
  2598. taskENTER_CRITICAL( &xKernelLock );
  2599. {
  2600. /* Correct the tick count value after a period during which the tick
  2601. * was suppressed. Note this does *not* call the tick hook function for
  2602. * each stepped tick. */
  2603. configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
  2604. if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
  2605. {
  2606. /* Arrange for xTickCount to reach xNextTaskUnblockTime in
  2607. * xTaskIncrementTick() when the scheduler resumes. This ensures
  2608. * that any delayed tasks are resumed at the correct time. */
  2609. #if ( configNUMBER_OF_CORES > 1 )
  2610. {
  2611. /* In SMP, the entire tickless idle handling block
  2612. * is replaced with a critical section, taking the kernel lock. */
  2613. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdFALSE );
  2614. }
  2615. #else /* configNUMBER_OF_CORES > 1 */
  2616. {
  2617. /* In single-core, the entire tickless idle handling block
  2618. * is done with scheduler suspended. */
  2619. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdTRUE );
  2620. }
  2621. #endif /* configNUMBER_OF_CORES > 1 */
  2622. configASSERT( xTicksToJump != ( TickType_t ) 0 );
  2623. xPendedTicks++;
  2624. xTicksToJump--;
  2625. }
  2626. else
  2627. {
  2628. mtCOVERAGE_TEST_MARKER();
  2629. }
  2630. xTickCount += xTicksToJump;
  2631. traceINCREASE_TICK_COUNT( xTicksToJump );
  2632. }
  2633. /* SINGLE-CORE MODIFICATION: Expanded critical section */
  2634. taskEXIT_CRITICAL( &xKernelLock );
  2635. }
  2636. #endif /* configUSE_TICKLESS_IDLE */
  2637. /*----------------------------------------------------------*/
  2638. BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
  2639. {
  2640. BaseType_t xYieldOccurred;
  2641. /* Must not be called with the scheduler suspended as the implementation
  2642. * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
  2643. configASSERT( taskIS_SCHEDULER_SUSPENDED() == pdFALSE );
  2644. /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
  2645. * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
  2646. vTaskSuspendAll();
  2647. /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
  2648. taskENTER_CRITICAL( &xKernelLock );
  2649. {
  2650. xPendedTicks += xTicksToCatchUp;
  2651. }
  2652. taskEXIT_CRITICAL( &xKernelLock );
  2653. xYieldOccurred = xTaskResumeAll();
  2654. return xYieldOccurred;
  2655. }
  2656. /*----------------------------------------------------------*/
  2657. #if ( INCLUDE_xTaskAbortDelay == 1 )
  2658. BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
  2659. {
  2660. TCB_t * pxTCB = xTask;
  2661. BaseType_t xReturn;
  2662. configASSERT( pxTCB );
  2663. prvENTER_CRITICAL_OR_SUSPEND_ALL( &xKernelLock );
  2664. {
  2665. /* A task can only be prematurely removed from the Blocked state if
  2666. * it is actually in the Blocked state. */
  2667. if( eTaskGetState( xTask ) == eBlocked )
  2668. {
  2669. xReturn = pdPASS;
  2670. /* Remove the reference to the task from the blocked list. An
  2671. * interrupt won't touch the xStateListItem because the
  2672. * scheduler is suspended. */
  2673. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  2674. /* Is the task waiting on an event also? If so remove it from
  2675. * the event list too. Interrupts can touch the event list item,
  2676. * even though the scheduler is suspended, so a critical section
  2677. * is used. */
  2678. prvENTER_CRITICAL_SC_ONLY( &xKernelLock );
  2679. {
  2680. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  2681. {
  2682. ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
  2683. /* This lets the task know it was forcibly removed from the
  2684. * blocked state so it should not re-evaluate its block time and
  2685. * then block again. */
  2686. pxTCB->ucDelayAborted = pdTRUE;
  2687. }
  2688. else
  2689. {
  2690. mtCOVERAGE_TEST_MARKER();
  2691. }
  2692. }
  2693. prvEXIT_CRITICAL_SC_ONLY( &xKernelLock );
  2694. /* Place the unblocked task into the appropriate ready list. */
  2695. prvAddTaskToReadyList( pxTCB );
  2696. /* A task being unblocked cannot cause an immediate context
  2697. * switch if preemption is turned off. */
  2698. #if ( configUSE_PREEMPTION == 1 )
  2699. {
  2700. /* Get current core ID as we can no longer be preempted. */
  2701. const BaseType_t xCurCoreID = portGET_CORE_ID();
  2702. /* Preemption is on, but a context switch should only be
  2703. * performed if the unblocked task has a priority that is
  2704. * higher than the currently executing task. */
  2705. if( taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, pdFALSE ) == pdTRUE )
  2706. {
  2707. /* Pend the yield to be performed when the scheduler
  2708. * is unsuspended. */
  2709. xYieldPending[ xCurCoreID ] = pdTRUE;
  2710. }
  2711. else
  2712. {
  2713. mtCOVERAGE_TEST_MARKER();
  2714. }
  2715. }
  2716. #endif /* configUSE_PREEMPTION */
  2717. }
  2718. else
  2719. {
  2720. xReturn = pdFAIL;
  2721. }
  2722. }
  2723. ( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &xKernelLock );
  2724. return xReturn;
  2725. }
  2726. #endif /* INCLUDE_xTaskAbortDelay */
  2727. /*----------------------------------------------------------*/
  2728. BaseType_t xTaskIncrementTick( void )
  2729. {
  2730. #if ( configNUMBER_OF_CORES > 1 )
  2731. /* Only Core 0 should ever call this function. */
  2732. configASSERT( portGET_CORE_ID() == 0 );
  2733. #endif /* configNUMBER_OF_CORES > 1 */
  2734. TCB_t * pxTCB;
  2735. TickType_t xItemValue;
  2736. BaseType_t xSwitchRequired = pdFALSE;
  2737. #if ( configUSE_TICK_HOOK == 1 )
  2738. BaseType_t xCallTickHook;
  2739. #endif /* configUSE_TICK_HOOK == 1 */
  2740. /* Called by the portable layer each time a tick interrupt occurs.
  2741. * Increments the tick then checks to see if the new tick value will cause any
  2742. * tasks to be unblocked. */
  2743. traceTASK_INCREMENT_TICK( xTickCount );
  2744. /* For SMP, we need to take the kernel lock here as we are about to access
  2745. * kernel data structures (unlike single core which calls this function with
  2746. * interrupts disabled). */
  2747. prvENTER_CRITICAL_SAFE_SMP_ONLY( &xKernelLock );
  2748. {
  2749. if( uxSchedulerSuspended[ 0 ] == ( UBaseType_t ) pdFALSE )
  2750. {
  2751. /* Minor optimisation. The tick count cannot change in this
  2752. * block. */
  2753. const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
  2754. /* Increment the RTOS tick, switching the delayed and overflowed
  2755. * delayed lists if it wraps to 0. */
  2756. xTickCount = xConstTickCount;
  2757. if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
  2758. {
  2759. taskSWITCH_DELAYED_LISTS();
  2760. }
  2761. else
  2762. {
  2763. mtCOVERAGE_TEST_MARKER();
  2764. }
  2765. /* See if this tick has made a timeout expire. Tasks are stored in
  2766. * the queue in the order of their wake time - meaning once one task
  2767. * has been found whose block time has not expired there is no need to
  2768. * look any further down the list. */
  2769. if( xConstTickCount >= xNextTaskUnblockTime )
  2770. {
  2771. for( ; ; )
  2772. {
  2773. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  2774. {
  2775. /* The delayed list is empty. Set xNextTaskUnblockTime
  2776. * to the maximum possible value so it is extremely
  2777. * unlikely that the
  2778. * if( xTickCount >= xNextTaskUnblockTime ) test will pass
  2779. * next time through. */
  2780. xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  2781. break;
  2782. }
  2783. else
  2784. {
  2785. /* The delayed list is not empty, get the value of the
  2786. * item at the head of the delayed list. This is the time
  2787. * at which the task at the head of the delayed list must
  2788. * be removed from the Blocked state. */
  2789. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  2790. xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
  2791. if( xConstTickCount < xItemValue )
  2792. {
  2793. /* It is not time to unblock this item yet, but the
  2794. * item value is the time at which the task at the head
  2795. * of the blocked list must be removed from the Blocked
  2796. * state - so record the item value in
  2797. * xNextTaskUnblockTime. */
  2798. xNextTaskUnblockTime = xItemValue;
  2799. break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
  2800. }
  2801. else
  2802. {
  2803. mtCOVERAGE_TEST_MARKER();
  2804. }
  2805. /* It is time to remove the item from the Blocked state. */
  2806. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  2807. /* Is the task waiting on an event also? If so remove
  2808. * it from the event list. */
  2809. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  2810. {
  2811. listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
  2812. }
  2813. else
  2814. {
  2815. mtCOVERAGE_TEST_MARKER();
  2816. }
  2817. /* Place the unblocked task into the appropriate ready
  2818. * list. */
  2819. prvAddTaskToReadyList( pxTCB );
  2820. /* A task being unblocked cannot cause an immediate
  2821. * context switch if preemption is turned off. */
  2822. #if ( configUSE_PREEMPTION == 1 )
  2823. {
  2824. /* Preemption is on, but a context switch should
  2825. * only be performed if the unblocked task has a
  2826. * priority that is equal to or higher than the
  2827. * currently executing task.
  2828. *
  2829. * For SMP, since this function is only run on core
  2830. * 0, we only need to context switch if the unblocked
  2831. * task can run on core 0 and has a higher priority
  2832. * than the current task. */
  2833. if( ( taskIS_AFFINITY_COMPATIBLE( 0, pxTCB->xCoreID ) == pdTRUE ) && ( pxTCB->uxPriority > pxCurrentTCBs[ 0 ]->uxPriority ) )
  2834. {
  2835. xSwitchRequired = pdTRUE;
  2836. }
  2837. else
  2838. {
  2839. mtCOVERAGE_TEST_MARKER();
  2840. }
  2841. }
  2842. #endif /* configUSE_PREEMPTION */
  2843. }
  2844. }
  2845. }
  2846. /* Tasks of equal priority to the currently running task will share
  2847. * processing time (time slice) if preemption is on, and the application
  2848. * writer has not explicitly turned time slicing off. */
  2849. #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
  2850. {
  2851. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ 0 ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
  2852. {
  2853. xSwitchRequired = pdTRUE;
  2854. }
  2855. else
  2856. {
  2857. mtCOVERAGE_TEST_MARKER();
  2858. }
  2859. }
  2860. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
  2861. #if ( configUSE_TICK_HOOK == 1 )
  2862. {
  2863. /* Guard against the tick hook being called when the pended tick
  2864. * count is being unwound (when the scheduler is being unlocked). */
  2865. if( xPendedTicksTemp == ( TickType_t ) 0 )
  2866. {
  2867. xCallTickHook = pdTRUE;
  2868. }
  2869. else
  2870. {
  2871. xCallTickHook = pdFALSE;
  2872. }
  2873. }
  2874. #endif /* configUSE_TICK_HOOK */
  2875. #if ( configUSE_PREEMPTION == 1 )
  2876. {
  2877. if( xYieldPending[ 0 ] != pdFALSE )
  2878. {
  2879. xSwitchRequired = pdTRUE;
  2880. }
  2881. else
  2882. {
  2883. mtCOVERAGE_TEST_MARKER();
  2884. }
  2885. }
  2886. #endif /* configUSE_PREEMPTION */
  2887. }
  2888. else
  2889. {
  2890. ++xPendedTicks;
  2891. /* The tick hook gets called at regular intervals, even if the
  2892. * scheduler is locked. */
  2893. #if ( configUSE_TICK_HOOK == 1 )
  2894. {
  2895. xCallTickHook = pdTRUE;
  2896. }
  2897. #endif
  2898. }
  2899. }
  2900. /* Release the previously taken kernel lock as we have finished accessing
  2901. * the kernel data structures. */
  2902. prvEXIT_CRITICAL_SAFE_SMP_ONLY( &xKernelLock );
  2903. #if ( configUSE_TICK_HOOK == 1 )
  2904. {
  2905. if( xCallTickHook == pdTRUE )
  2906. {
  2907. vApplicationTickHook();
  2908. }
  2909. }
  2910. #endif
  2911. return xSwitchRequired;
  2912. }
  2913. /*-----------------------------------------------------------*/
  2914. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  2915. void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
  2916. TaskHookFunction_t pxHookFunction )
  2917. {
  2918. TCB_t * xTCB;
  2919. /* If xTask is NULL then it is the task hook of the calling task that is
  2920. * getting set. */
  2921. if( xTask == NULL )
  2922. {
  2923. xTCB = ( TCB_t * ) xTaskGetCurrentTaskHandle();
  2924. }
  2925. else
  2926. {
  2927. xTCB = xTask;
  2928. }
  2929. /* Save the hook function in the TCB. A critical section is required as
  2930. * the value can be accessed from an interrupt. */
  2931. taskENTER_CRITICAL( &xKernelLock );
  2932. {
  2933. xTCB->pxTaskTag = pxHookFunction;
  2934. }
  2935. taskEXIT_CRITICAL( &xKernelLock );
  2936. }
  2937. #endif /* configUSE_APPLICATION_TASK_TAG */
  2938. /*-----------------------------------------------------------*/
  2939. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  2940. TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
  2941. {
  2942. TCB_t * pxTCB;
  2943. TaskHookFunction_t xReturn;
  2944. /* If xTask is NULL then set the calling task's hook. */
  2945. pxTCB = prvGetTCBFromHandle( xTask );
  2946. /* Save the hook function in the TCB. A critical section is required as
  2947. * the value can be accessed from an interrupt. */
  2948. taskENTER_CRITICAL( &xKernelLock );
  2949. {
  2950. xReturn = pxTCB->pxTaskTag;
  2951. }
  2952. taskEXIT_CRITICAL( &xKernelLock );
  2953. return xReturn;
  2954. }
  2955. #endif /* configUSE_APPLICATION_TASK_TAG */
  2956. /*-----------------------------------------------------------*/
  2957. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  2958. TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
  2959. {
  2960. TCB_t * pxTCB;
  2961. TaskHookFunction_t xReturn;
  2962. UBaseType_t uxSavedInterruptStatus;
  2963. /* If xTask is NULL then set the calling task's hook. */
  2964. pxTCB = prvGetTCBFromHandle( xTask );
  2965. /* Save the hook function in the TCB. A critical section is required as
  2966. * the value can be accessed from an interrupt. */
  2967. prvENTER_CRITICAL_OR_MASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  2968. {
  2969. xReturn = pxTCB->pxTaskTag;
  2970. }
  2971. prvEXIT_CRITICAL_OR_UNMASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  2972. return xReturn;
  2973. }
  2974. #endif /* configUSE_APPLICATION_TASK_TAG */
  2975. /*-----------------------------------------------------------*/
  2976. #if ( configUSE_APPLICATION_TASK_TAG == 1 )
  2977. BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
  2978. void * pvParameter )
  2979. {
  2980. TCB_t * xTCB;
  2981. BaseType_t xReturn;
  2982. /* If xTask is NULL then we are calling our own task hook. */
  2983. if( xTask == NULL )
  2984. {
  2985. xTCB = xTaskGetCurrentTaskHandle();
  2986. }
  2987. else
  2988. {
  2989. xTCB = xTask;
  2990. }
  2991. if( xTCB->pxTaskTag != NULL )
  2992. {
  2993. xReturn = xTCB->pxTaskTag( pvParameter );
  2994. }
  2995. else
  2996. {
  2997. xReturn = pdFAIL;
  2998. }
  2999. return xReturn;
  3000. }
  3001. #endif /* configUSE_APPLICATION_TASK_TAG */
  3002. /*-----------------------------------------------------------*/
  3003. #if ( configNUMBER_OF_CORES > 1 )
  3004. static void prvSelectHighestPriorityTaskSMP( void )
  3005. {
  3006. /* This function is called from a critical section. So some optimizations are made */
  3007. BaseType_t uxCurPriority;
  3008. BaseType_t xTaskScheduled = pdFALSE;
  3009. BaseType_t xNewTopPrioritySet = pdFALSE;
  3010. BaseType_t xCurCoreID = portGET_CORE_ID();
  3011. /* Search for tasks, starting form the highest ready priority. If nothing is
  3012. * found, we eventually default to the IDLE tasks at priority 0 */
  3013. for( uxCurPriority = uxTopReadyPriority; uxCurPriority >= 0 && xTaskScheduled == pdFALSE; uxCurPriority-- )
  3014. {
  3015. /* Check if current priority has one or more ready tasks. Skip if none */
  3016. if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurPriority ] ) ) )
  3017. {
  3018. continue;
  3019. }
  3020. /* Save a copy of highest priority that has a ready state task */
  3021. if( xNewTopPrioritySet == pdFALSE )
  3022. {
  3023. xNewTopPrioritySet = pdTRUE;
  3024. uxTopReadyPriority = uxCurPriority;
  3025. }
  3026. /* We now search this priority's ready task list for a runnable task.
  3027. * We always start searching from the head of the list, so we reset
  3028. * pxIndex to point to the tail so that we start walking the list from
  3029. * the first item */
  3030. pxReadyTasksLists[ uxCurPriority ].pxIndex = ( ListItem_t * ) &( pxReadyTasksLists[ uxCurPriority ].xListEnd );
  3031. /* Get the first item on the list */
  3032. TCB_t * pxTCBCur;
  3033. TCB_t * pxTCBFirst;
  3034. listGET_OWNER_OF_NEXT_ENTRY( pxTCBCur, &( pxReadyTasksLists[ uxCurPriority ] ) );
  3035. pxTCBFirst = pxTCBCur;
  3036. do
  3037. {
  3038. /* Check if the current task is currently being executed. However, if
  3039. * it's being executed by the current core, we can still schedule it.
  3040. * Todo: Each task can store a xTaskRunState, instead of needing to
  3041. * check each core */
  3042. UBaseType_t x;
  3043. for( x = 0; x < configNUMBER_OF_CORES; x++ )
  3044. {
  3045. if( x == xCurCoreID )
  3046. {
  3047. continue;
  3048. }
  3049. else if( pxCurrentTCBs[ x ] == pxTCBCur )
  3050. {
  3051. /* Current task is already being executed. Get the next task */
  3052. goto get_next_task;
  3053. }
  3054. }
  3055. /* Check if the current task has a compatible affinity */
  3056. if( taskIS_AFFINITY_COMPATIBLE( xCurCoreID, pxTCBCur->xCoreID ) == pdFALSE )
  3057. {
  3058. goto get_next_task;
  3059. }
  3060. /* The current task is runnable. Schedule it */
  3061. pxCurrentTCBs[ xCurCoreID ] = pxTCBCur;
  3062. xTaskScheduled = pdTRUE;
  3063. /* Move the current tasks list item to the back of the list in order
  3064. * to implement best effort round robin. To do this, we need to reset
  3065. * the pxIndex to point to the tail again. */
  3066. pxReadyTasksLists[ uxCurPriority ].pxIndex = ( ListItem_t * ) &( pxReadyTasksLists[ uxCurPriority ].xListEnd );
  3067. listREMOVE_ITEM( &( pxTCBCur->xStateListItem ) );
  3068. listINSERT_END( &( pxReadyTasksLists[ uxCurPriority ] ), &( pxTCBCur->xStateListItem ) );
  3069. break;
  3070. get_next_task:
  3071. /* The current task cannot be scheduled. Get the next task in the list */
  3072. listGET_OWNER_OF_NEXT_ENTRY( pxTCBCur, &( pxReadyTasksLists[ uxCurPriority ] ) );
  3073. } while( pxTCBCur != pxTCBFirst ); /* Check to see if we've walked the entire list */
  3074. }
  3075. configASSERT( xTaskScheduled == pdTRUE ); /* At this point, a task MUST have been scheduled */
  3076. }
  3077. #endif /* configNUMBER_OF_CORES > 1 */
  3078. /*-----------------------------------------------------------*/
  3079. void vTaskSwitchContext( void )
  3080. {
  3081. /* For SMP, we need to take the kernel lock here as we are about to access
  3082. * kernel data structures (unlike single core which calls this function with
  3083. * either interrupts disabled or when the scheduler hasn't started yet). */
  3084. prvENTER_CRITICAL_SAFE_SMP_ONLY( &xKernelLock );
  3085. {
  3086. /* Get current core ID as we can no longer be preempted. */
  3087. const BaseType_t xCurCoreID = portGET_CORE_ID();
  3088. if( uxSchedulerSuspended[ xCurCoreID ] != ( UBaseType_t ) pdFALSE )
  3089. {
  3090. /* The scheduler is currently suspended - do not allow a context
  3091. * switch. */
  3092. xYieldPending[ xCurCoreID ] = pdTRUE;
  3093. }
  3094. else
  3095. {
  3096. xYieldPending[ xCurCoreID ] = pdFALSE;
  3097. traceTASK_SWITCHED_OUT();
  3098. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  3099. {
  3100. #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
  3101. portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
  3102. #else
  3103. ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
  3104. #endif
  3105. /* Add the amount of time the task has been running to the
  3106. * accumulated time so far. The time the task started running was
  3107. * stored in ulTaskSwitchedInTime. Note that there is no overflow
  3108. * protection here so count values are only valid until the timer
  3109. * overflows. The guard against negative values is to protect
  3110. * against suspect run time stat counter implementations - which
  3111. * are provided by the application, not the kernel. */
  3112. if( ulTotalRunTime > ulTaskSwitchedInTime[ xCurCoreID ] )
  3113. {
  3114. pxCurrentTCBs[ xCurCoreID ]->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime[ xCurCoreID ] );
  3115. }
  3116. else
  3117. {
  3118. mtCOVERAGE_TEST_MARKER();
  3119. }
  3120. ulTaskSwitchedInTime[ xCurCoreID ] = ulTotalRunTime;
  3121. }
  3122. #endif /* configGENERATE_RUN_TIME_STATS */
  3123. /* Check for stack overflow, if configured. */
  3124. taskCHECK_FOR_STACK_OVERFLOW( xCurCoreID );
  3125. /* Before the currently running task is switched out, save its errno. */
  3126. #if ( configUSE_POSIX_ERRNO == 1 )
  3127. {
  3128. pxCurrentTCBs[ xCurCoreID ]->iTaskErrno = FreeRTOS_errno;
  3129. }
  3130. #endif
  3131. /* Select a new task to run using either the generic C or port
  3132. * optimised asm code. */
  3133. taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  3134. traceTASK_SWITCHED_IN();
  3135. /* After the new task is switched in, update the global errno. */
  3136. #if ( configUSE_POSIX_ERRNO == 1 )
  3137. {
  3138. FreeRTOS_errno = pxCurrentTCBs[ xCurCoreID ]->iTaskErrno;
  3139. }
  3140. #endif
  3141. /* Wrap this call in a macro. IDF-8434 */
  3142. #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
  3143. {
  3144. vPortSetStackWatchpoint( pxCurrentTCBs[ xCurCoreID ]->pxStack );
  3145. }
  3146. #endif /* CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK */
  3147. #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
  3148. {
  3149. /* Switch C-Runtime's TLS Block to point to the TLS
  3150. * Block specific to this task. */
  3151. configSET_TLS_BLOCK( pxCurrentTCBs[ xCurCoreID ]->xTLSBlock );
  3152. }
  3153. #endif
  3154. }
  3155. }
  3156. /* Release the previously taken kernel lock as we have finished accessing
  3157. * the kernel data structures. */
  3158. prvEXIT_CRITICAL_SAFE_SMP_ONLY( &xKernelLock );
  3159. }
  3160. /*-----------------------------------------------------------*/
  3161. void vTaskPlaceOnEventList( List_t * const pxEventList,
  3162. const TickType_t xTicksToWait )
  3163. {
  3164. configASSERT( pxEventList );
  3165. /* IN SINGLE-CORE THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED
  3166. * OR THE SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. IN SMP
  3167. * THIS FUNCTION MUST BE CALLED WITH THE QUEUE'S xQueueLock TAKEN. */
  3168. /* For SMP, we need to take the kernel lock here as we are about to access
  3169. * kernel data structures. */
  3170. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  3171. {
  3172. /* Place the event list item of the TCB in the appropriate event list.
  3173. * This is placed in the list in priority order so the highest priority task
  3174. * is the first to be woken by the event.
  3175. *
  3176. * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
  3177. * Normally, the xItemValue of a TCB's ListItem_t members is:
  3178. * xItemValue = ( configMAX_PRIORITIES - uxPriority )
  3179. * Therefore, the event list is sorted in descending priority order.
  3180. *
  3181. * The queue that contains the event list is locked, preventing
  3182. * simultaneous access from interrupts. */
  3183. vListInsert( pxEventList, &( pxCurrentTCBs[ portGET_CORE_ID() ]->xEventListItem ) );
  3184. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  3185. }
  3186. /* Release the previously taken kernel lock. */
  3187. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  3188. }
  3189. /*-----------------------------------------------------------*/
  3190. void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
  3191. const TickType_t xItemValue,
  3192. const TickType_t xTicksToWait )
  3193. {
  3194. /* Get current core ID as we can no longer be preempted. */
  3195. const BaseType_t xCurCoreID = portGET_CORE_ID();
  3196. configASSERT( pxEventList );
  3197. #if ( configNUMBER_OF_CORES > 1 )
  3198. {
  3199. /* IN SMP, THIS FUNCTION MUST BE CALLED WITH THE EVENT GROUP'S
  3200. * xEventGroupLock ALREADY TAKEN. */
  3201. }
  3202. #else /* configNUMBER_OF_CORES > 1 */
  3203. {
  3204. /* IN SINGLE-CORE, THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.
  3205. * It is used by the event groups implementation. */
  3206. configASSERT( uxSchedulerSuspended[ xCurCoreID ] != 0 );
  3207. }
  3208. #endif /* configNUMBER_OF_CORES > 1 */
  3209. /* For SMP, we need to take the kernel lock here as we are about to access
  3210. * kernel data structures. */
  3211. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  3212. {
  3213. /* Store the item value in the event list item. It is safe to access the
  3214. * event list item here as interrupts won't access the event list item of a
  3215. * task that is not in the Blocked state. */
  3216. listSET_LIST_ITEM_VALUE( &( pxCurrentTCBs[ xCurCoreID ]->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  3217. /* Place the event list item of the TCB at the end of the appropriate event
  3218. * list. It is safe to access the event list here because it is part of an
  3219. * event group implementation - and interrupts don't access event groups
  3220. * directly (instead they access them indirectly by pending function calls to
  3221. * the task level). */
  3222. listINSERT_END( pxEventList, &( pxCurrentTCBs[ xCurCoreID ]->xEventListItem ) );
  3223. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  3224. }
  3225. /* Release the previously taken kernel lock. */
  3226. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  3227. }
  3228. /*-----------------------------------------------------------*/
  3229. #if ( configUSE_TIMERS == 1 )
  3230. void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
  3231. TickType_t xTicksToWait,
  3232. const BaseType_t xWaitIndefinitely )
  3233. {
  3234. configASSERT( pxEventList );
  3235. /* This function should not be called by application code hence the
  3236. * 'Restricted' in its name. It is not part of the public API. It is
  3237. * designed for use by kernel code, and has special calling requirements -
  3238. * it should be called with the scheduler suspended in single-core, or
  3239. * with the queue's xQueueLock already taken in SMP. */
  3240. /* For SMP, we need to take the kernel lock here as we are about to access
  3241. * kernel data structures. */
  3242. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  3243. {
  3244. /* Place the event list item of the TCB in the appropriate event list.
  3245. * In this case it is assume that this is the only task that is going to
  3246. * be waiting on this event list, so the faster vListInsertEnd() function
  3247. * can be used in place of vListInsert. */
  3248. listINSERT_END( pxEventList, &( pxCurrentTCBs[ portGET_CORE_ID() ]->xEventListItem ) );
  3249. /* If the task should block indefinitely then set the block time to a
  3250. * value that will be recognised as an indefinite delay inside the
  3251. * prvAddCurrentTaskToDelayedList() function. */
  3252. if( xWaitIndefinitely != pdFALSE )
  3253. {
  3254. xTicksToWait = portMAX_DELAY;
  3255. }
  3256. traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
  3257. prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
  3258. }
  3259. /* Release the previously taken kernel lock. */
  3260. taskEXIT_CRITICAL( &xKernelLock );
  3261. }
  3262. #endif /* configUSE_TIMERS */
  3263. /*-----------------------------------------------------------*/
  3264. #if ( configNUMBER_OF_CORES > 1 )
  3265. BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
  3266. {
  3267. TCB_t * pxUnblockedTCB;
  3268. BaseType_t xReturn;
  3269. /* For SMP, we need to take the kernel lock here as we are about to access
  3270. * kernel data structures.
  3271. * This function can also be called from an ISR context, so we
  3272. * need to check whether we are in an ISR.*/
  3273. if( portCHECK_IF_IN_ISR() == pdFALSE )
  3274. {
  3275. taskENTER_CRITICAL( &xKernelLock );
  3276. }
  3277. else
  3278. {
  3279. taskENTER_CRITICAL_ISR( &xKernelLock );
  3280. }
  3281. {
  3282. /* Before taking the kernel lock, another task/ISR could have already
  3283. * emptied the pxEventList. So we insert a check here to see if
  3284. * pxEventList is empty before attempting to remove an item from it. */
  3285. if( listLIST_IS_EMPTY( pxEventList ) == pdFALSE )
  3286. {
  3287. /* Get current core ID as we can no longer be preempted. */
  3288. const BaseType_t xCurCoreID = portGET_CORE_ID();
  3289. /* Remove the task from its current event list */
  3290. pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
  3291. configASSERT( pxUnblockedTCB );
  3292. listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
  3293. /* Add the task to the ready list if a core with compatible affinity
  3294. * has NOT suspended its scheduler. This occurs when:
  3295. * - The task is pinned, and the pinned core's scheduler is running
  3296. * - The task is unpinned, and at least one of the core's scheduler is running */
  3297. if( taskCAN_BE_SCHEDULED( pxUnblockedTCB ) == pdTRUE )
  3298. {
  3299. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  3300. prvAddTaskToReadyList( pxUnblockedTCB );
  3301. #if ( configUSE_TICKLESS_IDLE != 0 )
  3302. {
  3303. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  3304. * might be set to the blocked task's time out time. If the task is
  3305. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  3306. * normally left unchanged, because it is automatically reset to a new
  3307. * value when the tick count equals xNextTaskUnblockTime. However if
  3308. * tickless idling is used it might be more important to enter sleep mode
  3309. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  3310. * ensure it is updated at the earliest possible time. */
  3311. prvResetNextTaskUnblockTime();
  3312. }
  3313. #endif
  3314. }
  3315. else
  3316. {
  3317. /* We arrive here due to one of the following possibilities:
  3318. * - The task is pinned to core X and core X has suspended its scheduler
  3319. * - The task is unpinned and both cores have suspend their schedulers
  3320. * Therefore, we add the task to one of the pending lists:
  3321. * - If the task is pinned to core X, add it to core X's pending list
  3322. * - If the task is unpinned, add it to the current core's pending list */
  3323. UBaseType_t uxPendCore = ( ( pxUnblockedTCB->xCoreID == tskNO_AFFINITY ) ? xCurCoreID : pxUnblockedTCB->xCoreID );
  3324. configASSERT( uxSchedulerSuspended[ uxPendCore ] != ( UBaseType_t ) 0U );
  3325. /* Add the task to the current core's pending list */
  3326. listINSERT_END( &( xPendingReadyList[ uxPendCore ] ), &( pxUnblockedTCB->xEventListItem ) );
  3327. }
  3328. if( taskIS_YIELD_REQUIRED( pxUnblockedTCB, xCurCoreID, pdFALSE ) == pdTRUE )
  3329. {
  3330. /* The unblocked task requires a the current core to yield */
  3331. xReturn = pdTRUE;
  3332. /* Mark that a yield is pending in case the user is not using the
  3333. * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
  3334. xYieldPending[ xCurCoreID ] = pdTRUE;
  3335. }
  3336. else
  3337. {
  3338. xReturn = pdFALSE;
  3339. }
  3340. }
  3341. else
  3342. {
  3343. /* The pxEventList was emptied before we entered the critical
  3344. * section, Nothing to do except return pdFALSE. */
  3345. xReturn = pdFALSE;
  3346. }
  3347. }
  3348. /* Release the previously taken kernel lock. */
  3349. if( portCHECK_IF_IN_ISR() == pdFALSE )
  3350. {
  3351. taskEXIT_CRITICAL( &xKernelLock );
  3352. }
  3353. else
  3354. {
  3355. taskEXIT_CRITICAL_ISR( &xKernelLock );
  3356. }
  3357. return xReturn;
  3358. }
  3359. #else /* configNUMBER_OF_CORES > 1 */
  3360. BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
  3361. {
  3362. TCB_t * pxUnblockedTCB;
  3363. BaseType_t xReturn;
  3364. /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
  3365. * called from a critical section within an ISR. */
  3366. /* The event list is sorted in priority order, so the first in the list can
  3367. * be removed as it is known to be the highest priority. Remove the TCB from
  3368. * the delayed list, and add it to the ready list.
  3369. *
  3370. * If an event is for a queue that is locked then this function will never
  3371. * get called - the lock count on the queue will get modified instead. This
  3372. * means exclusive access to the event list is guaranteed here.
  3373. *
  3374. * This function assumes that a check has already been made to ensure that
  3375. * pxEventList is not empty. */
  3376. pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  3377. configASSERT( pxUnblockedTCB );
  3378. listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
  3379. if( uxSchedulerSuspended[ 0 ] == ( UBaseType_t ) pdFALSE )
  3380. {
  3381. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  3382. prvAddTaskToReadyList( pxUnblockedTCB );
  3383. #if ( configUSE_TICKLESS_IDLE != 0 )
  3384. {
  3385. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  3386. * might be set to the blocked task's time out time. If the task is
  3387. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  3388. * normally left unchanged, because it is automatically reset to a new
  3389. * value when the tick count equals xNextTaskUnblockTime. However if
  3390. * tickless idling is used it might be more important to enter sleep mode
  3391. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  3392. * ensure it is updated at the earliest possible time. */
  3393. prvResetNextTaskUnblockTime();
  3394. }
  3395. #endif
  3396. }
  3397. else
  3398. {
  3399. /* The delayed and ready lists cannot be accessed, so hold this task
  3400. * pending until the scheduler is resumed. */
  3401. listINSERT_END( &( xPendingReadyList[ 0 ] ), &( pxUnblockedTCB->xEventListItem ) );
  3402. }
  3403. if( pxUnblockedTCB->uxPriority > pxCurrentTCBs[ 0 ]->uxPriority )
  3404. {
  3405. /* Return true if the task removed from the event list has a higher
  3406. * priority than the calling task. This allows the calling task to know if
  3407. * it should force a context switch now. */
  3408. xReturn = pdTRUE;
  3409. /* Mark that a yield is pending in case the user is not using the
  3410. * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
  3411. xYieldPending[ 0 ] = pdTRUE;
  3412. }
  3413. else
  3414. {
  3415. xReturn = pdFALSE;
  3416. }
  3417. return xReturn;
  3418. }
  3419. #endif /* configNUMBER_OF_CORES > 1 */
  3420. /*-----------------------------------------------------------*/
  3421. void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
  3422. const TickType_t xItemValue )
  3423. {
  3424. TCB_t * pxUnblockedTCB;
  3425. /* Get current core ID as we can no longer be preempted. */
  3426. const BaseType_t xCurCoreID = portGET_CORE_ID();
  3427. #if ( configNUM_CORES > 1 )
  3428. /* THIS FUNCTION MUST BE CALLED WITH THE KERNEL LOCK ALREADY TAKEN.
  3429. * It is used by the event flags implementation, thus those functions
  3430. * should call prvTakeKernelLock() before calling this function. */
  3431. #else /* configNUM_CORES > 1 */
  3432. /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
  3433. * the event flags implementation. */
  3434. configASSERT( uxSchedulerSuspended[ 0 ] != ( UBaseType_t ) 0U );
  3435. #endif /* configNUM_CORES > 1 */
  3436. /* Store the new item value in the event list. */
  3437. listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
  3438. /* Remove the event list form the event flag. Interrupts do not access
  3439. * event flags. */
  3440. pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  3441. configASSERT( pxUnblockedTCB );
  3442. listREMOVE_ITEM( pxEventListItem );
  3443. #if ( configUSE_TICKLESS_IDLE != 0 )
  3444. {
  3445. /* If a task is blocked on a kernel object then xNextTaskUnblockTime
  3446. * might be set to the blocked task's time out time. If the task is
  3447. * unblocked for a reason other than a timeout xNextTaskUnblockTime is
  3448. * normally left unchanged, because it is automatically reset to a new
  3449. * value when the tick count equals xNextTaskUnblockTime. However if
  3450. * tickless idling is used it might be more important to enter sleep mode
  3451. * at the earliest possible time - so reset xNextTaskUnblockTime here to
  3452. * ensure it is updated at the earliest possible time. */
  3453. prvResetNextTaskUnblockTime();
  3454. }
  3455. #endif
  3456. #if ( configNUM_CORES > 1 )
  3457. /* Add the task to the ready list if a core with compatible affinity
  3458. * has NOT suspended its scheduler. This occurs when:
  3459. * - The task is pinned, and the pinned core's scheduler is running
  3460. * - The task is unpinned, and at least one of the core's scheduler is
  3461. * running */
  3462. if( taskCAN_BE_SCHEDULED( pxUnblockedTCB ) == pdFALSE )
  3463. {
  3464. /* We arrive here due to one of the following possibilities:
  3465. * - The task is pinned to core X and core X has suspended its scheduler
  3466. * - The task is unpinned and both cores have suspend their schedulers
  3467. * Therefore, we add the task to one of the pending lists:
  3468. * - If the task is pinned to core X, add it to core X's pending list
  3469. * - If the task is unpinned, add it to the current core's pending list */
  3470. BaseType_t xPendingListCore = ( ( pxUnblockedTCB->xCoreID == tskNO_AFFINITY ) ? xCurCoreID : pxUnblockedTCB->xCoreID );
  3471. configASSERT( uxSchedulerSuspended[ xPendingListCore ] != ( UBaseType_t ) 0U );
  3472. /* The delayed and ready lists cannot be accessed, so hold this task
  3473. * pending until the scheduler is resumed. */
  3474. listINSERT_END( &( xPendingReadyList[ xPendingListCore ] ), &( pxUnblockedTCB->xEventListItem ) );
  3475. }
  3476. else
  3477. #else /* configNUM_CORES > 1 */
  3478. /* In single core, the caller of this function has already suspended the
  3479. * scheduler, which means we have exclusive access to the ready list.
  3480. * We add the unblocked task to the ready list directly. */
  3481. #endif /* configNUM_CORES > 1 */
  3482. {
  3483. /* Remove the task from the delayed list and add it to the ready list. The
  3484. * scheduler is suspended so interrupts will not be accessing the ready
  3485. * lists. */
  3486. listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
  3487. prvAddTaskToReadyList( pxUnblockedTCB );
  3488. if( taskIS_YIELD_REQUIRED( pxUnblockedTCB, xCurCoreID, pdFALSE ) == pdTRUE )
  3489. {
  3490. /* The unblocked task has a priority above that of the calling task, so
  3491. * a context switch is required. This function is called with the
  3492. * scheduler suspended so xYieldPending is set so the context switch
  3493. * occurs immediately that the scheduler is resumed (unsuspended). */
  3494. xYieldPending[ xCurCoreID ] = pdTRUE;
  3495. }
  3496. }
  3497. }
  3498. /*-----------------------------------------------------------*/
  3499. void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
  3500. {
  3501. configASSERT( pxTimeOut );
  3502. taskENTER_CRITICAL( &xKernelLock );
  3503. {
  3504. pxTimeOut->xOverflowCount = xNumOfOverflows;
  3505. pxTimeOut->xTimeOnEntering = xTickCount;
  3506. }
  3507. taskEXIT_CRITICAL( &xKernelLock );
  3508. }
  3509. /*-----------------------------------------------------------*/
  3510. void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
  3511. {
  3512. /* For internal use only as it does not use a critical section. */
  3513. pxTimeOut->xOverflowCount = xNumOfOverflows;
  3514. pxTimeOut->xTimeOnEntering = xTickCount;
  3515. }
  3516. /*-----------------------------------------------------------*/
  3517. BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
  3518. TickType_t * const pxTicksToWait )
  3519. {
  3520. BaseType_t xReturn;
  3521. configASSERT( pxTimeOut );
  3522. configASSERT( pxTicksToWait );
  3523. taskENTER_CRITICAL( &xKernelLock );
  3524. {
  3525. /* Minor optimisation. The tick count cannot change in this block. */
  3526. const TickType_t xConstTickCount = xTickCount;
  3527. const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
  3528. /* Get current core ID as we can no longer be preempted. */
  3529. const BaseType_t xCurCoreID = portGET_CORE_ID();
  3530. #if ( INCLUDE_xTaskAbortDelay == 1 )
  3531. if( pxCurrentTCBs[ xCurCoreID ]->ucDelayAborted != ( uint8_t ) pdFALSE )
  3532. {
  3533. /* The delay was aborted, which is not the same as a time out,
  3534. * but has the same result. */
  3535. pxCurrentTCBs[ xCurCoreID ]->ucDelayAborted = pdFALSE;
  3536. xReturn = pdTRUE;
  3537. }
  3538. else
  3539. #endif
  3540. #if ( INCLUDE_vTaskSuspend == 1 )
  3541. if( *pxTicksToWait == portMAX_DELAY )
  3542. {
  3543. /* If INCLUDE_vTaskSuspend is set to 1 and the block time
  3544. * specified is the maximum block time then the task should block
  3545. * indefinitely, and therefore never time out. */
  3546. xReturn = pdFALSE;
  3547. }
  3548. else
  3549. #endif
  3550. if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
  3551. {
  3552. /* The tick count is greater than the time at which
  3553. * vTaskSetTimeout() was called, but has also overflowed since
  3554. * vTaskSetTimeOut() was called. It must have wrapped all the way
  3555. * around and gone past again. This passed since vTaskSetTimeout()
  3556. * was called. */
  3557. xReturn = pdTRUE;
  3558. *pxTicksToWait = ( TickType_t ) 0;
  3559. }
  3560. else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
  3561. {
  3562. /* Not a genuine timeout. Adjust parameters for time remaining. */
  3563. *pxTicksToWait -= xElapsedTime;
  3564. vTaskInternalSetTimeOutState( pxTimeOut );
  3565. xReturn = pdFALSE;
  3566. }
  3567. else
  3568. {
  3569. *pxTicksToWait = ( TickType_t ) 0;
  3570. xReturn = pdTRUE;
  3571. }
  3572. }
  3573. taskEXIT_CRITICAL( &xKernelLock );
  3574. return xReturn;
  3575. }
  3576. /*-----------------------------------------------------------*/
  3577. void vTaskMissedYield( void )
  3578. {
  3579. xYieldPending[ portGET_CORE_ID() ] = pdTRUE;
  3580. }
  3581. /*-----------------------------------------------------------*/
  3582. #if ( configUSE_TRACE_FACILITY == 1 )
  3583. UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
  3584. {
  3585. UBaseType_t uxReturn;
  3586. TCB_t const * pxTCB;
  3587. if( xTask != NULL )
  3588. {
  3589. pxTCB = xTask;
  3590. uxReturn = pxTCB->uxTaskNumber;
  3591. }
  3592. else
  3593. {
  3594. uxReturn = 0U;
  3595. }
  3596. return uxReturn;
  3597. }
  3598. #endif /* configUSE_TRACE_FACILITY */
  3599. /*-----------------------------------------------------------*/
  3600. #if ( configUSE_TRACE_FACILITY == 1 )
  3601. void vTaskSetTaskNumber( TaskHandle_t xTask,
  3602. const UBaseType_t uxHandle )
  3603. {
  3604. TCB_t * pxTCB;
  3605. if( xTask != NULL )
  3606. {
  3607. pxTCB = xTask;
  3608. pxTCB->uxTaskNumber = uxHandle;
  3609. }
  3610. }
  3611. #endif /* configUSE_TRACE_FACILITY */
  3612. /*
  3613. * -----------------------------------------------------------
  3614. * The Idle task.
  3615. * ----------------------------------------------------------
  3616. *
  3617. * The portTASK_FUNCTION() macro is used to allow port/compiler specific
  3618. * language extensions. The equivalent prototype for this function is:
  3619. *
  3620. * void prvIdleTask( void *pvParameters );
  3621. *
  3622. */
  3623. static portTASK_FUNCTION( prvIdleTask, pvParameters )
  3624. {
  3625. /* Stop warnings. */
  3626. ( void ) pvParameters;
  3627. /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
  3628. * SCHEDULER IS STARTED. **/
  3629. /* In case a task that has a secure context deletes itself, in which case
  3630. * the idle task is responsible for deleting the task's secure context, if
  3631. * any. */
  3632. portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
  3633. for( ; ; )
  3634. {
  3635. /* See if any tasks have deleted themselves - if so then the idle task
  3636. * is responsible for freeing the deleted task's TCB and stack. */
  3637. prvCheckTasksWaitingTermination();
  3638. #if ( configUSE_PREEMPTION == 0 )
  3639. {
  3640. /* If we are not using preemption we keep forcing a task switch to
  3641. * see if any other task has become available. If we are using
  3642. * preemption we don't need to do this as any task becoming available
  3643. * will automatically get the processor anyway. */
  3644. taskYIELD();
  3645. }
  3646. #endif /* configUSE_PREEMPTION */
  3647. #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
  3648. {
  3649. /* When using preemption tasks of equal priority will be
  3650. * timesliced. If a task that is sharing the idle priority is ready
  3651. * to run then the idle task should yield before the end of the
  3652. * timeslice.
  3653. *
  3654. * A critical region is not required here as we are just reading from
  3655. * the list, and an occasional incorrect value will not matter. If
  3656. * the ready list at the idle priority contains more than one task
  3657. * then a task other than the idle task is ready to execute. */
  3658. if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
  3659. {
  3660. taskYIELD();
  3661. }
  3662. else
  3663. {
  3664. mtCOVERAGE_TEST_MARKER();
  3665. }
  3666. }
  3667. #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
  3668. #if ( configUSE_IDLE_HOOK == 1 )
  3669. {
  3670. extern void vApplicationIdleHook( void );
  3671. /* Call the user defined function from within the idle task. This
  3672. * allows the application designer to add background functionality
  3673. * without the overhead of a separate task.
  3674. * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
  3675. * CALL A FUNCTION THAT MIGHT BLOCK. */
  3676. vApplicationIdleHook();
  3677. }
  3678. #endif /* configUSE_IDLE_HOOK */
  3679. /* Call the esp-idf idle hook system. Todo IDF-8180 */
  3680. extern void esp_vApplicationIdleHook( void );
  3681. esp_vApplicationIdleHook();
  3682. /* This conditional compilation should use inequality to 0, not equality
  3683. * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
  3684. * user defined low power mode implementations require
  3685. * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
  3686. #if ( configUSE_TICKLESS_IDLE != 0 )
  3687. {
  3688. TickType_t xExpectedIdleTime;
  3689. /* It is not desirable to suspend then resume the scheduler on
  3690. * each iteration of the idle task. Therefore, a preliminary
  3691. * test of the expected idle time is performed without the
  3692. * scheduler suspended. The result here is not necessarily
  3693. * valid. */
  3694. xExpectedIdleTime = prvGetExpectedIdleTime();
  3695. if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  3696. {
  3697. prvENTER_CRITICAL_OR_SUSPEND_ALL( &xKernelLock );
  3698. {
  3699. /* Now the scheduler is suspended, the expected idle
  3700. * time can be sampled again, and this time its value can
  3701. * be used. */
  3702. configASSERT( xNextTaskUnblockTime >= xTickCount );
  3703. xExpectedIdleTime = prvGetExpectedIdleTime();
  3704. /* Define the following macro to set xExpectedIdleTime to 0
  3705. * if the application does not want
  3706. * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
  3707. configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
  3708. if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
  3709. {
  3710. traceLOW_POWER_IDLE_BEGIN();
  3711. portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
  3712. traceLOW_POWER_IDLE_END();
  3713. }
  3714. else
  3715. {
  3716. mtCOVERAGE_TEST_MARKER();
  3717. }
  3718. }
  3719. ( void ) prvEXIT_CRITICAL_OR_RESUME_ALL( &xKernelLock );
  3720. }
  3721. else
  3722. {
  3723. mtCOVERAGE_TEST_MARKER();
  3724. }
  3725. }
  3726. #endif /* configUSE_TICKLESS_IDLE */
  3727. }
  3728. }
  3729. /*-----------------------------------------------------------*/
  3730. #if ( configUSE_TICKLESS_IDLE != 0 )
  3731. eSleepModeStatus eTaskConfirmSleepModeStatus( void )
  3732. {
  3733. #if ( INCLUDE_vTaskSuspend == 1 )
  3734. /* The idle task exists in addition to the application tasks. */
  3735. const UBaseType_t uxNonApplicationTasks = 1;
  3736. #endif /* INCLUDE_vTaskSuspend */
  3737. eSleepModeStatus eReturn = eStandardSleep;
  3738. /* This function must be called from a critical section. */
  3739. /* Get current core ID as we can no longer be preempted. */
  3740. const BaseType_t xCurCoreID = portGET_CORE_ID();
  3741. if( listCURRENT_LIST_LENGTH( &xPendingReadyList[ xCurCoreID ] ) != 0 )
  3742. {
  3743. /* A task was made ready while the scheduler was suspended. */
  3744. eReturn = eAbortSleep;
  3745. }
  3746. else if( xYieldPending[ xCurCoreID ] != pdFALSE )
  3747. {
  3748. /* A yield was pended while the scheduler was suspended. */
  3749. eReturn = eAbortSleep;
  3750. }
  3751. #if ( configNUMBER_OF_CORES == 1 )
  3752. else if( xPendedTicks != 0 )
  3753. {
  3754. /* A tick interrupt has already occurred but was held pending
  3755. * because the scheduler is suspended. */
  3756. eReturn = eAbortSleep;
  3757. }
  3758. #endif /* configNUMBER_OF_CORES == 1 */
  3759. #if ( INCLUDE_vTaskSuspend == 1 )
  3760. else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
  3761. {
  3762. /* If all the tasks are in the suspended list (which might mean they
  3763. * have an infinite block time rather than actually being suspended)
  3764. * then it is safe to turn all clocks off and just wait for external
  3765. * interrupts. */
  3766. eReturn = eNoTasksWaitingTimeout;
  3767. }
  3768. #endif /* INCLUDE_vTaskSuspend */
  3769. else
  3770. {
  3771. mtCOVERAGE_TEST_MARKER();
  3772. }
  3773. return eReturn;
  3774. }
  3775. #endif /* configUSE_TICKLESS_IDLE */
  3776. /*-----------------------------------------------------------*/
  3777. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  3778. void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
  3779. BaseType_t xIndex,
  3780. void * pvValue )
  3781. {
  3782. TCB_t * pxTCB;
  3783. if( ( xIndex >= 0 ) &&
  3784. ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
  3785. {
  3786. pxTCB = prvGetTCBFromHandle( xTaskToSet );
  3787. configASSERT( pxTCB != NULL );
  3788. pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
  3789. }
  3790. }
  3791. #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
  3792. /*-----------------------------------------------------------*/
  3793. #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
  3794. void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
  3795. BaseType_t xIndex )
  3796. {
  3797. void * pvReturn = NULL;
  3798. TCB_t * pxTCB;
  3799. if( ( xIndex >= 0 ) &&
  3800. ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
  3801. {
  3802. pxTCB = prvGetTCBFromHandle( xTaskToQuery );
  3803. pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
  3804. }
  3805. else
  3806. {
  3807. pvReturn = NULL;
  3808. }
  3809. return pvReturn;
  3810. }
  3811. #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
  3812. /*-----------------------------------------------------------*/
  3813. #if ( portUSING_MPU_WRAPPERS == 1 )
  3814. void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
  3815. const MemoryRegion_t * const xRegions )
  3816. {
  3817. TCB_t * pxTCB;
  3818. /* If null is passed in here then we are modifying the MPU settings of
  3819. * the calling task. */
  3820. pxTCB = prvGetTCBFromHandle( xTaskToModify );
  3821. vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
  3822. }
  3823. #endif /* portUSING_MPU_WRAPPERS */
  3824. /*-----------------------------------------------------------*/
  3825. static void prvInitialiseTaskLists( void )
  3826. {
  3827. UBaseType_t uxPriority;
  3828. UBaseType_t x;
  3829. for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
  3830. {
  3831. vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
  3832. }
  3833. vListInitialise( &xDelayedTaskList1 );
  3834. vListInitialise( &xDelayedTaskList2 );
  3835. for( x = 0; x < configNUMBER_OF_CORES; x++ )
  3836. {
  3837. vListInitialise( &xPendingReadyList[ x ] );
  3838. }
  3839. #if ( INCLUDE_vTaskDelete == 1 )
  3840. {
  3841. vListInitialise( &xTasksWaitingTermination );
  3842. }
  3843. #endif /* INCLUDE_vTaskDelete */
  3844. #if ( INCLUDE_vTaskSuspend == 1 )
  3845. {
  3846. vListInitialise( &xSuspendedTaskList );
  3847. }
  3848. #endif /* INCLUDE_vTaskSuspend */
  3849. /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
  3850. * using list2. */
  3851. pxDelayedTaskList = &xDelayedTaskList1;
  3852. pxOverflowDelayedTaskList = &xDelayedTaskList2;
  3853. }
  3854. /*-----------------------------------------------------------*/
  3855. static void prvCheckTasksWaitingTermination( void )
  3856. {
  3857. /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
  3858. #if ( INCLUDE_vTaskDelete == 1 )
  3859. {
  3860. TCB_t * pxTCB;
  3861. /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
  3862. * being called too often in the idle task. */
  3863. while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
  3864. {
  3865. #if ( configNUMBER_OF_CORES > 1 )
  3866. {
  3867. pxTCB = NULL;
  3868. taskENTER_CRITICAL( &xKernelLock );
  3869. {
  3870. /* List may have already been cleared by the other core. Check again */
  3871. if( listLIST_IS_EMPTY( &xTasksWaitingTermination ) == pdFALSE )
  3872. {
  3873. /* We can't delete a task if it is still running on
  3874. * the other core. Keep walking the list until we
  3875. * find a task we can free, or until we walk the
  3876. * entire list. */
  3877. ListItem_t * xEntry;
  3878. for( xEntry = listGET_HEAD_ENTRY( &xTasksWaitingTermination ); xEntry != listGET_END_MARKER( &xTasksWaitingTermination ); xEntry = listGET_NEXT( xEntry ) )
  3879. {
  3880. if( taskIS_CURRENTLY_RUNNING( ( ( TCB_t * ) listGET_LIST_ITEM_OWNER( xEntry ) ) ) == pdFALSE )
  3881. {
  3882. pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( xEntry );
  3883. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  3884. --uxCurrentNumberOfTasks;
  3885. --uxDeletedTasksWaitingCleanUp;
  3886. break;
  3887. }
  3888. }
  3889. }
  3890. }
  3891. taskEXIT_CRITICAL( &xKernelLock );
  3892. if( pxTCB != NULL )
  3893. {
  3894. prvDeleteTCB( pxTCB );
  3895. }
  3896. else
  3897. {
  3898. /* No task found to delete, break out of loop */
  3899. break;
  3900. }
  3901. }
  3902. #else /* configNUMBER_OF_CORES > 1 */
  3903. {
  3904. taskENTER_CRITICAL( &xKernelLock );
  3905. {
  3906. pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  3907. ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
  3908. --uxCurrentNumberOfTasks;
  3909. --uxDeletedTasksWaitingCleanUp;
  3910. }
  3911. taskEXIT_CRITICAL( &xKernelLock );
  3912. prvDeleteTCB( pxTCB );
  3913. }
  3914. #endif /* configNUMBER_OF_CORES > 1 */
  3915. }
  3916. }
  3917. #endif /* INCLUDE_vTaskDelete */
  3918. }
  3919. /*-----------------------------------------------------------*/
  3920. #if ( configUSE_TRACE_FACILITY == 1 )
  3921. void vTaskGetInfo( TaskHandle_t xTask,
  3922. TaskStatus_t * pxTaskStatus,
  3923. BaseType_t xGetFreeStackSpace,
  3924. eTaskState eState )
  3925. {
  3926. TCB_t * pxTCB;
  3927. /* xTask is NULL then get the state of the calling task. */
  3928. pxTCB = prvGetTCBFromHandle( xTask );
  3929. /* A critical section is required for SMP in case another core modifies
  3930. * the task simultaneously. */
  3931. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  3932. {
  3933. pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
  3934. pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
  3935. pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
  3936. pxTaskStatus->pxStackBase = pxTCB->pxStack;
  3937. #if ( ( portSTACK_GROWTH > 0 ) && ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
  3938. pxTaskStatus->pxTopOfStack = pxTCB->pxTopOfStack;
  3939. pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
  3940. #endif
  3941. pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
  3942. /* Todo: Remove xCoreID for single core builds (IDF-7894) */
  3943. pxTaskStatus->xCoreID = pxTCB->xCoreID;
  3944. #if ( configUSE_MUTEXES == 1 )
  3945. {
  3946. pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
  3947. }
  3948. #else
  3949. {
  3950. pxTaskStatus->uxBasePriority = 0;
  3951. }
  3952. #endif
  3953. #if ( configGENERATE_RUN_TIME_STATS == 1 )
  3954. {
  3955. pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
  3956. }
  3957. #else
  3958. {
  3959. pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
  3960. }
  3961. #endif
  3962. /* Obtaining the task state is a little fiddly, so is only done if the
  3963. * value of eState passed into this function is eInvalid - otherwise the
  3964. * state is just set to whatever is passed in. */
  3965. if( eState != eInvalid )
  3966. {
  3967. if( pxTCB == pxCurrentTCBs[ portGET_CORE_ID() ] )
  3968. {
  3969. pxTaskStatus->eCurrentState = eRunning;
  3970. }
  3971. else
  3972. {
  3973. pxTaskStatus->eCurrentState = eState;
  3974. #if ( INCLUDE_vTaskSuspend == 1 )
  3975. {
  3976. /* If the task is in the suspended list then there is a
  3977. * chance it is actually just blocked indefinitely - so really
  3978. * it should be reported as being in the Blocked state. */
  3979. if( eState == eSuspended )
  3980. {
  3981. #if ( configNUMBER_OF_CORES == 1 )
  3982. {
  3983. /* Single core uses a scheduler suspension to
  3984. * atomically check if the task task is blocked. */
  3985. vTaskSuspendAll();
  3986. }
  3987. #endif /* configNUMBER_OF_CORES == 1 */
  3988. {
  3989. if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
  3990. {
  3991. pxTaskStatus->eCurrentState = eBlocked;
  3992. }
  3993. }
  3994. #if ( configNUMBER_OF_CORES == 1 )
  3995. {
  3996. ( void ) xTaskResumeAll();
  3997. }
  3998. #endif /* configNUMBER_OF_CORES == 1 */
  3999. }
  4000. }
  4001. #endif /* INCLUDE_vTaskSuspend */
  4002. }
  4003. }
  4004. else
  4005. {
  4006. pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
  4007. }
  4008. /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
  4009. * parameter is provided to allow it to be skipped. */
  4010. if( xGetFreeStackSpace != pdFALSE )
  4011. {
  4012. #if ( portSTACK_GROWTH > 0 )
  4013. {
  4014. pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
  4015. }
  4016. #else
  4017. {
  4018. pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
  4019. }
  4020. #endif
  4021. }
  4022. else
  4023. {
  4024. pxTaskStatus->usStackHighWaterMark = 0;
  4025. }
  4026. }
  4027. /* Exit the previously entered critical section. */
  4028. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  4029. }
  4030. #endif /* configUSE_TRACE_FACILITY */
  4031. /*-----------------------------------------------------------*/
  4032. #if ( configUSE_TRACE_FACILITY == 1 )
  4033. static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
  4034. List_t * pxList,
  4035. eTaskState eState )
  4036. {
  4037. configLIST_VOLATILE TCB_t * pxNextTCB;
  4038. configLIST_VOLATILE TCB_t * pxFirstTCB;
  4039. UBaseType_t uxTask = 0;
  4040. if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
  4041. {
  4042. listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  4043. /* Populate an TaskStatus_t structure within the
  4044. * pxTaskStatusArray array for each task that is referenced from
  4045. * pxList. See the definition of TaskStatus_t in task.h for the
  4046. * meaning of each TaskStatus_t structure member. */
  4047. do
  4048. {
  4049. listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
  4050. vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
  4051. uxTask++;
  4052. } while( pxNextTCB != pxFirstTCB );
  4053. }
  4054. else
  4055. {
  4056. mtCOVERAGE_TEST_MARKER();
  4057. }
  4058. return uxTask;
  4059. }
  4060. #endif /* configUSE_TRACE_FACILITY */
  4061. /*-----------------------------------------------------------*/
  4062. #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
  4063. static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
  4064. {
  4065. uint32_t ulCount = 0U;
  4066. while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
  4067. {
  4068. pucStackByte -= portSTACK_GROWTH;
  4069. ulCount++;
  4070. }
  4071. ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
  4072. return ( configSTACK_DEPTH_TYPE ) ulCount;
  4073. }
  4074. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
  4075. /*-----------------------------------------------------------*/
  4076. #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
  4077. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
  4078. * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
  4079. * user to determine the return type. It gets around the problem of the value
  4080. * overflowing on 8-bit types without breaking backward compatibility for
  4081. * applications that expect an 8-bit return type. */
  4082. configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
  4083. {
  4084. TCB_t * pxTCB;
  4085. uint8_t * pucEndOfStack;
  4086. configSTACK_DEPTH_TYPE uxReturn;
  4087. /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
  4088. * the same except for their return type. Using configSTACK_DEPTH_TYPE
  4089. * allows the user to determine the return type. It gets around the
  4090. * problem of the value overflowing on 8-bit types without breaking
  4091. * backward compatibility for applications that expect an 8-bit return
  4092. * type. */
  4093. pxTCB = prvGetTCBFromHandle( xTask );
  4094. #if portSTACK_GROWTH < 0
  4095. {
  4096. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  4097. }
  4098. #else
  4099. {
  4100. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  4101. }
  4102. #endif
  4103. uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
  4104. return uxReturn;
  4105. }
  4106. #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
  4107. /*-----------------------------------------------------------*/
  4108. #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
  4109. UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
  4110. {
  4111. TCB_t * pxTCB;
  4112. uint8_t * pucEndOfStack;
  4113. UBaseType_t uxReturn;
  4114. pxTCB = prvGetTCBFromHandle( xTask );
  4115. #if portSTACK_GROWTH < 0
  4116. {
  4117. pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
  4118. }
  4119. #else
  4120. {
  4121. pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
  4122. }
  4123. #endif
  4124. uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
  4125. return uxReturn;
  4126. }
  4127. #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
  4128. /*-----------------------------------------------------------*/
  4129. #if ( INCLUDE_vTaskDelete == 1 )
  4130. static void prvDeleteTCB( TCB_t * pxTCB )
  4131. {
  4132. /* This call is required specifically for the TriCore port. It must be
  4133. * above the vPortFree() calls. The call is also used by ports/demos that
  4134. * want to allocate and clean RAM statically. */
  4135. portCLEAN_UP_TCB( pxTCB );
  4136. #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
  4137. {
  4138. /* Free up the memory allocated for the task's TLS Block. */
  4139. /* Note: Fixed bug in upstream. Free TLS block of pxTCB, NOT pxCurrentTCBs */
  4140. configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
  4141. }
  4142. #endif
  4143. #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
  4144. {
  4145. /* The task can only have been allocated dynamically - free both
  4146. * the stack and TCB. */
  4147. vPortFreeStack( pxTCB->pxStack );
  4148. vPortFree( pxTCB );
  4149. }
  4150. #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
  4151. {
  4152. /* The task could have been allocated statically or dynamically, so
  4153. * check what was statically allocated before trying to free the
  4154. * memory. */
  4155. if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
  4156. {
  4157. /* Both the stack and TCB were allocated dynamically, so both
  4158. * must be freed. */
  4159. vPortFreeStack( pxTCB->pxStack );
  4160. vPortFree( pxTCB );
  4161. }
  4162. else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
  4163. {
  4164. /* Only the stack was statically allocated, so the TCB is the
  4165. * only memory that must be freed. */
  4166. vPortFree( pxTCB );
  4167. }
  4168. else
  4169. {
  4170. /* Neither the stack nor the TCB were allocated dynamically, so
  4171. * nothing needs to be freed. */
  4172. configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
  4173. mtCOVERAGE_TEST_MARKER();
  4174. }
  4175. }
  4176. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
  4177. }
  4178. #endif /* INCLUDE_vTaskDelete */
  4179. /*-----------------------------------------------------------*/
  4180. static void prvResetNextTaskUnblockTime( void )
  4181. {
  4182. if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
  4183. {
  4184. /* The new current delayed list is empty. Set xNextTaskUnblockTime to
  4185. * the maximum possible value so it is extremely unlikely that the
  4186. * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
  4187. * there is an item in the delayed list. */
  4188. xNextTaskUnblockTime = portMAX_DELAY;
  4189. }
  4190. else
  4191. {
  4192. /* The new current delayed list is not empty, get the value of
  4193. * the item at the head of the delayed list. This is the time at
  4194. * which the task at the head of the delayed list should be removed
  4195. * from the Blocked state. */
  4196. xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
  4197. }
  4198. }
  4199. /*-----------------------------------------------------------*/
  4200. #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
  4201. TaskHandle_t xTaskGetCurrentTaskHandle( void )
  4202. {
  4203. TaskHandle_t xReturn;
  4204. UBaseType_t uxSavedInterruptStatus;
  4205. /* For SMP, we need to disable interrupts to ensure the caller does not
  4206. * switch cores in between portGET_CORE_ID() and fetching the current
  4207. * core's TCB. We use the ISR versions of interrupt macros as this
  4208. * function could be called inside critical sections.
  4209. *
  4210. * For single-core a critical section is not required as this is not
  4211. * called from an interrupt and the current TCB will always be the same
  4212. * for any individual execution thread. */
  4213. uxSavedInterruptStatus = prvDISABLE_INTERRUPTS_ISR_SMP_ONLY();
  4214. {
  4215. xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
  4216. }
  4217. prvENABLE_INTERRUPTS_ISR_SMP_ONLY( uxSavedInterruptStatus );
  4218. return xReturn;
  4219. }
  4220. #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
  4221. /*-----------------------------------------------------------*/
  4222. #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
  4223. BaseType_t xTaskGetSchedulerState( void )
  4224. {
  4225. BaseType_t xReturn;
  4226. UBaseType_t uxSavedInterruptStatus;
  4227. /* For SMP, we need to disable interrupts here to ensure we don't switch
  4228. * cores midway. We forego taking the kernel lock here as a minor
  4229. * optimization as it is not required.
  4230. *
  4231. * - xSchedulerRunning is only ever set by core 0 atomically
  4232. * - Each core will only ever update its own copy of uxSchedulerSuspended.
  4233. *
  4234. * We use the ISR versions of interrupt macros as this function could be
  4235. * called inside critical sections. */
  4236. uxSavedInterruptStatus = prvDISABLE_INTERRUPTS_ISR_SMP_ONLY();
  4237. {
  4238. if( xSchedulerRunning == pdFALSE )
  4239. {
  4240. xReturn = taskSCHEDULER_NOT_STARTED;
  4241. }
  4242. else
  4243. {
  4244. if( uxSchedulerSuspended[ portGET_CORE_ID() ] == ( UBaseType_t ) pdFALSE )
  4245. {
  4246. xReturn = taskSCHEDULER_RUNNING;
  4247. }
  4248. else
  4249. {
  4250. xReturn = taskSCHEDULER_SUSPENDED;
  4251. }
  4252. }
  4253. }
  4254. prvENABLE_INTERRUPTS_ISR_SMP_ONLY( uxSavedInterruptStatus );
  4255. return xReturn;
  4256. }
  4257. #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
  4258. /*-----------------------------------------------------------*/
  4259. #if ( configUSE_MUTEXES == 1 )
  4260. BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
  4261. {
  4262. TCB_t * const pxMutexHolderTCB = pxMutexHolder;
  4263. BaseType_t xReturn = pdFALSE;
  4264. /* For SMP, we need to take the kernel lock here as we are about to
  4265. * access kernel data structures. */
  4266. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  4267. {
  4268. /* Get current core ID as we can no longer be preempted. */
  4269. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4270. /* If the mutex was given back by an interrupt while the queue was
  4271. * locked then the mutex holder might now be NULL. _RB_ Is this still
  4272. * needed as interrupts can no longer use mutexes? */
  4273. if( pxMutexHolder != NULL )
  4274. {
  4275. /* If the holder of the mutex has a priority below the priority of
  4276. * the task attempting to obtain the mutex then it will temporarily
  4277. * inherit the priority of the task attempting to obtain the mutex. */
  4278. if( pxMutexHolderTCB->uxPriority < pxCurrentTCBs[ xCurCoreID ]->uxPriority )
  4279. {
  4280. /* Adjust the mutex holder state to account for its new
  4281. * priority. Only reset the event list item value if the value is
  4282. * not being used for anything else. */
  4283. if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  4284. {
  4285. listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCBs[ xCurCoreID ]->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  4286. }
  4287. else
  4288. {
  4289. mtCOVERAGE_TEST_MARKER();
  4290. }
  4291. /* If the task being modified is in the ready state it will need
  4292. * to be moved into a new list. */
  4293. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
  4294. {
  4295. if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  4296. {
  4297. /* It is known that the task is in its ready list so
  4298. * there is no need to check again and the port level
  4299. * reset macro can be called directly. */
  4300. portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
  4301. }
  4302. else
  4303. {
  4304. mtCOVERAGE_TEST_MARKER();
  4305. }
  4306. /* Inherit the priority before being moved into the new list. */
  4307. pxMutexHolderTCB->uxPriority = pxCurrentTCBs[ xCurCoreID ]->uxPriority;
  4308. prvAddTaskToReadyList( pxMutexHolderTCB );
  4309. }
  4310. else
  4311. {
  4312. /* Just inherit the priority. */
  4313. pxMutexHolderTCB->uxPriority = pxCurrentTCBs[ xCurCoreID ]->uxPriority;
  4314. }
  4315. traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCBs[ xCurCoreID ]->uxPriority );
  4316. /* Inheritance occurred. */
  4317. xReturn = pdTRUE;
  4318. }
  4319. else
  4320. {
  4321. if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCBs[ xCurCoreID ]->uxPriority )
  4322. {
  4323. /* The base priority of the mutex holder is lower than the
  4324. * priority of the task attempting to take the mutex, but the
  4325. * current priority of the mutex holder is not lower than the
  4326. * priority of the task attempting to take the mutex.
  4327. * Therefore the mutex holder must have already inherited a
  4328. * priority, but inheritance would have occurred if that had
  4329. * not been the case. */
  4330. xReturn = pdTRUE;
  4331. }
  4332. else
  4333. {
  4334. mtCOVERAGE_TEST_MARKER();
  4335. }
  4336. }
  4337. }
  4338. else
  4339. {
  4340. mtCOVERAGE_TEST_MARKER();
  4341. }
  4342. }
  4343. /* Release the previously taken kernel lock. */
  4344. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  4345. return xReturn;
  4346. }
  4347. #endif /* configUSE_MUTEXES */
  4348. /*-----------------------------------------------------------*/
  4349. #if ( configUSE_MUTEXES == 1 )
  4350. BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
  4351. {
  4352. TCB_t * const pxTCB = pxMutexHolder;
  4353. BaseType_t xReturn = pdFALSE;
  4354. /* For SMP, we need to take the kernel lock here as we are about to
  4355. * access kernel data structures. */
  4356. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  4357. {
  4358. if( pxMutexHolder != NULL )
  4359. {
  4360. /* A task can only have an inherited priority if it holds the mutex.
  4361. * If the mutex is held by a task then it cannot be given from an
  4362. * interrupt, and if a mutex is given by the holding task then it must
  4363. * be the running state task. */
  4364. configASSERT( pxTCB == pxCurrentTCBs[ portGET_CORE_ID() ] );
  4365. configASSERT( pxTCB->uxMutexesHeld );
  4366. ( pxTCB->uxMutexesHeld )--;
  4367. /* Has the holder of the mutex inherited the priority of another
  4368. * task? */
  4369. if( pxTCB->uxPriority != pxTCB->uxBasePriority )
  4370. {
  4371. /* Only disinherit if no other mutexes are held. */
  4372. if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
  4373. {
  4374. /* A task can only have an inherited priority if it holds
  4375. * the mutex. If the mutex is held by a task then it cannot be
  4376. * given from an interrupt, and if a mutex is given by the
  4377. * holding task then it must be the running state task. Remove
  4378. * the holding task from the ready list. */
  4379. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  4380. {
  4381. portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
  4382. }
  4383. else
  4384. {
  4385. mtCOVERAGE_TEST_MARKER();
  4386. }
  4387. /* Disinherit the priority before adding the task into the
  4388. * new ready list. */
  4389. traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
  4390. pxTCB->uxPriority = pxTCB->uxBasePriority;
  4391. /* Reset the event list item value. It cannot be in use for
  4392. * any other purpose if this task is running, and it must be
  4393. * running to give back the mutex. */
  4394. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  4395. prvAddTaskToReadyList( pxTCB );
  4396. /* Return true to indicate that a context switch is required.
  4397. * This is only actually required in the corner case whereby
  4398. * multiple mutexes were held and the mutexes were given back
  4399. * in an order different to that in which they were taken.
  4400. * If a context switch did not occur when the first mutex was
  4401. * returned, even if a task was waiting on it, then a context
  4402. * switch should occur when the last mutex is returned whether
  4403. * a task is waiting on it or not. */
  4404. xReturn = pdTRUE;
  4405. }
  4406. else
  4407. {
  4408. mtCOVERAGE_TEST_MARKER();
  4409. }
  4410. }
  4411. else
  4412. {
  4413. mtCOVERAGE_TEST_MARKER();
  4414. }
  4415. }
  4416. else
  4417. {
  4418. mtCOVERAGE_TEST_MARKER();
  4419. }
  4420. }
  4421. /* Release the previously taken kernel lock. */
  4422. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  4423. return xReturn;
  4424. }
  4425. #endif /* configUSE_MUTEXES */
  4426. /*-----------------------------------------------------------*/
  4427. #if ( configUSE_MUTEXES == 1 )
  4428. void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
  4429. UBaseType_t uxHighestPriorityWaitingTask )
  4430. {
  4431. TCB_t * const pxTCB = pxMutexHolder;
  4432. UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
  4433. const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
  4434. /* For SMP, we need to take the kernel lock here as we are about to
  4435. * access kernel data structures. */
  4436. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  4437. {
  4438. if( pxMutexHolder != NULL )
  4439. {
  4440. /* If pxMutexHolder is not NULL then the holder must hold at least
  4441. * one mutex. */
  4442. configASSERT( pxTCB->uxMutexesHeld );
  4443. /* Determine the priority to which the priority of the task that
  4444. * holds the mutex should be set. This will be the greater of the
  4445. * holding task's base priority and the priority of the highest
  4446. * priority task that is waiting to obtain the mutex. */
  4447. if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
  4448. {
  4449. uxPriorityToUse = uxHighestPriorityWaitingTask;
  4450. }
  4451. else
  4452. {
  4453. uxPriorityToUse = pxTCB->uxBasePriority;
  4454. }
  4455. /* Does the priority need to change? */
  4456. if( pxTCB->uxPriority != uxPriorityToUse )
  4457. {
  4458. /* Only disinherit if no other mutexes are held. This is a
  4459. * simplification in the priority inheritance implementation. If
  4460. * the task that holds the mutex is also holding other mutexes then
  4461. * the other mutexes may have caused the priority inheritance. */
  4462. if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
  4463. {
  4464. /* If a task has timed out because it already holds the
  4465. * mutex it was trying to obtain then it cannot of inherited
  4466. * its own priority. */
  4467. configASSERT( pxTCB != pxCurrentTCBs[ portGET_CORE_ID() ] );
  4468. /* Disinherit the priority, remembering the previous
  4469. * priority to facilitate determining the subject task's
  4470. * state. */
  4471. traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
  4472. uxPriorityUsedOnEntry = pxTCB->uxPriority;
  4473. pxTCB->uxPriority = uxPriorityToUse;
  4474. /* Only reset the event list item value if the value is not
  4475. * being used for anything else. */
  4476. if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
  4477. {
  4478. listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  4479. }
  4480. else
  4481. {
  4482. mtCOVERAGE_TEST_MARKER();
  4483. }
  4484. /* If the running task is not the task that holds the mutex
  4485. * then the task that holds the mutex could be in either the
  4486. * Ready, Blocked or Suspended states. Only remove the task
  4487. * from its current state list if it is in the Ready state as
  4488. * the task's priority is going to change and there is one
  4489. * Ready list per priority. */
  4490. if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
  4491. {
  4492. if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
  4493. {
  4494. /* It is known that the task is in its ready list so
  4495. * there is no need to check again and the port level
  4496. * reset macro can be called directly. */
  4497. portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
  4498. }
  4499. else
  4500. {
  4501. mtCOVERAGE_TEST_MARKER();
  4502. }
  4503. prvAddTaskToReadyList( pxTCB );
  4504. }
  4505. else
  4506. {
  4507. mtCOVERAGE_TEST_MARKER();
  4508. }
  4509. }
  4510. else
  4511. {
  4512. mtCOVERAGE_TEST_MARKER();
  4513. }
  4514. }
  4515. else
  4516. {
  4517. mtCOVERAGE_TEST_MARKER();
  4518. }
  4519. }
  4520. else
  4521. {
  4522. mtCOVERAGE_TEST_MARKER();
  4523. }
  4524. }
  4525. /* Release the previously taken kernel lock. */
  4526. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  4527. }
  4528. #endif /* configUSE_MUTEXES */
  4529. /*-----------------------------------------------------------*/
  4530. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  4531. void vTaskEnterCritical( void )
  4532. {
  4533. portDISABLE_INTERRUPTS();
  4534. if( xSchedulerRunning != pdFALSE )
  4535. {
  4536. /* Get current core ID as we can no longer be preempted. */
  4537. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4538. ( pxCurrentTCBs[ xCurCoreID ]->uxCriticalNesting )++;
  4539. /* This is not the interrupt safe version of the enter critical
  4540. * function so assert() if it is being called from an interrupt
  4541. * context. Only API functions that end in "FromISR" can be used in an
  4542. * interrupt. Only assert if the critical nesting count is 1 to
  4543. * protect against recursive calls if the assert function also uses a
  4544. * critical section. */
  4545. if( pxCurrentTCBs[ xCurCoreID ]->uxCriticalNesting == 1 )
  4546. {
  4547. portASSERT_IF_IN_ISR();
  4548. }
  4549. }
  4550. else
  4551. {
  4552. mtCOVERAGE_TEST_MARKER();
  4553. }
  4554. }
  4555. #endif /* portCRITICAL_NESTING_IN_TCB */
  4556. /*-----------------------------------------------------------*/
  4557. #if ( portCRITICAL_NESTING_IN_TCB == 1 )
  4558. void vTaskExitCritical( void )
  4559. {
  4560. if( xSchedulerRunning != pdFALSE )
  4561. {
  4562. /* Get current core ID as we can no longer be preempted. */
  4563. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4564. if( pxCurrentTCBs[ xCurCoreID ]->uxCriticalNesting > 0U )
  4565. {
  4566. ( pxCurrentTCBs[ xCurCoreID ]->uxCriticalNesting )--;
  4567. if( pxCurrentTCBs[ xCurCoreID ]->uxCriticalNesting == 0U )
  4568. {
  4569. portENABLE_INTERRUPTS();
  4570. }
  4571. else
  4572. {
  4573. mtCOVERAGE_TEST_MARKER();
  4574. }
  4575. }
  4576. else
  4577. {
  4578. mtCOVERAGE_TEST_MARKER();
  4579. }
  4580. }
  4581. else
  4582. {
  4583. mtCOVERAGE_TEST_MARKER();
  4584. }
  4585. }
  4586. #endif /* portCRITICAL_NESTING_IN_TCB */
  4587. /*-----------------------------------------------------------*/
  4588. #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
  4589. static char * prvWriteNameToBuffer( char * pcBuffer,
  4590. const char * pcTaskName )
  4591. {
  4592. size_t x;
  4593. /* Start by copying the entire string. */
  4594. strcpy( pcBuffer, pcTaskName );
  4595. /* Pad the end of the string with spaces to ensure columns line up when
  4596. * printed out. */
  4597. for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
  4598. {
  4599. pcBuffer[ x ] = ' ';
  4600. }
  4601. /* Terminate. */
  4602. pcBuffer[ x ] = ( char ) 0x00;
  4603. /* Return the new end of string. */
  4604. return &( pcBuffer[ x ] );
  4605. }
  4606. #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
  4607. /*-----------------------------------------------------------*/
  4608. #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
  4609. void vTaskList( char * pcWriteBuffer )
  4610. {
  4611. TaskStatus_t * pxTaskStatusArray;
  4612. UBaseType_t uxArraySize, x;
  4613. char cStatus;
  4614. /*
  4615. * PLEASE NOTE:
  4616. *
  4617. * This function is provided for convenience only, and is used by many
  4618. * of the demo applications. Do not consider it to be part of the
  4619. * scheduler.
  4620. *
  4621. * vTaskList() calls uxTaskGetSystemState(), then formats part of the
  4622. * uxTaskGetSystemState() output into a human readable table that
  4623. * displays task: names, states, priority, stack usage and task number.
  4624. * Stack usage specified as the number of unused StackType_t words stack can hold
  4625. * on top of stack - not the number of bytes.
  4626. *
  4627. * vTaskList() has a dependency on the sprintf() C library function that
  4628. * might bloat the code size, use a lot of stack, and provide different
  4629. * results on different platforms. An alternative, tiny, third party,
  4630. * and limited functionality implementation of sprintf() is provided in
  4631. * many of the FreeRTOS/Demo sub-directories in a file called
  4632. * printf-stdarg.c (note printf-stdarg.c does not provide a full
  4633. * snprintf() implementation!).
  4634. *
  4635. * It is recommended that production systems call uxTaskGetSystemState()
  4636. * directly to get access to raw stats data, rather than indirectly
  4637. * through a call to vTaskList().
  4638. */
  4639. /* Make sure the write buffer does not contain a string. */
  4640. *pcWriteBuffer = ( char ) 0x00;
  4641. /* Take a snapshot of the number of tasks in case it changes while this
  4642. * function is executing. */
  4643. uxArraySize = uxCurrentNumberOfTasks;
  4644. /* Allocate an array index for each task. NOTE! if
  4645. * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
  4646. * equate to NULL. */
  4647. pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
  4648. if( pxTaskStatusArray != NULL )
  4649. {
  4650. /* Generate the (binary) data. */
  4651. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
  4652. /* Create a human readable table from the binary data. */
  4653. for( x = 0; x < uxArraySize; x++ )
  4654. {
  4655. switch( pxTaskStatusArray[ x ].eCurrentState )
  4656. {
  4657. case eRunning:
  4658. cStatus = tskRUNNING_CHAR;
  4659. break;
  4660. case eReady:
  4661. cStatus = tskREADY_CHAR;
  4662. break;
  4663. case eBlocked:
  4664. cStatus = tskBLOCKED_CHAR;
  4665. break;
  4666. case eSuspended:
  4667. cStatus = tskSUSPENDED_CHAR;
  4668. break;
  4669. case eDeleted:
  4670. cStatus = tskDELETED_CHAR;
  4671. break;
  4672. case eInvalid: /* Fall through. */
  4673. default: /* Should not get here, but it is included
  4674. * to prevent static checking errors. */
  4675. cStatus = ( char ) 0x00;
  4676. break;
  4677. }
  4678. /* Write the task name to the string, padding with spaces so it
  4679. * can be printed in tabular form more easily. */
  4680. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  4681. /* Write the rest of the string. */
  4682. sprintf( pcWriteBuffer, "\t%c\t%u\t%d\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, pxTaskStatusArray[ x ].xCoreID, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
  4683. pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
  4684. }
  4685. /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
  4686. * is 0 then vPortFree() will be #defined to nothing. */
  4687. vPortFree( pxTaskStatusArray );
  4688. }
  4689. else
  4690. {
  4691. mtCOVERAGE_TEST_MARKER();
  4692. }
  4693. }
  4694. #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  4695. /*----------------------------------------------------------*/
  4696. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
  4697. void vTaskGetRunTimeStats( char * pcWriteBuffer )
  4698. {
  4699. TaskStatus_t * pxTaskStatusArray;
  4700. UBaseType_t uxArraySize, x;
  4701. configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
  4702. /*
  4703. * PLEASE NOTE:
  4704. *
  4705. * This function is provided for convenience only, and is used by many
  4706. * of the demo applications. Do not consider it to be part of the
  4707. * scheduler.
  4708. *
  4709. * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
  4710. * of the uxTaskGetSystemState() output into a human readable table that
  4711. * displays the amount of time each task has spent in the Running state
  4712. * in both absolute and percentage terms.
  4713. *
  4714. * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
  4715. * function that might bloat the code size, use a lot of stack, and
  4716. * provide different results on different platforms. An alternative,
  4717. * tiny, third party, and limited functionality implementation of
  4718. * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
  4719. * a file called printf-stdarg.c (note printf-stdarg.c does not provide
  4720. * a full snprintf() implementation!).
  4721. *
  4722. * It is recommended that production systems call uxTaskGetSystemState()
  4723. * directly to get access to raw stats data, rather than indirectly
  4724. * through a call to vTaskGetRunTimeStats().
  4725. */
  4726. /* Make sure the write buffer does not contain a string. */
  4727. *pcWriteBuffer = ( char ) 0x00;
  4728. /* Take a snapshot of the number of tasks in case it changes while this
  4729. * function is executing. */
  4730. uxArraySize = uxCurrentNumberOfTasks;
  4731. /* Allocate an array index for each task. NOTE! If
  4732. * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
  4733. * equate to NULL. */
  4734. pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
  4735. if( pxTaskStatusArray != NULL )
  4736. {
  4737. /* Generate the (binary) data. */
  4738. uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
  4739. /* For percentage calculations. */
  4740. ulTotalTime /= 100UL;
  4741. /* Avoid divide by zero errors. */
  4742. if( ulTotalTime > 0UL )
  4743. {
  4744. /* Create a human readable table from the binary data. */
  4745. for( x = 0; x < uxArraySize; x++ )
  4746. {
  4747. /* What percentage of the total run time has the task used?
  4748. * This will always be rounded down to the nearest integer.
  4749. * ulTotalRunTime has already been divided by 100. */
  4750. ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
  4751. /* Write the task name to the string, padding with
  4752. * spaces so it can be printed in tabular form more
  4753. * easily. */
  4754. pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
  4755. if( ulStatsAsPercentage > 0UL )
  4756. {
  4757. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  4758. {
  4759. sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
  4760. }
  4761. #else
  4762. {
  4763. /* sizeof( int ) == sizeof( long ) so a smaller
  4764. * printf() library can be used. */
  4765. sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
  4766. }
  4767. #endif
  4768. }
  4769. else
  4770. {
  4771. /* If the percentage is zero here then the task has
  4772. * consumed less than 1% of the total run time. */
  4773. #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
  4774. {
  4775. sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
  4776. }
  4777. #else
  4778. {
  4779. /* sizeof( int ) == sizeof( long ) so a smaller
  4780. * printf() library can be used. */
  4781. sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
  4782. }
  4783. #endif
  4784. }
  4785. pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
  4786. }
  4787. }
  4788. else
  4789. {
  4790. mtCOVERAGE_TEST_MARKER();
  4791. }
  4792. /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
  4793. * is 0 then vPortFree() will be #defined to nothing. */
  4794. vPortFree( pxTaskStatusArray );
  4795. }
  4796. else
  4797. {
  4798. mtCOVERAGE_TEST_MARKER();
  4799. }
  4800. }
  4801. #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
  4802. /*-----------------------------------------------------------*/
  4803. TickType_t uxTaskResetEventItemValue( void )
  4804. {
  4805. TickType_t uxReturn;
  4806. /* For SMP, we need to take the kernel lock here to ensure nothing else
  4807. * modifies the task's event item value simultaneously. */
  4808. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  4809. {
  4810. /* Get current core ID as we can no longer be preempted. */
  4811. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4812. uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCBs[ xCurCoreID ]->xEventListItem ) );
  4813. /* Reset the event list item to its normal value - so it can be used with
  4814. * queues and semaphores. */
  4815. listSET_LIST_ITEM_VALUE( &( pxCurrentTCBs[ xCurCoreID ]->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCBs[ xCurCoreID ]->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
  4816. }
  4817. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  4818. /* Release the previously taken kernel lock. */
  4819. return uxReturn;
  4820. }
  4821. /*-----------------------------------------------------------*/
  4822. #if ( configUSE_MUTEXES == 1 )
  4823. TaskHandle_t pvTaskIncrementMutexHeldCount( void )
  4824. {
  4825. TaskHandle_t xReturn;
  4826. /* For SMP, we need to take the kernel lock here as we are about to
  4827. * access kernel data structures. */
  4828. prvENTER_CRITICAL_SMP_ONLY( &xKernelLock );
  4829. {
  4830. /* Get current core ID as we can no longer be preempted. */
  4831. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4832. /* If xSemaphoreCreateMutex() is called before any tasks have been created
  4833. * then pxCurrentTCBs will be NULL. */
  4834. if( pxCurrentTCBs[ xCurCoreID ] != NULL )
  4835. {
  4836. ( pxCurrentTCBs[ xCurCoreID ]->uxMutexesHeld )++;
  4837. }
  4838. xReturn = pxCurrentTCBs[ xCurCoreID ];
  4839. }
  4840. /* Release the previously taken kernel lock. */
  4841. prvEXIT_CRITICAL_SMP_ONLY( &xKernelLock );
  4842. return xReturn;
  4843. }
  4844. #endif /* configUSE_MUTEXES */
  4845. /*-----------------------------------------------------------*/
  4846. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4847. uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
  4848. BaseType_t xClearCountOnExit,
  4849. TickType_t xTicksToWait )
  4850. {
  4851. uint32_t ulReturn;
  4852. configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4853. taskENTER_CRITICAL( &xKernelLock );
  4854. {
  4855. /* Get current core ID as we can no longer be preempted. */
  4856. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4857. /* Only block if the notification count is not already non-zero. */
  4858. if( pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ] == 0UL )
  4859. {
  4860. /* Mark this task as waiting for a notification. */
  4861. pxCurrentTCBs[ xCurCoreID ]->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
  4862. if( xTicksToWait > ( TickType_t ) 0 )
  4863. {
  4864. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  4865. traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
  4866. /* All ports are written to allow a yield in a critical
  4867. * section (some will yield immediately, others wait until the
  4868. * critical section exits) - but it is not something that
  4869. * application code should ever do. */
  4870. portYIELD_WITHIN_API();
  4871. }
  4872. else
  4873. {
  4874. mtCOVERAGE_TEST_MARKER();
  4875. }
  4876. }
  4877. else
  4878. {
  4879. mtCOVERAGE_TEST_MARKER();
  4880. }
  4881. }
  4882. taskEXIT_CRITICAL( &xKernelLock );
  4883. taskENTER_CRITICAL( &xKernelLock );
  4884. {
  4885. /* Get current core ID as we can no longer be preempted. */
  4886. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4887. traceTASK_NOTIFY_TAKE( uxIndexToWait );
  4888. ulReturn = pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ];
  4889. if( ulReturn != 0UL )
  4890. {
  4891. if( xClearCountOnExit != pdFALSE )
  4892. {
  4893. pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ] = 0UL;
  4894. }
  4895. else
  4896. {
  4897. pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
  4898. }
  4899. }
  4900. else
  4901. {
  4902. mtCOVERAGE_TEST_MARKER();
  4903. }
  4904. pxCurrentTCBs[ xCurCoreID ]->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
  4905. }
  4906. taskEXIT_CRITICAL( &xKernelLock );
  4907. return ulReturn;
  4908. }
  4909. #endif /* configUSE_TASK_NOTIFICATIONS */
  4910. /*-----------------------------------------------------------*/
  4911. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4912. BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
  4913. uint32_t ulBitsToClearOnEntry,
  4914. uint32_t ulBitsToClearOnExit,
  4915. uint32_t * pulNotificationValue,
  4916. TickType_t xTicksToWait )
  4917. {
  4918. BaseType_t xReturn;
  4919. configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4920. taskENTER_CRITICAL( &xKernelLock );
  4921. {
  4922. /* Get current core ID as we can no longer be preempted. */
  4923. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4924. /* Only block if a notification is not already pending. */
  4925. if( pxCurrentTCBs[ xCurCoreID ]->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
  4926. {
  4927. /* Clear bits in the task's notification value as bits may get
  4928. * set by the notifying task or interrupt. This can be used to
  4929. * clear the value to zero. */
  4930. pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
  4931. /* Mark this task as waiting for a notification. */
  4932. pxCurrentTCBs[ xCurCoreID ]->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
  4933. if( xTicksToWait > ( TickType_t ) 0 )
  4934. {
  4935. prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
  4936. traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
  4937. /* All ports are written to allow a yield in a critical
  4938. * section (some will yield immediately, others wait until the
  4939. * critical section exits) - but it is not something that
  4940. * application code should ever do. */
  4941. portYIELD_WITHIN_API();
  4942. }
  4943. else
  4944. {
  4945. mtCOVERAGE_TEST_MARKER();
  4946. }
  4947. }
  4948. else
  4949. {
  4950. mtCOVERAGE_TEST_MARKER();
  4951. }
  4952. }
  4953. taskEXIT_CRITICAL( &xKernelLock );
  4954. taskENTER_CRITICAL( &xKernelLock );
  4955. {
  4956. /* Get current core ID as we can no longer be preempted. */
  4957. const BaseType_t xCurCoreID = portGET_CORE_ID();
  4958. traceTASK_NOTIFY_WAIT( uxIndexToWait );
  4959. if( pulNotificationValue != NULL )
  4960. {
  4961. /* Output the current notification value, which may or may not
  4962. * have changed. */
  4963. *pulNotificationValue = pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ];
  4964. }
  4965. /* If ucNotifyValue is set then either the task never entered the
  4966. * blocked state (because a notification was already pending) or the
  4967. * task unblocked because of a notification. Otherwise the task
  4968. * unblocked because of a timeout. */
  4969. if( pxCurrentTCBs[ xCurCoreID ]->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
  4970. {
  4971. /* A notification was not received. */
  4972. xReturn = pdFALSE;
  4973. }
  4974. else
  4975. {
  4976. /* A notification was already pending or a notification was
  4977. * received while the task was waiting. */
  4978. pxCurrentTCBs[ xCurCoreID ]->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
  4979. xReturn = pdTRUE;
  4980. }
  4981. pxCurrentTCBs[ xCurCoreID ]->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
  4982. }
  4983. taskEXIT_CRITICAL( &xKernelLock );
  4984. return xReturn;
  4985. }
  4986. #endif /* configUSE_TASK_NOTIFICATIONS */
  4987. /*-----------------------------------------------------------*/
  4988. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  4989. BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
  4990. UBaseType_t uxIndexToNotify,
  4991. uint32_t ulValue,
  4992. eNotifyAction eAction,
  4993. uint32_t * pulPreviousNotificationValue )
  4994. {
  4995. TCB_t * pxTCB;
  4996. BaseType_t xReturn = pdPASS;
  4997. uint8_t ucOriginalNotifyState;
  4998. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  4999. configASSERT( xTaskToNotify );
  5000. pxTCB = xTaskToNotify;
  5001. taskENTER_CRITICAL( &xKernelLock );
  5002. {
  5003. if( pulPreviousNotificationValue != NULL )
  5004. {
  5005. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  5006. }
  5007. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  5008. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  5009. switch( eAction )
  5010. {
  5011. case eSetBits:
  5012. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  5013. break;
  5014. case eIncrement:
  5015. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  5016. break;
  5017. case eSetValueWithOverwrite:
  5018. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  5019. break;
  5020. case eSetValueWithoutOverwrite:
  5021. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  5022. {
  5023. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  5024. }
  5025. else
  5026. {
  5027. /* The value could not be written to the task. */
  5028. xReturn = pdFAIL;
  5029. }
  5030. break;
  5031. case eNoAction:
  5032. /* The task is being notified without its notify value being
  5033. * updated. */
  5034. break;
  5035. default:
  5036. /* Should not get here if all enums are handled.
  5037. * Artificially force an assert by testing a value the
  5038. * compiler can't assume is const. */
  5039. configASSERT( xTickCount == ( TickType_t ) 0 );
  5040. break;
  5041. }
  5042. traceTASK_NOTIFY( uxIndexToNotify );
  5043. /* If the task is in the blocked state specifically to wait for a
  5044. * notification then unblock it now. */
  5045. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  5046. {
  5047. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  5048. prvAddTaskToReadyList( pxTCB );
  5049. /* The task should not have been on an event list. */
  5050. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  5051. #if ( configUSE_TICKLESS_IDLE != 0 )
  5052. {
  5053. /* If a task is blocked waiting for a notification then
  5054. * xNextTaskUnblockTime might be set to the blocked task's time
  5055. * out time. If the task is unblocked for a reason other than
  5056. * a timeout xNextTaskUnblockTime is normally left unchanged,
  5057. * because it will automatically get reset to a new value when
  5058. * the tick count equals xNextTaskUnblockTime. However if
  5059. * tickless idling is used it might be more important to enter
  5060. * sleep mode at the earliest possible time - so reset
  5061. * xNextTaskUnblockTime here to ensure it is updated at the
  5062. * earliest possible time. */
  5063. prvResetNextTaskUnblockTime();
  5064. }
  5065. #endif
  5066. if( taskIS_YIELD_REQUIRED( pxTCB, portGET_CORE_ID(), pdFALSE ) == pdTRUE )
  5067. {
  5068. /* The notified task has a priority above the currently
  5069. * executing task so a yield is required. */
  5070. taskYIELD_IF_USING_PREEMPTION();
  5071. }
  5072. else
  5073. {
  5074. mtCOVERAGE_TEST_MARKER();
  5075. }
  5076. }
  5077. else
  5078. {
  5079. mtCOVERAGE_TEST_MARKER();
  5080. }
  5081. }
  5082. taskEXIT_CRITICAL( &xKernelLock );
  5083. return xReturn;
  5084. }
  5085. #endif /* configUSE_TASK_NOTIFICATIONS */
  5086. /*-----------------------------------------------------------*/
  5087. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  5088. BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
  5089. UBaseType_t uxIndexToNotify,
  5090. uint32_t ulValue,
  5091. eNotifyAction eAction,
  5092. uint32_t * pulPreviousNotificationValue,
  5093. BaseType_t * pxHigherPriorityTaskWoken )
  5094. {
  5095. TCB_t * pxTCB;
  5096. uint8_t ucOriginalNotifyState;
  5097. BaseType_t xReturn = pdPASS;
  5098. UBaseType_t uxSavedInterruptStatus;
  5099. configASSERT( xTaskToNotify );
  5100. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  5101. /* RTOS ports that support interrupt nesting have the concept of a
  5102. * maximum system call (or maximum API call) interrupt priority.
  5103. * Interrupts that are above the maximum system call priority are keep
  5104. * permanently enabled, even when the RTOS kernel is in a critical section,
  5105. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  5106. * is defined in FreeRTOSConfig.h then
  5107. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  5108. * failure if a FreeRTOS API function is called from an interrupt that has
  5109. * been assigned a priority above the configured maximum system call
  5110. * priority. Only FreeRTOS functions that end in FromISR can be called
  5111. * from interrupts that have been assigned a priority at or (logically)
  5112. * below the maximum system call interrupt priority. FreeRTOS maintains a
  5113. * separate interrupt safe API to ensure interrupt entry is as fast and as
  5114. * simple as possible. More information (albeit Cortex-M specific) is
  5115. * provided on the following link:
  5116. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  5117. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  5118. pxTCB = xTaskToNotify;
  5119. prvENTER_CRITICAL_OR_MASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  5120. {
  5121. /* Get current core ID as we can no longer be preempted. */
  5122. const BaseType_t xCurCoreID = portGET_CORE_ID();
  5123. if( pulPreviousNotificationValue != NULL )
  5124. {
  5125. *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
  5126. }
  5127. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  5128. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  5129. switch( eAction )
  5130. {
  5131. case eSetBits:
  5132. pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
  5133. break;
  5134. case eIncrement:
  5135. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  5136. break;
  5137. case eSetValueWithOverwrite:
  5138. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  5139. break;
  5140. case eSetValueWithoutOverwrite:
  5141. if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
  5142. {
  5143. pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
  5144. }
  5145. else
  5146. {
  5147. /* The value could not be written to the task. */
  5148. xReturn = pdFAIL;
  5149. }
  5150. break;
  5151. case eNoAction:
  5152. /* The task is being notified without its notify value being
  5153. * updated. */
  5154. break;
  5155. default:
  5156. /* Should not get here if all enums are handled.
  5157. * Artificially force an assert by testing a value the
  5158. * compiler can't assume is const. */
  5159. configASSERT( xTickCount == ( TickType_t ) 0 );
  5160. break;
  5161. }
  5162. traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
  5163. /* If the task is in the blocked state specifically to wait for a
  5164. * notification then unblock it now. */
  5165. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  5166. {
  5167. /* The task should not have been on an event list. */
  5168. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  5169. if( taskCAN_BE_SCHEDULED( pxTCB ) == pdTRUE )
  5170. {
  5171. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  5172. prvAddTaskToReadyList( pxTCB );
  5173. }
  5174. else
  5175. {
  5176. /* The delayed and ready lists cannot be accessed, so hold
  5177. * this task pending until the scheduler is resumed. */
  5178. listINSERT_END( &( xPendingReadyList[ xCurCoreID ] ), &( pxTCB->xEventListItem ) );
  5179. }
  5180. if( taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, pdFALSE ) == pdTRUE )
  5181. {
  5182. /* The notified task has a priority above the currently
  5183. * executing task so a yield is required. */
  5184. if( pxHigherPriorityTaskWoken != NULL )
  5185. {
  5186. *pxHigherPriorityTaskWoken = pdTRUE;
  5187. }
  5188. /* Mark that a yield is pending in case the user is not
  5189. * using the "xHigherPriorityTaskWoken" parameter to an ISR
  5190. * safe FreeRTOS function. */
  5191. xYieldPending[ xCurCoreID ] = pdTRUE;
  5192. }
  5193. else
  5194. {
  5195. mtCOVERAGE_TEST_MARKER();
  5196. }
  5197. }
  5198. }
  5199. prvEXIT_CRITICAL_OR_UNMASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  5200. return xReturn;
  5201. }
  5202. #endif /* configUSE_TASK_NOTIFICATIONS */
  5203. /*-----------------------------------------------------------*/
  5204. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  5205. void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
  5206. UBaseType_t uxIndexToNotify,
  5207. BaseType_t * pxHigherPriorityTaskWoken )
  5208. {
  5209. TCB_t * pxTCB;
  5210. uint8_t ucOriginalNotifyState;
  5211. UBaseType_t uxSavedInterruptStatus;
  5212. configASSERT( xTaskToNotify );
  5213. configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  5214. /* RTOS ports that support interrupt nesting have the concept of a
  5215. * maximum system call (or maximum API call) interrupt priority.
  5216. * Interrupts that are above the maximum system call priority are keep
  5217. * permanently enabled, even when the RTOS kernel is in a critical section,
  5218. * but cannot make any calls to FreeRTOS API functions. If configASSERT()
  5219. * is defined in FreeRTOSConfig.h then
  5220. * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
  5221. * failure if a FreeRTOS API function is called from an interrupt that has
  5222. * been assigned a priority above the configured maximum system call
  5223. * priority. Only FreeRTOS functions that end in FromISR can be called
  5224. * from interrupts that have been assigned a priority at or (logically)
  5225. * below the maximum system call interrupt priority. FreeRTOS maintains a
  5226. * separate interrupt safe API to ensure interrupt entry is as fast and as
  5227. * simple as possible. More information (albeit Cortex-M specific) is
  5228. * provided on the following link:
  5229. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  5230. portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
  5231. pxTCB = xTaskToNotify;
  5232. prvENTER_CRITICAL_OR_MASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  5233. {
  5234. /* Get current core ID as we can no longer be preempted. */
  5235. const BaseType_t xCurCoreID = portGET_CORE_ID();
  5236. ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
  5237. pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
  5238. /* 'Giving' is equivalent to incrementing a count in a counting
  5239. * semaphore. */
  5240. ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
  5241. traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
  5242. /* If the task is in the blocked state specifically to wait for a
  5243. * notification then unblock it now. */
  5244. if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
  5245. {
  5246. /* The task should not have been on an event list. */
  5247. configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
  5248. if( taskCAN_BE_SCHEDULED( pxTCB ) == pdTRUE )
  5249. {
  5250. listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
  5251. prvAddTaskToReadyList( pxTCB );
  5252. }
  5253. else
  5254. {
  5255. /* The delayed and ready lists cannot be accessed, so hold
  5256. * this task pending until the scheduler is resumed. */
  5257. listINSERT_END( &( xPendingReadyList[ xCurCoreID ] ), &( pxTCB->xEventListItem ) );
  5258. }
  5259. if( taskIS_YIELD_REQUIRED( pxTCB, xCurCoreID, pdFALSE ) == pdTRUE )
  5260. {
  5261. /* The notified task has a priority above the currently
  5262. * executing task so a yield is required. */
  5263. if( pxHigherPriorityTaskWoken != NULL )
  5264. {
  5265. *pxHigherPriorityTaskWoken = pdTRUE;
  5266. }
  5267. /* Mark that a yield is pending in case the user is not
  5268. * using the "xHigherPriorityTaskWoken" parameter in an ISR
  5269. * safe FreeRTOS function. */
  5270. xYieldPending[ xCurCoreID ] = pdTRUE;
  5271. }
  5272. else
  5273. {
  5274. mtCOVERAGE_TEST_MARKER();
  5275. }
  5276. }
  5277. }
  5278. prvEXIT_CRITICAL_OR_UNMASK_ISR( &xKernelLock, uxSavedInterruptStatus );
  5279. }
  5280. #endif /* configUSE_TASK_NOTIFICATIONS */
  5281. /*-----------------------------------------------------------*/
  5282. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  5283. BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
  5284. UBaseType_t uxIndexToClear )
  5285. {
  5286. TCB_t * pxTCB;
  5287. BaseType_t xReturn;
  5288. configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
  5289. /* If null is passed in here then it is the calling task that is having
  5290. * its notification state cleared. */
  5291. pxTCB = prvGetTCBFromHandle( xTask );
  5292. taskENTER_CRITICAL( &xKernelLock );
  5293. {
  5294. if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
  5295. {
  5296. pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
  5297. xReturn = pdPASS;
  5298. }
  5299. else
  5300. {
  5301. xReturn = pdFAIL;
  5302. }
  5303. }
  5304. taskEXIT_CRITICAL( &xKernelLock );
  5305. return xReturn;
  5306. }
  5307. #endif /* configUSE_TASK_NOTIFICATIONS */
  5308. /*-----------------------------------------------------------*/
  5309. #if ( configUSE_TASK_NOTIFICATIONS == 1 )
  5310. uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
  5311. UBaseType_t uxIndexToClear,
  5312. uint32_t ulBitsToClear )
  5313. {
  5314. TCB_t * pxTCB;
  5315. uint32_t ulReturn;
  5316. /* If null is passed in here then it is the calling task that is having
  5317. * its notification state cleared. */
  5318. pxTCB = prvGetTCBFromHandle( xTask );
  5319. taskENTER_CRITICAL( &xKernelLock );
  5320. {
  5321. /* Return the notification as it was before the bits were cleared,
  5322. * then clear the bit mask. */
  5323. ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
  5324. pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
  5325. }
  5326. taskEXIT_CRITICAL( &xKernelLock );
  5327. return ulReturn;
  5328. }
  5329. #endif /* configUSE_TASK_NOTIFICATIONS */
  5330. /*-----------------------------------------------------------*/
  5331. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
  5332. configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
  5333. {
  5334. return ulTaskGetIdleRunTimeCounterForCore( portGET_CORE_ID() );
  5335. }
  5336. #endif
  5337. /*-----------------------------------------------------------*/
  5338. #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
  5339. configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
  5340. {
  5341. return ulTaskGetIdleRunTimePercentForCore( portGET_CORE_ID() );
  5342. }
  5343. #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
  5344. /*-----------------------------------------------------------*/
  5345. static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
  5346. const BaseType_t xCanBlockIndefinitely )
  5347. {
  5348. TickType_t xTimeToWake;
  5349. const TickType_t xConstTickCount = xTickCount;
  5350. /* Get current core ID as we can no longer be preempted. */
  5351. const BaseType_t xCurCoreID = portGET_CORE_ID();
  5352. #if ( configNUMBER_OF_CORES > 1 )
  5353. {
  5354. if( listIS_CONTAINED_WITHIN( &xTasksWaitingTermination, &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) ) == pdTRUE )
  5355. {
  5356. /* In SMP, it is possible that another core has already deleted the
  5357. * current task (via vTaskDelete()) which will result in the current
  5358. * task being placed on the waiting termination list. In this case,
  5359. * we do nothing and return, the current task will yield as soon
  5360. * as it re-enables interrupts. */
  5361. return;
  5362. }
  5363. }
  5364. #endif /* configNUMBER_OF_CORES > 1 */
  5365. #if ( INCLUDE_xTaskAbortDelay == 1 )
  5366. {
  5367. /* About to enter a delayed list, so ensure the ucDelayAborted flag is
  5368. * reset to pdFALSE so it can be detected as having been set to pdTRUE
  5369. * when the task leaves the Blocked state. */
  5370. pxCurrentTCBs[ xCurCoreID ]->ucDelayAborted = pdFALSE;
  5371. }
  5372. #endif
  5373. /* Remove the task from the ready list before adding it to the blocked list
  5374. * as the same list item is used for both lists. */
  5375. if( uxListRemove( &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) ) == ( UBaseType_t ) 0 )
  5376. {
  5377. /* The current task must be in a ready list, so there is no need to
  5378. * check, and the port reset macro can be called directly. */
  5379. portRESET_READY_PRIORITY( pxCurrentTCBs[ xCurCoreID ]->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCBs cannot change as it is the calling task. pxCurrentTCBs->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
  5380. }
  5381. else
  5382. {
  5383. mtCOVERAGE_TEST_MARKER();
  5384. }
  5385. #if ( INCLUDE_vTaskSuspend == 1 )
  5386. {
  5387. if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
  5388. {
  5389. /* Add the task to the suspended task list instead of a delayed task
  5390. * list to ensure it is not woken by a timing event. It will block
  5391. * indefinitely. */
  5392. listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) );
  5393. }
  5394. else
  5395. {
  5396. /* Calculate the time at which the task should be woken if the event
  5397. * does not occur. This may overflow but this doesn't matter, the
  5398. * kernel will manage it correctly. */
  5399. xTimeToWake = xConstTickCount + xTicksToWait;
  5400. /* The list item will be inserted in wake time order. */
  5401. listSET_LIST_ITEM_VALUE( &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ), xTimeToWake );
  5402. if( xTimeToWake < xConstTickCount )
  5403. {
  5404. /* Wake time has overflowed. Place this item in the overflow
  5405. * list. */
  5406. vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) );
  5407. }
  5408. else
  5409. {
  5410. /* The wake time has not overflowed, so the current block list
  5411. * is used. */
  5412. vListInsert( pxDelayedTaskList, &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) );
  5413. /* If the task entering the blocked state was placed at the
  5414. * head of the list of blocked tasks then xNextTaskUnblockTime
  5415. * needs to be updated too. */
  5416. if( xTimeToWake < xNextTaskUnblockTime )
  5417. {
  5418. xNextTaskUnblockTime = xTimeToWake;
  5419. }
  5420. else
  5421. {
  5422. mtCOVERAGE_TEST_MARKER();
  5423. }
  5424. }
  5425. }
  5426. }
  5427. #else /* INCLUDE_vTaskSuspend */
  5428. {
  5429. /* Calculate the time at which the task should be woken if the event
  5430. * does not occur. This may overflow but this doesn't matter, the kernel
  5431. * will manage it correctly. */
  5432. xTimeToWake = xConstTickCount + xTicksToWait;
  5433. /* The list item will be inserted in wake time order. */
  5434. listSET_LIST_ITEM_VALUE( &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ), xTimeToWake );
  5435. if( xTimeToWake < xConstTickCount )
  5436. {
  5437. /* Wake time has overflowed. Place this item in the overflow list. */
  5438. vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) );
  5439. }
  5440. else
  5441. {
  5442. /* The wake time has not overflowed, so the current block list is used. */
  5443. vListInsert( pxDelayedTaskList, &( pxCurrentTCBs[ xCurCoreID ]->xStateListItem ) );
  5444. /* If the task entering the blocked state was placed at the head of the
  5445. * list of blocked tasks then xNextTaskUnblockTime needs to be updated
  5446. * too. */
  5447. if( xTimeToWake < xNextTaskUnblockTime )
  5448. {
  5449. xNextTaskUnblockTime = xTimeToWake;
  5450. }
  5451. else
  5452. {
  5453. mtCOVERAGE_TEST_MARKER();
  5454. }
  5455. }
  5456. /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
  5457. ( void ) xCanBlockIndefinitely;
  5458. }
  5459. #endif /* INCLUDE_vTaskSuspend */
  5460. }
  5461. /* Code below here allows additional code to be inserted into this source file,
  5462. * especially where access to file scope functions and data is needed (for example
  5463. * when performing module tests). */
  5464. #ifdef FREERTOS_MODULE_TEST
  5465. #include "tasks_test_access_functions.h"
  5466. #endif
  5467. #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
  5468. #include "freertos_tasks_c_additions.h"
  5469. #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
  5470. static void freertos_tasks_c_additions_init( void )
  5471. {
  5472. FREERTOS_TASKS_C_ADDITIONS_INIT();
  5473. }
  5474. #endif
  5475. #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */