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