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