%%==============================================================================
%%
%%
%%
%%
%%Abstract:
%%SupportforimplementingAsynchronoussourceblocks(Interrupt,Task,
%%Alarm,etc).
%%
%%Copyright2003-2016TheMathWorks,Inc.
%%
 
%if EXISTS("_ASYNCLIB_") == 0
%assign _ASYNCLIB_ = 1
%selectfile NULL_FILE
 
%%=============================================================================
%%Publicfunctions
%%=============================================================================
 
%%DocFunction{OtherUsefulFunctions}:LibGetBlockAttribute===================
%%Abstract:
%%GetafieldvalueinsideaBlockrecord.
%%
%%Callsyntax:
%%%ifLibIsEqual(LibGetBlockAttribute(ssBlock,"MaskType"),"TaskBlock")
%%%assignisTaskBlock=1
%%%endif
%%
%%Returns:
%%Returnsthevalueoftheattribute(field)oremptystringifitdoesn'texist.
%function LibGetBlockAttribute(block,attr) void
  %if ISFIELD(block,attr)
    %return block.%<attr>
  %else
    %return ""
  %endif
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibGetFcnCallBlock=====================
%%Abstract:
%%Givenas-functionblockandindexof'SFcnSystemOutputCall'record,return
%%theblockrecordforthedownstreamfunction-callsubsystemblock.
%%
%%Callsyntax:
%%
%%%assignssBlock=LibGetFcnCallBlock(block,0)
%%
%%Returns:
%%Returnstheblockrecordofthedownstreamf-csubsystemconnectedtothat
%%element(callindex).
%%
 
%function LibGetFcnCallBlock(sfcnblock, recIdx) void
  %with sfcnblock
    %if LibIsEqual(SFcnSystemOutputCall[recIdx].BlockToCall, "unconnected")
      %return ""
    %elseif ISFIELD(sfcnblock,"SFcnSystemOutputCall") && (SIZE(SFcnSystemOutputCall,1) > recIdx)
      %assign ssSysIdx = SFcnSystemOutputCall[recIdx].BlockToCall[0]
      %assign ssBlkIdx = SFcnSystemOutputCall[recIdx].BlockToCall[1]
      %return ::CompiledModel.System[ssSysIdx].Block[ssBlkIdx]
    %else
      %assign ::ShowErrorStackTrace = TLC_TRUE
      %<SLibReportErrorWithIdAndArgs("RTW:tlc:InvalidArg", "LibGetFcnCallBLock")>
    %endif
  %endwith
%endfunction
 
%%FcnGetAsyncTidFromSfcnBlk(blk,el)============================
%%Abstract:
%%Eachelementofasynchronousfunctioncallsignal
%%introduceauniqueasyncsampletimeinmodel.
%%ThisfunctionreturnstheTIDofthesampletimeintroduced
%%bythesfcnblock
%%
%function FcnGetAsyncTidFromSfcnBlk(blk, el)
  %foreach idx = LibGetNumAsyncTasks()
     %assign tid = idx + NumRuntimeExportedRates
     %if ISFIELD(SampleTime[tid], "AsyncSfcnBlkIdx")
       %assign asyncSfcnBlkIdx = SampleTime[tid].AsyncSfcnBlkIdx
       %assign thisBlkIdx = blk.BlockIdx
       %if SampleTime[tid].AsyncSfcnBlkElm == el && ...
         asyncSfcnBlkIdx[0] == thisBlkIdx[0] && ...
         asyncSfcnBlkIdx[1] == thisBlkIdx[2]
         %break
       %endif
     %endif
  %endforeach
  %assert tid < NumSampleTimes
  %return tid
%endfunction
 
%%SLibDumpOutputUpdateCodeForAsyncTid(tidVal)============================
%%Abstract:
%%Thisfunctiondumpstheoutputandupdatecoderategroupedfora
%%giveasynctidatrootlevel.
%%
%function SLibDumpOutputUpdateCodeForAsyncTid(tidVal)
  %return SLibDumpOutputUpdateCodeForAsyncTidInSS(GetBaseSystemIdx(),tidVal)
%endfunction
 
