1 /* This test is designed to test the mutex suspension and mutex delete with
2    suspended threads.  */
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_delete_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 Delete 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 Delete 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 Delete Test........................................... ERROR #3\n");
86         test_control_return(1);
87     }
88 
89     /* Create a mutex.  */
90     status =  tx_mutex_create(&mutex_0, "mutex 0", TX_NO_INHERIT);
91 
92     /* Check for status.  */
93     if (status != TX_SUCCESS)
94     {
95 
96         printf("Running Mutex Delete Test........................................... ERROR #4\n");
97         test_control_return(1);
98     }
99 }
100 
101 
102 
103 /* Define the test threads.  */
104 
thread_0_entry(ULONG thread_input)105 static void    thread_0_entry(ULONG thread_input)
106 {
107 
108 UINT    status;
109 
110 
111     /* Inform user.  */
112     printf("Running Mutex Delete Test........................................... ");
113 
114     /* Increment thread 0 counter.  */
115     thread_0_counter++;
116 
117     /* Grab the mutex so it is owned by this thread.  */
118     status =  tx_mutex_get(&mutex_0, TX_NO_WAIT);
119 
120     /* Check status.  */
121     if (status != TX_SUCCESS)
122     {
123 
124         /* Mutex error.  */
125         printf("ERROR #5\n");
126         test_control_return(1);
127     }
128 
129     /* Relinquish to let other threads run.  */
130     tx_thread_relinquish();
131 
132     /* Other threads should now be suspended on the mutex.  */
133 
134     /* Delete the mutex to test it out!  */
135     status =  tx_mutex_delete(&mutex_0);
136 
137     /* Check status.  */
138     if (status != TX_SUCCESS)
139     {
140 
141         /* Mutex error.  */
142         printf("ERROR #6\n");
143         test_control_return(1);
144     }
145 
146     /* Relinquish to allow other threads to run again before we return.  */
147     tx_thread_relinquish();
148 
149     /* Now check the run counter of each thread.  */
150     if ((thread_1_counter != 1) || (thread_2_counter != 1))
151     {
152 
153         /* Mutex error.  */
154         printf("ERROR #7\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 UINT    status;
171 
172 
173     /* Suspend on the mutex. */
174     status =  tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
175 
176     /* Did we get the right status?  */
177     if (status == TX_DELETED)
178         thread_1_counter++;
179 }
180 
181 
thread_2_entry(ULONG thread_input)182 static void    thread_2_entry(ULONG thread_input)
183 {
184 
185 UINT    status;
186 
187 
188     /* Suspend on the mutex. */
189     status =  tx_mutex_get(&mutex_0, TX_WAIT_FOREVER);
190 
191     /* Did we get the right status?  */
192     if (status == TX_DELETED)
193         thread_2_counter++;
194 }
195