1 /* This test is designed to see if one thread can be created, executed, and
2    return to the thread shell function.  The thread shell function places
3    the thread in a finished state.  */
4 
5 #include   <stdio.h>
6 #include   "tx_api.h"
7 #include   "tx_thread.h"
8 
9 
10 static unsigned long   thread_0_counter =  0;
11 static unsigned long   thread_0_enter =  0;
12 static unsigned long   thread_0_exit =  0;
13 static unsigned long   thread_1_counter =  0;
14 
15 static TX_THREAD       thread_0;
16 static TX_THREAD       thread_1;
17 static TX_THREAD       thread_2;
18 static TX_THREAD       *saved_ptr;
19 static ULONG           saved_count;
20 
21 /* Define task prototypes.  */
22 
23 static void    thread_0_entry(ULONG thread_input);
24 static void    thread_1_entry(ULONG thread_input);
25 static void    thread_2_entry(ULONG thread_input);
26 
27 
28 /* Prototype for test control return.  */
29 
30 void  test_control_return(UINT status);
31 
32 
entry_exit_notify(TX_THREAD * thread_ptr,UINT type)33 static void   entry_exit_notify(TX_THREAD *thread_ptr, UINT type)
34 {
35 
36     /* Check for the appropriate thread.  */
37     if (thread_ptr != &thread_0)
38         return;
39 
40     /* Check for type.  */
41     if (type == TX_THREAD_ENTRY)
42         thread_0_enter++;
43     else if (type == TX_THREAD_EXIT)
44         thread_0_exit++;
45 }
46 
47 
48 
49 /* Define what the initial system looks like.  */
50 
51 #ifdef CTEST
test_application_define(void * first_unused_memory)52 void test_application_define(void *first_unused_memory)
53 #else
54 void    threadx_thread_completed_application_define(void *first_unused_memory)
55 #endif
56 {
57 
58 UINT     status;
59 CHAR    *pointer;
60 
61     /* Put first available memory address into a character pointer.  */
62     pointer =  (CHAR *) first_unused_memory;
63 
64     /* Put system definition stuff in here, e.g. thread creates and other assorted
65        create information.  */
66 
67     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
68                         pointer, TEST_STACK_SIZE_PRINTF,
69             16, 16, TX_NO_TIME_SLICE, TX_AUTO_START);
70     pointer = pointer + TEST_STACK_SIZE_PRINTF;
71 
72     /* Setup the notify call to test that logic.  */
73     status += tx_thread_entry_exit_notify(&thread_0, entry_exit_notify);
74 
75 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
76 
77     /* Check for status.  */
78     if (status != TX_SUCCESS)
79     {
80 
81         printf("Running Thread Completed Test....................................... ERROR #1\n");
82         test_control_return(1);
83     }
84 
85 #else
86 
87     /* Check for status.  */
88     if (status != TX_FEATURE_NOT_ENABLED)
89     {
90 
91         printf("Running Thread Completed Test....................................... ERROR #2\n");
92         test_control_return(1);
93     }
94 
95 #endif
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 Completed 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 Completed Test....................................... ERROR #4\n");
120         test_control_return(1);
121     }
122 
123     /* Move the created pointer to thread 1 to test the delete path fully.  */
124     saved_ptr =  _tx_thread_created_ptr;
125     _tx_thread_created_ptr =  &thread_0;
126 }
127 
128 
129 
130 /* Define the test thread.  */
131 
thread_0_entry(ULONG thread_input)132 static void    thread_0_entry(ULONG thread_input)
133 {
134 
135     /* Increment thread 0 counter.  */
136     thread_0_counter++;
137 
138     /* Fall through to the return in order to place the thread in a finished
139        state.  */
140 }
141 
142 
thread_1_entry(ULONG thread_input)143 static void    thread_1_entry(ULONG thread_input)
144 {
145 
146 UINT    status;
147 
148 
149     /* Inform user.  */
150     printf("Running Thread Completed Test....................................... ");
151 
152     /* Increment thread 1 counter.  */
153     thread_1_counter++;
154 
155     /* Attempt to delete thread 2, which is in the wrong stat for deleting.  */
156     status =  tx_thread_delete(&thread_2);
157 
158     /* Check for the proper status.  */
159     if (status != TX_DELETE_ERROR)
160     {
161 
162         /* Thread delete error.  */
163         printf("ERROR #5\n");
164         test_control_return(1);
165     }
166 
167     /* Attempt to suspend thread 0, which is in a completed state.  */
168     status =  tx_thread_suspend(&thread_0);
169 
170     /* Check for the correct status.  */
171     if (status != TX_SUSPEND_ERROR)
172     {
173 
174         /* Thread suspend error.  */
175         printf("ERROR #6\n");
176         test_control_return(1);
177     }
178 
179     /* Attempt to delete thread 0.  */
180     status =  tx_thread_delete(&thread_0);
181 
182     /* Check for the proper status.  */
183     if (status != TX_SUCCESS)
184     {
185 
186         /* Thread delete error.  */
187         printf("ERROR #7\n");
188         test_control_return(1);
189     }
190 
191     /* Sleep to let thread 2 run.  */
192     tx_thread_sleep(2);
193 
194     /* Save the created count.  */
195     saved_count =  _tx_thread_created_count;
196 
197     /* Now setup things so we can fake a delete of one thread.  */
198     _tx_thread_created_ptr =  &thread_2;
199     thread_2.tx_thread_created_next =  &thread_2;
200     thread_2.tx_thread_created_previous =  &thread_2;
201     _tx_thread_created_count =  1;
202 
203     /* Attempt to delete thread 2.  */
204     status =  tx_thread_delete(&thread_2);
205 
206     /* Check for the proper status.  */
207     if (status != TX_SUCCESS)
208     {
209 
210         /* Thread delete error.  */
211         printf("ERROR #8\n");
212         test_control_return(1);
213     }
214 
215     /* if still okay, restore the saved thread pointer.  */
216     if (saved_ptr -> tx_thread_id == TX_THREAD_ID)
217     {
218         /* Restore.  */
219         _tx_thread_created_ptr =  saved_ptr;
220 
221         /* Setup the link pointers again.  */
222         saved_ptr -> tx_thread_created_previous =  &thread_1;
223         thread_1.tx_thread_created_next =  saved_ptr;
224 
225         /* Setup the created count. */
226         _tx_thread_created_count =  saved_count - 1;
227     }
228 
229     /* Determine if the first Thread has run and if it's current state is
230        finished.  */
231     if ((thread_0.tx_thread_state == TX_COMPLETED) && (thread_0_counter == 1) &&
232 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
233         (thread_0_enter == 1) && (thread_0_exit == 1))
234 #else
235         (thread_0_enter == 0) && (thread_0_exit == 0))
236 #endif
237     {
238 
239         /* Successful thread finish test.  */
240         printf("SUCCESS!\n");
241         test_control_return(0);
242     }
243     else
244     {
245 
246         /* Thread Finish error.  */
247         printf("ERROR #9\n");
248         test_control_return(1);
249     }
250 }
251 
252 
thread_2_entry(ULONG thread_input)253 static void    thread_2_entry(ULONG thread_input)
254 {
255 
256     /* Fall through to the return in order to place the thread in a finished
257        state.  */
258 }
259