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