1 /* This test is designed to test thread terminate calls when threads are suspended on
2    a mutex.  */
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_MUTEX        mutex_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 void  test_control_return(UINT status);
29 
30 
31 /* Define what the initial system looks like.  */
32 
33 #ifdef CTEST
test_application_define(void * first_unused_memory)34 void test_application_define(void *first_unused_memory)
35 #else
36 void    threadx_mutex_thread_terminate_application_define(void *first_unused_memory)
37 #endif
38 {
39 
40 UINT    status;
41 CHAR    *pointer;
42 
43 
44     /* Put first available memory address into a character pointer.  */
45     pointer =  (CHAR *) first_unused_memory;
46 
47     /* Put system definition stuff in here, e.g. thread creates and other assorted
48        create information.  */
49 
50     status =  tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
51             pointer, TEST_STACK_SIZE_PRINTF,
52             16, 16, 100, TX_AUTO_START);
53     pointer = pointer + TEST_STACK_SIZE_PRINTF;
54 
55     /* Check for status.  */
56     if (status != TX_SUCCESS)
57     {
58 
59         printf("Running Mutex Thread Terminate Test................................. ERROR #1\n");
60         test_control_return(1);
61     }
62 
63     status =  tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
64             pointer, TEST_STACK_SIZE_PRINTF,
65             16, 16, 100, TX_AUTO_START);
66     pointer = pointer + TEST_STACK_SIZE_PRINTF;
67 
68     /* Check for status.  */
69     if (status != TX_SUCCESS)
70     {
71 
72         printf("Running Mutex Thread Terminate Test................................. ERROR #2\n");
73         test_control_return(1);
74     }
75 
76     status =  tx_thread_create(&thread_2, "thread 2", thread_2_entry, 1,
77             pointer, TEST_STACK_SIZE_PRINTF,
78             16, 16, 100, TX_AUTO_START);
79     pointer = pointer + TEST_STACK_SIZE_PRINTF;
80 
81     /* Check for status.  */
82     if (status != TX_SUCCESS)
83     {
84 
85         printf("Running Mutex Thread Terminate Test................................. ERROR #3\n");
86         test_control_return(1);
87     }
88 
89     /* Create a mutex.  */
90     status =  tx_mutex_create(&mutex_0, "mutex 0", 0);
91 
92     /* Check for status.  */
93     if (status != TX_SUCCESS)
94     {
95 
96         printf("Running Mutex Thread Terminate Test................................. ERROR #4\n");
97         test_control_return(1);
98     }
99 
100     /* Get the mutex.  */
101     tx_mutex_get(&mutex_0, TX_NO_WAIT);
102 }
103 
104 
105 
106 /* Define the test threads.  */
107 
thread_0_entry(ULONG thread_input)108 static void    thread_0_entry(ULONG thread_input)
109 {
110 
111 UINT    status;
112 
113 
114     /* Inform user.  */
115     printf("Running Mutex Thread Terminate Test................................. ");
116 
117     /* Increment thread 0 counter.  */
118     thread_0_counter++;
119 
120     /* Relinquish to let other threads run.  */
121     tx_thread_relinquish();
122 
123     /* Other threads should now be suspended on the mutex.  */
124     if ((thread_1_counter != 1) || (thread_2_counter != 1))
125     {
126 
127         /* Mutex error.  */
128         printf("ERROR #5\n");
129         test_control_return(1);
130     }
131 
132     /* Terminate the other threads to make sure the mutex gets
133        cleaned up.  */
134     status =  tx_thread_terminate(&thread_1);
135 
136     /* Check status and run counters of other threads.  */
137     if ((status != TX_SUCCESS) || (thread_1_counter != 1) || (thread_2_counter != 1))
138     {
139 
140         /* Mutex error.  */
141         printf("ERROR #6\n");
142         test_control_return(1);
143     }
144 
145     /* Terminate the other thread.  */
146     status =  tx_thread_terminate(&thread_2);
147 
148     /* Relinquish just to make sure. */
149     tx_thread_relinquish();
150 
151     /* Check status and run counters of other threads.  */
152     if ((status != TX_SUCCESS) || (thread_1_counter != 1) || (thread_2_counter != 1))
153     {
154 
155         /* Mutex error.  */
156         printf("ERROR #7\n");
157         test_control_return(1);
158     }
159     else
160     {
161 
162         /* Successful test.  */
163         printf("SUCCESS!\n");
164         test_control_return(0);
165     }
166 }
167 
168 
thread_1_entry(ULONG thread_input)169 static void    thread_1_entry(ULONG thread_input)
170 {
171 
172 //UINT    status;
173 
174     /* Increment thread run counter.  */
175     thread_1_counter++;
176 
177     /* Suspend on the mutex. */
178     tx_mutex_get(&mutex_0, 33);
179 
180     /* Should never get here!  */
181     thread_1_counter++;
182 }
183 
184 
thread_2_entry(ULONG thread_input)185 static void    thread_2_entry(ULONG thread_input)
186 {
187 
188 //UINT    status;
189 
190     /* Increment thread run counter.  */
191     thread_2_counter++;
192 
193     /* Suspend on the mutex. */
194     tx_mutex_get(&mutex_0, 44);
195 
196     /* Should never get here! */
197     thread_2_counter++;
198 }
199