Skip to content

Commit 3b33c34

Browse files
Create ptread_detach. (#11)
Create ptread_detach.
1 parent e197ed1 commit 3b33c34

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_pthread.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,65 @@ int pthread_join( pthread_t pthread,
475475

476476
/*-----------------------------------------------------------*/
477477

478+
int pthread_detach(pthread_t pthread)
479+
{
480+
int iStatus = 0;
481+
pthread_internal_t * pxThread = ( pthread_internal_t * ) pthread;
482+
eTaskState pThreadState;
483+
484+
/* Make sure pthread is joinable. */
485+
if( !pthreadIS_JOINABLE( pxThread->xAttr.usSchedPriorityDetachState ) )
486+
{
487+
iStatus = EINVAL;
488+
}
489+
490+
if ( iStatus == 0 )
491+
{
492+
/* Create a critical section to verify that pthread is joinable. */
493+
vTaskSuspendAll();
494+
495+
pThreadState = eTaskGetState(pxThread->xTaskHandle);
496+
497+
/* Thread has been deleted or is invalid. */
498+
if ( (pThreadState == eDeleted) || (pThreadState == eInvalid) )
499+
{
500+
iStatus = EINVAL;
501+
}
502+
else
503+
{
504+
/* Release xJoinBarrier and delete it. */
505+
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
506+
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinBarrier );
507+
508+
/* Release xJoinMutex and delete it. */
509+
( void ) xSemaphoreGive( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );
510+
vSemaphoreDelete( ( SemaphoreHandle_t ) &pxThread->xJoinMutex );
511+
512+
/* Thread has been finished */
513+
if ( pThreadState == eSuspended )
514+
{
515+
/* Delete the FreeRTOS task that ran the thread. */
516+
vTaskDelete( pxThread->xTaskHandle );
517+
518+
/* Free the thread object. */
519+
vPortFree( pxThread );
520+
}
521+
else
522+
{
523+
/* Thread is in the running or ready state. */
524+
pthread_attr_setdetachstate( (pthread_attr_t *) &pxThread->xAttr, PTHREAD_CREATE_DETACHED );
525+
}
526+
}
527+
528+
/* End the critical section. */
529+
xTaskResumeAll();
530+
}
531+
532+
return iStatus;
533+
}
534+
535+
/*-----------------------------------------------------------*/
536+
478537
pthread_t pthread_self( void )
479538
{
480539
/* Return a reference to this pthread object, which is stored in the

include/FreeRTOS_POSIX/pthread.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,17 @@ int pthread_getschedparam( pthread_t thread,
362362
int pthread_join( pthread_t thread,
363363
void ** retval );
364364

365+
/**
366+
* @brief Marks the thread identified by thread as detached.
367+
*
368+
* @see https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_detach.html
369+
*
370+
* @retval 0 - Upon successful completion.
371+
* @retval EINVAL - The implementation has detected that the value specified by thread does not refer
372+
* to a joinable thread.
373+
*/
374+
int pthread_detach(pthread_t thread);
375+
365376
/**
366377
* @brief Destroy a mutex.
367378
*

0 commit comments

Comments
 (0)