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