Check termination state

The following code is an example implementation of the wait function using a specific amount of retries with a defined timeout not to block other tasks' execution. If the function does not succeed within the given time and retries, the PXERR_REQUEST_TIMEOUT error code is returned.

Assuming the default definitions are defined as follows.

Code 1. Default termination check values
#define TERMINATION_CHECK_RETRY_DEFAULT 10
#define TERMINATION_CHECK_RETRY_TIMEOUT 10
Code 2. HtcCheckTermination, the wait function implementation
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * FUNCTION: HtcCheckTermination
 *     Check the task termination state with the wait/retry option
 * IN:
 *     taskId       : ID of the task we want to know the state
 *     retryCount   : number of retries
 *                    0 = use a default value TERMINATION_CHECK_RETRY_DEFAULT
 *     retryTimeout : wait time in PX Ticks
 *                    0 = use a default value TERMINATION_CHECK_RETRY_TIMEOUT
 *     retryEvent   : Caller event to use for retry timeout events
 *                    calling task shall provide the event
 * OUT:
 *     PxError      : PXROS type of error code
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

PxError_t HtcCheckTermination(PxTask_t taskId, int retryCount, int retryTimeout,
                              PxEvents_t retryEvent)
{
    PxError_t err = PXERR_NOERROR;
    PxBool_t taskAlive = false;

    if (!PxTaskIdIsValid(taskId))
        return PXERR_TASK_ILLTASK;

    /* Check the Default values requests by passing value '0' */
    if (retryTimeout == 0)
        retryTimeout = TERMINATION_CHECK_RETRY_TIMEOUT;

    if (retryCount == 0)
        retryCount = TERMINATION_CHECK_RETRY_DEFAULT;

    /* Ask for a timeout object from the task default object pool to generate
     * wake-up event */
    PxTo_t to = PxToRequest(PXOpoolTaskdefault, retryTimeout, retryEvent);

    if (PxToIdError(to) != PXERR_NOERROR)
    {
        err = PxToIdError(to);
        return err;
    }

    /* Retry loop with waiting sleep in between retries */
    do
    {
        taskAlive = PxTaskCheck(taskId);

        if (taskAlive == true)
        {
            PxToStart(to);
            PxAwaitEvents(retryEvent);
        }
    } while (taskAlive == true && (--retryCount > 0));

    /* Task is still alive
     * Something must be wrong with the complete application
     */
    if (retryCount == 0)
        err = PXERR_REQUEST_TIMEOUT;

    /* Stop and release the temporary timeout object */
    PxToStop(to);
    PxToRelease(to);

    return err;
}