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_setcanceltype                               PORTABLE C      */
34 /*                                                           6.1.7        */
35 /*  AUTHOR                                                                */
36 /*                                                                        */
37 /*    William E. Lamie, Microsoft Corporation                             */
38 /*                                                                        */
39 /*  DESCRIPTION                                                           */
40 /*                                                                        */
41 /*    The pthread_setcancelstate()function shall atomically both get the  */
42 /*    calling thread's cancelability type to the indicated type and       */
43 /*    return the previous cancelability type at the location referenced   */
44 /*    by oldtype. Legal values for type are PTHREAD_CANCEL_DEFERRED and   */
45 /*    PTHREAD_CANCEL_ASYNCHRONOUS.                                        */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    type                           New cancelability type to be set     */
50 /*    oldtype                        Pointer to old cancelability type    */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*     0                             if successful                        */
55 /*     Value                         in case of any error                 */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
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_setcanceltype(INT type,INT * oldtype)72 INT pthread_setcanceltype (INT type, INT *oldtype)
73 {
74 TX_INTERRUPT_SAVE_AREA
75 
76 TX_THREAD     *thread_ptr;
77 POSIX_TCB     *pthread_ptr;
78 
79     /* First check for validity of the new cancel type to be set  */
80     if ( ( type  == PTHREAD_CANCEL_DEFERRED ) || ( type == PTHREAD_CANCEL_ASYNCHRONOUS ) )
81     {
82         TX_DISABLE
83 
84             /* Get the thread identifier of the currently running thread */
85             thread_ptr = tx_thread_identify();
86             /* get posix TCB for this pthread */
87             pthread_ptr = (POSIX_TCB *)thread_ptr;
88             *oldtype = pthread_ptr->cancel_type;
89             pthread_ptr->cancel_type = type;
90 
91         TX_RESTORE
92 
93         return(OK);
94     }
95     else
96     {
97         posix_errno  = EINVAL;
98         posix_set_pthread_errno(EINVAL);
99         return(EINVAL);
100     }
101 }
102