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 /** ThreadX Component                                                     */
17 /**                                                                       */
18 /**   Mutex                                                               */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define TX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "tx_api.h"
29 #include "tx_initialize.h"
30 #include "tx_thread.h"
31 #ifndef TX_TIMER_PROCESS_IN_ISR
32 #include "tx_timer.h"
33 #endif
34 #include "tx_mutex.h"
35 
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _txe_mutex_get                                      PORTABLE C      */
42 /*                                                           6.1          */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    William E. Lamie, Microsoft Corporation                             */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function checks for errors in the mutex get function call.     */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    mutex_ptr                         Pointer to mutex control block    */
54 /*    wait_option                       Suspension option                 */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    TX_MUTEX_ERROR                    Invalid mutex pointer             */
59 /*    TX_WAIT_ERROR                     Invalid wait option               */
60 /*    status                            Actual completion status          */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _tx_mutex_get                     Actual get mutex function         */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application Code                                                    */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
75 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_txe_mutex_get(TX_MUTEX * mutex_ptr,ULONG wait_option)79 UINT  _txe_mutex_get(TX_MUTEX *mutex_ptr, ULONG wait_option)
80 {
81 
82 UINT            status;
83 #ifndef TX_TIMER_PROCESS_IN_ISR
84 TX_THREAD       *current_thread;
85 #endif
86 
87 
88     /* Default status to success.  */
89     status =  TX_SUCCESS;
90 
91     /* Check for an invalid mutex pointer.  */
92     if (mutex_ptr == TX_NULL)
93     {
94 
95         /* Mutex pointer is invalid, return appropriate error code.  */
96         status =  TX_MUTEX_ERROR;
97     }
98 
99     /* Now check for a valid mutex ID.  */
100     else if (mutex_ptr -> tx_mutex_id != TX_MUTEX_ID)
101     {
102 
103         /* Mutex pointer is invalid, return appropriate error code.  */
104         status =  TX_MUTEX_ERROR;
105     }
106     else
107     {
108 
109         /* Check for a wait option error.  Only threads are allowed any form of
110            suspension.  */
111         if (wait_option != TX_NO_WAIT)
112         {
113 
114             /* Is the call from an ISR or Initialization?  */
115             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
116             {
117 
118                 /* A non-thread is trying to suspend, return appropriate error code.  */
119                 status =  TX_WAIT_ERROR;
120             }
121 
122 #ifndef TX_TIMER_PROCESS_IN_ISR
123             else
124             {
125 
126                 /* Pickup thread pointer.  */
127                 TX_THREAD_GET_CURRENT(current_thread)
128 
129                 /* Is the current thread the timer thread?  */
130                 if (current_thread == &_tx_timer_thread)
131                 {
132 
133                     /* A non-thread is trying to suspend, return appropriate error code.  */
134                     status =  TX_WAIT_ERROR;
135                 }
136             }
137 #endif
138         }
139     }
140 
141     /* Determine if everything is okay.  */
142     if (status == TX_SUCCESS)
143     {
144 
145         /* Check for interrupt call.  */
146         if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
147         {
148 
149             /* Now, make sure the call is from an interrupt and not initialization.  */
150             if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
151             {
152 
153                 /* Yes, invalid caller of this function, return appropriate error code.  */
154                 status =  TX_CALLER_ERROR;
155             }
156         }
157     }
158 
159     /* Determine if everything is okay.  */
160     if (status == TX_SUCCESS)
161     {
162 
163         /* Call actual get mutex function.  */
164         status =  _tx_mutex_get(mutex_ptr, wait_option);
165     }
166 
167     /* Return completion status.  */
168     return(status);
169 }
170 
171