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 /** 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_cancel                                      PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    The pthread_cancel function shall request that thread be  canceled. */
42 /*    The target thread�s cancelability state and type determines when */
43 /*    the cancellation takes effect. When the cancellation is acted on,   */
44 /*    the cancelation cleanup handlers for thread shall be called. When   */
45 /*    the last cancelation cleanup handler returns, the thread-specific   */
46 /*    data destructor functions shall be called for thread. When the last */
47 /*    destructor function returns, thread shall be terminated.            */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    thread                      pthread handle to thread to be canceled */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    zero                        If successful                           */
56 /*    error number                If fails                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
70 /*                                                                        */
71 /**************************************************************************/
pthread_cancel(pthread_t thread)72 INT pthread_cancel(pthread_t thread)
73 {
74 
75 TX_THREAD   *thread_ptr;
76 POSIX_TCB   *pthread_ptr;
77 
78 
79 
80 
81     /* Get the thread identifier of the pthread to be canceled */
82     thread_ptr = posix_tid2thread(thread);
83 
84     if( (thread_ptr->tx_thread_state == TX_COMPLETED) || (thread_ptr->tx_thread_state  == TX_TERMINATED) )
85     {
86         posix_errno = EINVAL;
87         posix_set_pthread_errno(EINVAL);
88         return(EINVAL);
89     }
90     /* get posix TCB for this pthread */
91     pthread_ptr = (POSIX_TCB *)thread_ptr;
92 
93    /* Check if target pthread is created with cancel enable */
94    if ( pthread_ptr->cancel_state != PTHREAD_CANCEL_ENABLE)
95    {
96         posix_errno= EINVAL;
97         posix_set_pthread_errno(EINVAL);
98         return(EINVAL);
99    }
100    if( pthread_ptr->cancel_type==PTHREAD_CANCEL_DEFERRED )
101    {
102        /* set cancel request for cancelation point */
103        pthread_ptr->cancel_request=TRUE;
104    }
105    else if(pthread_ptr->cancel_type==PTHREAD_CANCEL_ASYNCHRONOUS )
106    {
107         /* Signal the housekeeping ThreadX thread to cancel (delete) the requested pthread now */
108 
109        posix_destroy_pthread(pthread_ptr,(VOID *)0);
110    }
111    else /* illegal value in pthread_ptr->cancel_type */
112    {
113         posix_errno = EINVAL;
114         posix_set_pthread_errno(EINVAL);
115         return(EINVAL);
116     }
117 
118    /* Indicate success.  */
119    return(OK);
120 }
121