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_delete                               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 delete function    */
46 /*    call.                                                               */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    semaphore_ptr                     Pointer to semaphore control block*/
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    TX_SEMAPHORE_ERROR                Invalid semaphore pointer         */
55 /*    TX_CALLER_ERROR                   Invalid caller of this function   */
56 /*    status                            Actual completion status          */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _tx_semaphore_delete              Actual delete 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_delete(TX_SEMAPHORE * semaphore_ptr)75 UINT  _txe_semaphore_delete(TX_SEMAPHORE *semaphore_ptr)
76 {
77 
78 UINT            status;
79 #ifndef TX_TIMER_PROCESS_IN_ISR
80 TX_THREAD       *thread_ptr;
81 #endif
82 
83 
84     /* Default status to success.  */
85     status =  TX_SUCCESS;
86 
87     /* Check for an invalid semaphore pointer.  */
88     if (semaphore_ptr == TX_NULL)
89     {
90 
91         /* Semaphore pointer is invalid, return appropriate error code.  */
92         status =  TX_SEMAPHORE_ERROR;
93     }
94 
95     /* Now check for invalid semaphore ID.  */
96     else if (semaphore_ptr -> tx_semaphore_id != TX_SEMAPHORE_ID)
97     {
98 
99         /* Semaphore pointer is invalid, return appropriate error code.  */
100         status =  TX_SEMAPHORE_ERROR;
101     }
102     else
103     {
104 
105         /* Check for invalid caller of this function.  */
106 
107         /* Is the caller an ISR or Initialization?  */
108         if (TX_THREAD_GET_SYSTEM_STATE() != ((ULONG) 0))
109         {
110 
111             /* Invalid caller of this function, return appropriate error code.  */
112             status =  TX_CALLER_ERROR;
113         }
114 
115 #ifndef TX_TIMER_PROCESS_IN_ISR
116         else
117         {
118 
119             /* Pickup thread pointer.  */
120             TX_THREAD_GET_CURRENT(thread_ptr)
121 
122             /* Is the caller the system timer thread?  */
123             if (thread_ptr == &_tx_timer_thread)
124             {
125 
126                 /* Invalid caller of this function, return appropriate error code.  */
127                 status =  TX_CALLER_ERROR;
128             }
129         }
130 #endif
131     }
132 
133     /* Determine if everything is okay.  */
134     if (status == TX_SUCCESS)
135     {
136 
137         /* Call actual semaphore delete function.  */
138         status =  _tx_semaphore_delete(semaphore_ptr);
139     }
140 
141     /* Return completion status.  */
142     return(status);
143 }
144 
145