* @brief Marks a thread as detached, allowing its resources to be automatically released upon termination. * * The `pthread_detach` function separates the specified thread from the calling thread. Once a thread is detached, * its resources will be automatically reclaimed by the system upon the thread's termination. A detached thread cannot * be joined using `pthread_join`. * * @param[in] thr
| 531 | * @see pthread_create, pthread_join, pthread_attr_setdetachstate |
| 532 | */ |
| 533 | int pthread_detach(pthread_t thread) |
| 534 | { |
| 535 | int ret = 0; |
| 536 | _pthread_data_t *ptd = _pthread_get_data(thread); |
| 537 | if (ptd == RT_NULL) |
| 538 | { |
| 539 | /* invalid pthread id */ |
| 540 | ret = EINVAL; |
| 541 | goto __exit; |
| 542 | } |
| 543 | |
| 544 | if (ptd->attr.detachstate == PTHREAD_CREATE_DETACHED) |
| 545 | { |
| 546 | /* The implementation has detected that the value specified by thread does not refer |
| 547 | * to a joinable thread. |
| 548 | */ |
| 549 | ret = EINVAL; |
| 550 | goto __exit; |
| 551 | } |
| 552 | |
| 553 | if ((RT_SCHED_CTX(ptd->tid).stat & RT_THREAD_STAT_MASK) == RT_THREAD_CLOSE) |
| 554 | { |
| 555 | /* destroy this pthread */ |
| 556 | _pthread_data_destroy(ptd); |
| 557 | goto __exit; |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | /* change to detach state */ |
| 562 | ptd->attr.detachstate = PTHREAD_CREATE_DETACHED; |
| 563 | |
| 564 | /* detach joinable semaphore */ |
| 565 | if (ptd->joinable_sem) |
| 566 | { |
| 567 | rt_sem_delete(ptd->joinable_sem); |
| 568 | ptd->joinable_sem = RT_NULL; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | __exit: |
| 573 | return ret; |
| 574 | } |
| 575 | RTM_EXPORT(pthread_detach); |
| 576 | |
| 577 | /** |
no test coverage detected