1 /* This test is designed to see if two threads can be created and execute with a time-slice.
2    Thread 7 should run twice as long because it has more of a time-slice. */
3 
4 #include   <stdio.h>
5 #include   "tx_api.h"
6 
7 static volatile unsigned long   thread_0_counter =  0;
8 static          TX_THREAD       thread_0;
9 static volatile unsigned long   thread_1_counter =  0;
10 static          TX_THREAD       thread_1;
11 static volatile unsigned long   thread_2_counter =  0;
12 static          TX_THREAD       thread_2;
13 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
14 static volatile unsigned long   thread_3_counter =  0;
15 static          TX_THREAD       thread_3;
16 static volatile unsigned long   thread_4_counter =  0;
17 static          TX_THREAD       thread_4;
18 #endif
19 
20 
21 /* Define thread 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 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
27 static void    thread_3_entry(ULONG thread_input);
28 static void    thread_4_entry(ULONG thread_input);
29 #endif
30 
31 
32 /* Prototype for test control return.  */
33 void  test_control_return(UINT status);
34 
35 
36 /* Define what the initial system looks like.  */
37 
38 #ifdef CTEST
test_application_define(void * first_unused_memory)39 void test_application_define(void *first_unused_memory)
40 #else
41 void    threadx_thread_multiple_time_slice_application_define(void *first_unused_memory)
42 #endif
43 {
44 
45 UINT    status;
46 CHAR    *pointer;
47 
48     /* Put first available memory address into a character pointer.  */
49     pointer =  (CHAR *) first_unused_memory;
50 
51     /* Put system definition stuff in here, e.g. thread creates and other assorted
52        create information.  */
53     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
54             pointer, TEST_STACK_SIZE_PRINTF,
55             16, 16, 2, TX_AUTO_START);
56     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
57 
58     /* Check for status.  */
59     if (status != TX_SUCCESS)
60     {
61 
62         printf("Running Thread Multiple Thread Time-Slice Test...................... ERROR #1\n");
63         test_control_return(1);
64     }
65 
66     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
67             pointer, TEST_STACK_SIZE_PRINTF,
68             16, 16, 4, TX_AUTO_START);
69     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
70 
71     /* Check for status.  */
72     if (status != TX_SUCCESS)
73     {
74 
75         printf("Running Thread Multiple Thread Time-Slice Test...................... ERROR #2\n");
76         test_control_return(1);
77     }
78 
79     /* Create control thread.  */
80     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 1,
81             pointer, TEST_STACK_SIZE_PRINTF,
82             15, 15, TX_NO_TIME_SLICE, TX_AUTO_START);
83     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
84 
85     /* Check for status.  */
86     if (status != TX_SUCCESS)
87     {
88 
89         printf("Running Thread Multiple Thread Time-Slice Test...................... ERROR #3\n");
90         test_control_return(1);
91     }
92 
93 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
94 
95     /* Create threads with preemption-threshold and time-slice, such and make sure time-slice is defeated by
96        preemption-threshold.  */
97     status =  tx_thread_create(&thread_3, "thread 3", thread_3_entry, 3,
98             pointer, TEST_STACK_SIZE_PRINTF,
99             18, 17, 2, 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 Multiple Thread Time-Slice Test...................... ERROR #4\n");
107         test_control_return(1);
108     }
109 
110     status =  tx_thread_create(&thread_4, "thread 4", thread_4_entry, 4,
111             pointer, TEST_STACK_SIZE_PRINTF,
112             18, 18, 4, 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 Multiple Thread Time-Slice Test...................... ERROR #5\n");
120         test_control_return(1);
121     }
122 #endif
123 }
124 
125 
126 
127 /* Define the test threads.  */
128 
thread_0_entry(ULONG thread_input)129 static void    thread_0_entry(ULONG thread_input)
130 {
131 
132     /* Enter into a forever loop.  */
133     while(1)
134     {
135 
136         /* Increment thread 0 counter.  */
137         thread_0_counter++;
138 
139         /* Call thread identify for Win32 test case.  */
140         tx_thread_identify();
141     }
142 }
143 
144 
thread_1_entry(ULONG thread_input)145 static void    thread_1_entry(ULONG thread_input)
146 {
147 
148 
149     /* Enter into a forever loop.  */
150     while(1)
151     {
152 
153         /* Increment thread 1 counter.  */
154         thread_1_counter++;
155 
156         /* Call thread identify for Win32 test case.  */
157         tx_thread_identify();
158     }
159 }
160 
161 
thread_2_entry(ULONG thread_input)162 static void    thread_2_entry(ULONG thread_input)
163 {
164 
165 unsigned long   counter_sum;
166 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
167 UINT            status;
168 #endif
169 
170 
171     /* Inform user.  */
172     printf("Running Thread Multiple Thread Time-Slice Test...................... ");
173 
174     /* Sleep for some multiple of 6.  */
175     tx_thread_sleep(48);
176 
177     /* Increment thread 2 counter.  */
178     thread_2_counter++;
179 
180     /* Compute the delta. Should be twice as much, but some test environments (Windows/Linux) are
181        not as good in terms of real time processing.  */
182     counter_sum =  thread_0_counter;
183     counter_sum =  counter_sum + (thread_0_counter/4);
184     if (thread_1_counter <= counter_sum)
185     {
186 
187         /* Thread Time-slice error.  */
188         printf("ERROR #6\n");
189         test_control_return(1);
190     }
191 #ifdef TX_DISABLE_PREEMPTION_THRESHOLD
192     else
193     {
194         /* Successful Thread Time-slice test.  */
195         printf("SUCCESS!\n");
196         test_control_return(0);
197     }
198 #else
199     /* Now suspend threads 0 and 1 so we can let 3 and 4 run.  */
200     status =  tx_thread_suspend(&thread_0);
201     status += tx_thread_suspend(&thread_1);
202 
203     /* Check status.  */
204     if (status)
205     {
206         /* Thread Time-slice error.  */
207         printf("ERROR #7\n");
208         test_control_return(1);
209     }
210 
211     /* Now sleep and see if thread 4 ever runs.  */
212     tx_thread_sleep(4);
213 
214     /* Determine if thread 3 ran and thread 4 didn't.  */
215     if ((thread_3_counter) && (thread_4_counter == 0))
216     {
217         /* Successful Thread Time-slice test.  */
218         printf("SUCCESS!\n");
219         test_control_return(0);
220     }
221     else
222     {
223 
224         /* Thread Time-slice error.  */
225         printf("ERROR #8\n");
226         test_control_return(1);
227     }
228 #endif
229 }
230 
231 #ifndef TX_DISABLE_PREEMPTION_THRESHOLD
232 
233 /* Define the test threads.  */
234 
thread_3_entry(ULONG thread_input)235 static void    thread_3_entry(ULONG thread_input)
236 {
237 
238     /* Enter into a forever loop.  */
239     while(1)
240     {
241 
242         /* Increment thread 3 counter.  */
243         thread_3_counter++;
244 
245         /* Call thread identify for Win32 test case.  */
246         tx_thread_identify();
247     }
248 }
249 
250 
thread_4_entry(ULONG thread_input)251 static void    thread_4_entry(ULONG thread_input)
252 {
253 
254 
255     /* Enter into a forever loop.  */
256     while(1)
257     {
258 
259         /* We should never get here!  */
260 
261         /* Increment thread 4 counter.  */
262         thread_4_counter++;
263 
264         /* Call thread identify for Win32 test case.  */
265         tx_thread_identify();
266     }
267 }
268 #endif
269 
270