%%SLibDumpOutputUpdateCodeForAsyncTid(ssIdx,tidVal)============================
%%Abstract:
%%Thisfunctiondumpstheoutputandupdatecoderategroupedfora
%%giveasynctidatthegivensubsystemlevel.
%%
%function SLibDumpOutputUpdateCodeForAsyncTidInSS(ssIdx, tidVal)
  %assign baseSystem = ::CompiledModel.System[ssIdx]
 
  %openfile retBuf
  %assign prevRSCurrTid = baseSystem.CurrentTID
  %assign baseSystem.CurrentTID = tidVal
  %if SLibPartitionGroupingSystem(baseSystem)
    %assign prevRSCurrPid = baseSystem.CurrentPID
    %assign pidVec = SLibGetPidFromTid(tidVal)
    %assign baseSystem.CurrentPID = pidVec[0]
  %endif
  %openfile outputFcnLocalVars
  %if MultiInstanceERTCode && !GenerateClassInterface
    %<SLibDumpLocalVariablesForBlockFcn(baseSystem, "OutputUpdate")>
  %endif
  %<LibGetSystemLocalVars(baseSystem, "Output", tidVal)>
  %<LibGetSystemLocalVars(baseSystem, "OutputUpdate", tidVal)>
  %<SLibGetFcnLocalVars(baseSystem, "Output", tidVal)>
  %closefile outputFcnLocalVars
  %assign updateFcnLocalVars = SLibGetFcnLocalVars(baseSystem, "Update", tidVal)
  %assign needSeperateScope = !WHITE_SPACE(outputFcnLocalVars) && !WHITE_SPACE(updateFcnLocalVars)
  %<SLibDumpLocalTidInSystem(baseSystem, "OutputUpdate")>
  %if needSeperateScope
   {
     %<outputFcnLocalVars>
     %<LibDumpSystemUserCode(baseSystem,"Output","Header")>/
     %<LibDumpSystemUserCode(baseSystem,"Output","Body")>/
     %if SLibPartitionGroupingSystem(baseSystem)
       %<LibDumpSystemFcn(baseSystem,"Output%<tidVal>%<baseSystem.CurrentPID>")>/
     %else
       %<LibDumpSystemFcn(baseSystem,"Output%<tidVal>")>/
     %endif
     %<LibDumpSystemUserCode(baseSystem,"Output","Trailer")>/
   }
   {
     %<updateFcnLocalVars>
     %<LibDumpSystemUserCode(baseSystem,"Update","Header")>/
     %<LibDumpSystemUserCode(baseSystem,"Update","Body")>/
     %if SLibPartitionGroupingSystem(baseSystem)
       %<LibDumpSystemFcn(baseSystem,"Update%<tidVal>%<baseSystem.CurrentPID>")>/
     %else
       %<LibDumpSystemFcn(baseSystem,"Update%<tidVal>")>/
     %endif
     %<LibDumpSystemUserCode(baseSystem,"Update","Trailer")>/
   }
  %else
    %<outputFcnLocalVars>
    %<updateFcnLocalVars>
    %<LibDumpSystemUserCode(baseSystem,"Output","Header")>/
    %<LibDumpSystemUserCode(baseSystem,"Output","Body")>/
    %if SLibPartitionGroupingSystem(baseSystem)
      %<LibDumpSystemFcn(baseSystem,"Output%<tidVal>%<baseSystem.CurrentPID>")>/
    %else
      %<LibDumpSystemFcn(baseSystem,"Output%<tidVal>")>/
    %endif
    %<LibDumpSystemUserCode(baseSystem,"Output","Trailer")>/
    %<LibDumpSystemUserCode(baseSystem,"Update","Header")>/
    %<LibDumpSystemUserCode(baseSystem,"Update","Body")>/
    %if SLibPartitionGroupingSystem(baseSystem)
      %<LibDumpSystemFcn(baseSystem,"Update%<tidVal>%<baseSystem.CurrentPID>")>/
    %else
      %<LibDumpSystemFcn(baseSystem,"Update%<tidVal>")>/
    %endif
    %<LibDumpSystemUserCode(baseSystem,"Update","Trailer")>/
  %endif
   
  %if EXISTS(buffsRec.ExtModeTrailer%<tidVal>) && ...
    !WHITE_SPACE(buffsRec.ExtModeTrailer%<tidVal>)
     
    /* external mode */
    %assign tmpBuff = buffsRec.ExtModeTrailer%<tidVal>
    %<tmpBuff>/
  %endif
  %assign baseSystem.CurrentTID = prevRSCurrTid
  %if SLibPartitionGroupingSystem(baseSystem)
    %assign baseSystem.CurrentPID = prevRSCurrPid
  %endif
  %closefile retBuf
  %if !ISFIELD(SampleTime[tidVal], "DumpedAsyncOutputCode")
    %addtorecord SampleTime[tidVal] DumpedAsyncOutputCode TLC_TRUE
  %endif
  %return retBuf
