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