1 /* This test is designed to test the semaphore suspension and semaphore delete with
2 suspended threads. */
3
4 #include <stdio.h>
5 #include "tx_api.h"
6
7 static unsigned long thread_0_counter = 0;
8 static TX_THREAD thread_0;
9
10 static unsigned long thread_1_counter = 0;
11 static TX_THREAD thread_1;
12
13 static unsigned long thread_2_counter = 0;
14 static TX_THREAD thread_2;
15
16
17 static TX_SEMAPHORE semaphore_0;
18
19
20 /* Define thread prototypes. */
21
22 static void thread_0_entry(ULONG thread_input);
23 static void thread_1_entry(ULONG thread_input);
24 static void thread_2_entry(ULONG thread_input);
25
26
27 /* Prototype for test control return. */
28
29 void test_control_return(UINT status);
30
31
put_notify(TX_SEMAPHORE * semaphore_ptr)32 static void put_notify(TX_SEMAPHORE *semaphore_ptr)
33 {
34
35 /* Don't need to do anything in here... */
36 }
37
38
39 /* Define what the initial system looks like. */
40
41 #ifdef CTEST
test_application_define(void * first_unused_memory)42 void test_application_define(void *first_unused_memory)
43 #else
44 void threadx_semaphore_delete_application_define(void *first_unused_memory)
45 #endif
46 {
47
48 UINT status;
49 CHAR *pointer;
50
51 /* Put first available memory address into a character pointer. */
52 pointer = (CHAR *) first_unused_memory;
53
54 /* Put system definition stuff in here, e.g. thread creates and other assorted
55 create information. */
56
57 status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
58 pointer, TEST_STACK_SIZE_PRINTF,
59 16, 16, 100, TX_AUTO_START);
60 pointer = pointer + TEST_STACK_SIZE_PRINTF;
61
62 /* Check for status. */
63 if (status != TX_SUCCESS)
64 {
65
66 printf("Running Semaphore Delete Test....................................... ERROR #1\n");
67 test_control_return(1);
68 }
69
70 status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
71 pointer, TEST_STACK_SIZE_PRINTF,
72 16, 16, 100, TX_AUTO_START);
73 pointer = pointer + TEST_STACK_SIZE_PRINTF;
74
75 /* Check for status. */
76 if (status != TX_SUCCESS)
77 {
78
79 printf("Running Semaphore Delete Test....................................... ERROR #2!\n");
80 test_control_return(1);
81 }
82
83 status = tx_thread_create(&thread_2, "thread 2", thread_2_entry, 1,
84 pointer, TEST_STACK_SIZE_PRINTF,
85 16, 16, 100, TX_AUTO_START);
86 pointer = pointer + TEST_STACK_SIZE_PRINTF;
87
88 /* Check for status. */
89 if (status != TX_SUCCESS)
90 {
91
92 printf("Running Semaphore Delete Test....................................... ERROR #3\n");
93 test_control_return(1);
94 }
95
96 /* Create a semaphore with an initial count of 0. */
97 status = tx_semaphore_create(&semaphore_0, "semaphore 0", 0);
98
99 /* Check for status. */
100 if (status != TX_SUCCESS)
101 {
102
103 printf("Running Semaphore Delete Test....................................... ERROR #4\n");
104 test_control_return(1);
105 }
106
107 /* Setup the semaphore notify callback. */
108 status = tx_semaphore_put_notify(&semaphore_0, put_notify);
109
110 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
111
112 /* Check for status. */
113 if (status != TX_SUCCESS)
114 {
115
116 printf("Running Semaphore Delete Test....................................... ERROR #5\n");
117 test_control_return(1);
118 }
119 #else
120
121 /* Check for status. */
122 if (status != TX_FEATURE_NOT_ENABLED)
123 {
124
125 printf("Running Semaphore Delete Test....................................... ERROR #6\n");
126 test_control_return(1);
127 }
128
129 #endif
130 }
131
132
133
134 /* Define the test threads. */
135
thread_0_entry(ULONG thread_input)136 static void thread_0_entry(ULONG thread_input)
137 {
138
139 UINT status;
140
141
142 /* Inform user. */
143 printf("Running Semaphore Delete Test....................................... ");
144
145 /* Increment thread 0 counter. */
146 thread_0_counter++;
147
148 /* Relinquish to let other threads run. */
149 tx_thread_relinquish();
150
151 /* Other threads should now be suspended on the semaphore. */
152
153 /* Delete the semaphore to test it out! */
154 status = tx_semaphore_delete(&semaphore_0);
155
156 /* Check status. */
157 if (status != TX_SUCCESS)
158 {
159
160 /* Semaphore error. */
161 printf("ERROR #7\n");
162 test_control_return(1);
163 }
164
165 /* Relinquish to allow other threads to run again before we return. */
166 tx_thread_relinquish();
167
168 /* Now check the run counter of each thread. */
169 if ((thread_1_counter != 1) || (thread_2_counter != 1))
170 {
171
172 /* Semaphore error. */
173 printf("ERROR #8\n");
174 test_control_return(1);
175 }
176 else
177 {
178
179 /* Successful test. */
180 printf("SUCCESS!\n");
181 test_control_return(0);
182 }
183 }
184
185
thread_1_entry(ULONG thread_input)186 static void thread_1_entry(ULONG thread_input)
187 {
188
189 UINT status;
190
191
192 /* Suspend on the semaphore. */
193 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
194
195 /* Did we get the right status? */
196 if (status == TX_DELETED)
197 thread_1_counter++;
198 }
199
200
thread_2_entry(ULONG thread_input)201 static void thread_2_entry(ULONG thread_input)
202 {
203
204 UINT status;
205
206
207 /* Suspend on the semaphore. */
208 status = tx_semaphore_get(&semaphore_0, TX_WAIT_FOREVER);
209
210 /* Did we get the right status? */
211 if (status == TX_DELETED)
212 thread_2_counter++;
213 }
214