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_initialize.h"
30 #include "tx_thread.h"
31 #include "tx_timer.h"
32 #include "tx_semaphore.h"
33 
34 
35 /**************************************************************************/
36 /*                                                                        */
37 /*  FUNCTION                                               RELEASE        */
38 /*                                                                        */
39 /*    _txe_semaphore_create                               PORTABLE C      */
40 /*                                                           6.1          */
41 /*  AUTHOR                                                                */
42 /*                                                                        */
43 /*    William E. Lamie, Microsoft Corporation                             */
44 /*                                                                        */
45 /*  DESCRIPTION                                                           */
46 /*                                                                        */
47 /*    This function checks for errors in the create semaphore function    */
48 /*    call.                                                               */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    semaphore_ptr                     Pointer to semaphore control block*/
53 /*    name_ptr                          Pointer to semaphore name         */
54 /*    initial_count                     Initial semaphore count           */
55 /*    semaphore_control_block_size      Size of semaphore control block   */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    TX_SEMAPHORE_ERROR                Invalid semaphore pointer         */
60 /*    TX_CALLER_ERROR                   Invalid caller of this function   */
61 /*    status                            Actual completion status          */
62 /*                                                                        */
63 /*  CALLS                                                                 */
64 /*                                                                        */
65 /*    _tx_semaphore_create              Actual create semaphore function  */
66 /*    _tx_thread_system_preempt_check   Check for preemption              */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    Application Code                                                    */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     William E. Lamie         Initial Version 6.0           */
77 /*  09-30-2020     Yuxin Zhou               Modified comment(s),          */
78 /*                                            resulting in version 6.1    */
79 /*                                                                        */
80 /**************************************************************************/
_txe_semaphore_create(TX_SEMAPHORE * semaphore_ptr,CHAR * name_ptr,ULONG initial_count,UINT semaphore_control_block_size)81 UINT  _txe_semaphore_create(TX_SEMAPHORE *semaphore_ptr, CHAR *name_ptr, ULONG initial_count, UINT semaphore_control_block_size)
82 {
83 
84 TX_INTERRUPT_SAVE_AREA
85 
86 UINT                status;
87 ULONG               i;
88 TX_SEMAPHORE        *next_semaphore;
89 #ifndef TX_TIMER_PROCESS_IN_ISR
90 TX_THREAD           *thread_ptr;
91 #endif
92 
93 
94     /* Default status to success.  */
95     status =  TX_SUCCESS;
96 
97     /* Check for an invalid semaphore pointer.  */
98     if (semaphore_ptr == TX_NULL)
99     {
100 
101         /* Semaphore pointer is invalid, return appropriate error code.  */
102         status =  TX_SEMAPHORE_ERROR;
103     }
104 
105     /* Now check for a valid semaphore ID.  */
106     else if (semaphore_control_block_size != (sizeof(TX_SEMAPHORE)))
107     {
108 
109         /* Semaphore pointer is invalid, return appropriate error code.  */
110         status =  TX_SEMAPHORE_ERROR;
111     }
112     else
113     {
114 
115         /* Disable interrupts.  */
116         TX_DISABLE
117 
118         /* Increment the preempt disable flag.  */
119         _tx_thread_preempt_disable++;
120 
121         /* Restore interrupts.  */
122         TX_RESTORE
123 
124         /* Next see if it is already in the created list.  */
125         next_semaphore =  _tx_semaphore_created_ptr;
126         for (i = ((ULONG) 0); i < _tx_semaphore_created_count; i++)
127         {
128 
129             /* Determine if this semaphore matches the current semaphore in the list.  */
130             if (semaphore_ptr == next_semaphore)
131             {
132 
133                 break;
134             }
135             else
136             {
137 
138                 /* Move to next semaphore.  */
139                 next_semaphore =  next_semaphore -> tx_semaphore_created_next;
140             }
141         }
142 
143         /* Disable interrupts.  */
144         TX_DISABLE
145 
146         /* Decrement the preempt disable flag.  */
147         _tx_thread_preempt_disable--;
148 
149         /* Restore interrupts.  */
150         TX_RESTORE
151 
152         /* Check for preemption.  */
153         _tx_thread_system_preempt_check();
154 
155         /* At this point, check to see if there is a duplicate semaphore.  */
156         if (semaphore_ptr == next_semaphore)
157         {
158 
159             /* Semaphore is already created, return appropriate error code.  */
160             status =  TX_SEMAPHORE_ERROR;
161         }
162 
163 #ifndef TX_TIMER_PROCESS_IN_ISR
164         else
165         {
166 
167             /* Pickup thread pointer.  */
168             TX_THREAD_GET_CURRENT(thread_ptr)
169 
170             /* Check for invalid caller of this function.  First check for a calling thread.  */
171             if (thread_ptr == &_tx_timer_thread)
172             {
173 
174                 /* Invalid caller of this function, return appropriate error code.  */
175                 status =  TX_CALLER_ERROR;
176             }
177         }
178 #endif
179     }
180 
181     /* Determine if everything is okay.  */
182     if (status == TX_SUCCESS)
183     {
184 
185         /* Check for interrupt call.  */
186         if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
187         {
188 
189             /* Now, make sure the call is from an interrupt and not initialization.  */
190             if (TX_THREAD_GET_SYSTEM_STATE() < TX_INITIALIZE_IN_PROGRESS)
191             {
192 
193                 /* Invalid caller of this function, return appropriate error code.  */
194                 status =  TX_CALLER_ERROR;
195             }
196         }
197     }
198 
199     /* Determine if everything is okay.  */
200     if (status == TX_SUCCESS)
201     {
202 
203         /* Call actual semaphore create function.  */
204         status =  _tx_semaphore_create(semaphore_ptr, name_ptr, initial_count);
205     }
206 
207     /* Return completion status.  */
208     return(status);
209 }
210 
211