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_trace.h"
30 #include "tx_semaphore.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _tx_semaphore_create                                PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    William E. Lamie, Microsoft Corporation                             */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function creates a counting semaphore with the initial count   */
46 /*    specified in this call.                                             */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    semaphore_ptr                     Pointer to semaphore control block*/
51 /*    name_ptr                          Pointer to semaphore name         */
52 /*    initial_count                     Initial semaphore count           */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    TX_SUCCESS                        Successful completion status      */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    None                                                                */
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 /**************************************************************************/
_tx_semaphore_create(TX_SEMAPHORE * semaphore_ptr,CHAR * name_ptr,ULONG initial_count)75 UINT  _tx_semaphore_create(TX_SEMAPHORE *semaphore_ptr, CHAR *name_ptr, ULONG initial_count)
76 {
77 
78 TX_INTERRUPT_SAVE_AREA
79 
80 TX_SEMAPHORE    *next_semaphore;
81 TX_SEMAPHORE    *previous_semaphore;
82 
83 
84     /* Initialize semaphore control block to all zeros.  */
85     TX_MEMSET(semaphore_ptr, 0, (sizeof(TX_SEMAPHORE)));
86 
87     /* Setup the basic semaphore fields.  */
88     semaphore_ptr -> tx_semaphore_name =             name_ptr;
89     semaphore_ptr -> tx_semaphore_count =            initial_count;
90 
91     /* Disable interrupts to place the semaphore on the created list.  */
92     TX_DISABLE
93 
94     /* Setup the semaphore ID to make it valid.  */
95     semaphore_ptr -> tx_semaphore_id =  TX_SEMAPHORE_ID;
96 
97     /* Place the semaphore on the list of created semaphores.  First,
98        check for an empty list.  */
99     if (_tx_semaphore_created_count == TX_EMPTY)
100     {
101 
102         /* The created semaphore list is empty.  Add semaphore to empty list.  */
103         _tx_semaphore_created_ptr =                       semaphore_ptr;
104         semaphore_ptr -> tx_semaphore_created_next =      semaphore_ptr;
105         semaphore_ptr -> tx_semaphore_created_previous =  semaphore_ptr;
106     }
107     else
108     {
109 
110         /* This list is not NULL, add to the end of the list.  */
111         next_semaphore =      _tx_semaphore_created_ptr;
112         previous_semaphore =  next_semaphore -> tx_semaphore_created_previous;
113 
114         /* Place the new semaphore in the list.  */
115         next_semaphore -> tx_semaphore_created_previous =  semaphore_ptr;
116         previous_semaphore -> tx_semaphore_created_next =  semaphore_ptr;
117 
118         /* Setup this semaphore's next and previous created links.  */
119         semaphore_ptr -> tx_semaphore_created_previous =  previous_semaphore;
120         semaphore_ptr -> tx_semaphore_created_next =      next_semaphore;
121     }
122 
123     /* Increment the created count.  */
124     _tx_semaphore_created_count++;
125 
126     /* Optional semaphore create extended processing.  */
127     TX_SEMAPHORE_CREATE_EXTENSION(semaphore_ptr)
128 
129     /* If trace is enabled, register this object.  */
130     TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_SEMAPHORE, semaphore_ptr, name_ptr, initial_count, 0)
131 
132     /* If trace is enabled, insert this event into the trace buffer.  */
133     TX_TRACE_IN_LINE_INSERT(TX_TRACE_SEMAPHORE_CREATE, semaphore_ptr, initial_count, TX_POINTER_TO_ULONG_CONVERT(&next_semaphore), 0, TX_TRACE_SEMAPHORE_EVENTS)
134 
135     /* Log this kernel call.  */
136     TX_EL_SEMAPHORE_CREATE_INSERT
137 
138     /* Restore interrupts.  */
139     TX_RESTORE
140 
141     /* Return TX_SUCCESS.  */
142     return(TX_SUCCESS);
143 }
144 
145