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_timedlock                             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 as in the pthread_mutex_lock( ) function. If the  */
45 /*    mutex cannot be locked without waiting for another thread to unlock */
46 /*    the mutex, this wait shall be terminated when the specified timeout */
47 /*    expires. This operation shall return with the mutex object          */
48 /*    referenced by mutex in the locked state with the calling thread as  */
49 /*    its owner.                                                          */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    mutex                          Address of the mutex                 */
54 /*    timespec                       Pointer to timespec structure which  */
55 /*                                   holds timeout period in clock ticks  */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*     0                             if successful                        */
60 /*     Value                         in case of any error                 */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*  tx_thread_identify               Get calling thread's pointer         */
65 /*  tx_mutex_get                     ThreadX Mutex Service                */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application Code                                                    */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
76 /*                                                                        */
77 /**************************************************************************/
pthread_mutex_timedlock(pthread_mutex_t * mutex,struct timespec * abs_timeout)78 INT  pthread_mutex_timedlock(pthread_mutex_t *mutex, struct timespec *abs_timeout)
79 {
80 
81     TX_MUTEX         *mutex_ptr;
82     TX_THREAD        *thread_ptr;
83     INT               retval,status;
84     ULONG             timeout_ticks;
85 
86 
87     mutex_ptr = (TX_MUTEX*)mutex;
88 
89     thread_ptr = tx_thread_identify();
90     if ( (mutex_ptr->tx_mutex_ownership_count > 0 ) && (thread_ptr == (mutex_ptr->tx_mutex_owner )))
91     {
92         posix_errno = EDEADLK;
93         posix_set_pthread_errno(EDEADLK);
94         return (EDEADLK);
95     }
96 
97     timeout_ticks = posix_abs_time_to_rel_ticks(abs_timeout);
98 
99     status = tx_mutex_get( mutex_ptr, timeout_ticks);
100 
101     switch ( status)
102     {
103         case TX_SUCCESS:
104             retval = OK;
105             break;
106 
107         case TX_NOT_AVAILABLE:
108             posix_errno  = ETIMEDOUT;
109             posix_set_pthread_errno(ETIMEDOUT);
110             retval = ETIMEDOUT;
111             break;
112 
113         default:
114             posix_errno = EINVAL;
115             posix_set_pthread_errno(EINVAL);
116             retval = EINVAL;
117             break;
118     }
119     return(retval);
120 }
121