1 /* This test is designed to test thread termination of a thread in a sleep condition. */
2
3 #include <stdio.h>
4 #include "tx_api.h"
5
6 static unsigned long thread_0_counter = 0;
7 static TX_THREAD thread_0;
8 static unsigned long thread_1_counter = 0;
9 static unsigned long thread_1_enter = 0;
10 static unsigned long thread_1_exit = 0;
11 static TX_THREAD thread_1;
12
13 extern UINT _tx_thread_preempt_disable;
14
15 /* Define thread prototypes. */
16
17 static void thread_0_entry(ULONG thread_input);
18 static void thread_1_entry(ULONG thread_input);
19
20
21 /* Prototype for test control return. */
22
23 void test_control_return(UINT status);
24
25
26
entry_exit_notify(TX_THREAD * thread_ptr,UINT type)27 static void entry_exit_notify(TX_THREAD *thread_ptr, UINT type)
28 {
29
30 /* Check for the appropriate thread. */
31 if (thread_ptr != &thread_1)
32 return;
33
34 /* Check for type. */
35 if (type == TX_THREAD_ENTRY)
36 thread_1_enter++;
37 else if (type == TX_THREAD_EXIT)
38 thread_1_exit++;
39 }
40
41
42 /* Define what the initial system looks like. */
43
44 #ifdef CTEST
test_application_define(void * first_unused_memory)45 void test_application_define(void *first_unused_memory)
46 #else
47 void threadx_thread_sleep_terminate_application_define(void *first_unused_memory)
48 #endif
49 {
50
51 UINT status;
52 CHAR *pointer;
53
54
55 /* Put first available memory address into a character pointer. */
56 pointer = (CHAR *) first_unused_memory;
57
58 /* Put system definition stuff in here, e.g. thread creates and other assorted
59 create information. */
60
61 status = tx_thread_create(&thread_0, "thread 0", thread_0_entry, 1,
62 pointer, TEST_STACK_SIZE_PRINTF,
63 17, 17, 100, TX_AUTO_START);
64 pointer = pointer + TEST_STACK_SIZE_PRINTF;
65
66 /* Check status. */
67 if (status != TX_SUCCESS)
68 {
69
70 printf("Running Thread Suspend Terminate Test............................... ERROR #1\n");
71 test_control_return(1);
72 }
73
74 status = tx_thread_create(&thread_1, "thread 1", thread_1_entry, 1,
75 pointer, TEST_STACK_SIZE_PRINTF,
76 18, 18, 100, TX_AUTO_START);
77 pointer = pointer + TEST_STACK_SIZE_PRINTF;
78
79 /* Check status. */
80 if (status != TX_SUCCESS)
81 {
82
83 printf("Running Thread Suspend Terminate Test............................... ERROR #2\n");
84 test_control_return(1);
85 }
86
87 /* Setup the notify call to test that logic. */
88 status += tx_thread_entry_exit_notify(&thread_1, entry_exit_notify);
89
90 #ifndef TX_DISABLE_NOTIFY_CALLBACKS
91
92 /* Check for status. */
93 if (status != TX_SUCCESS)
94 {
95
96 printf("Running Thread Suspend Terminate Test............................... ERROR #3\n");
97 test_control_return(1);
98 }
99
100 #else
101
102 /* Check for status. */
103 if (status != TX_FEATURE_NOT_ENABLED)
104 {
105
106 printf("Running Thread Suspend Terminate Test............................... ERROR #4\n");
107 test_control_return(1);
108 }
109
110 #endif
111
112 }
113
114
115
116 /* Define the test threads. */
117
thread_0_entry(ULONG thread_input)118 static void thread_0_entry(ULONG thread_input)
119 {
120
121 UINT status;
122
123
124 /* Inform user. */
125 printf("Running Thread Suspend Terminate Test............................... ");
126
127 /* Increment run counter. */
128 thread_0_counter++;
129
130 /* Sleep to allow lower-priority thread 1 to run. */
131 tx_thread_sleep(5);
132
133 /* Now terminate thread 1. */
134 status = tx_thread_terminate(&thread_1);
135
136 /* Check status. */
137 if ((status != TX_SUCCESS) || (thread_1_counter != 1) || (_tx_thread_preempt_disable))
138 {
139
140 /* Terminate error. */
141 printf("ERROR #5\n");
142 test_control_return(1);
143 }
144
145 /* Now try to suspend a terminated thread. */
146 status = tx_thread_suspend(&thread_1);
147
148 /* Check status. */
149 if (status != TX_SUSPEND_ERROR)
150 {
151
152 /* Suspend error. */
153 printf("ERROR #6\n");
154 test_control_return(1);
155 }
156
157 /* Successful test. */
158 printf("SUCCESS!\n");
159 test_control_return(0);
160 }
161
162
thread_1_entry(ULONG thread_input)163 static void thread_1_entry(ULONG thread_input)
164 {
165
166 UINT status;
167
168
169 while(1)
170 {
171
172 /* Increment run counter. */
173 thread_1_counter++;
174
175 /* Suspend thread 1. */
176 status = tx_thread_sleep(100);
177
178 /* Check status. */
179 if (status != TX_SUCCESS)
180 {
181 thread_1_counter = 0; /* Make an error! */
182 return;
183 }
184 }
185 }
186
187
188