%endfunction
 
 
%%SLibGetAsyncSysTid(ssBlock,recIdx)============================
%%Abstract:
%%Whenanasyncsubsystemhasnestedasyncsubsystem,SubsystemTID
%%ofthisasyncsubsystemisavectorinsteadofscalar.Nestedasyncsubsystem
%%tidisincludedintheSubsystemTID.ThisfunctionreturnsAsyncShellTID
%%inthiscase.
%%
%function SLibGetAsyncSysTid(ssBlock, recIdx)
  %if ssBlock.Type == "SubSystem"
    %assign ssTid = ssBlock.TaskToFirstTID
    %return ssTid[0]
  %else
    %assert ssBlock.Type == "ModelReference"
    %return ssBlock.InputPortTIDs[SFcnSystemOutputCall[recIdx].FcnPortIdx]
  %endif
   
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibSetAsyncCounter=====================
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustasks
%%canmanagetheirowntime.Thisfunctiontoreturncodethatsets
%%thecountervariablethatistobemaintainedbytheasynchronoustask.
%%
%%Example:
%%%ifLibNeedAsyncCounter(block,0)
%%%<LibSetAsyncCounter(block,0,"tickGet()")>
%%%endif
%%
%function LibSetAsyncCounter(sfcnBlock,callIdx,buf) void
  %assign recIdx = sfcnBlock.SFcnSystemOutputCallMappingInfo[callIdx].StartingIdx
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock, recIdx)
  %assign ssTid = SLibGetAsyncSysTid(ssBlock, recIdx)
  %return "%<LibGetClockTick(ssTid)> = %<buf>;"
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibSetAsyncCounterHigh===============
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustasks
%%canmanagetheirowntime.Thisfunctiontoreturncodethatsetsthehigher
%%wordofthecountervariablethatistobemaintainedbytheasynchronoustask.
%%
%%Example:
%%%ifLibNeedAsyncCounter(block,0)
%%%<LibSetAsyncCounterHigh(block,0,"hightTickGet()")>
%%%endif
%%
%function LibSetAsyncCounterHigh(sfcnBlock,callIdx,buf) void
  %assign recIdx = sfcnBlock.SFcnSystemOutputCallMappingInfo[callIdx].StartingIdx
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock, recIdx)
  %assign ssTid = SLibGetAsyncSysTid(ssBlock, recIdx)
  %if SLibClockTickIsUnit32Pair(ssTid)
    %return "%<LibGetClockTickHigh(ssTid)> = %<buf>;"
  %else
    %return ""
  %endif
%endfunction
 
