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 /**   Semaphore                                                           */
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_thread.h"
30 #include "tx_timer.h"
31 #include "tx_semaphore.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _txe_semaphore_get                                  PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function checks for errors in the semaphore get function call. */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    semaphore_ptr                     Pointer to semaphore control block*/
51 /*    wait_option                       Suspension option                 */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    TX_SEMAPHORE_ERROR                Invalid semaphore pointer         */
56 /*    TX_WAIT_ERROR                     Invalid wait option               */
57 /*    status                            Actual completion status          */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _tx_semaphore_get                 Actual get semaphore function     */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application Code                                                    */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
72 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_txe_semaphore_get(TX_SEMAPHORE * semaphore_ptr,ULONG wait_option)76 UINT  _txe_semaphore_get(TX_SEMAPHORE *semaphore_ptr, ULONG wait_option)
77 {
78 
79 UINT        status;
80 
81 #ifndef TX_TIMER_PROCESS_IN_ISR
82 TX_THREAD   *current_thread;
83 #endif
84 
85 
86     /* Default status to success.  */
87     status =  TX_SUCCESS;
88 
89     /* Check for an invalid semaphore pointer.  */
90     if (semaphore_ptr == TX_NULL)
91     {
92 
93         /* Semaphore pointer is invalid, return appropriate error code.  */
94         status =  TX_SEMAPHORE_ERROR;
95     }
96 
97     /* Now check for invalid semaphore ID.  */
98     else if (semaphore_ptr -> tx_semaphore_id != TX_SEMAPHORE_ID)
99     {
100 
101         /* Semaphore pointer is invalid, return appropriate error code.  */
102         status =  TX_SEMAPHORE_ERROR;
103     }
104     else
105     {
106 
107         /* Check for a wait option error.  Only threads are allowed any form of
108            suspension.  */
109         if (wait_option != TX_NO_WAIT)
110         {
111 
112             /* Is the call from an ISR or Initialization?  */
113             if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
114             {
115 
116                 /* A non-thread is trying to suspend, return appropriate error code.  */
117                 status =  TX_WAIT_ERROR;
118             }
119 
120 #ifndef TX_TIMER_PROCESS_IN_ISR
121             else
122             {
123 
124                 /* Pickup thread pointer.  */
125                 TX_THREAD_GET_CURRENT(current_thread)
126 
127                 /* Is the current thread the timer thread?  */
128                 if (current_thread == &_tx_timer_thread)
129                 {
130 
131                     /* A non-thread is trying to suspend, return appropriate error code.  */
132                     status =  TX_WAIT_ERROR;
133                 }
134             }
135 #endif
136         }
137     }
138 
139     /* Determine if everything is okay.  */
140     if (status == TX_SUCCESS)
141     {
142 
143         /* Call actual get semaphore function.  */
144         status =  _tx_semaphore_get(semaphore_ptr, wait_option);
145     }
146 
147     /* Return completion status.  */
148     return(status);
149 }
150 
151