1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** POSIX wrapper for THREADX                                             */
16 /**                                                                       */
17 /**                                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 /* Include necessary system files.  */
23 
24 #include "tx_api.h"    /* Threadx API */
25 #include "pthread.h"  /* Posix API */
26 #include "px_int.h"    /* Posix helper functions */
27 
28 
29 /**************************************************************************/
30 /*                                                                        */
31 /*  FUNCTION                                               RELEASE        */
32 /*                                                                        */
33 /*    pthread_detach                                      PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*                                                                        */
42 /*    The pthread_detach() function indicates that system resources for   */
43 /*    the specified thread should be reclaimed when the thread ends.      */
44 /*    If the thread is already ended, resources are reclaimed immediately.*/
45 /*    This routine does not cause the thread to end.                      */
46 /*    After pthread_detach() has been issued, it is not valid to try to   */
47 /*    pthread_join() with the target thread.                              */
48 /*    Eventually, you should call pthread_join() or pthread_detach() for  */
49 /*    every thread that is created joinable (with a detachstate of        */
50 /*    PTHREAD_CREATE_JOINABLE)so that the system can reclaim all resources*/
51 /*    associated with the thread. Failure to join to or detach joinable   */
52 /*    threads will result in memory and other resource leaks until the    */
53 /*    process ends. If thread doesn't represent a valid undetached thread,*/
54 /*    pthread_detach() will return ESRCH.                                 */
55 /*                                                                        */
56 /*                                                                        */
57 /*  INPUT                                                                 */
58 /*                                                                        */
59 /*    thread                      pthread handle to the target thread     */
60 /*                                                                        */
61 /*                                                                        */
62 /*  OUTPUT                                                                */
63 /*                                                                        */
64 /*    zero                        If successful                           */
65 /*    error number                If fails                                */
66 /*                                                                        */
67 /*  CALLS                                                                 */
68 /*                                                                        */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application Code                                                    */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
79 /*                                                                        */
80 /**************************************************************************/
pthread_detach(pthread_t thread)81 INT pthread_detach(pthread_t thread)
82 
83 {
84 
85 TX_INTERRUPT_SAVE_AREA
86 
87 POSIX_TCB    *pthread_ptr;
88 
89     TX_DISABLE
90 
91     pthread_ptr = posix_tid2tcb(thread);
92     if(pthread_ptr==NULL)
93     {
94         TX_RESTORE
95         return(ESRCH);
96     }
97     if(pthread_ptr->is_detached==TX_TRUE)
98     {
99         TX_RESTORE
100         return (EINVAL);
101     }
102     pthread_ptr->is_detached = TX_TRUE;
103 
104     TX_RESTORE
105     return(OK);
106 }
107