/*============================================================================= Name: ffExec.c ffExecThread - the main routine of the FF Executive Thread setStopFlagsTrue setStopFlagsFalse stopThreads cleanup Abs: ffExec contains the ffExec thread. See the FF Executive and ff-IOC Initialization Design document Auth: 13-dec-2004, diane fairley (DFAIRLEY): created from ffMain.c Rev: 02-feb-2005 Debbie Rogind -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) jul 2009 drogind used for fast feedback project 10-Apr-2005, Ron MacKenzie (ronm): ffAsyncGetJMSK argument changed to reference 07-Apr-2005, Ron MacKenzie (ronm): Call ffAsync utility to access asyncExists. 25-Jan-2005, Ron MacKenzie (ronm): Add cstrAsync startup 13-dec-2004, diane fairley (DFAIRLEY): split ffExec thread code out of ffMain into this file =============================================================================*/ #include "debugPrint.h" #ifdef DEBUG_PRINT int ffExecFlag = DP_DEBUG; #include #endif /* test comment */ /* c library includes */ #include /* memcpy */ /* epics includes */ #include /* freeListFree */ #include /* epicsEventId */ #include "cadef.h" /* channel access */ #include "caerr.h" /* channel access status codes */ #include "errlog.h" /* errlogPrintf */ #include "registryFunction.h" /* fiducial registration */ #include "evrTime.h" /* evr functions */ #include "evrPattern.h" /* evr pattern functions */ #ifdef __rtems__ #include /* This is MVME6100/5500 only */ #include /* fcom module */ #endif /* ff common includes */ #include "ffGlob.h" /* globals, vmsstat_t, SUCCESS, NULL, etc MUST BE FIRST INCLUDE*/ #include "ffTimer.h" /* ffTimer_ts */ #include "msgTypes.h" /* msgmail_ts */ #include "ffMsg.h" /* ffIOC internal msg function codes */ #include "functionUtil.h" /* findFunctionByName prototype */ #include "loopConfig.h" /* loopConfigInitTimerDelay */ /* ff utility prototypes */ #include "msgQUtil.h" /* message queue utilities */ /* thread prototypes */ /*#include "ffEpicsSend.h" epicsSend thread */ #include "loopHdlr.h" /* ffLoop threads */ /* internal function prototypes */ LOCAL int ffExecInit (void); LOCAL epicsBoolean waitForThreadReady(ffThread_te threadEnum); LOCAL epicsBoolean startLoopThread (int loopNum, char * loopString); LOCAL epicsBoolean allThreadsTerminated(void); LOCAL epicsBoolean waitForThreadsTerminated(void); LOCAL void setStopFlagsTrue( void); LOCAL void setStopFlagsFalse( void); LOCAL epicsBoolean stopThreads(void); LOCAL void cleanup(void); /* global, declared extern in loopCACTypes.h */ struct ca_client_context * context_p; /* pointer to ca context */ /*============================================================================= Name: ffExecThread Abs: Start all the FF aware IOC threads. Args: None Rem: The ffExecThread handles the IOC_RESTART and IOC_STOP messages IOC_RESTART: first attempts to stop all threads except ffExec, then starts all ffIOC threads IOC_STOP: stops all running ffIOC threads, except ffExec Side: The ffExec threads attaches to the socket library, creates a message queue Ret: none generates EXNET_INITFAIL if cannot attach to socket lib generates errors from message queue utilities upon message queue failures =============================================================================*/ void ffExecThread(void) { msgmail_ts* msg_ps = NULL; epicsBoolean go = epicsTrue; ffThread_ts* ffExec_ps = &ffThreads_as[ffExec]; unsigned short vnum = 9; int loopNum=-1; char loopString[4]; loopStartMsg_ts lsMsg_s; /* create this thread's message queue */ if ( !SUCCESS(messageQCreate(ffExec, MAX_QUEUED_MSGS, MAX_MSGPTR_BYTES)) ) { ffExec_ps->tid_ps = NO_THREAD; return; } /* ffExecInit */ ffExecInit(); /* set this thread to active */ ffExec_ps->active = epicsTrue; /*DEBUGPRINT(DP_INFO,ffExecFlag, ("ffExec is active now\n"));*/ /* now we wait at the message queue for thread status messages */ go = epicsTrue; /* go is set to false only when ffExec must exit */ while (go) { /* wait for a message at the queue */ if ( SUCCESS(msgQMsgRecv(ffExec, NULL, (void*)&msg_ps)) ) { DEBUGPRINT(DP_INFO,ffExecFlag, ("EXEC: received message ! ")); /* if a message is received, it will be a thread stop or start msg */ /* set the thread.active flag accordingly, */ /* no need to swap bytes for internal messages */ switch ( msg_ps->msgheader.func) { case IOC_LOOP_START: DEBUGPRINT(DP_INFO,ffExecFlag,("EXEC: reached IOC_LOOP_START\n")); memcpy (&lsMsg_s, &msg_ps->reqdata, sizeof(loopStartMsg_ts)); loopNum = lsMsg_s.loopNum; memcpy (&loopString, &lsMsg_s.loopString, 4); DEBUGPRINT(DP_INFO,ffExecFlag, ("EXEC:executing StartLoopNum command for loop= %d; %s \n", loopNum, (char *)loopString)); startLoopThread (loopNum, (char *) loopString); break; case IOC_RESTART: /* DEBUGPRINT(DP_INFO,ffExecFlag, ("EXEC:executing RESTART command.\n")); if ((go = stopThreads()) ) { if ( (go = startThreads()) ) { errlogPrintf("%s v.%u started and at your service\n", ffGetIOCName(), vnum); all necessary threads are going, start and continue our main loop } } */ break; case IOC_STOP: DEBUGPRINT(DP_INFO,ffExecFlag, ("EXEC:executing STOP command.\n")); go = stopThreads(); break; } /* release the message buffer */ msgQFreeBuffer(sizeof(msgmail_ts), (void *)msg_ps); } /* if message in queue */ } /*while go */ /* if we got here somehow there's been a fatal error, exit */ DEBUGPRINT(DP_FATAL,ffExecFlag, ("EXEC: FATAL ERROR during restart or stop\n")); cleanup(); } #ifdef __rtems__ /*============================================================================= Name: ffFiducialRoutine Abs: fiducial routine to check current pattern for loop processing Args: None Rem: This function gets the current pattern and compares the current pattern to all POIs to see if each loop must process only processes loops that are ON Side: RTEMS ONLY this function is registered with the evr fiducial processing Ret: void =============================================================================*/ void ffFiducialRoutine(void *args) { int pattern; ffPOIs_ts *pLPs; loopPOI_ts *pPOI; int matches_current; epicsTimeStamp time_s; evrModifier_ta modifier_a; epicsUInt32 rate_mask =MOD5_HALFHZ_MASK; /* DEBUG Check 1/2 HZ*/ /* epicsUInt32 rate_mask =MOD5_10HZ_MASK; DEBUG Check 10 HZ */ /* get the current pattern data - check for good status */ unsigned long patternStatus; /* see evrPattern.h for values */ int status = evrTimeGetFromPipeline(&time_s, evrTimeCurrent, modifier_a, &patternStatus, 0,0,0); if (!status) { /* DEBUG **************************************************/ if ( (modifier_a[4] & MOD5_BEAMFULL_MASK) /*&& (modifier_a[4] & rate_mask) */ ) { epicsTimeGetCurrent(&ffWakeupEvents_s.clockTime_s); ffWakeupEvents_s.pulseId = PULSEID(time_s); ffWakeupEvents_s.current_time_s = time_s; memcpy(&(ffWakeupEvents_s.current_modifier_a), &(modifier_a), sizeof(evrModifier_ta)); status = ffTimerStart( &ffTimer_s, (uint64_t)ffDelay_in_usec ); /* DEBUGPRINT(DP_DEBUG,ffExecFlag, ("ffFiducial: got half-hertz pulse \n"));*/ } /*DEBUG **************************************************/ /* now save the wakeup event of each loop that should process ffWakeupEvents_s.numPOIs = 0; for(pattern=0,pLPs = &ffPOIs_s; patternPOIs[pattern]; matches_current = evrPatternCheck(pPOI->pattern.beamCode, pPOI->pattern.timeSlot, pPOI->pattern.inclusion, pPOI->pattern.exclusion, ffWakeupEvents_s.current_modifier_a); if(matches_current && loop_as[ffThreads_as[pPOI->ffThread_e].loopNum].on) { /* store the event for this loop ffWakeupEvents_s.events[ffWakeupEvents_s.numPOIs++] = ffThreads_as[pPOI->ffThread_e].wakeupFromTimerEvent_ps; } } /* start the timer if there are loops to process if (ffWakeupEvents_s.numPOIs) { status = ffTimerStart( &ffTimer_s, ffDelay_in_usec ); }*/ } return; } #endif #ifdef __rtems__ /*============================================================================= Name: ffTimer_isr Abs: timer isr to signal all loops that must process the current pulse Args: None Rem: goes thru the ffPOIThreads list and signals each feedback loop Side: RTEMS only routine Ret: void =============================================================================*/ static void ffTimer_isr(void *arg) { int poi; /* printk("timer fired pois=%d\n",ffWakeupEvents_s.numPOIs ); */ /* go thru list of POIs and fire event for each match */ for(poi=0; poimsgheader.func = IOC_STOP; msgptr_s.msgbytes = MSG_HDRS_BYTES; msgptr_s.msgptr_p = msg_ps; /* send the message, wait at queue if full */ if (!SUCCESS(messageQSend(cnt, epicsTrue, sizeof(msgptr_s), &msgptr_s))) { DEBUGPRINT(DP_ERROR, ffExecFlag, ("EXEC: stop failed on thread enum #%d\n",cnt)); } else DEBUGPRINT(DP_DEBUG, ffExecFlag, ("EXEC: created and sent a msg service message\n")); } } } /* now wait for threads to stop */ ok = waitForThreadsTerminated(); DEBUGPRINT(DP_INFO, ffExecFlag, ("EXEC: all threads are gone\n")); setStopFlagsFalse(); /* incr. the Stops count */ execDiagnostics_s.numStops++; errlogPrintf("%s Stopped and out of service\n", ffGetIOCName()); return (ok); } /*============================================================================= Name: waitForThreadsTerminated Abs: wait for all threads to terminate, except the ffExec thread Args: none Rem: Waits for all the thread tid_ps's to be NO_THREAD Side: NOTE: We may want to include a timeout - which would then cause an epicsFalse to be returned if the timeout is reached. would remove a possible infinite loop. Ret: epicsTrue if threads terminated successfully, otherwise does not return ==============================================================================*/ LOCAL epicsBoolean waitForThreadsTerminated(void) { /* * wait to receive startup complete msgs from the threads? */ while (!allThreadsTerminated()) { epicsThreadSleep(0.5); } /*while not all threads terminated */ return epicsTrue; } /*============================================================================= Name: setStopFlagsTrue Abs: set all the ffThreads_as[].stop = epicsTrue; Args: none Rem: this function sets all the ffThreads_as[].stop = epicsTrue; ffExec thread is not included, since it never stops Side: dmf 12/13/07 made this stop at dbSend so that dbSend does not stop here Ret: none ==============================================================================*/ /**procedure**/ LOCAL void setStopFlagsTrue(void) { int cnt; for (cnt=ffExec+1; cnt