%%SLibUpdateFloatTimeForAsyncTask================================
%%Abstract:
%%
%function SLibUpdateFloatTimeForAsyncTask(tid)
  %% Update float absolute time for async task
  %openfile tmpBuf
  %if SLibClockTickIsUnit32Pair(tid)
    %assign ct = RTMDispatchToAccessFcn("ClockTick%<tid>", "get", ...
      "", "", ::tSimStruct)
    %assign cth = RTMDispatchToAccessFcn("ClockTickH%<tid>", "get", ...
      "", "", ::tSimStruct)
    %assign ss = RTMDispatchToAccessFcn("StepSize%<tid>", "get", ...
      "", "", ::tSimStruct)
    %assign tt = "%<LibGetTaskTime(tid)>"
    %if PurelyIntegerCode
      %% Extmode mode require floating point absolute time, in this
      %% case, PurelyIntegerCode need provide floating point time.
      %%
      %<tt> = %<ct> + %<cth> * 4294967296.0;
    %else
      %<tt> = %<ct> * %<ss> + %<cth> * %<ss> * 4294967296.0;
    %endif
  %else
    %assign ct = RTMDispatchToAccessFcn("ClockTick%<tid>", "get", ...
      "", "", ::tSimStruct)
    %assign ss = RTMDispatchToAccessFcn("StepSize%<tid>", "get", ...
      "", "", ::tSimStruct)
    %assign tt = "%<LibGetTaskTime(tid)>"
     
    %if PurelyIntegerCode
      %<tt> = (%<ct>);
    %else
      %<tt> = (%<ct>) * %<ss>;
    %endif
  %endif
  %closefile tmpBuf
   
  %return tmpBuf
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibSetAsyncClockTicks=====================
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustasks
%%canmanagetheirowntime.ThisfunctiontoreturncodethatsetsclockTick
%%counter(s)thatis(are)tobemaintainebytheasynchronoustask.
%%IfthedatatypeoftheclockTickcounter(s)maintainedbytheasynchronous
%%taskistSS_TIMER_UINT32_PAIR,therlowandhighwordoftheclockTick
%%countersareset.
%%
%%
%%Example:
%%%ifLibNeedAsyncCounter(block,callIdx)
%%%<LibSetAsyncCounter(block,0),buf1,buf2)>
%%%endif
%%
%%buf1:codereadinglowwordofhardwarecounter
%%buf2:codereadinghighwordofhardwarecounter.Leaveitemptyif
%%hardwarecounterlengthislessthan32bits.
%%Whenbuf2isemptybutthisasynchronoustaskneedsa64-bit
%%time,high32-bitvalueof64-bittimerincrementswhenever
%%thelow32-bitwordoverflow.Lower32-bitstimermustnever
%%counteraroundpassit'spreviousvaluewhentheasynchronous
%%taskruns.
%%
%function LibSetAsyncClockTicks(sfcnBlock, callIdx, buf1, buf2) void
  %assign recIdx = sfcnBlock.SFcnSystemOutputCallMappingInfo[callIdx].StartingIdx
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock, recIdx)
  %assign ssTid = SLibGetAsyncSysTid(ssBlock, recIdx)
  %openfile tmpBuf
  %if SLibClockTickIsUnit32Pair(ssTid)
    %if WHITE_SPACE(buf2)
      %assign warnTxt = "Asynchronous task associated with "...
      "sfunction %<sfcnBlock.Name> requires 64-bits hardware timer."...
      "If target doesn't provide method to access the higher 32-bits "...
      "of the hardware timer, higher 32-bits timer is calculated using " ...
      "lower 32-bits. Lower 32-bits timer must never counter around pass "...
      "it's previous value"
      %<LibBlockReportWarning(sfcnBlock,warnTxt)>
      if (%<LibGetClockTick(ssTid)> > %<buf1>) {
        %<LibGetClockTickHigh(ssTid)>++;
      }
    %else
      %<LibSetAsyncCounterHigh(sfcnBlock, callIdx, buf2)>
    %endif
  %endif
  %<LibSetAsyncCounter(sfcnBlock, callIdx, buf1)>
   
  %% Update float absolute time for async task
  %if SampleTime[ssTid].NeedFloatTime == "yes"
    %<SLibUpdateFloatTimeForAsyncTask(ssTid)>
  %endif
  %closefile tmpBuf
   
  %return tmpBuf
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibNeedAsyncCounter====================
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustasks
%%canmanagetheirowntimeandusesthisfunctiontodetermineifthereis
%%aneedtodo.
%%
%%Example:
%%%ifLibNeedAsyncCounter(block,0)
%%%<LibSetAsyncCounter(block,0),CodeBufGetCounter)>
%%
%%whereCoddeGetCounteriscodereadingabsolutetimefrommodelbaserate,or
%%callertimer,orhardwaretimer,dependsontheabsolutetimesourceof
%%theasynchronoustask.
%%
%%ReturnsTLC_TRUEifaasynchronouscounterisneeded,otherwiseTLC_FALSE.
%%
%function LibNeedAsyncCounter(sfcnBlock,callIdx) void
  %% Get TID of downstream f-c ss, this sfcnblock TID will be for my caller
  %% which is not the TID the sfcn defines
  %assign recIdx = sfcnBlock.SFcnSystemOutputCallMappingInfo[callIdx].StartingIdx
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock, recIdx)
  %assign ssTid = SLibGetAsyncSysTid(ssBlock, recIdx)
  %return (::CompiledModel.SampleTime[ssTid].NeedAbsoluteTime == "yes"? TLC_TRUE:TLC_FALSE)
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibManageAsyncCounter==============
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustaskuses
%%thisfunctiontodetermineifthetaskneedscounterandmanagesitsowntimer
%%
%%Example:
%%%ifLibManageAsyncCounter(block,callIdx)
%%%<LibSetAsyncCounter(block,callIdx),CodeGetCounter)>
%%
%%whereCoddeGetCounteristargetspecificcodereadinghardwaretimer.
%%
%%ReturnsTLC_TRUEifaasynchronouscounterisneeded,otherwiseTLC_FALSE.
%%
%function LibManageAsyncCounter(sfcnBlock,callIdx) void
  %assign recIdx = sfcnBlock.SFcnSystemOutputCallMappingInfo[callIdx].StartingIdx
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock, recIdx)
  %assign ssTid = SLibGetAsyncSysTid(ssBlock, recIdx)
  %return (LibNeedAsyncCounter(sfcnBlock,callIdx) && ...
    (::CompiledModel.SampleTime[ssTid].TimeSource == "Self"))
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibGetCallerClockTickCounter===========
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustasks
%%canmanagetheirowntime.Thisfunctionisusedtoaccessaupstream
%%asynchrounoustask'stimecounter.Thisispreferredwhenbeingdrivenby
%%anotherasynchronousrate(e.g.InterruptBlockdrivingaTaskblock)as
%%thetimetheinterruptoccurredwillbeusedasapposedtothetimethe
%%Taskisallowedtorun.
%%
%%Example:
%%LibGetCallerClockTickCounter(sfcnBlock))
%%
%%Returnsastringforthecountervariablefortheupstreamasynchronous
%%task.
%%
%function LibGetCallerClockTickCounter(sfcnBlock) void
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock,0)
  %assign callerTid = FcnGetAsyncPromotedTID(SLibGetAsyncSysTid(ssBlock, 0))
   
  %assert ::CompiledModel.SampleTime[callerTid].NeedAbsoluteTime == "yes"
  %return LibGetClockTick(callerTid)
