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