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 /** POSIX wrapper for THREADX                                             */
15 /**                                                                       */
16 /**                                                                       */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 /* Include necessary system files.  */
22 
23 #include "tx_api.h"     /* Threadx API */
24 #include "pthread.h"    /* Posix API */
25 #include "px_int.h"     /* Posix helper functions */
26 
27 
28 /**************************************************************************/
29 /*                                                                        */
30 /*  FUNCTION                                               RELEASE        */
31 /*                                                                        */
32 /*    pthread_testcancel                                  PORTABLE C      */
33 /*                                                           6.1.7        */
34 /*  AUTHOR                                                                */
35 /*                                                                        */
36 /*    William E. Lamie, Microsoft Corporation                             */
37 /*                                                                        */
38 /*  DESCRIPTION                                                           */
39 /*                                                                        */
40 /*    The pthread_testcancel function shall request that thread be        */
41 /*    canceled if the cancel state is enabled and cancel type is          */
42 /*    deferred, and a cancellation request has happened in the past.      */
43 /*    The target thread's cancelability state and type determines when    */
44 /*    the cancellation takes effect. When the cancellation is acted on,   */
45 /*    the cancelation cleanup handlers for thread shall be called. When   */
46 /*    the last cancelation cleanup handler returns, the thread-specific   */
47 /*    data destructor functions shall be called for thread. When the last */
48 /*    destructor function returns, thread shall be terminated.            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    thread                      pthread handle to thread to be canceled */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*                                                                        */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*         posix_destroy_thread                                           */
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_testcancel(VOID)72 VOID pthread_testcancel(VOID)
73 {
74 
75 POSIX_TCB   *pthread_ptr;
76 
77 
78     /* Get the thread identifier of the calling pthread */
79     pthread_ptr = posix_tid2tcb(pthread_self());
80 
81     /* Check if thread was created with cancel enable */
82     if ( (pthread_ptr->cancel_state == PTHREAD_CANCEL_ENABLE) &&
83            (pthread_ptr->cancel_type==PTHREAD_CANCEL_DEFERRED) &&
84            (pthread_ptr->cancel_request==TRUE) )
85     {
86 
87         /* Signal the housekeeping ThreadX thread to cancel (delete) this pthread */
88         posix_destroy_pthread(pthread_ptr,(VOID *)0);
89    }
90 }
91 
92