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 /* sem_trywait PORTABLE C */ 34 /* 6.1.7 */ 35 /* AUTHOR */ 36 /* */ 37 /* William E. Lamie, Microsoft Corporation */ 38 /* */ 39 /* DESCRIPTION */ 40 /* */ 41 /* This routine locks(takes) a semaphore if the semaphore is not */ 42 /* currently locked. */ 43 /* */ 44 /* INPUT */ 45 /* */ 46 /* *sem Pointer to Semaphore */ 47 /* */ 48 /* OUTPUT */ 49 /* */ 50 /* OK If successful */ 51 /* ERROR If error occurs */ 52 /* */ 53 /* CALLS */ 54 /* */ 55 /* posix_internal_error Returns generic error */ 56 /* tx_semaphore_get ThreadX Semaphore get */ 57 /* tx_thread_identify Returns currently running thread */ 58 /* */ 59 /* CALLED BY */ 60 /* */ 61 /* Application Code */ 62 /* */ 63 /* RELEASE HISTORY */ 64 /* */ 65 /* DATE NAME DESCRIPTION */ 66 /* */ 67 /* 06-02-2021 William E. Lamie Initial Version 6.1.7 */ 68 /* */ 69 /**************************************************************************/ sem_trywait(sem_t * sem)70INT sem_trywait(sem_t * sem) 71 { 72 73 TX_SEMAPHORE * TheSem; 74 INT retval; 75 INT pxerror; 76 77 /* Make sure we're calling this routine from a thread context. */ 78 if (!tx_thread_identify() ) 79 { 80 /* No error in POSIX. */ 81 posix_internal_error(242); 82 83 /* Return error. */ 84 return (ERROR); 85 } 86 87 /* Get ThreadX semaphore. */ 88 TheSem = (TX_SEMAPHORE *)sem; 89 90 /* Check for an invalid semaphore pointer. */ 91 if ((!TheSem) || (TheSem -> tx_semaphore_id != TX_SEMAPHORE_ID)) 92 { 93 /* error in POSIX. */ 94 posix_errno = EINVAL; 95 posix_set_pthread_errno(EINVAL); 96 return (EINVAL); 97 } 98 else 99 retval = tx_semaphore_get(TheSem,TX_NO_WAIT); 100 101 switch(retval) 102 { 103 104 case TX_SUCCESS : 105 106 sem->refCnt -= 1; 107 pxerror = OK; 108 break; 109 110 case TX_NO_INSTANCE: 111 112 posix_errno = EAGAIN; 113 posix_set_pthread_errno(EAGAIN); 114 pxerror = ERROR; 115 break; 116 117 case TX_DELETED : 118 case TX_WAIT_ABORTED : 119 case TX_SEMAPHORE_ERROR : 120 case TX_WAIT_ERROR : 121 122 /* No error in POSIX, give default. */ 123 posix_errno = EINVAL; 124 posix_set_pthread_errno(EINVAL); 125 pxerror = ERROR; 126 break; 127 } 128 return(pxerror); 129 } 130