/*============================================================================= Name: msgQUtil.c messageQSend - low-level: send a message to a message queue messageQRecv - low-level: receive a message from a message queue messageQCreate - create a message queue messageQDestroy - destroy a mesage queue messageQRelease - release all buffers then destroy the queue msgQMsgRecv - hi level: receive from a queue msgQMsgReply - hi level: send a reply to a queue msgQGetBuffer - get a message buffer from the proper pool msgQFreeBuffer - return a message buffer to the proper pool msgQMsgProcess - Process mail message based on function code Abs: ff-aware thread utilities for using epics message queues refer to web page LCLS Controls | Facilities | FF-Aware IOC | General Purpose Utilities Auth: 09-dec-2004, diane fairley (dfairley): Rev: 02 feb 2005, Debbie Rogind -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) DD-MMM-YYYY, My Name: Changed such and such to so and so. etc. etc. DD-MMM-YYYY, Your Name: More changes ... The ordering of the revision history should be such that the NEWEST changes are at the HEAD of the list. 23-feb-2010 dfairley changed return value to 0=OK=SUCCESS and fail = -1=NOT_OK =============================================================================*/ #include "debugPrint.h" #ifdef DEBUG_PRINT int msgQUtilFlag = DP_ERROR; #endif /* c includes */ #include /* memset, memcpy */ /* epics includes */ #include /* freeListCalloc, freeListFree */ #include /* epicsTimeShow */ #include /* errlogPrintf */ /* shared ff msg codes */ #include "msgdef.h" /* MSG_NOMBX error code */ /* ff global vars */ #include "ffGlob.h" /* ffThreads */ #include "ffUtil.h" /* ffGetThreadEnumByTid */ /* function prototypes */ #include "msgQUtil.h" /*=========================================================== Name: messageQSend Abs: send a message queue message Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- ffThread_te thrd_e read only thread enumeration of the message queue to send to void * msg_p read/write message structure being sent const int msgBytes byte count of the message structure Rem: Utility for tasks to send message to another threads message queue. Handles any size msg structure up to the maxsize the message queue was created with. This routine will return an error if the thread sending to does not exist, if the mq does not exist, or if the message is too big for the queue. All are fatal problems that indicate a programmer error or an entire ffIOC failure. no reason to try to re-send if this function call returns an error. Side: issues all relevent error messages with errorlogPrintf Ret: int error codes OKOK, or NOT_OK, or MSG_NOJOBTOKEN ===========================================================*/ int messageQSend(const ffThread_te thrd_e, const epicsBoolean wait_at_queue, const int msgBytes, const void *msg_p ) { int rv; /* return variable */ int iss=OKOK; char name_a[MAX_THREADNAME_SIZE]; /* see that the input thread enum is valid */ if (thrd_e >= ffInvalid) { iss = MSG_NOJOBTOKEN; errlogPrintf("messageQSend: Trying to send msg to an unknown thread.\n"); return iss; } /* see that the message queue is valid */ if (ffThreads_as[thrd_e].mqId_ps == NULL) { iss = MSG_NOMBX; errlogPrintf("messageQSend: Trying to send a message to an unknown message queue.\n"); return iss; } /* now try to send the message */ if (wait_at_queue == epicsTrue) { if ((rv=epicsMessageQueueSend(ffThreads_as[thrd_e].mqId_ps, (void *)msg_p, msgBytes))<0) { iss = NOT_OK; /* get the name of the thread we're sending to */ epicsThreadGetName(ffThreads_as[thrd_e].tid_ps, (char *)&name_a, MAX_THREADNAME_SIZE); errlogPrintf("messageQSend: send message to %s message queue failed.\n", (char *)&name_a); } } else { if ((rv=epicsMessageQueueTrySend(ffThreads_as[thrd_e].mqId_ps, (void *)msg_p, msgBytes))<0) { iss = NOT_OK; /* get the name of the thread we're sending to */ epicsThreadGetName(ffThreads_as[thrd_e].tid_ps, (char *)&name_a, MAX_THREADNAME_SIZE); errlogPrintf("messageQSend:send message to %s message queue failed, or the queue may be full.\n", (char *)&name_a); /* debug code */ /* if (epicsTimeGetCurrent(¤tTS)==epicsTimeERROR) { iss = MSG_BADTIME; errlogPrintf(iss, "BADTIME", "messageQSend cannot get the current time.\n"); } else { DEBUGPRINT(DP_ERROR,msgQUtilFlag,("send message to %s message queue failed, or the queue may be full; TIME:\n", (char *)&name_a)); epicsTimeShow (¤tTS, 1); }*/ } } return iss; } /*=========================================================== Abs: receive a message queue message Name: messageQRecv Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- ffThread_te thrd_e read only thread enumeration for queue void* msg_p read/write message structure being sent const int msgBytes byte count of the message structure epicsTimestamp* currentTime_ps read/write pointer to current time value Rem: Utility for tasks to recv message from their own message queue. Handles any size msg structure if the currentTime_ps is NULL then this routine will not get a current time. This routine will return an error if the thread receiving from does not exist, if the mq does not exist, or if the message is too big for the receiving buffer, or if the epicsTimeGet current call returns an error. All are fatal problems that indicate a programmer error or an entire ffIOC failure. no reason to try to receive again if this function call returns an error. Side: handles all error checking. waits forever for a message Ret: iss error code OKOK, NOT_OK ===========================================================*/ int messageQRecv(const ffThread_te thrd_e, const int msgBytes, epicsTimeStamp* currentTime_ps, void* msg_p ) { int rv; /* return variable */ int iss=OKOK; /* see that the input thread enum is valid */ if (thrd_e >= ffInvalid) { iss = NOT_OK; errlogPrintf("messageQRecv: trying to send msg to an unknown thread.\n"); return iss; } /* see that the message queue is valid */ if (ffThreads_as[thrd_e].mqId_ps == NULL) { iss = NOT_OK; errlogPrintf("messageQRecv: cannot recv message; its message queue does not exist.\n"); return iss; } /* now try to receive the message */ if ((rv=epicsMessageQueueReceive(ffThreads_as[thrd_e].mqId_ps, msg_p, msgBytes))<0) { iss = NOT_OK; errlogPrintf("messageQRecv: recv message from message queue failed.\n"); } else if (currentTime_ps != NULL) { if (epicsTimeGetCurrent(currentTime_ps)==epicsTimeERROR) { iss = NOT_OK; errlogPrintf("messageQRecv cannot get the current time.\n"); } } return iss; } /*=========================================================== Abs: create a message queue Name: messageQCreate() Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- const int maxMsgs read only max number of messages this queue may hold const int maxMsgBytes read only max byte count of each message this queue may hold Rem: Utility for threads to create their own message queue. Side: handles all error checking and places the mqId into the the ffThreads_as global for the calling thread. Ret: iss OKOK or NOT_OK ===========================================================*/ int messageQCreate(const ffThread_te thrd_e, const int maxMsgs, const int maxMsgBytes) { int iss = OKOK; /* see that the input thread enum is valid */ if (thrd_e >= ffInvalid) { iss = NOT_OK; errlogPrintf("messageQCreate trying to receive msg but is not a known ffIOC thread.\n"); return iss; } /* create a message queue for this thread */ ffThreads_as[thrd_e].mqId_ps = epicsMessageQueueCreate( maxMsgs, maxMsgBytes); if (ffThreads_as[thrd_e].mqId_ps == NULL ) { iss = NOT_OK; errlogPrintf("messageQCreate message queue create failed.\n" ); } return iss; } /*=========================================================== Abs: release all message in the queue, then destroy message queue Name: messageQRelease Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- ffThreads_te thrd_e read only enumeration of the thread Rem: release all message in the queue, then destroy the message queue ONLY FOR THREADS WORKING WITH MESSAGE SERVICE Messages that are allocated from the message service free lists. Side: sets the ffThreads_as[].mqId_ps to NULL after storing the mqid in a local var. this is done to protect other jobs from accessing a message queue that is in the process of being destroyed. Ret: none ===========================================================*/ void messageQRelease(const ffThread_te thrd_e ) { msgptr_ts msgptr_s; int rv = 1; epicsMessageQueueId mqId_ps = NULL; /* see that the input thread enum is valid */ if (thrd_e < ffInvalid) { /* see that the message queue is valid */ if (ffThreads_as[thrd_e].mqId_ps != NULL) { mqId_ps = ffThreads_as[thrd_e].mqId_ps; ffThreads_as[thrd_e].mqId_ps = NULL; /* read thru the queue and release message buffers! */ while (rv > 0) { if ((rv=epicsMessageQueueTryReceive(mqId_ps, &msgptr_s, MAX_MSGPTR_BYTES))>0) msgQFreeBuffer(msgptr_s.msgbytes, msgptr_s.msgptr_p); } epicsMessageQueueDestroy(mqId_ps); } } } /*=========================================================== Abs: destroy a message queue message Name: messageQDestroy Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- ffThreads_te thrd_e read only enumeration of the thread Rem: destroy the message queue - Used by Database Service threads that do not use a memory pool for messages. Side: sets the ffThreads_as[].mqId_ps to NULL after storing the mqid in a local var. this is done to protect other jobs from accessing a message queue that is in the process of being destroyed. Ret: none ===========================================================*/ void messageQDestroy(const ffThread_te thrd_e ) { epicsMessageQueueId mqId = NULL; /* see that the input thread enum is valid */ if (thrd_e < ffInvalid) { mqId = ffThreads_as[thrd_e].mqId_ps; ffThreads_as[thrd_e].mqId_ps = NULL; /* see that the message queue is valid */ if (mqId != NULL) { epicsMessageQueueDestroy(mqId); } } } /*=====================================source FF process====================== Name: msgQMsgRecv Abs: receive a msg from queue, return the msg buffer pointer, set destination, Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- ffThread_te thrd_e read only thread enumeration void * msg_pp read/write pointer to the pointer to the incoming message Rem: this routine recieves a message from a queue, and passes back a pointer to the message buffer. that message buffer may be a small or large buffer, hence the void * pointer type. the calling function must figure the actual buffer size from the msgheader.datalen field Side: sets the thread destination field according to the incoming message Ret: vmsstat_t messageQRecv return code see messageQRecv header ===========================================================*/ int msgQMsgRecv(const ffThread_te thrd_e, epicsTimeStamp* currentTime_ps, void **const msg_pp) { msgptr_ts msgptr_s; int iss = OKOK; memset ( &msgptr_s, 0, sizeof(msgptr_s) ); DEBUGPRINT(DP_DEBUG, msgQUtilFlag, ("msgQMsgRecv reached..........before messageQRecv \n")); iss = messageQRecv( thrd_e, sizeof(msgptr_s), currentTime_ps, &msgptr_s); if ( SUCCESS(iss) ) { *msg_pp =(void *)msgptr_s.msgptr_p; /* save the source FF process name for use by errlogPrintf */ /* though this may be a lrgmail_ts, it still has the msgheader.source in the same place memcpy(&ffThreads_as[thrd_e].destination, ((msgmail_ts*)(*msg_pp))->msgheader.source, 4);*/ } return (iss); } /*=========================================================== Name: msgQMsgReply Abs: send the message buffer to the queue, reset the thread destination field wait at the queue for room if there is none Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- ffThread_te thrd_e read only thread enumeration msgmail_ts* msg_ps read only pointer to the outgoing message Rem: must cast the buffer pointer to a msgmail to get the data byte counts from the msgmail_ts header. Side: sets the thread destination field to the default after sending the message Ret: vmsstat_t messageQSend return code see messageQSend header ===========================================================*/ int msgQMsgReply(const ffThread_te thrd_e, const void* msg_p) { msgptr_ts msgptr_s; int iss = OKOK; msgmail_ts* msg_ps; msg_ps = (msgmail_ts*)msg_p; msgptr_s.msgptr_p = msg_ps; msgptr_s.msgbytes = sizeof(msg_ps->job_id)+sizeof(msgheader_ts)+ (msg_ps->msgheader.datalen*2); /* send the message, wait at queue if there is no room */ iss = messageQSend( thrd_e, epicsTrue, sizeof(msgptr_s), &msgptr_s ); /*ffThreads_as[thrd_e].destination = V017_UL;*/ return (iss); } /*=========================================================== Name: msgQGetBuffer Abs: get a message buffer Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- unsigned long bytes read only minimum number of bytes required in buffer void ** msg_pp read/write pointer to the pointer to the message buffer Rem: gets a message buffer from the correct memory pool based on the number of bytes requested Side: Ret: vmsstat_t return code MICR_OKOK - alls well MICR_MALLOC- could not get memory ===========================================================*/ int msgQGetBuffer( const unsigned long bytes, void **const msg_pp) { int iss = OKOK; void *buf_p; if (bytes <= MAX_SMALL_MSG_BYTES) { buf_p = freeListCalloc(ffSmMsgPvt_p); if (!buf_p) { iss = NOT_OK; errlogPrintf("msgQGetBuffer unable to malloc small msg buffer from freelist.\n"); } } else { buf_p = freeListCalloc(ffLrgMsgPvt_p); if (!buf_p) { iss = NOT_OK; errlogPrintf("msgQGetBuffer unable to malloc large msg buffer from freelist.\n"); } } *msg_pp = buf_p; DEBUGPRINT(DP_DEBUG, msgQUtilFlag, ("%s got a msg buffer size %lu\n", epicsThreadGetNameSelf(), bytes)); return iss; } /*=========================================================== Name: msgQFreeBuffer Abs: free a message buffer Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- unsigned long bytes read only minimum number of bytes required in buffer void * msg_p read/write pointer to the message buffer Rem: releases a message buffer back to the correct memory pool based on the size of the buffer Side: Ret: none ===========================================================*/ void msgQFreeBuffer(const unsigned long bytes, const void* msg_p) { if (msg_p != NULL) { if (bytes <= MAX_SMALL_MSG_BYTES) freeListFree(ffSmMsgPvt_p, (void *)msg_p); else freeListFree(ffLrgMsgPvt_p, (void *)msg_p); DEBUGPRINT(DP_DEBUG, msgQUtilFlag, ("%s free'd a msg buffer size %lu\n", epicsThreadGetNameSelf(), bytes)); } }