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 /**   Mutex                                                               */
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_trace.h"
31 #include "tx_mutex.h"
32 
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _tx_mutex_create                                    PORTABLE C      */
39 /*                                                           6.1          */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    William E. Lamie, Microsoft Corporation                             */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function creates a mutex with optional priority inheritance as */
47 /*    specified in this call.                                             */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    mutex_ptr                             Pointer to mutex control block*/
52 /*    name_ptr                              Pointer to mutex name         */
53 /*    inherit                               Priority inheritance option   */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    TX_SUCCESS                        Successful completion status      */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    None                                                                */
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 /**************************************************************************/
_tx_mutex_create(TX_MUTEX * mutex_ptr,CHAR * name_ptr,UINT inherit)76 UINT  _tx_mutex_create(TX_MUTEX *mutex_ptr, CHAR *name_ptr, UINT inherit)
77 {
78 
79 TX_INTERRUPT_SAVE_AREA
80 
81 TX_MUTEX        *next_mutex;
82 TX_MUTEX        *previous_mutex;
83 
84 
85     /* Initialize mutex control block to all zeros.  */
86     TX_MEMSET(mutex_ptr, 0, (sizeof(TX_MUTEX)));
87 
88     /* Setup the basic mutex fields.  */
89     mutex_ptr -> tx_mutex_name =             name_ptr;
90     mutex_ptr -> tx_mutex_inherit =          inherit;
91 
92     /* Disable interrupts to place the mutex on the created list.  */
93     TX_DISABLE
94 
95     /* Setup the mutex ID to make it valid.  */
96     mutex_ptr -> tx_mutex_id =  TX_MUTEX_ID;
97 
98     /* Setup the thread mutex release function pointer.  */
99     _tx_thread_mutex_release =  &(_tx_mutex_thread_release);
100 
101     /* Place the mutex on the list of created mutexes.  First,
102        check for an empty list.  */
103     if (_tx_mutex_created_count == TX_EMPTY)
104     {
105 
106         /* The created mutex list is empty.  Add mutex to empty list.  */
107         _tx_mutex_created_ptr =                   mutex_ptr;
108         mutex_ptr -> tx_mutex_created_next =      mutex_ptr;
109         mutex_ptr -> tx_mutex_created_previous =  mutex_ptr;
110     }
111     else
112     {
113 
114         /* This list is not NULL, add to the end of the list.  */
115         next_mutex =      _tx_mutex_created_ptr;
116         previous_mutex =  next_mutex -> tx_mutex_created_previous;
117 
118         /* Place the new mutex in the list.  */
119         next_mutex -> tx_mutex_created_previous =  mutex_ptr;
120         previous_mutex -> tx_mutex_created_next =  mutex_ptr;
121 
122         /* Setup this mutex's next and previous created links.  */
123         mutex_ptr -> tx_mutex_created_previous =  previous_mutex;
124         mutex_ptr -> tx_mutex_created_next =      next_mutex;
125     }
126 
127     /* Increment the ownership count.  */
128     _tx_mutex_created_count++;
129 
130     /* Optional mutex create extended processing.  */
131     TX_MUTEX_CREATE_EXTENSION(mutex_ptr)
132 
133     /* If trace is enabled, register this object.  */
134     TX_TRACE_OBJECT_REGISTER(TX_TRACE_OBJECT_TYPE_MUTEX, mutex_ptr, name_ptr, inherit, 0)
135 
136     /* If trace is enabled, insert this event into the trace buffer.  */
137     TX_TRACE_IN_LINE_INSERT(TX_TRACE_MUTEX_CREATE, mutex_ptr, inherit, TX_POINTER_TO_ULONG_CONVERT(&next_mutex), 0, TX_TRACE_MUTEX_EVENTS)
138 
139     /* Log this kernel call.  */
140     TX_EL_MUTEX_CREATE_INSERT
141 
142     /* Restore interrupts.  */
143     TX_RESTORE
144 
145     /* Return TX_SUCCESS.  */
146     return(TX_SUCCESS);
147 }
148 
149