1 /* Define the ThreadX SMP rebalance exclusion test.  */
2 
3 #include   <stdio.h>
4 #include   "tx_api.h"
5 
6 
7 
8 static TX_THREAD       thread_0;
9 static TX_THREAD       thread_1;
10 static TX_THREAD       thread_2;
11 static TX_THREAD       thread_31k;
12 
13 
14 static ULONG       thread_0_counter;
15 static ULONG       thread_1_counter;
16 static ULONG       thread_2_counter;
17 static ULONG       thread_31k_counter;
18 
19 
20 static unsigned long error =  0;
21 
22 
23 /* Define thread prototypes.  */
24 
25 static void    thread_0_entry(ULONG thread_input);
26 static void    thread_1_entry(ULONG thread_input);
27 static void    thread_2_entry(ULONG thread_input);
28 static void    thread_31k_entry(ULONG thread_input);
29 
30 
31 /* Prototype for test control return.  */
32 
33 void  test_control_return(UINT status);
34 
35 
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_smp_rebalance_exclustion_test(void *first_unused_memory)
45 #endif
46 {
47 
48 UINT    status;
49 CHAR    *pointer;
50 
51 
52     /* Put first available memory address into a character pointer.  */
53     pointer =  (CHAR *) first_unused_memory;
54 
55     /* Put system definition stuff in here, e.g. thread creates and other assorted
56        create information.  */
57 
58     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 0,
59             pointer, TEST_STACK_SIZE_PRINTF,
60             0, 0, TX_NO_TIME_SLICE, TX_DONT_START);
61     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
62     status +=   tx_thread_smp_core_exclude(&thread_0, 0x0);      /* No exclusions! */
63 
64     /* Check status.  */
65     if (status != TX_SUCCESS)
66     {
67 
68         printf("Running SMP Rebalance Exclusion Test................................ ERROR #1\n");
69         test_control_return(1);
70     }
71 
72     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 0,
73             pointer, TEST_STACK_SIZE_PRINTF,
74             1, 1, TX_NO_TIME_SLICE, TX_DONT_START);
75     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
76     status +=   tx_thread_smp_core_exclude(&thread_1, 0x0);      /* No exclusions! */
77 
78     /* Check status.  */
79     if (status != TX_SUCCESS)
80     {
81 
82         printf("Running SMP Rebalance Exclusion Test................................ ERROR #2\n");
83         test_control_return(1);
84     }
85 
86     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 0,
87             pointer, TEST_STACK_SIZE_PRINTF,
88             2, 2, TX_NO_TIME_SLICE, TX_DONT_START);
89     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
90     status +=   tx_thread_smp_core_exclude(&thread_2, 0x0);      /* No exclusions! */
91 
92     /* Check status.  */
93     if (status != TX_SUCCESS)
94     {
95 
96         printf("Running SMP Rebalance Exclusion Test................................ ERROR #3\n");
97         test_control_return(1);
98     }
99 
100     status =  tx_thread_create(&thread_31k, "thread 31k", thread_31k_entry, 0,
101             pointer, TEST_STACK_SIZE_PRINTF,
102             31, 15, 16, TX_DONT_START);
103     pointer =  pointer + TEST_STACK_SIZE_PRINTF;
104     status +=   tx_thread_smp_core_exclude(&thread_31k, 0xE);      /* Core 0 only! */
105 
106     /* Check status.  */
107     if (status != TX_SUCCESS)
108     {
109 
110         printf("Running SMP Rebalance Exclusion Test................................ ERROR #4\n");
111         test_control_return(1);
112     }
113 
114 	/* Resume thread 0.  */
115     status =  tx_thread_resume(&thread_0);
116 
117 	/* Check status.  */
118     if (status != TX_SUCCESS)
119     {
120 
121         printf("Running SMP Rebalance Exclusion Test................................ ERROR #5\n");
122         test_control_return(1);
123     }
124 }
125 
126 
127 
128 /* Define the test threads.  */
129 
thread_0_entry(ULONG thread_input)130 static void    thread_0_entry(ULONG thread_input)
131 {
132 
133 UINT    status;
134 
135 
136 
137     /* Inform user.  */
138     printf("Running SMP Rebalance Exclusion Test................................ ");
139 
140     /* Resume all the threads.  */
141     status =  tx_thread_resume(&thread_1);
142     status +=  tx_thread_resume(&thread_2);
143     status +=  tx_thread_resume(&thread_31k);
144 
145     /* Suspend this thread to let the others run.  */
146     tx_thread_sleep(5);
147 
148     /* Determine if the test was successful or there was an error.  */
149     if ((status != TX_SUCCESS) || (error) || (thread_1_counter != 1) ||
150         (thread_2_counter != 1) || (thread_31k_counter != 1))
151     {
152 
153         /* Execution error.  */
154         printf("ERROR #6\n");
155         test_control_return(1);
156     }
157     else
158     {
159 
160         /* Successful test.  */
161         printf("SUCCESS!\n");
162         test_control_return(0);
163     }
164 }
165 
166 
thread_1_entry(ULONG thread_input)167 static void    thread_1_entry(ULONG thread_input)
168 {
169 
170     while(1)
171     {
172 
173         thread_1_counter++;
174         tx_thread_suspend(&thread_1);
175     }
176 }
177 
178 
thread_2_entry(ULONG thread_input)179 static void    thread_2_entry(ULONG thread_input)
180 {
181 
182     while(1)
183     {
184 
185         thread_2_counter++;
186         tx_thread_suspend(&thread_2);
187     }
188 }
189 
190 
thread_31k_entry(ULONG thread_input)191 static void    thread_31k_entry(ULONG thread_input)
192 {
193 
194     while(1)
195     {
196 
197         /* Ensure this thread is on the correct core.  */
198         if (tx_thread_smp_core_get() != 0)
199             error++;
200 
201         thread_31k_counter++;
202         tx_thread_suspend(&thread_31k);
203     }
204 }
205