tasks.c 362 KB

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