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_mutex_trylock PORTABLE C */ 34 /* 6.1.7 */ 35 /* AUTHOR */ 36 /* */ 37 /* William E. Lamie, Microsoft Corporation */ 38 /* */ 39 /* DESCRIPTION */ 40 /* */ 41 /* This function shall be equivalent to pthread_mutex_lock(), except */ 42 /* that if the mutex object referenced by mutex is currently locked */ 43 /* (by any thread,including the current thread), the call shall return */ 44 /* immediately. If the mutex type is PTHREAD_MUTEX_RECURSIVE and the */ 45 /* mutex is currently owned by the calling thread,the mutex lock count */ 46 /* shall be incremented by one and the pthread_mutex_trylock()function */ 47 /* shall immediately return success. */ 48 /* */ 49 /* INPUT */ 50 /* */ 51 /* mutex Pointer to the mutex object */ 52 /* */ 53 /* OUTPUT */ 54 /* */ 55 /* 0 If successful */ 56 /* Value In case of any error */ 57 /* */ 58 /* CALLS */ 59 /* */ 60 /* tx_mutex_get ThreadX Mutex get service. */ 61 /* */ 62 /* CALLED BY */ 63 /* */ 64 /* Application Code */ 65 /* */ 66 /* RELEASE HISTORY */ 67 /* */ 68 /* DATE NAME DESCRIPTION */ 69 /* */ 70 /* 06-02-2021 William E. Lamie Initial Version 6.1.7 */ 71 /* */ 72 /**************************************************************************/ pthread_mutex_trylock(pthread_mutex_t * mutex)73INT pthread_mutex_trylock(pthread_mutex_t *mutex) 74 { 75 76 TX_MUTEX *mutex_ptr; 77 INT retval,status; 78 79 /* convert pthread mutex object to ThreadX mutex */ 80 mutex_ptr = (TX_MUTEX *)mutex; 81 82 /* Try to get the mutex */ 83 status = tx_mutex_get( mutex_ptr, TX_NO_WAIT); 84 85 switch ( status) 86 { 87 case TX_SUCCESS: 88 retval = OK; 89 break; 90 91 case TX_DELETED: 92 posix_errno = EINVAL; 93 posix_set_pthread_errno(EINVAL); 94 retval = EINVAL; 95 break; 96 97 case TX_NOT_AVAILABLE: 98 posix_errno = EBUSY; 99 posix_set_pthread_errno(EBUSY); 100 retval = EBUSY; 101 break; 102 103 default: 104 posix_errno = EINVAL; 105 posix_set_pthread_errno(EINVAL); 106 retval = EINVAL; 107 break; 108 } 109 return (retval); 110 } 111