1 /**************************************************************************/ 2 /* */ 3 /* Copyright (c) Microsoft Corporation. All rights reserved. */ 4 /* */ 5 /* This software is licensed under the Microsoft Software License */ 6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */ 7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ 8 /* and in the root directory of this software. */ 9 /* */ 10 /**************************************************************************/ 11 12 13 /**************************************************************************/ 14 /**************************************************************************/ 15 /** */ 16 /** POSIX wrapper for THREADX */ 17 /** */ 18 /** */ 19 /** */ 20 /**************************************************************************/ 21 /**************************************************************************/ 22 23 /* Include necessary system files. */ 24 25 #include "tx_api.h" /* Threadx API */ 26 #include "pthread.h" /* Posix API */ 27 #include "px_int.h" /* Posix helper functions */ 28 29 30 /**************************************************************************/ 31 /* */ 32 /* FUNCTION RELEASE */ 33 /* */ 34 /* pthread_mutex_lock PORTABLE C */ 35 /* 6.1.7 */ 36 /* AUTHOR */ 37 /* */ 38 /* William E. Lamie, Microsoft Corporation */ 39 /* */ 40 /* DESCRIPTION */ 41 /* */ 42 /* This function locks the mutex object referenced. If the mutex is */ 43 /* already locked, the calling thread shall block until the mutex */ 44 /* becomes available. This operation shall return with the mutex object*/ 45 /* referenced by mutex in the locked state with the calling thread as */ 46 /* its owner. */ 47 /* */ 48 /* INPUT */ 49 /* */ 50 /* mutex Address of the mutex */ 51 /* */ 52 /* OUTPUT */ 53 /* */ 54 /* 0 if successful */ 55 /* Value in case of any error */ 56 /* */ 57 /* CALLS */ 58 /* */ 59 /* tx_thread_identify Get calling thread's pointer */ 60 /* tx_mutex_get ThreadX Mutex 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_lock(pthread_mutex_t * mutex)73INT pthread_mutex_lock(pthread_mutex_t *mutex ) 74 { 75 76 TX_MUTEX *mutex_ptr; 77 TX_THREAD *thread_ptr; 78 INT retval,status; 79 80 mutex_ptr = (TX_MUTEX*)mutex; 81 82 thread_ptr = tx_thread_identify(); 83 if ( (mutex_ptr->tx_mutex_ownership_count > 0 ) && (thread_ptr == (mutex_ptr->tx_mutex_owner ))) 84 { 85 posix_errno = EDEADLK; 86 posix_set_pthread_errno(EINVAL); 87 return (EDEADLK); 88 } 89 status = tx_mutex_get( mutex_ptr, TX_WAIT_FOREVER); 90 switch ( status) 91 { 92 case TX_SUCCESS: 93 retval = OK; 94 break; 95 96 default: 97 posix_errno = EINVAL; 98 posix_set_pthread_errno(EINVAL); 99 retval = EINVAL; 100 break; 101 } 102 return(retval); 103 } 104