Create ptread_detach. (#11)

Create ptread_detach.
This commit is contained in:
João Vitor Teixeira 2021-12-14 15:12:45 -03:00 committed by GitHub
parent e197ed1ac7
commit 3b33c3467d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 70 additions and 0 deletions

View File

@ -475,6 +475,65 @@ int pthread_join( pthread_t pthread,
/*-----------------------------------------------------------*/
int pthread_detach(pthread_t pthread)
{
int iStatus = 0;
pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread;
eTaskState pThreadState;
/* Make sure pthread is joinable. */
if( !pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) )
{
iStatus = EINVAL;
}
if ( iStatus == 0 )
{
/* Create a critical section to verify that pthread is joinable. */
vTaskSuspendAll();
pThreadState = eTaskGetState(pxThread->xTaskHandle);
/* Thread has been deleted or is invalid. */
if ( (pThreadState == eDeleted) || (pThreadState == eInvalid) )
{
iStatus = EINVAL;
}
else
{
/* Release xJoinBarrier and delete it. */
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
/* Release xJoinMutex and delete it. */
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );
/* Thread has been finished */
if ( pThreadState == eSuspended )
{
/* Delete the FreeRTOS task that ran the thread. */
vTaskDelete( pxThread->xTaskHandle );
/* Free the thread object. */
vPortFree( pxThread );
}
else
{
/* Thread is in the running or ready state. */
pthread_attr_setdetachstate( (pthread_attr_t *) &pxThread->xAttr, PTHREAD_CREATE_DETACHED );
}
}
/* End the critical section. */
xTaskResumeAll();
}
return iStatus;
}
/*-----------------------------------------------------------*/
pthread_t pthread_self( void )
{
/* Return a reference to this pthread object, which is stored in the

View File

@ -362,6 +362,17 @@ int pthread_getschedparam( pthread_t thread,
int pthread_join( pthread_t thread,
void ** retval );
/**
* @brief Marks the thread identified by thread as detached.
*
* @see https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_detach.html
*
* @retval 0 - Upon successful completion.
* @retval EINVAL - The implementation has detected that the value specified by thread does not refer
* to a joinable thread.
*/
int pthread_detach(pthread_t thread);
/**
* @brief Destroy a mutex.
*