%endfunction
 
%%DocFunction{OtherUsefulFunctions}:LibGetCallerClockTickCounterHigh=======
%%Abstract:
%%ForusebyAsyncS-Functionswithfunctioncalloutputs.Asynchronoustasks
%%canmanagetheirowntime.Thisfunctionisusedtoaccessaupstream
%%asynchrounoustask'stimecounter.Thisispreferredwhenbeingdrivenby
%%anotherasynchronousrate(e.g.InterruptBlockdrivingaTaskblock)as
%%thetimetheinterruptoccurredwillbeusedasapposedtothetimethe
%%Taskisallowedtorun.
%%
%%Example:
%%LibGetCallerClockTickCounterHigh(block)
%%
%%Returnsastringforthehighwordofthecountervariableforthe
%%upstreamasynchronoustask.
%%
%function LibGetCallerClockTickCounterHigh(sfcnBlock) void
  %assign ssBlock = LibGetFcnCallBlock(sfcnBlock,0)
  %assign callerTid = FcnGetAsyncPromotedTID(SLibGetAsyncSysTid(ssBlock,0))
   
  %assert ::CompiledModel.SampleTime[callerTid].NeedAbsoluteTime == "yes"
   
  %if SLibClockTickIsUnit32Pair(callerTid)
    %return LibGetClockTickHigh(callerTid)
  %else
    %return ""
  %endif
   
%endfunction
 
