How to use Task Release Service

PXROS-HR API provides these functions for task termination:

PxError_t PxTerminate(PxBool_t release);
PxError_t PxTaskForceTermination(PxTask_t taskId);
PxError_t PxDie(void);

Self-termination

If the self-termination is successfully requested, the following functions for self-termination will never return as the task ends up suspended. The error code is only returned if the error occurs during the termination request.

PxTerminate() has an option to release resources allocated by the task and then calls PxDie().

Code 1. PxTerminate() with releasing resources called by the termination task
errRes = PxTerminate(true);     // Release resources = true
if (errRes != PXERR_NOERROR)
    PxPanic();

PxDie() ensures the task ID is put to the PxDieSrv Task Release Queue and sends the PXSERVICE_TASK_DIED event to the Task Release Service, which ensures its termination. PXSERVICE_TASK_DIED is a system reserved event defined in the "pxdef.h" header file.

Code 2. PxDie() called by the termination task
errRes = PxDie();
if (errRes != PXERR_NOERROR)
    PxPanic();

Forced termination

PXROS-HR API also offers a way how to terminate other tasks. The caller of PxTaskForceTermination(taskId) must be the direct creator of the task to terminate (parent). The task that is forced to be terminated has no option for how to react to this action.

A suspended task cannot be force-terminated unless it is resumed first. The termination is requested by the caller of the PxTaskForceTermination() and executed by the task to be terminated once it gets execution time.

Code 3. PxTaskForceTermination() termination called by the task requesting a termination
/* Create the task that will be later directly terminated */
PxTask_t forcedTermination = ForcedTermination_Create(FORCEDTERMINATION_PRIO,
                                                      (PxEvents_t) 0,
                                                      PXMcTaskdefault,
                                                      PXOpoolTaskdefault);
if (PxTaskIdError(forcedTermination) != PXERR_NOERROR)
    PxPanic();

...

/* Do the forced termination of the created task */
errRes = PxTaskForceTermination(forcedTermination);
if (errRes != PXERR_NOERROR)
    PxPanic();