1 /* This test is designed to test the tx_thread_reset function. */
2
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "tx_thread.h"
6
7
8 static unsigned long thread_0_counter = 0;
9 static unsigned long thread_0_enter = 0;
10 static unsigned long thread_0_exit = 0;
11 static unsigned long thread_1_counter = 0;
12 static unsigned long thread_2_counter = 0;
13
14
15 static TX_THREAD thread_0;
16 static TX_THREAD thread_1;
17 static TX_THREAD thread_2;
18
19
20 /* Define task 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
entry_exit_notify(TX_THREAD * thread_ptr,UINT type)32 static void entry_exit_notify(TX_THREAD *thread_ptr, UINT type)
33 {
34
35 /* Check for the appropriate thread. */
36 if (thread_ptr != &thread_0)
37 return;
38
39 /* Check for type. */
40 if (type == TX_THREAD_ENTRY)
41 thread_0_enter++;
42 else if (type == TX_THREAD_EXIT)
43 thread_0_exit++;
44 }
45
46
47
48 /* Define what the initial system looks like. */
49
50 #ifdef CTEST
test_application_define(void * first_unused_memory)51 void test_application_define(void *first_unused_memory)
52 #else
53 void threadx_thread_reset_application_define(void *first_unused_memory)
54 #endif
55 {
56
57 UINT status;
58 CHAR *pointer;
59
60 /* Put first available memory address into a character pointer. */
61 pointer = (CHAR *) first_unused_memory;
62
63 /* Put system definition stuff in here, e.g. thread creates and other assorted
64 create information. */
65
66 status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
67 pointer, TEST_STACK_SIZE_PRINTF,
68 16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
69 pointer = pointer + TEST_STACK_SIZE_PRINTF;
70
71 /* Setup the notify call to test that logic. */
72 status += tx_thread_entry_exit_notify(&thread_0, entry_exit_notify);
73
74 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
75
76 /* Check for status. */
77 if (status != TX_SUCCESS)
78 {
79
80 printf("Running Thread Reset Test........................................... ERROR #1\n");
81 test_control_return(1);
82 }
83
84 #else
85
86 /* Check for status. */
87 if (status != TX_FEATURE_NOT_ENABLED)
88 {
89
90 printf("Running Thread Reset Test........................................... ERROR #2\n");
91 test_control_return(1);
92 }
93
94 #endif
95
96
97 status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
98 pointer, TEST_STACK_SIZE_PRINTF,
99 17, 17, TX_NO_TIME_SLICE, TX_AUTO_START);
100 pointer = pointer + TEST_STACK_SIZE_PRINTF;
101
102 /* Check for status. */
103 if (status != TX_SUCCESS)
104 {
105
106 printf("Running Thread Reset Test........................................... ERROR #3\n");
107 test_control_return(1);
108 }
109
110 status = tx_thread_create(&thread_2, "thread 2", thread_2_entry, 2,
111 pointer, TEST_STACK_SIZE_PRINTF,
112 18, 18, TX_NO_TIME_SLICE, TX_AUTO_START);
113 pointer = pointer + TEST_STACK_SIZE_PRINTF;
114
115 /* Check for status. */
116 if (status != TX_SUCCESS)
117 {
118
119 printf("Running Thread Reset Test........................................... ERROR #4\n");
120 test_control_return(1);
121 }
122 }
123
124
125
126 /* Define the test thread. */
127
thread_0_entry(ULONG thread_input)128 static void thread_0_entry(ULONG thread_input)
129 {
130
131 /* Increment thread 0 counter. */
132 thread_0_counter++;
133
134 /* Fall through to the return in order to place the thread in a finished
135 state. */
136 }
137
138
thread_1_entry(ULONG thread_input)139 static void thread_1_entry(ULONG thread_input)
140 {
141
142 UINT status;
143
144
145 /* Inform user. */
146 printf("Running Thread Reset Test........................................... ");
147
148 /* Increment thread 1 counter. */
149 thread_1_counter++;
150
151 /* Attempt to delete thread 2, which is in the wrong stat for deleting. */
152 status = tx_thread_delete(&thread_2);
153
154 /* Check for the proper status. */
155 if (status != TX_DELETE_ERROR)
156 {
157
158 /* Thread delete error. */
159 printf("ERROR #5\n");
160 test_control_return(1);
161 }
162
163 /* Determine if the first Thread has run and if it's current state is
164 finished. */
165 if ((thread_0.tx_thread_state != TX_COMPLETED) || (thread_0_counter != 1) ||
166 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
167 (thread_0_enter != 1) || (thread_0_exit != 1) || (thread_2_counter != 0))
168 #else
169 (thread_0_enter != 0) || (thread_0_exit != 0) || (thread_2_counter != 0))
170 #endif
171 {
172
173 /* Thread reset error. */
174 printf("ERROR #6\n");
175 test_control_return(1);
176 }
177
178 /* Call thread reset on thread 2, which should result in an error. */
179 status = tx_thread_reset(&thread_2);
180
181 /* Check for proper status. */
182 if (status != TX_NOT_DONE)
183 {
184
185 /* Thread reset error. */
186 printf("ERROR #7\n");
187 test_control_return(1);
188 }
189
190 /* Now call the thread reset function on thread 0. */
191 status = tx_thread_reset(&thread_0);
192
193 /* Determine if the first Thread has been reset but not executed. */
194 if ((thread_0.tx_thread_state != TX_SUSPENDED) || (thread_0_counter != 1) ||
195 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
196 (thread_0_enter != 1) || (thread_0_exit != 1) || (thread_2_counter != 0))
197 #else
198 (thread_0_enter != 0) || (thread_0_exit != 0) || (thread_2_counter != 0))
199 #endif
200 {
201
202 /* Thread reset error. */
203 printf("ERROR #8\n");
204 test_control_return(1);
205 }
206
207 /* Terminate thread 0. */
208 status = tx_thread_terminate(&thread_0);
209 status += tx_thread_reset(&thread_0);
210
211
212 /* Now resume thread 0 to let it run. */
213 status += tx_thread_resume(&thread_0);
214
215 /* Determine if the first Thread has run and if it's current state is
216 finished. */
217 if ((thread_0.tx_thread_state != TX_COMPLETED) || (thread_0_counter != 2) ||
218 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
219 (thread_0_enter != 2) || (thread_0_exit != 3) || (thread_2_counter != 0))
220 #else
221 (thread_0_enter != 0) || (thread_0_exit != 0) || (thread_2_counter != 0))
222 #endif
223 {
224
225 /* Thread reset error. */
226 printf("ERROR #9\n");
227 test_control_return(1);
228 }
229 else
230 {
231
232
233 /* Successful thread finish test. */
234 printf("SUCCESS!\n");
235 test_control_return(0);
236 }
237 }
238
239
thread_2_entry(ULONG thread_input)240 static void thread_2_entry(ULONG thread_input)
241 {
242
243 /* Increment thread 2 counter. */
244 thread_2_counter++;
245
246 /* Fall through to the return in order to place the thread in a finished
247 state. */
248 }
249