%%FunctionFcnGetAsyncPromotedTID
%%Abstract:
%%ThisfunctionreturnsPromptedTIDofanasyncTID.
%%Whenanaynchrounoustaskneedsynchronizedatatransfer
%%(includingtime)withitscaller.PromptedTIDofthisasync
%%taskisitscaller'sTID,othewise,PromotedTIDis
%%thesameasoriginaltid.
%%AsyncPromotpedTIDiswrittento.rtwfileinfunction
%%WriteSampleTimes.
%%AsyncPromotpedTIDisusedwhilegeneratingcodeforoutputfcnof
%%ratetransitionblocks.Outputfcncodeofasyncratetransition
%%willbegroupedwithPromotedTID
%%
%%However,forAccelerationmode,wedon'tneed
%%synchronizedatatransferwithitscaller.
%%PromotedTIDistheoriginalTID
%%
%function FcnGetAsyncPromotedTID(tid)
  %assert(LibAsynchronousTriggeredTID(tid))
   
  %if Accelerator
    %return tid
  %else
    %return SampleTime[tid].AsyncPromotedTID
  %endif
 
%endfunction
 
%endif %% _ASYNCLIB__
 
%%Function:SLibAsyncSampleTimeGeneratesTimeManagementCode===============
%%Abstract:
%%Determineifthesampletimeneedstogeneratetimemanagement
%%code.Ifthisisfalse,thismeansoneofthefunction-callsubsysetms
%%willhandlethis.
%function SLibAsyncSampleTimeGeneratesTimeManagementCode(tid)
  %assert LibAsynchronousTriggeredTID(tid)
  %assign retVal = TLC_FALSE
  %if ISFIELD(SampleTime[tid],"AsyncSfcnBlkIdx")
    %assign asyncSfcnBlkIdx = SampleTime[tid].AsyncSfcnBlkIdx
    %assign asyncSfcnBlk = System[asyncSfcnBlkIdx[0]].Block[asyncSfcnBlkIdx[1]]
    %with asyncSfcnBlk
      %% The sample time needs to generate the time management code if
      %% the s-function is only hooked up to a Model block input, or
      %% if the fcn-call signal is branched.
      %if SLibSfcnHasBranchFcnCall(asyncSfcnBlk)
        %assign retVal = TLC_TRUE
      %elseif ISFIELD(asyncSfcnBlk,"NumSFcnSysOutputCallDsts") && ...
        NumSFcnSysOutputCallDsts == 1 && ...
        !ISEQUAL(SFcnSystemOutputCall[0].BlockToCall, "unconnected")
        %assign sysIdx = SFcnSystemOutputCall[0].BlockToCall[0]
        %assign blkIdx = SFcnSystemOutputCall[0].BlockToCall[1]
        %assign fcnCallBlock = System[sysIdx].Block[blkIdx]
         
        %assign retVal = (fcnCallBlock.Type == "ModelReference")
      %endif
    %endwith
  %endif
  %return retVal
%endfunction
 
%function FcnGetTaskTopSfcnBlock(tid)
  %assign taskTopSfcnBlock = ""
  %if LibAsynchronousTriggeredTID(tid)
    %if ISFIELD(SampleTime[tid], "AsyncSfcnBlkIdx")
      %assign blkIdx = SampleTime[tid].AsyncSfcnBlkIdx
      %assign taskTopSfcnBlock = System[blkIdx[0]].Block[blkIdx[1]]
    %endif
  %endif
  %return taskTopSfcnBlock
%endfunction
 
%function FcnIsForcedInlinedForAsync(system,activeTid)
  %assign sysTid = LibGetSubsystemTID(system, TLC_TRUE)
  %return sysTid >=0 && LibAsynchronousTriggeredTID(activeTid) && !ISEQUAL(activeTid, sysTid)
%endfunction
 
%function FcnAddAsyncFunctionToCodeDescriptor(functionName, taskIndex, priority) void
  %if !ISFIELD(::CompiledModel, "CodeDescriptorAsyncFunctions")
    %addtorecord ::CompiledModel CodeDescriptorAsyncFunctions {}
  %endif
  %createrecord entry {functionName "%<functionName>"}
  %addtorecord entry taskIndex "%<taskIndex>"
  %addtorecord entry priority "%<priority>"
  %addtorecord ::CompiledModel.CodeDescriptorAsyncFunctions %<functionName> entry
%endfunction