From 3b33c3467d1345d332752aa9c155da9683183c49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Vitor=20Teixeira?= <43542367+JoaoVitorTeixeira@users.noreply.github.com> Date: Tue, 14 Dec 2021 15:12:45 -0300 Subject: [PATCH] Create ptread_detach. (#11) Create ptread_detach. --- .../source/FreeRTOS_POSIX_pthread.c | 59 +++++++++++++++++++ include/FreeRTOS_POSIX/pthread.h | 11 ++++ 2 files changed, 70 insertions(+) diff --git a/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c b/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c index f34e9ec..f063160 100755 --- a/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c +++ b/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c @@ -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 diff --git a/include/FreeRTOS_POSIX/pthread.h b/include/FreeRTOS_POSIX/pthread.h index f38dccf..5923326 100755 --- a/include/FreeRTOS_POSIX/pthread.h +++ b/include/FreeRTOS_POSIX/pthread.h @@ -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. *