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_lock                                   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. This operation shall return with the mutex object*/
44 /*    referenced by mutex in the locked state with the calling thread as  */
45 /*    its owner.                                                          */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    mutex                          Address of the mutex                 */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*     0                             if successful                        */
54 /*     Value                         in case of any error                 */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*  tx_thread_identify               Get calling thread's pointer         */
59 /*  tx_mutex_get                     ThreadX Mutex Service                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application Code                                                    */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  06-02-2021     William E. Lamie         Initial Version 6.1.7         */
70 /*                                                                        */
71 /**************************************************************************/
pthread_mutex_lock(pthread_mutex_t * mutex)72 INT pthread_mutex_lock(pthread_mutex_t *mutex )
73 {
74 
75 TX_MUTEX         *mutex_ptr;
76 TX_THREAD        *thread_ptr;
77 INT               retval,status;
78 
79     mutex_ptr = (TX_MUTEX*)mutex;
80 
81     thread_ptr = tx_thread_identify();
82     if ( (mutex_ptr->tx_mutex_ownership_count > 0 ) && (thread_ptr == (mutex_ptr->tx_mutex_owner )))
83     {
84         posix_errno = EDEADLK;
85 	    posix_set_pthread_errno(EINVAL);
86         return (EDEADLK);
87     }
88     status = tx_mutex_get( mutex_ptr, TX_WAIT_FOREVER);
89     switch ( status)
90     {
91     case TX_SUCCESS:
92         retval = OK;
93         break;
94 
95     default:
96         posix_errno = EINVAL;
97 	    posix_set_pthread_errno(EINVAL);
98         retval = EINVAL;
99         break;
100     }
101     return(retval);
102 }
103