Thread in SylixOS [5] - end of thread

Posted by lpxxfaintxx on Mon, 27 Dec 2021 22:47:24 +0100

summary

The end of a thread means the end of the thread life cycle. Thread termination includes thread cancellation, thread exit and thread deletion.

APIexplain
API_ThreadDeleteThread delete function.
API_ThreadForceDeleteThread force delete function.
API_ThreadExitThe thread exited itself.
exitKernel thread or process exit
_exitThe kernel thread or process exits without executing the functions installed by atexit
atexitAction performed when a process exits (when using multi process mode, vp patch redirection will be used)

Thread deletion

Thread deletion is to return the resources of a thread to the operating system. The deleted thread can no longer be scheduled.

/*********************************************************************************************************
** Function name: API_ThreadDelete
** Function Description: thread deletion function.
** Input: 
**           pulId         handle
**           pvRetVal      Return value (thread returned to JOIN)
** Output:
*********************************************************************************************************/
ULONG  API_ThreadDelete (LW_OBJECT_HANDLE  *pulId, PVOID  pvRetVal)
/*********************************************************************************************************
** Function name: API_ThreadForceDelete
** Function Description: thread forced deletion function
** Input: 
**           pulId         handle
**           pvRetVal      Return value (thread returned to JOIN)
** Output:
*********************************************************************************************************/
ULONG  API_ThreadForceDelete (LW_OBJECT_HANDLE  *pulId, PVOID  pvRetVal)

Calling the above two functions can end the thread and release thread resources. Since SylixOS supports processes, the deleted thread can only be the thread in the same process, and the main thread can only be deleted by itself.

Actively deleting other executing threads may cause locked resources not to be released or atomic operations to be interrupted. Therefore, unless safety is ensured, SylixOS and any other operating systems do not recommend directly using thread deletion function calls. When designing an application, the "request" deletion method should be considered. When the thread finds nothing to do or is requested to delete, the thread deletes itself (the thread exits).

Thread exit

Thread exit means that the thread itself calls the thread deletion function to safely recover the thread resources.

/*********************************************************************************************************
** Function name: API_ThreadExit
** Function Description: the thread exits by itself.
** Input: pvRetVal return value (thread returned to JOIN)
** Output: error is returned successfully_ None, the error number is returned in case of failure;
*********************************************************************************************************/ 
ULONG  API_ThreadExit (PVOID  pvRetVal)
/*********************************************************************************************************
** Function name: exit
** Function Description: kernel thread or process exit
** Input: iCode return value
** Output:
*********************************************************************************************************/
void  exit (int  iCode)
/*********************************************************************************************************
** Function name:_ exit
** Function Description: the kernel thread or process exits without executing the functions installed by atexit
** Input: iCode return value
** Output:
*********************************************************************************************************/ 
void  _exit (int  iCode)
/*********************************************************************************************************
** Function name: atexit
** Function Description: the operation performed when the process exits (when using multi process mode, vp patch redirection will be used)
** Input: iCode return value
** Output:
*********************************************************************************************************/ 
int  atexit (void (*func)(void))

Thread cancellation

The method of thread cancellation is to send a Cancel signal to the target thread, but how to deal with the Cancel signal is determined by the target thread. It should be noted that thread cancellation is a complex process, and the consistency of resources needs to be considered.

/*********************************************************************************************************
** Function name: API_ThreadTestCancel
** Function Description: check whether the thread has the request cancellation flag, and automatically cancel when the conditions are met
** Input: 
** Output: 
*********************************************************************************************************/
VOID  API_ThreadTestCancel (VOID)
/*********************************************************************************************************
** Function name: API_ThreadCancel
** Function Description: cancels a specified thread
** Input: pulId thread handle
** Output: ERROR_NONE or ESRCH
*********************************************************************************************************/
ULONG  API_ThreadCancel (LW_OBJECT_HANDLE  *pulId)
/*********************************************************************************************************
** Function name: API_ThreadSetCancelState
** Function Description: set whether thread cancellation is enabled, 
** Input: the updated state of iNewState
**           piOldState                    Early state
** Output: EINVAL parameter error
**           0
*********************************************************************************************************/
ULONG   API_ThreadSetCancelState (INT  iNewState, INT  *piOldState)
/*********************************************************************************************************
** Function name: API_ThreadSetCancelType
** Function Description: sets the action when the current thread is cancelled passively, 
**           Can be PTHREAD_CANCEL_ASYNCHRONOUS or PTHREAD_CANCEL_DEFERRED
** Input: 
**           iNewType           new type
**           piOldType          Early type
** Output: ERRNO
*********************************************************************************************************/
INT  API_ThreadSetCancelType (INT  iNewType, INT  *piOldType)

Topics: thread SylixOS