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